From 7e1c05fcaba3f531c948a17ea6116aa464356f19 Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Mon, 6 Feb 2017 22:09:00 -0500 Subject: [PATCH] [Simulator] Navigation buttons in radio UIs are now animated in response to keyboard shortcuts. --- .../src/simulation/widgets/buttonswidget.h | 97 +++++++++++-------- 1 file changed, 55 insertions(+), 42 deletions(-) diff --git a/companion/src/simulation/widgets/buttonswidget.h b/companion/src/simulation/widgets/buttonswidget.h index f67293d8c..1f9be33c2 100644 --- a/companion/src/simulation/widgets/buttonswidget.h +++ b/companion/src/simulation/widgets/buttonswidget.h @@ -27,9 +27,44 @@ #include #include +class Area : public QObject +{ + Q_OBJECT + + public: + explicit Area(const QPolygon & polygon, const QString &image, RadioUiAction * action = NULL, QObject * parent = NULL): + QObject(parent), + polygon(polygon), + imgFile(image), + action(action) + { + if (action) + connect(action, &RadioUiAction::triggered, this, &Area::onActionTriggered); + } + + bool contains(int x, int y) + { + return polygon.containsPoint(QPoint(x, y), Qt::OddEvenFill); + } + + void onActionTriggered(int, bool active) + { + if (active) + emit imageChanged(imgFile); + else + emit imageChanged(""); + } + + QPolygon polygon; + QString imgFile; + RadioUiAction * action; + + signals: + void imageChanged(const QString image); +}; + class ButtonsWidget : public QWidget { - Q_OBJECT public: @@ -54,59 +89,39 @@ class ButtonsWidget : public QWidget void addArea(const QPolygon & polygon, const char * image, RadioUiAction * action = NULL) { - areas.push_back(Area(polygon, image, action)); + Area * area = new Area(polygon, image, action, this); + areas.push_back(area); + connect(area, &Area::imageChanged, this, &ButtonsWidget::setBitmap); } protected: - class Area - { - public: - Area(const QPolygon & polygon, const QString &image, RadioUiAction * action = NULL): - polygon(polygon), - imgFile(image), - action(action) - { - } - - bool contains(int x, int y) - { - return polygon.containsPoint(QPoint(x, y), Qt::OddEvenFill); - } - - QPolygon polygon; - QString imgFile; - RadioUiAction * action; - }; - void setBitmap(QString bitmap) { - if (!bitmap.isEmpty()) { - QWidget::setStyleSheet(QString("background:url(:/images/simulator/%1);").arg(bitmap)); - } - else { - QWidget::setStyleSheet(defaultStyleSheet); - } + QString css = defaultStyleSheet; + if (!bitmap.isEmpty()) + css = QString("background:url(:/images/simulator/%1);").arg(bitmap); + + QWidget::setStyleSheet(css); + setFocus(); } void onMouseButtonEvent(bool press, QMouseEvent * event) { - bool trig; - QString img = ""; + bool anyTriggered = false; int x = event->x(); int y = event->y(); - foreach(Area area, areas) { - trig = false; - if (press && event->button() == Qt::LeftButton && area.contains(x, y)) { - img = area.imgFile; - setFocus(); - trig = true; + foreach(Area * area, areas) { + if (event->button() == Qt::LeftButton && area->contains(x, y)) { + if (area->action) + area->action->trigger(press); + anyTriggered = true; + break; } - if (area.action) - area.action->trigger(trig); } - setBitmap(img); + if (!anyTriggered) + setBitmap(""); } virtual void mousePressEvent(QMouseEvent * event) @@ -130,9 +145,7 @@ class ButtonsWidget : public QWidget style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); } - protected: - - QList areas; + QList areas; QString defaultStyleSheet; };