VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIMenuToolBar.cpp@ 82781

Last change on this file since 82781 was 76606, checked in by vboxsync, 5 years ago

FE/Qt: Cleaning out old precompiled header experiment.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use