Fix doc path
[opentx.git] / companion / src / simulation / widgets / buttonswidget.h
blob4fef883e6ae155c808139bc702180a5da45f0c9c
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _BUTTONSWIDGET_H_
22 #define _BUTTONSWIDGET_H_
24 #include "radiouiaction.h"
25 #include "radiokeywidget.h"
27 #include <QWidget>
28 #include <QtGui>
29 #include <QStyleOption>
31 class ButtonsWidget : public QWidget
33 Q_OBJECT
35 public:
37 explicit ButtonsWidget(QWidget * parent):
38 QWidget(parent)
42 virtual void setStyleSheet(const QString & sheet)
44 defaultStyleSheet = sheet;
45 QWidget::setStyleSheet(sheet);
48 RadioKeyWidget * addArea(const QRect & rect, const char * image, RadioUiAction * action = NULL)
50 return addArea(QPolygon(rect), image, action);
53 RadioKeyWidget * addArea(const QPolygon & polygon, const char * image, RadioUiAction * action = NULL)
55 RadioKeyWidget * btn = new RadioKeyWidget(polygon, image, action, this);
56 m_buttons.append(btn);
57 connect(btn, &RadioKeyWidget::imageChanged, this, &ButtonsWidget::setBitmap);
58 return btn;
61 protected:
63 void setBitmap(QString bitmap)
65 QString css = defaultStyleSheet;
66 if (!bitmap.isEmpty())
67 css = QString("background:url(:/images/simulator/%1);").arg(bitmap);
69 QWidget::setStyleSheet(css);
70 setFocus();
73 void onMouseButtonEvent(bool press, QMouseEvent * event)
75 if (!(event->button() & (Qt::LeftButton | Qt::MidButton))) {
76 event->ignore();
77 return;
80 foreach(RadioKeyWidget * key, m_buttons) {
81 if (key->contains(event->pos())) {
82 key->toggle(press);
83 event->accept();
84 return;
86 else if (key->getValue()) {
87 key->toggle(false);
90 event->ignore();
93 virtual void mousePressEvent(QMouseEvent * event)
95 onMouseButtonEvent(true, event);
97 virtual void mouseReleaseEvent(QMouseEvent * event)
99 onMouseButtonEvent(false, event);
102 void paintEvent(QPaintEvent *)
104 QStyleOption opt;
105 opt.init(this);
106 QPainter p(this);
107 style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
110 QList<RadioKeyWidget *> m_buttons;
111 QString defaultStyleSheet;
114 #endif // _BUTTONSWIDGET_H_