diff options
author | RJ Ryan <rryan@mixxx.org> | 2014-01-10 13:33:19 -0500 |
---|---|---|
committer | RJ Ryan <rryan@mixxx.org> | 2014-01-10 18:08:07 -0500 |
commit | 4837f3fb22cf9285cc0279c5d63380f2623cd7e5 (patch) | |
tree | a672493a93ee3b7e7a89146ecf227d13b307cca5 /src/widget | |
parent | bed60ca6d326a397fea175bbd0a75fe85989d7db (diff) |
Remove widget values.
* WWidget no longer stores a value independent of its connected controls.
* Make a connection explicitly the display value. For legacy reasons this is the
last connection with connectValueToWidget.
* Update all WWidget descendents to use the connected display value when
painting.
* Push buttons update the left or right connected control for push-button
behavior or toggling.
This allows WPushButton to toggle a multi-state button on left and right
click. This was previously impossible.
Diffstat (limited to 'src/widget')
-rw-r--r-- | src/widget/knobeventhandler.h | 51 | ||||
-rw-r--r-- | src/widget/wbasewidget.cpp | 79 | ||||
-rw-r--r-- | src/widget/wbasewidget.h | 9 | ||||
-rw-r--r-- | src/widget/wdisplay.cpp | 2 | ||||
-rw-r--r-- | src/widget/wknobcomposed.cpp | 2 | ||||
-rw-r--r-- | src/widget/wpushbutton.cpp | 31 | ||||
-rw-r--r-- | src/widget/wslidercomposed.cpp | 21 | ||||
-rw-r--r-- | src/widget/wslidercomposed.h | 1 | ||||
-rw-r--r-- | src/widget/wstatuslight.cpp | 1 | ||||
-rw-r--r-- | src/widget/wvumeter.cpp | 3 | ||||
-rw-r--r-- | src/widget/wwidget.cpp | 5 | ||||
-rw-r--r-- | src/widget/wwidget.h | 14 |
12 files changed, 135 insertions, 84 deletions
diff --git a/src/widget/knobeventhandler.h b/src/widget/knobeventhandler.h index c83aa25c29..24df1c061b 100644 --- a/src/widget/knobeventhandler.h +++ b/src/widget/knobeventhandler.h @@ -16,29 +16,32 @@ class KnobEventHandler { : m_bRightButtonPressed(false) { } + double valueFromMouseEvent(T* pWidget, QMouseEvent* e) { + QPoint cur(e->globalPos()); + QPoint diff(cur - m_startPos); + double dist = sqrt(static_cast<double>(diff.x() * diff.x() + diff.y() * diff.y())); + bool y_dominant = abs(diff.y()) > abs(diff.x()); + + // if y is dominant, then thread an increase in dy as negative (y is + // pointed downward). Otherwise, if y is not dominant and x has + // decreased, then thread it as negative. + if ((y_dominant && diff.y() > 0) || (!y_dominant && diff.x() < 0)) { + dist = -dist; + } + + // For legacy (MIDI) reasons this is tuned to 127. + double value = pWidget->getConnectedControlLeft() + dist / 127.0; + + // Clamp to [0.0, 1.0] + value = math_max(0.0, math_min(1.0, value)); + + return value; + } + void mouseMoveEvent(T* pWidget, QMouseEvent* e) { if (!m_bRightButtonPressed) { - QPoint cur(e->globalPos()); - QPoint diff(cur - m_startPos); - double dist = sqrt(static_cast<double>(diff.x() * diff.x() + diff.y() * diff.y())); - bool y_dominant = abs(diff.y()) > abs(diff.x()); - - // if y is dominant, then thread an increase in dy as negative (y is - // pointed downward). Otherwise, if y is not dominant and x has - // decreased, then thread it as negative. - if ((y_dominant && diff.y() > 0) || (!y_dominant && diff.x() < 0)) { - dist = -dist; - } - - double value = pWidget->getValue(); - // For legacy (MIDI) reasons this is tuned to 127. - value += dist / 127.0; QCursor::setPos(m_startPos); - - // Clamp to [0.0, 1.0] - value = math_max(0.0, math_min(1.0, value)); - - pWidget->onConnectedControlValueChanged(value); + double value = valueFromMouseEvent(pWidget, e); pWidget->setConnectedControlLeftDown(value); pWidget->update(); } @@ -61,12 +64,15 @@ class KnobEventHandler { } void mouseReleaseEvent(T* pWidget, QMouseEvent* e) { + double value = 0.0; switch (e->button()) { case Qt::LeftButton: case Qt::MidButton: QCursor::setPos(m_startPos); QApplication::restoreOverrideCursor(); - pWidget->setConnectedControlLeftUp(pWidget->getValue()); + value = valueFromMouseEvent(pWidget, e); + pWidget->setConnectedControlLeftUp(value); + pWidget->update(); break; case Qt::RightButton: m_bRightButtonPressed = false; @@ -81,12 +87,11 @@ class KnobEventHandler { void wheelEvent(T* pWidget, QWheelEvent* e) { // For legacy (MIDI) reasons this is tuned to 127. double wheelDirection = e->delta() / (120.0 * 127.0); - double newValue = pWidget->getValue() + wheelDirection; + double newValue = pWidget->getConnectedControl() + wheelDirection; // Clamp to [0.0, 1.0] newValue = math_max(0.0, math_min(1.0, newValue)); - pWidget->setValue(newValue); pWidget->setConnectedControlDown(newValue); pWidget->setConnectedControlUp(newValue); pWidget->update(); diff --git a/src/widget/wbasewidget.cpp b/src/widget/wbasewidget.cpp index f49452d957..4627a967c7 100644 --- a/src/widget/wbasewidget.cpp +++ b/src/widget/wbasewidget.cpp @@ -14,6 +14,10 @@ ControlWidgetConnection::ControlWidgetConnection(WBaseWidget* pBaseWidget, ControlWidgetConnection::~ControlWidgetConnection() { } +double ControlWidgetConnection::get() const { + return m_pControl->get(); +} + ValueControlWidgetConnection::ValueControlWidgetConnection(WBaseWidget* pBaseWidget, ControlObjectSlave* pControl, bool connectValueFromWidget, @@ -88,12 +92,17 @@ void DisabledControlWidgetConnection::setUp(double v) { WBaseWidget::WBaseWidget(QWidget* pWidget) : m_pWidget(pWidget), - m_bDisabled(false) { + m_bDisabled(false), + m_pDisplayConnection(NULL) { } WBaseWidget::~WBaseWidget() { } +void WBaseWidget::setDisplayConnection(ControlWidgetConnection* pConnection) { + m_pDisplayConnection = pConnection; +} + void WBaseWidget::addConnection(ControlWidgetConnection* pConnection) { m_connections.append(pConnection); } @@ -106,6 +115,46 @@ void WBaseWidget::addRightConnection(ControlWidgetConnection* pConnection) { m_rightConnections.append(pConnection); } +double WBaseWidget::getConnectedControl() const { + if (!m_leftConnections.isEmpty()) { + return m_leftConnections[0]->get(); + } + if (!m_rightConnections.isEmpty()) { + return m_rightConnections[0]->get(); + } + if (!m_connections.isEmpty()) { + return m_connections[0]->get(); + } + return 0.0; +} + +double WBaseWidget::getConnectedControlLeft() const { + if (!m_leftConnections.isEmpty()) { + return m_leftConnections[0]->get(); + } + if (!m_connections.isEmpty()) { + return m_connections[0]->get(); + } + return 0.0; +} + +double WBaseWidget::getConnectedControlRight() const { + if (!m_rightConnections.isEmpty()) { + return m_rightConnections[0]->get(); + } + if (!m_connections.isEmpty()) { + return m_connections[0]->get(); + } + return 0.0; +} + +double WBaseWidget::getConnectedDisplayValue() const { + if (m_pDisplayConnection) { + return m_pDisplayConnection->get(); + } + return 0.0; +} + void WBaseWidget::resetConnectedControls() { foreach (ControlWidgetConnection* pControlConnection, m_leftConnections) { pControlConnection->reset(); @@ -119,12 +168,24 @@ void WBaseWidget::resetConnectedControls() { } void WBaseWidget::setConnectedControlDown(double v) { + foreach (ControlWidgetConnection* pControlConnection, m_leftConnections) { + pControlConnection->setDown(v); + } + foreach (ControlWidgetConnection* pControlConnection, m_rightConnections) { + pControlConnection->setDown(v); + } foreach (ControlWidgetConnection* pControlConnection, m_connections) { pControlConnection->setDown(v); } } void WBaseWidget::setConnectedControlUp(double v) { + foreach (ControlWidgetConnection* pControlConnection, m_leftConnections) { + pControlConnection->setUp(v); + } + foreach (ControlWidgetConnection* pControlConnection, m_rightConnections) { + pControlConnection->setUp(v); + } foreach (ControlWidgetConnection* pControlConnection, m_connections) { pControlConnection->setUp(v); } @@ -134,26 +195,34 @@ void WBaseWidget::setConnectedControlLeftDown(double v) { foreach (ControlWidgetConnection* pControlConnection, m_leftConnections) { pControlConnection->setDown(v); } - setConnectedControlDown(v); + foreach (ControlWidgetConnection* pControlConnection, m_connections) { + pControlConnection->setDown(v); + } } void WBaseWidget::setConnectedControlLeftUp(double v) { foreach (ControlWidgetConnection* pControlConnection, m_leftConnections) { pControlConnection->setUp(v); } - setConnectedControlUp(v); + foreach (ControlWidgetConnection* pControlConnection, m_connections) { + pControlConnection->setUp(v); + } } void WBaseWidget::setConnectedControlRightDown(double v) { foreach (ControlWidgetConnection* pControlConnection, m_rightConnections) { pControlConnection->setDown(v); } - setConnectedControlDown(v); + foreach (ControlWidgetConnection* pControlConnection, m_connections) { + pControlConnection->setDown(v); + } } void WBaseWidget::setConnectedControlRightUp(double v) { foreach (ControlWidgetConnection* pControlConnection, m_rightConnections) { pControlConnection->setUp(v); } - setConnectedControlUp(v); + foreach (ControlWidgetConnection* pControlConnection, m_connections) { + pControlConnection->setUp(v); + } } diff --git a/src/widget/wbasewidget.h b/src/widget/wbasewidget.h index 0655de8727..6a98778bdd 100644 --- a/src/widget/wbasewidget.h +++ b/src/widget/wbasewidget.h @@ -26,6 +26,8 @@ class ControlWidgetConnection : public QObject { ControlObjectSlave* pControl); virtual ~ControlWidgetConnection(); + double get() const; + virtual void reset() = 0; virtual void setDown(double v) = 0; virtual void setUp(double v) = 0; @@ -107,6 +109,12 @@ class WBaseWidget { void addLeftConnection(ControlWidgetConnection* pConnection); void addRightConnection(ControlWidgetConnection* pConnection); void addConnection(ControlWidgetConnection* pConnection); + void setDisplayConnection(ControlWidgetConnection* pConnection); + + double getConnectedControl() const; + double getConnectedControlLeft() const; + double getConnectedControlRight() const; + double getConnectedDisplayValue() const; protected: virtual void onConnectedControlValueChanged(double v) { @@ -126,6 +134,7 @@ class WBaseWidget { bool m_bDisabled; QString m_baseTooltip; QList<ControlWidgetConnection*> m_connections; + ControlWidgetConnection* m_pDisplayConnection; QList<ControlWidgetConnection*> m_leftConnections; QList<ControlWidgetConnection*> m_rightConnections; diff --git a/src/widget/wdisplay.cpp b/src/widget/wdisplay.cpp index 614ebaa5a3..6791760f64 100644 --- a/src/widget/wdisplay.cpp +++ b/src/widget/wdisplay.cpp @@ -145,7 +145,7 @@ int WDisplay::getActivePixmapIndex() const { // Subtracting an epsilon prevents out of bound values at the end of the // range and biases the middle value towards the lower of the 2 center // pixmaps when there are an even number of pixmaps. - return static_cast<int>(getValue() * numPixmaps() - 0.00001); + return static_cast<int>(getConnectedDisplayValue() * numPixmaps() - 0.00001); } void WDisplay::paintEvent(QPaintEvent* ) { diff --git a/src/widget/wknobcomposed.cpp b/src/widget/wknobcomposed.cpp index d2c747ff47..5f9e9c06fa 100644 --- a/src/widget/wknobcomposed.cpp +++ b/src/widget/wknobcomposed.cpp @@ -72,7 +72,7 @@ void WKnobComposed::paintEvent(QPaintEvent* e) { p.translate(width() / 2.0, height() / 2.0); // Value is in the range [0, 1]. - double value = getValue(); + double value = getConnectedDisplayValue(); double angle = m_dMinAngle + (m_dMaxAngle - m_dMinAngle) * value; p.rotate(angle); diff --git a/src/widget/wpushbutton.cpp b/src/widget/wpushbutton.cpp index 7ca392f4ae..0f7b2df520 100644 --- a/src/widget/wpushbutton.cpp +++ b/src/widget/wpushbutton.cpp @@ -126,7 +126,6 @@ void WPushButton::setup(QDomNode node, const SkinContext& context) { } void WPushButton::setStates(int iStates) { - setValue(0.0); m_bPressed = false; m_iNoStates = 0; @@ -173,14 +172,8 @@ void WPushButton::setPixmapBackground(const QString &filename) { } void WPushButton::onConnectedControlValueChanged(double v) { - setValue(v); - if (m_iNoStates == 1) { - if (v == 1.0) { - m_bPressed = true; - } else { - m_bPressed = false; - } + m_bPressed = v == 1.0; } update(); } @@ -192,7 +185,7 @@ void WPushButton::paintEvent(QPaintEvent* e) { QStylePainter p(this); p.drawPrimitive(QStyle::PE_Widget, option); - double value = getValue(); + double value = getConnectedDisplayValue(); if (m_iNoStates == 0) { return; } @@ -230,11 +223,10 @@ void WPushButton::mousePressEvent(QMouseEvent * e) { if (leftPowerWindowStyle && m_iNoStates == 2) { if (leftClick) { - if (getValue() == 0.0) { + if (getConnectedControlLeft() == 0.0) { m_clickTimer.setSingleShot(true); m_clickTimer.start(ControlPushButtonBehavior::kPowerWindowTimeMillis); } - setValue(1.0); m_bPressed = true; setConnectedControlLeftDown(1.0); update(); @@ -252,7 +244,6 @@ void WPushButton::mousePressEvent(QMouseEvent * e) { update(); } else if (m_iNoStates == 1) { // This is a Pushbutton - setValue(1.0); m_bPressed = true; setConnectedControlRightDown(1.0); update(); @@ -270,18 +261,11 @@ void WPushButton::mousePressEvent(QMouseEvent * e) { if (leftClick) { double emitValue; - if (m_bLeftClickForcePush) { - // This may a button with different functions on each mouse button - // m_value is changed by a separate feedback connection - emitValue = 1.0; - } else if (m_iNoStates == 1) { - // This is a Pushbutton + if (m_bLeftClickForcePush || m_iNoStates == 1) { emitValue = 1.0; - setValue(emitValue); } else { // Toggle thru the states - emitValue = (int)(getValue() + 1.) % m_iNoStates; - setValue(emitValue); + emitValue = (int)(getConnectedControlLeft() + 1.0) % m_iNoStates; if (leftLongPressLatchingStyle) { m_clickTimer.setSingleShot(true); m_clickTimer.start(ControlPushButtonBehavior::kLongPressLatchingTimeMillis); @@ -310,7 +294,6 @@ void WPushButton::mouseReleaseEvent(QMouseEvent * e) { const bool rightButtonDown = QApplication::mouseButtons() & Qt::RightButton; if (m_bPressed && !m_clickTimer.isActive() && !rightButtonDown) { // Release button after timer, but not if right button is clicked - setValue(0.0); setConnectedControlLeftUp(0.0); } m_bPressed = false; @@ -338,7 +321,7 @@ void WPushButton::mouseReleaseEvent(QMouseEvent * e) { } if (leftClick) { - double emitValue = getValue(); + double emitValue = getConnectedControlLeft(); if (m_bLeftClickForcePush) { // This may a klickButton with different functions on each mouse button // m_fValue is changed by a separate feedback connection @@ -346,12 +329,10 @@ void WPushButton::mouseReleaseEvent(QMouseEvent * e) { } else if (m_iNoStates == 1) { // This is a Pushbutton emitValue = 0.0; - setValue(emitValue); } else { if (leftLongPressLatchingStyle && m_clickTimer.isActive() && emitValue >= 1.0) { // revert toggle if button is released too early emitValue = (int)(emitValue - 1.0) % m_iNoStates; - setValue(emitValue); } else { // Nothing special happens when releasing a normal toggle button } diff --git a/src/widget/wslidercomposed.cpp b/src/widget/wslidercomposed.cpp index 961208754a..1af40c73e6 100644 --- a/src/widget/wslidercomposed.cpp +++ b/src/widget/wslidercomposed.cpp @@ -26,6 +26,7 @@ WSliderComposed::WSliderComposed(QWidget * parent) : WWidget(parent), + m_dOldValue(0.0), m_bRightButtonPressed(false), m_iPos(0), m_iStartHandlePos(0), @@ -81,7 +82,7 @@ void WSliderComposed::setHandlePixmap(bool bHorizontal, const QString& filenameH m_iHandleLength = m_bHorizontal ? m_pHandle->width() : m_pHandle->height(); - onConnectedControlValueChanged(getValue()); + onConnectedControlValueChanged(getConnectedControlLeft()); update(); } } @@ -118,7 +119,10 @@ void WSliderComposed::mouseMoveEvent(QMouseEvent * e) { if (!m_bHorizontal) { newValue = 1.0 - newValue; } - setValue(newValue); + + // If we don't change this, then updates might be rejected in + // onConnectedControlValueChanged. + m_dOldValue = newValue; // Emit valueChanged signal if (m_bEventWhileDrag) { @@ -137,12 +141,11 @@ void WSliderComposed::mouseMoveEvent(QMouseEvent * e) { void WSliderComposed::wheelEvent(QWheelEvent *e) { // For legacy (MIDI) reasons this is tuned to 127. double wheelDirection = ((QWheelEvent *)e)->delta() / (120.0 * 127.0); - double newValue = getValue() + wheelDirection; + double newValue = getConnectedControl() + wheelDirection; // Clamp to [0.0, 1.0] newValue = math_max(0.0, math_min(1.0, newValue)); - setValue(newValue); setConnectedControlDown(newValue); setConnectedControlUp(newValue); update(); @@ -157,9 +160,9 @@ void WSliderComposed::mouseReleaseEvent(QMouseEvent * e) { mouseMoveEvent(e); if (e->button() == Qt::RightButton) { - setConnectedControlRightUp(getValue()); + setConnectedControlRightUp(getConnectedControlRight()); } else { - setConnectedControlLeftUp(getValue()); + setConnectedControlLeftUp(getConnectedControlLeft()); } m_bDrag = false; @@ -208,10 +211,8 @@ void WSliderComposed::paintEvent(QPaintEvent *) { } void WSliderComposed::onConnectedControlValueChanged(double dValue) { - if (!m_bDrag && getValue() != dValue) { - // Set value without emitting a valueChanged signal - // and force display update - setValue(dValue); + if (!m_bDrag && m_dOldValue != dValue) { + m_dOldValue = dValue; // Calculate handle position if (!m_bHorizontal) { diff --git a/src/widget/wslidercomposed.h b/src/widget/wslidercomposed.h index 86ce224184..414524c3ea 100644 --- a/src/widget/wslidercomposed.h +++ b/src/widget/wslidercomposed.h @@ -55,6 +55,7 @@ public slots: private: void unsetPixmaps(); + double m_dOldValue; // True if right mouse button is pressed. bool m_bRightButtonPressed; /** Internal storage of slider position in pixels */ diff --git a/src/widget/wstatuslight.cpp b/src/widget/wstatuslight.cpp index 44f3128c87..829ecd8636 100644 --- a/src/widget/wstatuslight.cpp +++ b/src/widget/wstatuslight.cpp @@ -45,7 +45,6 @@ void WStatusLight::setNoPos(int iNoPos) { iNoPos = 2; } m_pixmaps.resize(iNoPos); - setValue(0.0); } void WStatusLight::setup(QDomNode node, const SkinContext& context) { diff --git a/src/widget/wvumeter.cpp b/src/widget/wvumeter.cpp index f3dab29e8c..6d48c5e70e 100644 --- a/src/widget/wvumeter.cpp +++ b/src/widget/wvumeter.cpp @@ -112,7 +112,6 @@ void WVuMeter::onConnectedControlValueChanged(double dValue) { idx = 0; setPeak(idx); - setValue(dValue); QTime currentTime = QTime::currentTime(); int msecsElapsed = m_lastUpdate.msecsTo(currentTime); @@ -159,7 +158,7 @@ void WVuMeter::paintEvent(QPaintEvent *) { } if (!m_pPixmapVu.isNull() && !m_pPixmapVu->isNull()) { - int idx = static_cast<int>(getValue() * m_iNoPos); + int idx = static_cast<int>(getConnectedDisplayValue() * m_iNoPos); // Range check if (idx > m_iNoPos) diff --git a/src/widget/wwidget.cpp b/src/widget/wwidget.cpp index 8023bd00e9..9f21af1819 100644 --- a/src/widget/wwidget.cpp +++ b/src/widget/wwidget.cpp @@ -21,8 +21,7 @@ WWidget::WWidget(QWidget* parent, Qt::WindowFlags flags) : QWidget(parent, flags), - WBaseWidget(this), - m_value(0.0) { + WBaseWidget(this) { setAttribute(Qt::WA_StaticContents); setFocusPolicy(Qt::ClickFocus); } @@ -31,6 +30,6 @@ WWidget::~WWidget() { } void WWidget::onConnectedControlValueChanged(double value) { - m_value = value; + Q_UNUSED(value); update(); } diff --git a/src/widget/wwidget.h b/src/widget/wwidget.h index d0015fb973..27ff4eaffd 100644 --- a/src/widget/wwidget.h +++ b/src/widget/wwidget.h @@ -42,21 +42,9 @@ public: virtual ~WWidget(); Q_PROPERTY(bool controlDisabled READ controlDisabled); - Q_PROPERTY(double value READ getValue); - - double getValue() const { - return m_value; - } - - void setValue(double value) { - m_value = value; - } + Q_PROPERTY(double value READ getConnectedDisplayValue); virtual void onConnectedControlValueChanged(double value); - - private: - // Value/state of widget - double m_value; }; #endif |