1 | /* $Id: UIToolBar.cpp 74595 2018-10-03 14:10:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIToolBar class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2018 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 <QLayout>
|
---|
24 | # include <QMainWindow>
|
---|
25 | # include <QResizeEvent>
|
---|
26 | # ifdef VBOX_WS_MAC
|
---|
27 | # include <QPainter>
|
---|
28 | # endif
|
---|
29 |
|
---|
30 | /* GUI includes: */
|
---|
31 | # include "UIToolBar.h"
|
---|
32 | # ifdef VBOX_WS_MAC
|
---|
33 | # include "VBoxUtils.h"
|
---|
34 | # endif
|
---|
35 |
|
---|
36 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
37 |
|
---|
38 |
|
---|
39 | UIToolBar::UIToolBar(QWidget *pParent /* = 0 */)
|
---|
40 | : QToolBar(pParent)
|
---|
41 | , m_pMainWindow(qobject_cast<QMainWindow*>(pParent))
|
---|
42 | #ifdef VBOX_WS_MAC
|
---|
43 | , m_fEmulateUnifiedToolbar(false)
|
---|
44 | #endif
|
---|
45 | {
|
---|
46 | /* Prepare: */
|
---|
47 | prepare();
|
---|
48 | }
|
---|
49 |
|
---|
50 | void UIToolBar::setUseTextLabels(bool fEnable)
|
---|
51 | {
|
---|
52 | /* Determine tool-button style on the basis of passed flag: */
|
---|
53 | Qt::ToolButtonStyle tbs = fEnable ? Qt::ToolButtonTextUnderIcon : Qt::ToolButtonIconOnly;
|
---|
54 |
|
---|
55 | /* Depending on parent, assign this style: */
|
---|
56 | if (m_pMainWindow)
|
---|
57 | m_pMainWindow->setToolButtonStyle(tbs);
|
---|
58 | else
|
---|
59 | setToolButtonStyle(tbs);
|
---|
60 | }
|
---|
61 |
|
---|
62 | #ifdef VBOX_WS_MAC
|
---|
63 | void UIToolBar::enableMacToolbar()
|
---|
64 | {
|
---|
65 | /* Depending on parent, enable unified title/tool-bar: */
|
---|
66 | if (m_pMainWindow)
|
---|
67 | m_pMainWindow->setUnifiedTitleAndToolBarOnMac(true);
|
---|
68 | }
|
---|
69 |
|
---|
70 | void UIToolBar::emulateMacToolbar()
|
---|
71 | {
|
---|
72 | /* Remember request, to be used in paintEvent: */
|
---|
73 | m_fEmulateUnifiedToolbar = true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | void UIToolBar::setShowToolBarButton(bool fShow)
|
---|
77 | {
|
---|
78 | ::darwinSetShowsToolbarButton(this, fShow);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void UIToolBar::updateLayout()
|
---|
82 | {
|
---|
83 | // WORKAROUND:
|
---|
84 | // There is a bug in Qt Cocoa which result in showing a "more arrow" when
|
---|
85 | // the necessary size of the tool-bar is increased. Also for some languages
|
---|
86 | // the with doesn't match if the text increase. So manually adjust the size
|
---|
87 | // after changing the text.
|
---|
88 | QSizePolicy sp = sizePolicy();
|
---|
89 | setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
---|
90 | adjustSize();
|
---|
91 | setSizePolicy(sp);
|
---|
92 | layout()->invalidate();
|
---|
93 | layout()->activate();
|
---|
94 | }
|
---|
95 | #endif /* VBOX_WS_MAC */
|
---|
96 |
|
---|
97 | void UIToolBar::resizeEvent(QResizeEvent *pEvent)
|
---|
98 | {
|
---|
99 | /* Call to base-class: */
|
---|
100 | QToolBar::resizeEvent(pEvent);
|
---|
101 |
|
---|
102 | /* Notify listeners about new size: */
|
---|
103 | emit sigResized(pEvent->size());
|
---|
104 | }
|
---|
105 |
|
---|
106 | #ifdef VBOX_WS_MAC
|
---|
107 | void UIToolBar::paintEvent(QPaintEvent *pEvent)
|
---|
108 | {
|
---|
109 | /* Call to base-class: */
|
---|
110 | QToolBar::paintEvent(pEvent);
|
---|
111 |
|
---|
112 | /* If we have request to emulate unified tool-bar: */
|
---|
113 | if (m_fEmulateUnifiedToolbar)
|
---|
114 | {
|
---|
115 | /* Acquire rectangle: */
|
---|
116 | const QRect rectangle = pEvent->rect();
|
---|
117 |
|
---|
118 | /* Prepare gradient: */
|
---|
119 | const QColor backgroundColor = palette().color(QPalette::Active, QPalette::Mid);
|
---|
120 | QLinearGradient gradient(rectangle.topLeft(), rectangle.bottomLeft());
|
---|
121 | gradient.setColorAt(0, backgroundColor.lighter(130));
|
---|
122 | gradient.setColorAt(1, backgroundColor.lighter(125));
|
---|
123 |
|
---|
124 | /* Fill background: */
|
---|
125 | QPainter painter(this);
|
---|
126 | painter.fillRect(rectangle, gradient);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | #endif /* VBOX_WS_MAC */
|
---|
130 |
|
---|
131 | void UIToolBar::prepare()
|
---|
132 | {
|
---|
133 | /* Configure tool-bar: */
|
---|
134 | setFloatable(false);
|
---|
135 | setMovable(false);
|
---|
136 |
|
---|
137 | #ifdef VBOX_WS_MAC
|
---|
138 | setStyleSheet("QToolBar { border: 0px none black; }");
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | /* Configure tool-bar' layout: */
|
---|
142 | if (layout())
|
---|
143 | layout()->setContentsMargins(0, 0, 0, 0);
|
---|
144 |
|
---|
145 | /* Configure tool-bar' context-menu policy: */
|
---|
146 | setContextMenuPolicy(Qt::NoContextMenu);
|
---|
147 | }
|
---|