From 8f2218326a22977afddf3a6e2de3d22d519ab602 Mon Sep 17 00:00:00 2001 From: Person Date: Sat, 22 Jul 2017 18:30:25 -0700 Subject: [PATCH] Bug fixes: --Fix logic for showing context menu when the option for right click to toggle is true. --- Source/Fractorium/DoubleSpinBox.cpp | 64 ++++++++++++++++++++++++++--- Source/Fractorium/DoubleSpinBox.h | 26 ++++++++++-- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/Source/Fractorium/DoubleSpinBox.cpp b/Source/Fractorium/DoubleSpinBox.cpp index 425fb2b..94d7ba2 100644 --- a/Source/Fractorium/DoubleSpinBox.cpp +++ b/Source/Fractorium/DoubleSpinBox.cpp @@ -173,16 +173,18 @@ bool DoubleSpinBox::eventFilter(QObject* o, QEvent* e) if (isEnabled() && me) { + bool isRight = me->button() == Qt::RightButton; + if (!m_Settings->ToggleType() &&//Ensure double click toggles, not right click. me->type() == QMouseEvent::MouseButtonPress && - me->button() == Qt::RightButton) + isRight) { m_MouseDownPoint = m_MouseMovePoint = me->pos(); StartTimer(); } else if (!m_Settings->ToggleType() && me->type() == QMouseEvent::MouseButtonRelease && - me->button() == Qt::RightButton) + isRight) { StopTimer(); m_MouseDownPoint = m_MouseMovePoint = me->pos(); @@ -195,12 +197,14 @@ bool DoubleSpinBox::eventFilter(QObject* o, QEvent* e) } else if (m_DoubleClick && ((!m_Settings->ToggleType() && e->type() == QMouseEvent::MouseButtonDblClick && me->button() == Qt::LeftButton) || - (m_Settings->ToggleType() && me->type() == QMouseEvent::MouseButtonRelease && me->button() == Qt::RightButton))) + (m_Settings->ToggleType() && me->type() == QMouseEvent::MouseButtonRelease && isRight))) { if (IsClose(m_DoubleClickLowVal, value())) setValue(m_DoubleClickZero); else setValue(m_DoubleClickNonZero); + + return true; } } else @@ -288,6 +292,54 @@ void DoubleSpinBox::StopTimer() disconnect(&s_Timer, SIGNAL(timeout()), this, SLOT(OnTimeout())); } +/// +/// Constructor that does nothing but pass arguments to the base. +/// +/// The parent widget +/// The height of the spin box. Default: 16. +/// The step used to increment/decrement the spin box when using the mouse wheel. Default: 0.05. +SpecialDoubleSpinBox::SpecialDoubleSpinBox(QWidget* p, int h, double step) + : DoubleSpinBox(p, h, step) +{ +} + +/// +/// Event filter for taking special action on right click events. +/// +/// The object +/// The eevent +/// True to stop processing the event, else false./ +bool SpecialDoubleSpinBox::eventFilter(QObject* o, QEvent* e) +{ + if (isEnabled()) + { + auto me = dynamic_cast(e); + auto cme = dynamic_cast(e); + + if (m_DoubleClick && m_Settings->ToggleType())//If they use right click to toggle... + { + if (me) + { + if (me->type() == QMouseEvent::MouseButtonRelease && me->button() == Qt::RightButton) + { + if (me->modifiers().testFlag(Qt::ShiftModifier))//...then do not take the action if shift was pressed. + return false;//Shift was pressed, so continue normal event processing to show the menu, but do not call the base to toggle the value. + } + } + else if (cme)//Context menu. + { + if (!cme->modifiers().testFlag(Qt::ShiftModifier))//If they are not holding shift, call the base to toggle, and do not process further which suppresses showing the menu. + { + DoubleSpinBox::eventFilter(o, e); + return true; + } + } + } + } + + return DoubleSpinBox::eventFilter(o, e); +} + /// /// Constructor that passes agruments to the base and assigns the m_Param and m_Variation members. /// It also sets up the context menu for special numerical values. @@ -299,7 +351,7 @@ void DoubleSpinBox::StopTimer() /// The height of the spin box. Default: 16. /// The step used to increment/decrement the spin box when using the mouse wheel. Default: 0.05. VariationTreeDoubleSpinBox::VariationTreeDoubleSpinBox(QWidget* p, VariationTreeWidgetItem* widgetItem, eVariationId id, const string& param, int h, double step) - : DoubleSpinBox(p, h, step) + : SpecialDoubleSpinBox(p, h, step) { m_WidgetItem = widgetItem; m_Param = param; @@ -377,7 +429,7 @@ void VariationTreeDoubleSpinBox::SqrtThreeActionTriggered(bool checked) { setVal /// The height of the spin box. Default: 20. /// The step used to increment/decrement the spin box when using the mouse wheel. Default: 0.01. AffineDoubleSpinBox::AffineDoubleSpinBox(QWidget* p, int h, double step) - : DoubleSpinBox(p, h, step) + : SpecialDoubleSpinBox(p, h, step) { //-1 auto neg1Action = new QAction("-1", this); @@ -407,4 +459,4 @@ void AffineDoubleSpinBox::NegOneActionTriggered(bool checked) { setValue(-1); } void AffineDoubleSpinBox::ZeroActionTriggered(bool checked) { setValue(0); } void AffineDoubleSpinBox::OneActionTriggered(bool checked) { setValue(1); } void AffineDoubleSpinBox::FortyFiveActionTriggered(bool checked) { setValue(0.707107); } -void AffineDoubleSpinBox::NegFortyFiveActionTriggered(bool checked) { setValue(-0.707107); } +void AffineDoubleSpinBox::NegFortyFiveActionTriggered(bool checked) { setValue(-0.707107); } \ No newline at end of file diff --git a/Source/Fractorium/DoubleSpinBox.h b/Source/Fractorium/DoubleSpinBox.h index da59818..9d756e5 100644 --- a/Source/Fractorium/DoubleSpinBox.h +++ b/Source/Fractorium/DoubleSpinBox.h @@ -43,11 +43,13 @@ protected: virtual void enterEvent(QEvent* e); virtual void leaveEvent(QEvent* e); + bool m_DoubleClick; + shared_ptr m_Settings; + private: void StartTimer(); void StopTimer(); - bool m_DoubleClick; double m_DoubleClickLowVal; double m_DoubleClickNonZero; double m_DoubleClickZero; @@ -55,10 +57,26 @@ private: double m_SmallStep; QPoint m_MouseDownPoint; QPoint m_MouseMovePoint; - shared_ptr m_Settings; static QTimer s_Timer; }; +/// +/// Thin derivation to implement the eventFilter() override which subsequently derived +/// classes will use to suppress showing the context menu when right clicking is used for toggling, +/// unless shift is pressed. +/// +class SpecialDoubleSpinBox : public DoubleSpinBox +{ + Q_OBJECT + +public: + explicit SpecialDoubleSpinBox(QWidget* p = nullptr, int h = 16, double step = 0.05); + virtual ~SpecialDoubleSpinBox() { } + +protected: + virtual bool eventFilter(QObject* o, QEvent* e) override; +}; + /// /// VariationTreeWidgetItem and VariationTreeDoubleSpinBox need each other, but each can't include the other. /// So VariationTreeWidgetItem includes this file, and use a forward declaration here. @@ -69,7 +87,7 @@ class VariationTreeWidgetItem; /// Derivation for the double spin boxes that are in the /// variations tree. /// -class VariationTreeDoubleSpinBox : public DoubleSpinBox +class VariationTreeDoubleSpinBox : public SpecialDoubleSpinBox { Q_OBJECT @@ -105,7 +123,7 @@ private: /// Derivation for the double spin boxes that are in the /// affine controls. /// -class AffineDoubleSpinBox : public DoubleSpinBox +class AffineDoubleSpinBox : public SpecialDoubleSpinBox { Q_OBJECT