Commit 709b09c1 authored by Clinton Stimpson's avatar Clinton Stimpson
Browse files

BUG: Better handling of spin boxes.

parent 15d697a1
......@@ -42,15 +42,28 @@ pqAbstractDoubleEventPlayer::pqAbstractDoubleEventPlayer(QObject* p)
bool pqAbstractDoubleEventPlayer::playEvent(QObject* Object, const QString& Command, const QString& Arguments, bool& Error)
{
if(Command != "set_double")
if(Command != "set_double" && Command != "spin")
return false;
const double value = Arguments.toDouble();
if(QDoubleSpinBox* const object = qobject_cast<QDoubleSpinBox*>(Object))
{
object->setValue(value);
return true;
if(Command == "set_double")
{
object->setValue(value);
return true;
}
else if(Command == "spin" && Arguments == "up")
{
object->stepUp();
return true;
}
else if(Command == "spin" && Arguments == "down")
{
object->stepDown();
return true;
}
}
qCritical() << "calling set_double on unhandled type " << Object;
......
......@@ -43,7 +43,7 @@ pqAbstractIntEventPlayer::pqAbstractIntEventPlayer(QObject* p)
bool pqAbstractIntEventPlayer::playEvent(QObject* Object, const QString& Command, const QString& Arguments, bool& Error)
{
if(Command != "set_int")
if(Command != "set_int" && Command != "spin")
return false;
const int value = Arguments.toInt();
......@@ -56,8 +56,21 @@ bool pqAbstractIntEventPlayer::playEvent(QObject* Object, const QString& Command
if(QSpinBox* const object = qobject_cast<QSpinBox*>(Object))
{
object->setValue(value);
return true;
if(Command == "set_int")
{
object->setValue(value);
return true;
}
else if(Command == "spin" && Arguments == "up")
{
object->stepUp();
return true;
}
else if(Command == "spin" && Arguments == "down")
{
object->stepDown();
return true;
}
}
qCritical() << "calling set_int on unhandled type " << Object;
......
......@@ -33,44 +33,69 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "pqDoubleSpinBoxEventTranslator.h"
#include <QDoubleSpinBox>
#include <QEvent>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QStyle>
#include <QStyleOptionSpinBox>
pqDoubleSpinBoxEventTranslator::pqDoubleSpinBoxEventTranslator(QObject* p)
: pqWidgetEventTranslator(p),
CurrentObject(0)
: pqWidgetEventTranslator(p)
{
}
bool pqDoubleSpinBoxEventTranslator::translateEvent(QObject* Object, QEvent* Event, bool& /*Error*/)
{
QDoubleSpinBox* const object = qobject_cast<QDoubleSpinBox*>(Object);
if(!object)
return false;
// consume line edit events if part of spin box
if(!object && qobject_cast<QDoubleSpinBox*>(Object->parent()))
{
return true;
}
if(!object)
return false;
switch(Event->type())
{
case QEvent::FocusIn:
this->CurrentObject = Object;
connect(object, SIGNAL(valueChanged(double)), this, SLOT(onValueChanged(double)));
break;
case QEvent::FocusOut:
disconnect(Object, 0, this, 0);
this->CurrentObject = 0;
case QEvent::MouseButtonPress:
{
QMouseEvent* me = static_cast<QMouseEvent*>(Event);
if(me->button() == Qt::LeftButton)
{
QStyle* style = object->style();
QStyleOptionSpinBox option;
option.initFrom(object);
option.subControls = QStyle::SC_All;
QStyle::SubControl sub = style->hitTestComplexControl(
QStyle::CC_SpinBox, &option, me->pos(), object);
if(sub == QStyle::SC_SpinBoxUp)
{
emit recordEvent(object, "spin", "up");
}
else if(sub == QStyle::SC_SpinBoxDown)
{
emit recordEvent(object, "spin", "down");
}
}
}
break;
case QEvent::KeyRelease:
{
QKeyEvent* ke = static_cast<QKeyEvent*>(Event);
QString keyText = ke->text();
if(keyText.length() && keyText.at(0).isLetterOrNumber())
{
emit recordEvent(object, "set_double", QString("%1").arg(object->value()));
}
else
{
emit recordEvent(object, "key", QString("%1").arg(ke->key()));
}
}
default:
break;
}
return true;
}
void pqDoubleSpinBoxEventTranslator::onValueChanged(double Value)
{
emit recordEvent(this->CurrentObject, "set_double", QString().setNum(Value));
}
......@@ -55,10 +55,6 @@ private:
pqDoubleSpinBoxEventTranslator(const pqDoubleSpinBoxEventTranslator&);
pqDoubleSpinBoxEventTranslator& operator=(const pqDoubleSpinBoxEventTranslator&);
QObject* CurrentObject;
private slots:
void onValueChanged(double);
};
#endif // !_pqDoubleSpinBoxEventTranslator_h
......
......@@ -33,11 +33,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "pqSpinBoxEventTranslator.h"
#include <QSpinBox>
#include <QEvent>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QStyle>
#include <QStyleOptionSpinBox>
pqSpinBoxEventTranslator::pqSpinBoxEventTranslator(QObject* p)
: pqWidgetEventTranslator(p),
CurrentObject(0)
: pqWidgetEventTranslator(p)
{
}
......@@ -56,14 +58,41 @@ bool pqSpinBoxEventTranslator::translateEvent(QObject* Object, QEvent* Event, bo
switch(Event->type())
{
case QEvent::FocusIn:
this->CurrentObject = Object;
connect(object, SIGNAL(valueChanged(int)), this, SLOT(onValueChanged(int)));
break;
case QEvent::FocusOut:
disconnect(Object, 0, this, 0);
this->CurrentObject = 0;
case QEvent::MouseButtonPress:
{
QMouseEvent* me = static_cast<QMouseEvent*>(Event);
if(me->button() == Qt::LeftButton)
{
QStyle* style = object->style();
QStyleOptionSpinBox option;
option.initFrom(object);
option.subControls = QStyle::SC_All;
QStyle::SubControl sub = style->hitTestComplexControl(
QStyle::CC_SpinBox, &option, me->pos(), object);
if(sub == QStyle::SC_SpinBoxUp)
{
emit recordEvent(object, "spin", "up");
}
else if(sub == QStyle::SC_SpinBoxDown)
{
emit recordEvent(object, "spin", "down");
}
}
}
break;
case QEvent::KeyRelease:
{
QKeyEvent* ke = static_cast<QKeyEvent*>(Event);
QString keyText = ke->text();
if(keyText.length() && keyText.at(0).isLetterOrNumber())
{
emit recordEvent(object, "set_int", QString("%1").arg(object->value()));
}
else
{
emit recordEvent(object, "key", QString("%1").arg(ke->key()));
}
}
default:
break;
}
......@@ -71,7 +100,3 @@ bool pqSpinBoxEventTranslator::translateEvent(QObject* Object, QEvent* Event, bo
return true;
}
void pqSpinBoxEventTranslator::onValueChanged(int Value)
{
emit recordEvent(this->CurrentObject, "set_int", QString().setNum(Value));
}
......@@ -55,10 +55,6 @@ private:
pqSpinBoxEventTranslator(const pqSpinBoxEventTranslator&);
pqSpinBoxEventTranslator& operator=(const pqSpinBoxEventTranslator&);
QObject* CurrentObject;
private slots:
void onValueChanged(int);
};
#endif // !_pqSpinBoxEventTranslator_h
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment