LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / coreplugin / fancyactionbar.cpp
blobf9d39a02dfa1a533758f0d88499416fa0ffc1b41
1 /**
2 ******************************************************************************
4 * @file fancyactionbar.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @addtogroup GCSPlugins GCS Plugins
8 * @{
9 * @addtogroup CorePlugin Core Plugin
10 * @{
11 * @brief The Core GCS plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "fancyactionbar.h"
31 #include <QHBoxLayout>
32 #include <QPainter>
33 #include <QPicture>
34 #include <QVBoxLayout>
35 #include <QtSvg/QSvgRenderer>
36 #include <QAction>
38 using namespace Core;
39 using namespace Internal;
41 static const char *const svgIdButtonBase = "ButtonBase";
42 static const char *const svgIdButtonNormalBase = "ButtonNormalBase";
43 static const char *const svgIdButtonNormalOverlay = "ButtonNormalOverlay";
44 static const char *const svgIdButtonPressedBase = "ButtonPressedBase";
45 static const char *const svgIdButtonPressedOverlay = "ButtonPressedOverlay";
46 static const char *const svgIdButtonDisabledOverlay = "ButtonDisabledOverlay";
47 static const char *const svgIdButtonHoverOverlay = "ButtonHoverOverlay";
49 static const char *const elementsSvgIds[] = {
50 svgIdButtonBase,
51 svgIdButtonNormalBase,
52 svgIdButtonNormalOverlay,
53 svgIdButtonPressedBase,
54 svgIdButtonPressedOverlay,
55 svgIdButtonDisabledOverlay,
56 svgIdButtonHoverOverlay
59 const QMap<QString, QPicture> &buttonElementsMap()
61 static QMap<QString, QPicture> result;
63 if (result.isEmpty()) {
64 QSvgRenderer renderer(QLatin1String(":/fancyactionbar/images/fancytoolbutton.svg"));
65 for (size_t i = 0; i < sizeof(elementsSvgIds) / sizeof(elementsSvgIds[0]); i++) {
66 QString elementId(elementsSvgIds[i]);
67 QPicture elementPicture;
68 QPainter elementPainter(&elementPicture);
69 renderer.render(&elementPainter, elementId);
70 result.insert(elementId, elementPicture);
73 return result;
76 FancyToolButton::FancyToolButton(QWidget *parent)
77 : QToolButton(parent)
78 , m_buttonElements(buttonElementsMap())
80 setAttribute(Qt::WA_Hover, true);
81 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
84 void FancyToolButton::paintEvent(QPaintEvent *event)
86 Q_UNUSED(event)
87 QPainter p(this);
88 QSize sh(sizeHint());
89 double scale = (double)height() / sh.height();
90 if (scale < 1) {
91 p.save();
92 p.scale(1, scale);
94 p.drawPicture(0, 0, m_buttonElements.value(svgIdButtonBase));
95 p.drawPicture(0, 0, m_buttonElements.value(isDown() ? svgIdButtonPressedBase : svgIdButtonNormalBase));
96 #ifndef Q_WS_MAC // Mac UIs usually don't hover
97 if (underMouse() && isEnabled()) {
98 p.drawPicture(0, 0, m_buttonElements.value(svgIdButtonHoverOverlay));
100 #endif
102 if (scale < 1) {
103 p.restore();
106 if (!icon().isNull()) {
107 icon().paint(&p, rect());
108 } else {
109 const int margin = 4;
110 p.drawText(rect().adjusted(margin, margin, -margin, -margin), Qt::AlignCenter | Qt::TextWordWrap, text());
113 if (scale < 1) {
114 p.scale(1, scale);
117 if (isEnabled()) {
118 p.drawPicture(0, 0, m_buttonElements.value(isDown() ?
119 svgIdButtonPressedOverlay : svgIdButtonNormalOverlay));
120 } else {
121 p.drawPicture(0, 0, m_buttonElements.value(svgIdButtonDisabledOverlay));
125 void FancyActionBar::paintEvent(QPaintEvent *event)
127 Q_UNUSED(event)
130 QSize FancyToolButton::sizeHint() const
132 return m_buttonElements.value(svgIdButtonBase).boundingRect().size();
135 QSize FancyToolButton::minimumSizeHint() const
137 return QSize(8, 8);
140 FancyActionBar::FancyActionBar(QWidget *parent)
141 : QWidget(parent)
143 m_actionsLayout = new QVBoxLayout;
145 QHBoxLayout *centeringLayout = new QHBoxLayout;
146 centeringLayout->addStretch();
147 centeringLayout->addLayout(m_actionsLayout);
148 centeringLayout->addStretch();
149 setLayout(centeringLayout);
152 void FancyActionBar::insertAction(int index, QAction *action, QMenu *menu)
154 FancyToolButton *toolButton = new FancyToolButton(this);
156 toolButton->setDefaultAction(action);
157 if (menu) {
158 toolButton->setMenu(menu);
159 toolButton->setPopupMode(QToolButton::DelayedPopup);
161 // execute action also if a context menu item is select
162 connect(toolButton, SIGNAL(triggered(QAction *)),
163 this, SLOT(toolButtonContextMenuActionTriggered(QAction *)),
164 Qt::QueuedConnection);
166 m_actionsLayout->insertWidget(index, toolButton);
170 This slot is invoked when a context menu action of a tool button is triggered.
171 In this case we also want to trigger the default action of the button.
173 This allows the user e.g. to select and run a specific run configuration with one click.
175 void FancyActionBar::toolButtonContextMenuActionTriggered(QAction *action)
177 if (QToolButton * button = qobject_cast<QToolButton *>(sender())) {
178 if (action != button->defaultAction()) {
179 button->defaultAction()->trigger();