1 | /* $Id: UIMenuToolBar.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIMenuToolBar class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifdef VBOX_WITH_PRECOMPILED_HEADERS
|
---|
19 | # include <precomp.h>
|
---|
20 | #else /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
21 |
|
---|
22 | /* Qt includes: */
|
---|
23 | # include <QApplication>
|
---|
24 | # include <QHBoxLayout>
|
---|
25 | # include <QPainter>
|
---|
26 | # include <QStyle>
|
---|
27 | # include <QToolButton>
|
---|
28 |
|
---|
29 | /* GUI includes: */
|
---|
30 | # include "UIMenuToolBar.h"
|
---|
31 | # include "UIToolBar.h"
|
---|
32 |
|
---|
33 | /* Other VBox includes: */
|
---|
34 | #include "iprt/assert.h"
|
---|
35 |
|
---|
36 | #endif /* VBOX_WS_MAC */
|
---|
37 |
|
---|
38 |
|
---|
39 | /** UIToolBar extension
|
---|
40 | * holding single drop-down menu of actions. */
|
---|
41 | class UIMenuToolBarPrivate : public UIToolBar
|
---|
42 | {
|
---|
43 | Q_OBJECT;
|
---|
44 |
|
---|
45 | public:
|
---|
46 |
|
---|
47 | /** Constructs toolbar. */
|
---|
48 | UIMenuToolBarPrivate(QWidget *pParent = 0);
|
---|
49 |
|
---|
50 | /** Rebuilds toolbar shape. */
|
---|
51 | void rebuildShape();
|
---|
52 |
|
---|
53 | /** Defines toolbar alignment @a enmType. */
|
---|
54 | void setAlignmentType(UIMenuToolBar::AlignmentType enmType);
|
---|
55 |
|
---|
56 | /** Defines toolbar menu action. */
|
---|
57 | void setMenuAction(QAction *pAction);
|
---|
58 |
|
---|
59 | protected:
|
---|
60 |
|
---|
61 | /** Handles show @a pEvent. */
|
---|
62 | virtual void showEvent(QShowEvent *pEvent) /* override */;
|
---|
63 |
|
---|
64 | /** Handles polish @a pEvent. */
|
---|
65 | virtual void polishEvent(QShowEvent *pEvent);
|
---|
66 |
|
---|
67 | /** Handles resize @a pEvent. */
|
---|
68 | virtual void resizeEvent(QResizeEvent *pEvent) /* override */;
|
---|
69 |
|
---|
70 | /** Handles paint @a pEvent. */
|
---|
71 | virtual void paintEvent(QPaintEvent *pEvent) /* override */;
|
---|
72 |
|
---|
73 | private:
|
---|
74 |
|
---|
75 | /** Holds whether this widget was polished. */
|
---|
76 | bool m_fPolished;
|
---|
77 |
|
---|
78 | /** Holds the left margin instance. */
|
---|
79 | QWidget *m_pMarginLeft;
|
---|
80 | /** Holds the right margin instance. */
|
---|
81 | QWidget *m_pMarginRight;
|
---|
82 |
|
---|
83 | /** Holds the menu toolbar alignment type. */
|
---|
84 | UIMenuToolBar::AlignmentType m_enmAlignmentType;
|
---|
85 |
|
---|
86 | /** Holds the shape. */
|
---|
87 | QPainterPath m_shape;
|
---|
88 | };
|
---|
89 |
|
---|
90 |
|
---|
91 | /*********************************************************************************************************************************
|
---|
92 | * Class UIMenuToolBarPrivate implementation. *
|
---|
93 | *********************************************************************************************************************************/
|
---|
94 |
|
---|
95 | UIMenuToolBarPrivate::UIMenuToolBarPrivate(QWidget *pParent /* = 0 */)
|
---|
96 | : UIToolBar(pParent)
|
---|
97 | , m_fPolished(false)
|
---|
98 | , m_pMarginLeft(0)
|
---|
99 | , m_pMarginRight(0)
|
---|
100 | , m_enmAlignmentType(UIMenuToolBar::AlignmentType_TopLeft)
|
---|
101 | {
|
---|
102 | /* Rebuild shape: */
|
---|
103 | rebuildShape();
|
---|
104 | }
|
---|
105 |
|
---|
106 | void UIMenuToolBarPrivate::rebuildShape()
|
---|
107 | {
|
---|
108 | /* Get the metric: */
|
---|
109 | const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize);
|
---|
110 | const int iRounding = qMax(iIconMetric / 4, 4);
|
---|
111 |
|
---|
112 | /* Configure margins: */
|
---|
113 | if (m_pMarginLeft && m_pMarginRight)
|
---|
114 | {
|
---|
115 | const int iStandardMargin = iRounding;
|
---|
116 | int iLeftMargin = iStandardMargin;
|
---|
117 | int iRightMargin = iStandardMargin;
|
---|
118 | if ( m_enmAlignmentType == UIMenuToolBar::AlignmentType_TopLeft
|
---|
119 | || m_enmAlignmentType == UIMenuToolBar::AlignmentType_BottomLeft)
|
---|
120 | iRightMargin += iRounding;
|
---|
121 | if ( m_enmAlignmentType == UIMenuToolBar::AlignmentType_TopRight
|
---|
122 | || m_enmAlignmentType == UIMenuToolBar::AlignmentType_BottomRight)
|
---|
123 | iLeftMargin += iRounding;
|
---|
124 | m_pMarginLeft->setMinimumWidth(iLeftMargin);
|
---|
125 | m_pMarginRight->setMinimumWidth(iRightMargin);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* Rebuild shape: */
|
---|
129 | QPainterPath shape;
|
---|
130 | switch (m_enmAlignmentType)
|
---|
131 | {
|
---|
132 | case UIMenuToolBar::AlignmentType_TopLeft:
|
---|
133 | shape.moveTo(width(), height());
|
---|
134 | shape.lineTo(shape.currentPosition().x(), iRounding * 2);
|
---|
135 | shape.arcTo(QRectF(shape.currentPosition(), QSizeF(iRounding * 4, iRounding * 4))
|
---|
136 | .translated(-iRounding * 4, -iRounding * 2), 0, 90);
|
---|
137 | shape.lineTo(0, shape.currentPosition().y());
|
---|
138 | shape.lineTo(shape.currentPosition().x(), height());
|
---|
139 | shape.closeSubpath();
|
---|
140 | break;
|
---|
141 | case UIMenuToolBar::AlignmentType_TopRight:
|
---|
142 | shape.moveTo(0, height());
|
---|
143 | shape.lineTo(shape.currentPosition().x(), iRounding * 2);
|
---|
144 | shape.arcTo(QRectF(shape.currentPosition(), QSizeF(iRounding * 4, iRounding * 4))
|
---|
145 | .translated(0, -iRounding * 2), 180, -90);
|
---|
146 | shape.lineTo(width(), shape.currentPosition().y());
|
---|
147 | shape.lineTo(shape.currentPosition().x(), height());
|
---|
148 | shape.closeSubpath();
|
---|
149 | break;
|
---|
150 | case UIMenuToolBar::AlignmentType_BottomLeft:
|
---|
151 | shape.moveTo(width(), 0);
|
---|
152 | shape.lineTo(shape.currentPosition().x(), height() - iRounding * 2);
|
---|
153 | shape.arcTo(QRectF(shape.currentPosition(), QSizeF(iRounding * 4, iRounding * 4))
|
---|
154 | .translated(-iRounding * 4, -iRounding * 2), 0, -90);
|
---|
155 | shape.lineTo(0, shape.currentPosition().y());
|
---|
156 | shape.lineTo(shape.currentPosition().x(), 0);
|
---|
157 | shape.closeSubpath();
|
---|
158 | break;
|
---|
159 | case UIMenuToolBar::AlignmentType_BottomRight:
|
---|
160 | shape.moveTo(0, 0);
|
---|
161 | shape.lineTo(shape.currentPosition().x(), height() - iRounding * 2);
|
---|
162 | shape.arcTo(QRectF(shape.currentPosition(), QSizeF(iRounding * 4, iRounding * 4))
|
---|
163 | .translated(0, -iRounding * 2), 180, 90);
|
---|
164 | shape.lineTo(width(), shape.currentPosition().y());
|
---|
165 | shape.lineTo(shape.currentPosition().x(), 0);
|
---|
166 | shape.closeSubpath();
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | m_shape = shape;
|
---|
170 | }
|
---|
171 |
|
---|
172 | void UIMenuToolBarPrivate::setAlignmentType(UIMenuToolBar::AlignmentType enmType)
|
---|
173 | {
|
---|
174 | /* Set alignment type: */
|
---|
175 | m_enmAlignmentType = enmType;
|
---|
176 |
|
---|
177 | /* Rebuild shape: */
|
---|
178 | rebuildShape();
|
---|
179 | }
|
---|
180 |
|
---|
181 | void UIMenuToolBarPrivate::setMenuAction(QAction *pAction)
|
---|
182 | {
|
---|
183 | /* Clear first: */
|
---|
184 | clear();
|
---|
185 | delete m_pMarginLeft;
|
---|
186 | m_pMarginLeft = 0;
|
---|
187 | delete m_pMarginRight;
|
---|
188 | m_pMarginRight = 0;
|
---|
189 |
|
---|
190 | /* Create left margin: */
|
---|
191 | m_pMarginLeft = widgetForAction(addWidget(new QWidget));
|
---|
192 |
|
---|
193 | /* Add action itself: */
|
---|
194 | addAction(pAction);
|
---|
195 |
|
---|
196 | /* Configure the newly added action's button: */
|
---|
197 | QToolButton *pButton = qobject_cast<QToolButton*>(widgetForAction(pAction));
|
---|
198 | AssertPtrReturnVoid(pButton);
|
---|
199 | {
|
---|
200 | /* Configure tool-button: */
|
---|
201 | pButton->setAutoRaise(true);
|
---|
202 | pButton->setPopupMode(QToolButton::InstantPopup);
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* Create right margin: */
|
---|
206 | m_pMarginRight = widgetForAction(addWidget(new QWidget));
|
---|
207 |
|
---|
208 | /* Rebuild shape: */
|
---|
209 | rebuildShape();
|
---|
210 | }
|
---|
211 |
|
---|
212 | void UIMenuToolBarPrivate::showEvent(QShowEvent *pEvent)
|
---|
213 | {
|
---|
214 | /* Call to base-class: */
|
---|
215 | UIToolBar::showEvent(pEvent);
|
---|
216 |
|
---|
217 | /* Make sure we should polish dialog: */
|
---|
218 | if (m_fPolished)
|
---|
219 | return;
|
---|
220 |
|
---|
221 | /* Call to polish-event: */
|
---|
222 | polishEvent(pEvent);
|
---|
223 |
|
---|
224 | /* Mark dialog as polished: */
|
---|
225 | m_fPolished = true;
|
---|
226 | }
|
---|
227 |
|
---|
228 | void UIMenuToolBarPrivate::polishEvent(QShowEvent * /* pEvent */)
|
---|
229 | {
|
---|
230 | /* Rebuild shape: */
|
---|
231 | rebuildShape();
|
---|
232 | }
|
---|
233 |
|
---|
234 | void UIMenuToolBarPrivate::resizeEvent(QResizeEvent *pEvent)
|
---|
235 | {
|
---|
236 | /* Call to base-class: */
|
---|
237 | UIToolBar::resizeEvent(pEvent);
|
---|
238 |
|
---|
239 | /* Rebuild shape: */
|
---|
240 | rebuildShape();
|
---|
241 | }
|
---|
242 |
|
---|
243 | void UIMenuToolBarPrivate::paintEvent(QPaintEvent * /* pEvent */)
|
---|
244 | {
|
---|
245 | /* Prepare painter: */
|
---|
246 | QPainter painter(this);
|
---|
247 |
|
---|
248 | /* Fill background: */
|
---|
249 | if (!m_shape.isEmpty())
|
---|
250 | {
|
---|
251 | painter.setRenderHint(QPainter::Antialiasing);
|
---|
252 | painter.setClipPath(m_shape);
|
---|
253 | }
|
---|
254 | QRect backgroundRect = rect();
|
---|
255 | QColor backgroundColor = palette().color(QPalette::Window);
|
---|
256 | QLinearGradient headerGradient(backgroundRect.bottomLeft(), backgroundRect.topLeft());
|
---|
257 | headerGradient.setColorAt(0, backgroundColor.darker(120));
|
---|
258 | headerGradient.setColorAt(1, backgroundColor.darker(104));
|
---|
259 | painter.fillRect(backgroundRect, headerGradient);
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | /*********************************************************************************************************************************
|
---|
264 | * Class UIToolBarMenu implementation. *
|
---|
265 | *********************************************************************************************************************************/
|
---|
266 |
|
---|
267 | UIMenuToolBar::UIMenuToolBar(QWidget *pParent /* = 0 */)
|
---|
268 | : QWidget(pParent)
|
---|
269 | {
|
---|
270 | /* Prepare: */
|
---|
271 | prepare();
|
---|
272 | }
|
---|
273 |
|
---|
274 | void UIMenuToolBar::prepare()
|
---|
275 | {
|
---|
276 | /* Create layout: */
|
---|
277 | new QHBoxLayout(this);
|
---|
278 | AssertPtrReturnVoid(layout());
|
---|
279 | {
|
---|
280 | /* Configure layout: */
|
---|
281 | layout()->setContentsMargins(0, 0, 0, 0);
|
---|
282 |
|
---|
283 | /* Create menu-toolbar: */
|
---|
284 | m_pToolbar = new UIMenuToolBarPrivate;
|
---|
285 | AssertPtrReturnVoid(m_pToolbar);
|
---|
286 | {
|
---|
287 | /* Configure menu-toolbar: */
|
---|
288 | m_pToolbar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
---|
289 |
|
---|
290 | /* Add into layout: */
|
---|
291 | layout()->addWidget(m_pToolbar);
|
---|
292 | }
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | void UIMenuToolBar::setAlignmentType(AlignmentType enmType)
|
---|
297 | {
|
---|
298 | /* Pass to private object: */
|
---|
299 | return m_pToolbar->setAlignmentType(enmType);
|
---|
300 | }
|
---|
301 |
|
---|
302 | void UIMenuToolBar::setIconSize(const QSize &size)
|
---|
303 | {
|
---|
304 | /* Pass to private object: */
|
---|
305 | return m_pToolbar->setIconSize(size);
|
---|
306 | }
|
---|
307 |
|
---|
308 | void UIMenuToolBar::setMenuAction(QAction *pAction)
|
---|
309 | {
|
---|
310 | /* Pass to private object: */
|
---|
311 | return m_pToolbar->setMenuAction(pAction);
|
---|
312 | }
|
---|
313 |
|
---|
314 | void UIMenuToolBar::setToolButtonStyle(Qt::ToolButtonStyle enmStyle)
|
---|
315 | {
|
---|
316 | /* Pass to private object: */
|
---|
317 | return m_pToolbar->setToolButtonStyle(enmStyle);
|
---|
318 | }
|
---|
319 |
|
---|
320 | QWidget *UIMenuToolBar::widgetForAction(QAction *pAction) const
|
---|
321 | {
|
---|
322 | /* Pass to private object: */
|
---|
323 | return m_pToolbar->widgetForAction(pAction);
|
---|
324 | }
|
---|
325 |
|
---|
326 | #include "UIMenuToolBar.moc"
|
---|
327 |
|
---|