summaryrefslogtreecommitdiffstats
path: root/src/VBox/Frontends/VirtualBox/src/extensions/QIToolBar.cpp
blob: e3cd026157e5b931dd3ec3f8c6b453c6f16704fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* $Id: QIToolBar.cpp $ */
/** @file
 * VBox Qt GUI - QIToolBar class implementation.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

/* Qt includes: */
#include <QLayout>
#include <QMainWindow>
#include <QResizeEvent>
#ifdef VBOX_WS_MAC
# include <QApplication>
# include <QPainter>
# include <QPainterPath>
#endif

/* GUI includes: */
#include "QIToolBar.h"
#ifdef VBOX_WS_MAC
# include "VBoxUtils.h"
#endif


QIToolBar::QIToolBar(QWidget *pParent /* = 0 */)
    : QToolBar(pParent)
    , m_pMainWindow(qobject_cast<QMainWindow*>(pParent))
#ifdef VBOX_WS_MAC
    , m_fEmulateUnifiedToolbar(false)
    , m_iOverallContentsWidth(0)
    , m_iBrandingWidth(0)
#endif
{
    prepare();
}

void QIToolBar::setUseTextLabels(bool fEnable)
{
    /* Determine tool-button style on the basis of passed flag: */
    Qt::ToolButtonStyle tbs = fEnable ? Qt::ToolButtonTextUnderIcon : Qt::ToolButtonIconOnly;

    /* Depending on parent, assign this style: */
    if (m_pMainWindow)
        m_pMainWindow->setToolButtonStyle(tbs);
    else
        setToolButtonStyle(tbs);
}

bool QIToolBar::useTextLabels() const
{
    /* Depending on parent, return the style: */
    if (m_pMainWindow)
        return m_pMainWindow->toolButtonStyle() == Qt::ToolButtonTextUnderIcon;
    else
        return toolButtonStyle() == Qt::ToolButtonTextUnderIcon;
}

#ifdef VBOX_WS_MAC
void QIToolBar::enableMacToolbar()
{
    /* Depending on parent, enable unified title/tool-bar: */
    if (m_pMainWindow)
        m_pMainWindow->setUnifiedTitleAndToolBarOnMac(true);
}

void QIToolBar::emulateMacToolbar()
{
    /* Remember request, to be used in paintEvent: */
    m_fEmulateUnifiedToolbar = true;
}

void QIToolBar::setShowToolBarButton(bool fShow)
{
    ::darwinSetShowsToolbarButton(this, fShow);
}

void QIToolBar::updateLayout()
{
    // WORKAROUND:
    // There is a bug in Qt Cocoa which result in showing a "more arrow" when
    // the necessary size of the tool-bar is increased. Also for some languages
    // the with doesn't match if the text increase. So manually adjust the size
    // after changing the text.
    QSizePolicy sp = sizePolicy();
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    adjustSize();
    setSizePolicy(sp);
    layout()->invalidate();
    layout()->activate();
}

void QIToolBar::enableBranding(const QIcon &icnBranding,
                               const QString &strBranding,
                               const QColor &clrBranding,
                               int iBrandingWidth)
{
    m_icnBranding = icnBranding;
    m_strBranding = strBranding;
    m_clrBranding = clrBranding;
    m_iBrandingWidth = iBrandingWidth;
    update();
}
#endif /* VBOX_WS_MAC */

bool QIToolBar::event(QEvent *pEvent)
{
    /* Sanity check: */
    if (!pEvent)
        return QToolBar::event(pEvent);

    /* Handle required event types: */
    switch (pEvent->type())
    {
#ifdef VBOX_WS_MAC
        case QEvent::LayoutRequest:
        {
            /* Recalculate overall contents width on layout
             * request if we have branding stuff: */
            if (!m_icnBranding.isNull())
                recalculateOverallContentsWidth();
            break;
        }
#endif /* VBOX_WS_MAC */
        default:
            break;
    }

    /* Call to base-class: */
    return QToolBar::event(pEvent);
}

void QIToolBar::resizeEvent(QResizeEvent *pEvent)
{
    /* Call to base-class: */
    QToolBar::resizeEvent(pEvent);

    /* Notify listeners about new size: */
    emit sigResized(pEvent->size());
}

