Mercurial > hg > svgui
changeset 1586:bbc3f537564c
Add context menu to Thumbwheel
author | Chris Cannam |
---|---|
date | Fri, 27 Mar 2020 11:04:56 +0000 |
parents | 073ef72e8e60 |
children | 2108af725460 |
files | widgets/Thumbwheel.cpp widgets/Thumbwheel.h |
diffstat | 2 files changed, 82 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- 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 <QInputDialog> #include <QPainter> #include <QPainterPath> +#include <QMenu> + +#include "MenuTitle.h" #include <cmath> #include <iostream> @@ -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) {
--- 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;