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
9 * @addtogroup CorePlugin Core Plugin
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
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>
34 #include <QVBoxLayout>
35 #include <QtSvg/QSvgRenderer>
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
[] = {
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
);
76 FancyToolButton::FancyToolButton(QWidget
*parent
)
78 , m_buttonElements(buttonElementsMap())
80 setAttribute(Qt::WA_Hover
, true);
81 setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Preferred
);
84 void FancyToolButton::paintEvent(QPaintEvent
*event
)
89 double scale
= (double)height() / sh
.height();
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
));
106 if (!icon().isNull()) {
107 icon().paint(&p
, rect());
109 const int margin
= 4;
110 p
.drawText(rect().adjusted(margin
, margin
, -margin
, -margin
), Qt::AlignCenter
| Qt::TextWordWrap
, text());
118 p
.drawPicture(0, 0, m_buttonElements
.value(isDown() ?
119 svgIdButtonPressedOverlay
: svgIdButtonNormalOverlay
));
121 p
.drawPicture(0, 0, m_buttonElements
.value(svgIdButtonDisabledOverlay
));
125 void FancyActionBar::paintEvent(QPaintEvent
*event
)
130 QSize
FancyToolButton::sizeHint() const
132 return m_buttonElements
.value(svgIdButtonBase
).boundingRect().size();
135 QSize
FancyToolButton::minimumSizeHint() const
140 FancyActionBar::FancyActionBar(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
);
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();