#ifdef VBOX_WS_MAC
void QIToolBar::paintEvent(QPaintEvent *pEvent)
{
    /* Call to base-class: */
    QToolBar::paintEvent(pEvent);

    /* If we have request to emulate unified tool-bar: */
    if (m_fEmulateUnifiedToolbar)
    {
        /* Limit painting with incoming rectangle: */
        QPainter painter(this);
        painter.setClipRect(pEvent->rect());

        /* Acquire full rectangle: */
        const QRect rectangle = rect();

        /* Prepare gradient: */
        const QColor backgroundColor = QApplication::palette().color(QPalette::Active, QPalette::Window);
        QLinearGradient gradient(rectangle.topLeft(), rectangle.bottomLeft());
        gradient.setColorAt(0, backgroundColor.darker(105));
        gradient.setColorAt(1, backgroundColor.darker(115));

        /* Fill background: */
        painter.fillRect(rectangle, gradient);

        /* Do we have branding stuff and a place for it? */
        if (   !m_icnBranding.isNull()
            && width() >= m_iOverallContentsWidth + m_iBrandingWidth)
        {
            /* A bit of common stuff: */
            QFont fnt = font();
            int iTextWidth = 0;
            int iTextHeight = 0;

            /* Configure font to fit width (m_iBrandingWidth - 2 * 4): */
            if (useTextLabels())
            {
                for (int i = 0; i <= 10; ++i) // no more than 10 tries ..
                {
                    if (fnt.pixelSize() == -1)
                        fnt.setPointSize(fnt.pointSize() - i);
                    else
                        fnt.setPixelSize(fnt.pixelSize() - i);
                    iTextWidth = QFontMetrics(fnt).size(0, m_strBranding).width();
                    if (iTextWidth <= m_iBrandingWidth - 2 * 4)
                        break;
                }
                iTextHeight = QFontMetrics(fnt).height();
            }

            /* Draw pixmap: */
            const int iIconSize = qMin(rectangle.height(), 32 /* default */);
            const int iIconMarginH = (m_iBrandingWidth - iIconSize) / 2;
            const int iIconMarginV = (rectangle.height() - iIconSize - iTextHeight) / 2;
            const int iIconX = rectangle.width() - iIconSize - iIconMarginH;
            const int iIconY = iIconMarginV;
            painter.drawPixmap(iIconX, iIconY, m_icnBranding.pixmap(QSize(iIconSize, iIconSize)));

            /* Draw text path: */
            if (useTextLabels())
            {
                const int iTextMargingH = (m_iBrandingWidth - iTextWidth) / 2;
                const int iTextX = rectangle.width() - iTextWidth - iTextMargingH;
                const int iTextY = iIconY + iIconSize + iTextHeight;
                QPainterPath textPath;
                textPath.addText(0, 0, fnt, m_strBranding);
                textPath.translate(iTextX, iTextY);
                painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
                painter.setPen(QPen(m_clrBranding.darker(80), 2, Qt::SolidLine, Qt::RoundCap));
                painter.drawPath(QPainterPathStroker().createStroke(textPath));
                painter.setBrush(Qt::black);
                painter.setPen(Qt::NoPen);
                painter.drawPath(textPath);
            }
        }
    }
}
#endif /* VBOX_WS_MAC */

void QIToolBar::prepare()
{
    /* Configure tool-bar: */
    setFloatable(false);
    setMovable(false);

#ifdef VBOX_WS_MAC
    setStyleSheet("QToolBar { border: 0px none black; }");
#endif

    /* Configure tool-bar' layout: */
    if (layout())
        layout()->setContentsMargins(0, 0, 0, 0);

    /* Configure tool-bar' context-menu policy: */
    setContextMenuPolicy(Qt::PreventContextMenu);
}

#ifdef VBOX_WS_MAC
void QIToolBar::recalculateOverallContentsWidth()
{
    /* Reset contents width: */
    m_iOverallContentsWidth = 0;

    /* Caclulate new value: */
    if (!layout())
        return;
    int iResult = 0;
    const int iSpacing = layout()->spacing();
    foreach (QAction *pAction, actions())
    {
        if (!pAction || !pAction->isVisible())
            continue;
        QWidget *pWidget = widgetForAction(pAction);
        if (!pWidget)
            continue;
        /* Add each widget width and spacing: */
        const int iWidth = pWidget->width() + iSpacing;
        iResult += iWidth;
    }
    /* Subtract last spacing: */
    iResult -= iSpacing;

    /* Update result: */
    m_iOverallContentsWidth = qMax(m_iOverallContentsWidth, iResult);
}
#endif /* VBOX_WS_MAC */