1 | /* $Id: QIStatusBarIndicator.cpp 101561 2023-10-23 16:25:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIStatusBarIndicator interface implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QApplication>
|
---|
30 | #include <QHBoxLayout>
|
---|
31 | #include <QLabel>
|
---|
32 | #include <QIcon>
|
---|
33 | #include <QPainter>
|
---|
34 | #include <QStyle>
|
---|
35 | #include <QWindow>
|
---|
36 | #ifdef VBOX_WS_MAC
|
---|
37 | # include <QContextMenuEvent>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | /* GUI includes: */
|
---|
41 | #include "QIStatusBarIndicator.h"
|
---|
42 |
|
---|
43 | /* Other VBox includes: */
|
---|
44 | #include <iprt/assert.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Class QIStatusBarIndicator implementation. *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 |
|
---|
51 | QIStatusBarIndicator::QIStatusBarIndicator(QWidget *pParent /* = 0 */)
|
---|
52 | : QWidget(pParent)
|
---|
53 | {
|
---|
54 | /* Configure size-policy: */
|
---|
55 | setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
|
---|
56 | }
|
---|
57 |
|
---|
58 | #ifdef VBOX_WS_MAC
|
---|
59 | void QIStatusBarIndicator::mousePressEvent(QMouseEvent *pEvent)
|
---|
60 | {
|
---|
61 | // WORKAROUND:
|
---|
62 | // Do this for the left mouse button event only, cause in the case of the
|
---|
63 | // right mouse button it could happen that the context menu event is
|
---|
64 | // triggered twice. Also this isn't necessary for the middle mouse button
|
---|
65 | // which would be some kind of overstated.
|
---|
66 | if (pEvent->button() == Qt::LeftButton)
|
---|
67 | {
|
---|
68 | QContextMenuEvent cme(QContextMenuEvent::Mouse, pEvent->position().toPoint(), pEvent->globalPosition().toPoint());
|
---|
69 | emit sigContextMenuRequest(this, &cme);
|
---|
70 | if (cme.isAccepted())
|
---|
71 | pEvent->accept();
|
---|
72 | else
|
---|
73 | QWidget::mousePressEvent(pEvent);
|
---|
74 | }
|
---|
75 | else
|
---|
76 | QWidget::mousePressEvent(pEvent);
|
---|
77 | }
|
---|
78 | #endif /* VBOX_WS_MAC */
|
---|
79 |
|
---|
80 | void QIStatusBarIndicator::mouseDoubleClickEvent(QMouseEvent *pEvent)
|
---|
81 | {
|
---|
82 | emit sigMouseDoubleClick(this, pEvent);
|
---|
83 | }
|
---|
84 |
|
---|
85 | void QIStatusBarIndicator::contextMenuEvent(QContextMenuEvent *pEvent)
|
---|
86 | {
|
---|
87 | emit sigContextMenuRequest(this, pEvent);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | /*********************************************************************************************************************************
|
---|
92 | * Class QIStateStatusBarIndicator implementation. *
|
---|
93 | *********************************************************************************************************************************/
|
---|
94 |
|
---|
95 | QIStateStatusBarIndicator::QIStateStatusBarIndicator(QWidget *pParent /* = 0 */)
|
---|
96 | : QIStatusBarIndicator(pParent)
|
---|
97 | , m_iState(0)
|
---|
98 | {
|
---|
99 | }
|
---|
100 |
|
---|
101 | QIcon QIStateStatusBarIndicator::stateIcon(int iState) const
|
---|
102 | {
|
---|
103 | /* Check if state-icon was set before: */
|
---|
104 | return m_icons.value(iState, QIcon());
|
---|
105 | }
|
---|
106 |
|
---|
107 | void QIStateStatusBarIndicator::setStateIcon(int iState, const QIcon &icon)
|
---|
108 | {
|
---|
109 | /* Adjust size-hint: */
|
---|
110 | const QStyle *pStyle = QApplication::style();
|
---|
111 | const int iIconMetric = pStyle->pixelMetric(QStyle::PM_SmallIconSize);
|
---|
112 | m_size = QSize(iIconMetric, iIconMetric);
|
---|
113 | /* Cache passed-icon: */
|
---|
114 | m_icons[iState] = icon;
|
---|
115 | }
|
---|
116 |
|
---|
117 | void QIStateStatusBarIndicator::paintEvent(QPaintEvent *)
|
---|
118 | {
|
---|
119 | QPainter painter(this);
|
---|
120 | drawContents(&painter);
|
---|
121 | }
|
---|
122 |
|
---|
123 | void QIStateStatusBarIndicator::drawContents(QPainter *pPainter)
|
---|
124 | {
|
---|
125 | if (m_icons.contains(m_iState))
|
---|
126 | {
|
---|
127 | const qreal fDevicePixelRatio = window() && window()->windowHandle() ? window()->windowHandle()->devicePixelRatio() : 1;
|
---|
128 | pPainter->drawPixmap(contentsRect().topLeft(), m_icons.value(m_iState).pixmap(m_size, fDevicePixelRatio));
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /*********************************************************************************************************************************
|
---|
134 | * Class QITextStatusBarIndicator implementation. *
|
---|
135 | *********************************************************************************************************************************/
|
---|
136 |
|
---|
137 | QITextStatusBarIndicator::QITextStatusBarIndicator(QWidget *pParent /* = 0 */)
|
---|
138 | : QIStatusBarIndicator(pParent)
|
---|
139 | , m_pLabel(0)
|
---|
140 | {
|
---|
141 | /* Create main-layout: */
|
---|
142 | QHBoxLayout *pMainLayout = new QHBoxLayout(this);
|
---|
143 | if (pMainLayout)
|
---|
144 | {
|
---|
145 | /* Configure main-layout: */
|
---|
146 | pMainLayout->setContentsMargins(0, 0, 0, 0);
|
---|
147 | pMainLayout->setSpacing(0);
|
---|
148 | /* Create label: */
|
---|
149 | m_pLabel = new QLabel;
|
---|
150 | if (m_pLabel)
|
---|
151 | {
|
---|
152 | /* Add label into main-layout: */
|
---|
153 | pMainLayout->addWidget(m_pLabel);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | QString QITextStatusBarIndicator::text() const
|
---|
159 | {
|
---|
160 | AssertPtrReturn(m_pLabel, QString());
|
---|
161 | return m_pLabel->text();
|
---|
162 | }
|
---|
163 |
|
---|
164 | void QITextStatusBarIndicator::setText(const QString &strText)
|
---|
165 | {
|
---|
166 | AssertPtrReturnVoid(m_pLabel);
|
---|
167 | m_pLabel->setText(strText);
|
---|
168 | }
|
---|