2014-07-08 03:11:14 -04:00
|
|
|
#include "FractoriumPch.h"
|
|
|
|
#include "DoubleSpinBox.h"
|
2016-12-05 22:04:33 -05:00
|
|
|
#include "FractoriumSettings.h"
|
2014-07-08 03:11:14 -04:00
|
|
|
|
2016-02-12 00:38:21 -05:00
|
|
|
QTimer DoubleSpinBox::s_Timer;
|
2015-05-15 21:45:15 -04:00
|
|
|
|
2014-07-08 03:11:14 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Constructor that passes parent to the base and sets up height and step.
|
|
|
|
/// Specific focus policy is used to allow the user to hover over the control
|
|
|
|
/// and change its value using the mouse wheel without explicitly having to click
|
|
|
|
/// inside of it.
|
|
|
|
/// </summary>
|
2015-01-02 18:11:36 -05:00
|
|
|
/// <param name="p">The parent widget. Default: nullptr.</param>
|
2014-07-08 03:11:14 -04:00
|
|
|
/// <param name="height">The height of the spin box. Default: 16.</param>
|
|
|
|
/// <param name="step">The step used to increment/decrement the spin box when using the mouse wheel. Default: 0.05.</param>
|
2014-12-11 00:50:15 -05:00
|
|
|
DoubleSpinBox::DoubleSpinBox(QWidget* p, int h, double step)
|
|
|
|
: QDoubleSpinBox(p)
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
|
|
|
m_DoubleClick = false;
|
|
|
|
m_DoubleClickNonZero = 0;
|
|
|
|
m_DoubleClickZero = 1;
|
|
|
|
m_Step = step;
|
|
|
|
m_SmallStep = step / 10.0;
|
|
|
|
setSingleStep(step);
|
|
|
|
setFrame(false);
|
|
|
|
setButtonSymbols(QAbstractSpinBox::NoButtons);
|
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2014-12-11 00:50:15 -05:00
|
|
|
setMinimumHeight(h);//setGeometry() has no effect, so must set both of these instead.
|
|
|
|
setMaximumHeight(h);
|
2015-05-15 21:45:15 -04:00
|
|
|
setContextMenuPolicy(Qt::PreventContextMenu);
|
2014-07-08 03:11:14 -04:00
|
|
|
lineEdit()->installEventFilter(this);
|
|
|
|
lineEdit()->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
2015-05-15 21:45:15 -04:00
|
|
|
connect(this, SIGNAL(valueChanged(double)), this, SLOT(OnSpinBoxValueChanged(double)), Qt::QueuedConnection);
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Set the value of the control without triggering signals.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="d">The value to set it to</param>
|
|
|
|
void DoubleSpinBox::SetValueStealth(double d)
|
|
|
|
{
|
|
|
|
blockSignals(true);
|
|
|
|
setValue(d);
|
|
|
|
blockSignals(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Set whether to respond to double click events.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="b">True if this should respond to double click events, else false.</param>
|
|
|
|
void DoubleSpinBox::DoubleClick(bool b)
|
|
|
|
{
|
|
|
|
m_DoubleClick = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Set the value to be used when the user double clicks the spinner while
|
|
|
|
/// it contains zero.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="val">The value to be used</param>
|
|
|
|
void DoubleSpinBox::DoubleClickZero(double val)
|
|
|
|
{
|
|
|
|
m_DoubleClickZero = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Set the value to be used when the user double clicks the spinner while
|
|
|
|
/// it contains a non-zero value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="val">The value to be used</param>
|
|
|
|
void DoubleSpinBox::DoubleClickNonZero(double val)
|
|
|
|
{
|
|
|
|
m_DoubleClickNonZero = val;
|
|
|
|
}
|
|
|
|
|
2015-02-03 20:11:16 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Get the default step used when the user scrolls.
|
|
|
|
/// </summary>
|
|
|
|
double DoubleSpinBox::Step()
|
|
|
|
{
|
|
|
|
return m_Step;
|
|
|
|
}
|
|
|
|
|
2014-07-08 03:11:14 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Set the default step to be used when the user scrolls.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="step">The step to use for scrolling</param>
|
|
|
|
void DoubleSpinBox::Step(double step)
|
|
|
|
{
|
|
|
|
m_Step = step;
|
|
|
|
}
|
|
|
|
|
2015-02-03 20:11:16 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Get the small step to be used when the user holds down shift while scrolling.
|
|
|
|
/// </summary>
|
|
|
|
double DoubleSpinBox::SmallStep()
|
|
|
|
{
|
|
|
|
return m_SmallStep;
|
|
|
|
}
|
|
|
|
|
2014-07-08 03:11:14 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Set the small step to be used when the user holds down shift while scrolling.
|
|
|
|
/// The default is step / 10, so use this if something else is needed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="step">The small step to use for scrolling while the shift key is down</param>
|
|
|
|
void DoubleSpinBox::SmallStep(double step)
|
|
|
|
{
|
|
|
|
m_SmallStep = step;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Expose the underlying QLineEdit control to the caller.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A pointer to the QLineEdit</returns>
|
|
|
|
QLineEdit* DoubleSpinBox::lineEdit()
|
|
|
|
{
|
|
|
|
return QDoubleSpinBox::lineEdit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Another workaround for the persistent text selection bug in Qt.
|
|
|
|
/// </summary>
|
2015-05-15 21:45:15 -04:00
|
|
|
void DoubleSpinBox::OnSpinBoxValueChanged(double d)
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
|
|
|
lineEdit()->deselect();//Gets rid of nasty "feature" that always has text selected.
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:45:15 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Called while the timer is activated due to the right mouse button being held down.
|
|
|
|
/// </summary>
|
|
|
|
void DoubleSpinBox::OnTimeout()
|
|
|
|
{
|
|
|
|
int xdistance = m_MouseMovePoint.x() - m_MouseDownPoint.x();
|
|
|
|
int ydistance = m_MouseMovePoint.y() - m_MouseDownPoint.y();
|
|
|
|
int distance = abs(xdistance) > abs(ydistance) ? xdistance : ydistance;
|
|
|
|
double scale, val;
|
|
|
|
double d = value();
|
|
|
|
bool shift = QGuiApplication::keyboardModifiers().testFlag(Qt::ShiftModifier);
|
2016-02-20 21:44:52 -05:00
|
|
|
bool ctrl = QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier);
|
2015-05-15 21:45:15 -04:00
|
|
|
double amount = (m_SmallStep + m_Step) * 0.5;
|
|
|
|
|
|
|
|
if (shift)
|
|
|
|
scale = 0.0001;
|
2016-02-20 21:44:52 -05:00
|
|
|
else if (ctrl)
|
2015-05-15 21:45:15 -04:00
|
|
|
scale = 0.01;
|
|
|
|
else
|
|
|
|
scale = 0.001;
|
|
|
|
|
|
|
|
val = d + (distance * amount * scale);
|
|
|
|
setValue(val);
|
|
|
|
}
|
|
|
|
|
2014-07-08 03:11:14 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Event filter for taking special action on double click events.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The object</param>
|
|
|
|
/// <param name="e">The eevent</param>
|
|
|
|
/// <returns>false</returns>
|
|
|
|
bool DoubleSpinBox::eventFilter(QObject* o, QEvent* e)
|
|
|
|
{
|
2015-05-15 21:45:15 -04:00
|
|
|
QMouseEvent* me = dynamic_cast<QMouseEvent*>(e);
|
2016-12-05 22:04:33 -05:00
|
|
|
auto settings = FractoriumSettings::DefInstance();
|
2015-05-15 21:45:15 -04:00
|
|
|
|
2016-12-05 22:04:33 -05:00
|
|
|
if (isEnabled() && me)
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
2016-12-05 22:04:33 -05:00
|
|
|
if (!settings->ToggleType() &&//Ensure double click toggles, not right click.
|
|
|
|
me->type() == QMouseEvent::MouseButtonPress &&
|
|
|
|
me->button() == Qt::RightButton)
|
|
|
|
{
|
|
|
|
m_MouseDownPoint = m_MouseMovePoint = me->pos();
|
|
|
|
StartTimer();
|
|
|
|
}
|
|
|
|
else if (!settings->ToggleType() &&
|
|
|
|
me->type() == QMouseEvent::MouseButtonRelease &&
|
|
|
|
me->button() == Qt::RightButton)
|
|
|
|
{
|
|
|
|
StopTimer();
|
|
|
|
m_MouseDownPoint = m_MouseMovePoint = me->pos();
|
|
|
|
}
|
|
|
|
else if (!settings->ToggleType() &&
|
|
|
|
me->type() == QMouseEvent::MouseMove &&
|
|
|
|
QGuiApplication::mouseButtons() & Qt::RightButton)
|
|
|
|
{
|
|
|
|
m_MouseMovePoint = me->pos();
|
|
|
|
}
|
|
|
|
else if (m_DoubleClick &&
|
|
|
|
((!settings->ToggleType() && e->type() == QMouseEvent::MouseButtonDblClick && me->button() == Qt::LeftButton) ||
|
|
|
|
(settings->ToggleType() && me->type() == QMouseEvent::MouseButtonRelease && me->button() == Qt::RightButton)))
|
|
|
|
{
|
|
|
|
if (IsNearZero(value()))
|
|
|
|
setValue(m_DoubleClickZero);
|
|
|
|
else
|
|
|
|
setValue(m_DoubleClickNonZero);
|
|
|
|
}
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (e->type() == QEvent::Wheel)
|
|
|
|
{
|
|
|
|
//Take special action for shift to reduce the scroll amount. Control already
|
|
|
|
//increases it automatically.
|
2014-12-11 00:50:15 -05:00
|
|
|
if (QWheelEvent* we = dynamic_cast<QWheelEvent*>(e))
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
2014-12-11 00:50:15 -05:00
|
|
|
Qt::KeyboardModifiers mod = we->modifiers();
|
2014-07-08 03:11:14 -04:00
|
|
|
|
|
|
|
if (mod.testFlag(Qt::ShiftModifier))
|
|
|
|
setSingleStep(m_SmallStep);
|
|
|
|
else
|
|
|
|
setSingleStep(m_Step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QDoubleSpinBox::eventFilter(o, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when focus enters the spinner.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="e">The event</param>
|
|
|
|
void DoubleSpinBox::focusInEvent(QFocusEvent* e)
|
|
|
|
{
|
2015-05-15 21:45:15 -04:00
|
|
|
StopTimer();
|
2014-07-08 03:11:14 -04:00
|
|
|
QDoubleSpinBox::focusInEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when focus leaves the spinner.
|
|
|
|
/// Qt has a nasty "feature" that leaves the text in a spinner selected
|
|
|
|
/// and the cursor visible, regardless of whether it has the focus.
|
|
|
|
/// Manually clear both here.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="e">The event</param>
|
|
|
|
void DoubleSpinBox::focusOutEvent(QFocusEvent* e)
|
|
|
|
{
|
2015-05-15 21:45:15 -04:00
|
|
|
StopTimer();
|
|
|
|
QDoubleSpinBox::focusOutEvent(e);
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when focus enters the spinner.
|
|
|
|
/// Must set the focus to make sure key down messages don't erroneously go to the GLWidget.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="e">The event</param>
|
|
|
|
void DoubleSpinBox::enterEvent(QEvent* e)
|
|
|
|
{
|
2015-05-15 21:45:15 -04:00
|
|
|
StopTimer();
|
2014-07-08 03:11:14 -04:00
|
|
|
QDoubleSpinBox::enterEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when focus leaves the spinner.
|
|
|
|
/// Must clear the focus to make sure key down messages don't erroneously go to the GLWidget.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="e">The event</param>
|
|
|
|
void DoubleSpinBox::leaveEvent(QEvent* e)
|
|
|
|
{
|
2015-05-15 21:45:15 -04:00
|
|
|
StopTimer();
|
2014-07-08 03:11:14 -04:00
|
|
|
QDoubleSpinBox::leaveEvent(e);
|
|
|
|
}
|
2015-05-15 21:45:15 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Start the timer in response to the right mouse button being pressed.
|
|
|
|
/// </summary>
|
|
|
|
void DoubleSpinBox::StartTimer()
|
|
|
|
{
|
2016-02-12 00:38:21 -05:00
|
|
|
s_Timer.stop();
|
|
|
|
connect(&s_Timer, SIGNAL(timeout()), this, SLOT(OnTimeout()));
|
|
|
|
s_Timer.start(300);
|
2015-05-15 21:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Stop the timer in response to the left mouse button being pressed.
|
|
|
|
/// </summary>
|
|
|
|
void DoubleSpinBox::StopTimer()
|
|
|
|
{
|
2016-02-12 00:38:21 -05:00
|
|
|
s_Timer.stop();
|
|
|
|
disconnect(&s_Timer, SIGNAL(timeout()), this, SLOT(OnTimeout()));
|
2015-05-15 21:45:15 -04:00
|
|
|
}
|