diff --git a/common/lc_doublespinbox.cpp b/common/lc_doublespinbox.cpp index d7f93605..b45345ee 100644 --- a/common/lc_doublespinbox.cpp +++ b/common/lc_doublespinbox.cpp @@ -9,13 +9,13 @@ lcDoubleSpinBox::lcDoubleSpinBox(QWidget* Parent) { lineEdit()->installEventFilter(this); + connect(this, QOverload::of(&QDoubleSpinBox::valueChanged), this, &lcDoubleSpinBox::ValueChanged); connect(lineEdit(), &QLineEdit::returnPressed, this, &lcDoubleSpinBox::ReturnPressed); } void lcDoubleSpinBox::SetValue(double Value) { - if (!hasFocus()) - mInitialValue = Value; + mModified = false; setValue(Value); } @@ -48,31 +48,46 @@ void lcDoubleSpinBox::SetSnap(lcFloatPropertySnap Snap) } } +void lcDoubleSpinBox::ShowLadderWidget() +{ + lcLadderWidget* LadderWidget = new lcLadderWidget(this, mSnap); + + mLadderWidgetInitialValue = value(); + + connect(LadderWidget, &lcLadderWidget::EditingCanceled, this, &lcDoubleSpinBox::LadderWidgetCanceled); + + LadderWidget->Show(); +} + +void lcDoubleSpinBox::LadderWidgetCanceled() +{ + setValue(mLadderWidgetInitialValue); +} + QString lcDoubleSpinBox::textFromValue(double Value) const { return lcFormatValueLocalized(Value); } +void lcDoubleSpinBox::ValueChanged() +{ + mModified = true; +} + void lcDoubleSpinBox::CancelEditing() { - if (value() == mInitialValue) - return; - - emit EditingCanceled(); - - setValue(mInitialValue); + if (mModified) + { + emit EditingCanceled(); + } } void lcDoubleSpinBox::FinishEditing() { - if (value() == mInitialValue) - return; - - emit EditingFinished(); - - mInitialValue = value(); - - clearFocus(); + if (mModified) + { + emit EditingFinished(); + } } void lcDoubleSpinBox::ReturnPressed() @@ -80,128 +95,14 @@ void lcDoubleSpinBox::ReturnPressed() FinishEditing(); } -void lcDoubleSpinBox::HandleMousePressEvent(QMouseEvent* MouseEvent) -{ - if (MouseEvent->buttons() == Qt::LeftButton) - { - mDragMode = DragMode::Start; - mLastPosition = GetGlobalMousePosition(MouseEvent); - } - else - { - if (mDragMode == DragMode::Value) - CancelEditing(); - - if (mDragMode != DragMode::None) - { - mDragMode = DragMode::None; - return; - } - - if (MouseEvent->buttons() == Qt::MiddleButton) - { - lcLadderWidget* LadderWidget = new lcLadderWidget(this, mSnap); - - connect(LadderWidget, &lcLadderWidget::EditingCanceled, this, &lcDoubleSpinBox::CancelEditing); - connect(LadderWidget, &lcLadderWidget::EditingFinished, this, &lcDoubleSpinBox::FinishEditing); - - mInitialValue = value(); - - LadderWidget->Show(); - } - } -} - -bool lcDoubleSpinBox::HandleMouseMoveEvent(QMouseEvent* MouseEvent, QObject* Object) -{ - if (mDragMode != DragMode::None) - { - if (mDragMode == DragMode::Start) - { - if (lineEdit()->selectionLength() > 0 && Object != this) - { - mDragMode = DragMode::None; - } - else - { - QPoint Position = GetGlobalMousePosition(MouseEvent); - - if (abs(mLastPosition.y() - Position.y()) > 3) - { - mDragMode = DragMode::Value; - mLastPosition = QCursor::pos(); - } - } - } - - if (mDragMode == DragMode::Value) - { - QPoint Position = QCursor::pos(); - - stepBy(mLastPosition.y() - Position.y()); - - QScreen* Screen = QGuiApplication::screenAt(mapToGlobal(QPoint(width() / 2, height() / 2))); - - if (Screen) - { - int ScreenHeight = Screen->geometry().height(); - - if (Position.y() <= 0) - { - Position.setY(ScreenHeight - 2); - - QCursor::setPos(Position); - } - else if (Position.y() >= ScreenHeight - 1) - { - Position.setY(1); - - QCursor::setPos(Position); - } - } - - mLastPosition = Position; - - MouseEvent->accept(); - - return true; - } - } - - return false; -} - -void lcDoubleSpinBox::HandleMouseReleaseEvent(QMouseEvent* MouseEvent) -{ - Q_UNUSED(MouseEvent); - - if (mDragMode == DragMode::Value) - FinishEditing(); - - mDragMode = DragMode::None; -} - bool lcDoubleSpinBox::eventFilter(QObject* Object, QEvent* Event) { - switch (Event->type()) + if (mDragMode == DragMode::None && Event->type() == QEvent::MouseButtonPress && Object == lineEdit()) { - case QEvent::MouseButtonPress: - if (Object == lineEdit()) - HandleMousePressEvent(reinterpret_cast(Event)); - break; + QMouseEvent* MouseEvent = reinterpret_cast(Event); - case QEvent::MouseMove: - if (Object == lineEdit()) - HandleMouseMoveEvent(reinterpret_cast(Event), Object); - break; - - case QEvent::MouseButtonRelease: - if (Object == lineEdit()) - HandleMouseReleaseEvent(reinterpret_cast(Event)); - break; - - default: - break; + if (MouseEvent->buttons() == Qt::MiddleButton) + ShowLadderWidget(); } return QDoubleSpinBox::eventFilter(Object, Event); @@ -209,25 +110,82 @@ bool lcDoubleSpinBox::eventFilter(QObject* Object, QEvent* Event) void lcDoubleSpinBox::mousePressEvent(QMouseEvent* MouseEvent) { - HandleMousePressEvent(MouseEvent); + if (mDragMode == DragMode::Dragging) + { + if (MouseEvent->buttons() != Qt::LeftButton) + { + CancelEditing(); - double InitialValue = value(); + mDragMode = DragMode::None; + } + } + else if (mDragMode == DragMode::None) + { + if (MouseEvent->buttons() == Qt::LeftButton) + { + mDragMode = DragMode::Start; + mLastPosition = GetGlobalMousePosition(MouseEvent); + } + else if (MouseEvent->buttons() == Qt::MiddleButton) + { + ShowLadderWidget(); + } + } + else + { + mDragMode = DragMode::None; + } QDoubleSpinBox::mousePressEvent(MouseEvent); - - if (value() != InitialValue) - FinishEditing(); } void lcDoubleSpinBox::mouseMoveEvent(QMouseEvent* MouseEvent) { - if (!HandleMouseMoveEvent(MouseEvent, this)) - QDoubleSpinBox::mouseMoveEvent(MouseEvent); + if (mDragMode == DragMode::Dragging) + { + QPoint Position = QCursor::pos(); + + stepBy(mLastPosition.y() - Position.y()); + + QScreen* Screen = QGuiApplication::screenAt(Position); + + if (Screen) + { + int ScreenHeight = Screen->geometry().height(); + + if (Position.y() <= 0) + { + Position.setY(ScreenHeight - 2); + + QCursor::setPos(Position); + } + else if (Position.y() >= ScreenHeight - 1) + { + Position.setY(1); + + QCursor::setPos(Position); + } + } + + mLastPosition = Position; + } + else if (mDragMode == DragMode::Start) + { + QPoint Position = GetGlobalMousePosition(MouseEvent); + + if (abs(mLastPosition.y() - Position.y()) > 3) + { + mDragMode = DragMode::Dragging; + mLastPosition = QCursor::pos(); + } + } + + QDoubleSpinBox::mouseMoveEvent(MouseEvent); } void lcDoubleSpinBox::mouseReleaseEvent(QMouseEvent* MouseEvent) { - HandleMouseReleaseEvent(MouseEvent); + mDragMode = DragMode::None; QDoubleSpinBox::mouseReleaseEvent(MouseEvent); } @@ -240,8 +198,7 @@ bool lcDoubleSpinBox::event(QEvent* Event) if (KeyEvent->key() == Qt::Key_Escape) { - if (mDragMode == DragMode::Value) - CancelEditing(); + CancelEditing(); mDragMode = DragMode::None; @@ -253,6 +210,7 @@ bool lcDoubleSpinBox::event(QEvent* Event) else if (KeyEvent->key() == Qt::Key_Return || KeyEvent->key() == Qt::Key_Enter) { FinishEditing(); + KeyEvent->accept(); return true; @@ -262,10 +220,16 @@ bool lcDoubleSpinBox::event(QEvent* Event) return QDoubleSpinBox::event(Event); } +void lcDoubleSpinBox::focusInEvent(QFocusEvent* FocusEvent) +{ + mModified = false; + + QDoubleSpinBox::focusInEvent(FocusEvent); +} + void lcDoubleSpinBox::focusOutEvent(QFocusEvent* FocusEvent) { - if (value() != mInitialValue) - CancelEditing(); + FinishEditing(); QDoubleSpinBox::focusOutEvent(FocusEvent); } diff --git a/common/lc_doublespinbox.h b/common/lc_doublespinbox.h index 04b8ab88..a6dbd28a 100644 --- a/common/lc_doublespinbox.h +++ b/common/lc_doublespinbox.h @@ -19,6 +19,7 @@ public: void mousePressEvent(QMouseEvent* MouseEvent) override; void mouseMoveEvent(QMouseEvent* MouseEvent) override; void mouseReleaseEvent(QMouseEvent* MouseEvent) override; + void focusInEvent(QFocusEvent* FocusEvent) override; void focusOutEvent(QFocusEvent* FocusEvent) override; signals: @@ -26,13 +27,17 @@ signals: void EditingFinished(); protected slots: + void ValueChanged(); void CancelEditing(); void FinishEditing(); void ReturnPressed(); + void LadderWidgetCanceled(); protected: bool event(QEvent* Event) override; + void ShowLadderWidget(); + static QPoint GetGlobalMousePosition(QMouseEvent* MouseEvent) { #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) @@ -42,18 +47,15 @@ protected: #endif } - void HandleMousePressEvent(QMouseEvent* MouseEvent); - bool HandleMouseMoveEvent(QMouseEvent* MouseEvent, QObject* Object); - void HandleMouseReleaseEvent(QMouseEvent* MouseEvent); - enum class DragMode { None, Start, - Value + Dragging }; - double mInitialValue = 0.0; + bool mModified = false; + double mLadderWidgetInitialValue = 0.0; QPoint mLastPosition; DragMode mDragMode = DragMode::None; lcFloatPropertySnap mSnap = lcFloatPropertySnap::Auto; diff --git a/common/lc_minifigdialog.cpp b/common/lc_minifigdialog.cpp index 61ab5fa8..b09ab2b5 100644 --- a/common/lc_minifigdialog.cpp +++ b/common/lc_minifigdialog.cpp @@ -215,14 +215,14 @@ void lcMinifigDialog::on_TemplateComboBox_currentIndexChanged(const QString& Tem mMinifigWizard->SetColorIndex(PartIdx, lcGetColorIndex(Template.ColorCodes[PartIdx])); mColorPickers[PartIdx]->SetCurrentColorCode(Template.ColorCodes[PartIdx]); - QDoubleSpinBox* AngleSpinBox = mSpinBoxes[PartIdx]; + lcDoubleSpinBox* AngleSpinBox = mSpinBoxes[PartIdx]; if (AngleSpinBox) { QSignalBlocker AngleBlocker(AngleSpinBox); mMinifigWizard->SetAngle(PartIdx, Template.Angles[PartIdx]); - AngleSpinBox->setValue(Template.Angles[PartIdx]); + AngleSpinBox->SetValue(Template.Angles[PartIdx]); } } @@ -402,7 +402,7 @@ void lcMinifigDialog::ColorChanged(int Index) void lcMinifigDialog::AngleChanged(double Value) { - std::array::iterator Search = std::find(mSpinBoxes.begin(), mSpinBoxes.end(), sender()); + std::array::iterator Search = std::find(mSpinBoxes.begin(), mSpinBoxes.end(), sender()); if (Search == mSpinBoxes.end()) return; diff --git a/common/lc_minifigdialog.h b/common/lc_minifigdialog.h index fd22b8d5..bfe3ef5a 100644 --- a/common/lc_minifigdialog.h +++ b/common/lc_minifigdialog.h @@ -1,7 +1,9 @@ #pragma once #include "minifig.h" + class lcColorPicker; +class lcDoubleSpinBox; namespace Ui { @@ -36,7 +38,7 @@ protected: lcView* mView; std::array mPieceButtons; std::array mColorPickers; - std::array mSpinBoxes; + std::array mSpinBoxes; QString mCurrentTemplateName; bool mCurrentTemplateModified = false;