# HG changeset patch # User Chris Cannam # Date 1585307096 0 # Node ID bbc3f537564c340a20ced47c6d79c3f08af3e8bf # Parent 073ef72e8e60d8e37a8dee420c53c1bc9c01c9b1 Add context menu to Thumbwheel diff -r 073ef72e8e60 -r bbc3f537564c widgets/Thumbwheel.cpp --- a/widgets/Thumbwheel.cpp Fri Mar 27 11:04:48 2020 +0000 +++ b/widgets/Thumbwheel.cpp Fri Mar 27 11:04:56 2020 +0000 @@ -24,6 +24,9 @@ #include #include #include +#include + +#include "MenuTitle.h" #include #include @@ -46,13 +49,51 @@ m_atDefault(true), m_clickRotation(m_rotation), m_showTooltip(true), + m_provideContextMenu(true), + m_lastContextMenu(nullptr), m_rangeMapper(nullptr) { + setContextMenuPolicy(Qt::CustomContextMenu); + connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), + this, SLOT(contextMenuRequested(const QPoint &))); } Thumbwheel::~Thumbwheel() { delete m_rangeMapper; + delete m_lastContextMenu; +} + +void +Thumbwheel::contextMenuRequested(const QPoint &pos) +{ + if (!m_provideContextMenu) { + return; + } + + if (m_lastContextMenu) { + delete m_lastContextMenu; + } + + QMenu *m = new QMenu; + + if (m_title == "") { + MenuTitle::addTitle(m, tr("Thumbwheel")); + } else { + MenuTitle::addTitle(m, m_title); + } + + m->addAction(tr("&Edit..."), + [=]() { + edit(); + }); + m->addAction(tr("&Reset to Default"), + [=]() { + resetToDefault(); + }); + + m->popup(mapToGlobal(pos)); + m_lastContextMenu = m; } void @@ -183,7 +224,11 @@ m_rotation = float(m_value - m_min) / float(m_max - m_min); m_cache = QImage(); - if (isVisible()) update(); + + if (isVisible()) { + update(); + updateTitle(); + } } void @@ -223,17 +268,30 @@ } } + updateTitle(); +} + +void +Thumbwheel::updateTitle() +{ + QString name = objectName(); + QString unit = ""; + QString text; + double mappedValue = getMappedValue(); + + if (m_rangeMapper) unit = m_rangeMapper->getUnit(); + if (name != "") { + text = tr("%1: %2%3").arg(name).arg(mappedValue).arg(unit); + } else { + text = tr("%2%3").arg(mappedValue).arg(unit); + } + + m_title = text; + if (m_showTooltip) { - QString name = objectName(); - QString unit = ""; - QString text; - if (m_rangeMapper) unit = m_rangeMapper->getUnit(); - if (name != "") { - text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit); - } else { - text = tr("%2%3").arg(m_mappedValue).arg(unit); - } setToolTip(text); + } else { + setToolTip(""); } } @@ -323,6 +381,12 @@ return; } + edit(); +} + +void +Thumbwheel::edit() +{ bool ok = false; if (m_rangeMapper) { diff -r 073ef72e8e60 -r bbc3f537564c widgets/Thumbwheel.h --- a/widgets/Thumbwheel.h Fri Mar 27 11:04:48 2020 +0000 +++ b/widgets/Thumbwheel.h Fri Mar 27 11:04:56 2020 +0000 @@ -24,6 +24,7 @@ #include "WheelCounter.h" class RangeMapper; +class QMenu; class Thumbwheel : public QWidget { @@ -46,6 +47,7 @@ double getMappedValue() const; void setShowToolTip(bool show); + void setProvideContextMenu(bool provide); QSize sizeHint() const override; @@ -66,9 +68,12 @@ void setMappedValue(double mappedValue); void scroll(bool up); void resetToDefault(); + void edit(); protected slots: void updateMappedValue(int value); + void updateTitle(); + void contextMenuRequested(const QPoint &); protected: void mousePressEvent(QMouseEvent *e) override; @@ -96,6 +101,9 @@ QPoint m_clickPos; float m_clickRotation; bool m_showTooltip; + bool m_provideContextMenu; + QString m_title; + QMenu *m_lastContextMenu; RangeMapper *m_rangeMapper; QImage m_cache; WheelCounter m_wheelCounter;