1 | /* $Id: QIStatusBarIndicator.cpp 100344 2023-07-03 10:09:28Z 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 | #ifdef VBOX_WS_MAC
|
---|
36 | # include <QContextMenuEvent>
|
---|
37 | #endif /* VBOX_WS_MAC */
|
---|
38 | #ifdef VBOX_IS_QT6_OR_LATER
|
---|
39 | # include <QWindow>
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | /* GUI includes: */
|
---|
43 | #include "QIStatusBarIndicator.h"
|
---|
44 |
|
---|
45 | /* Other VBox includes: */
|
---|
46 | #include <iprt/assert.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Class QIStatusBarIndicator implementation. *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 |
|
---|
53 | QIStatusBarIndicator::QIStatusBarIndicator(QWidget *pParent /* = 0 */)
|
---|
54 | : QWidget(pParent)
|
---|
55 | {
|
---|
56 | /* Configure size-policy: */
|
---|
57 | setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
|
---|
58 | }
|
---|
59 |
|
---|
60 | #ifdef VBOX_WS_MAC
|
---|
61 | void QIStatusBarIndicator::mousePressEvent(QMouseEvent *pEvent)
|
---|
62 | {
|
---|
63 | // WORKAROUND:
|
---|
64 | // Do this for the left mouse button event only, cause in the case of the
|
---|
65 | // right mouse button it could happen that the context menu event is
|
---|
66 | // triggered twice. Also this isn't necessary for the middle mouse button
|
---|
67 | // which would be some kind of overstated.
|
---|
68 | if (pEvent->button() == Qt::LeftButton)
|
---|
69 | {
|
---|
70 | #ifndef VBOX_IS_QT6_OR_LATER /* QMouseEvent::globalPos was replaced with QSinglePointEvent::globalPosition in Qt6 */
|
---|
71 | QContextMenuEvent cme(QContextMenuEvent::Mouse, pEvent->pos(), pEvent->globalPos());
|
---|
72 | #else
|
---|
73 | QContextMenuEvent cme(QContextMenuEvent::Mouse, pEvent->position().toPoint(), pEvent->globalPosition().toPoint());
|
---|
74 | #endif
|
---|
75 | emit sigContextMenuRequest(this, &cme);
|
---|
76 | if (cme.isAccepted())
|
---|
77 | pEvent->accept();
|
---|
78 | else
|
---|
79 | QWidget::mousePressEvent(pEvent);
|
---|
80 | }
|
---|
81 | else
|
---|
82 | QWidget::mousePressEvent(pEvent);
|
---|
83 | }
|
---|
84 | #endif /* VBOX_WS_MAC */
|
---|
85 |
|
---|
86 | void QIStatusBarIndicator::mouseDoubleClickEvent(QMouseEvent *pEvent)
|
---|
87 | {
|
---|
88 | emit sigMouseDoubleClick(this, pEvent);
|
---|
89 | }
|
---|
90 |
|
---|
91 | void QIStatusBarIndicator::contextMenuEvent(QContextMenuEvent *pEvent)
|
---|
92 | {
|
---|
93 | emit sigContextMenuRequest(this, pEvent);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /*********************************************************************************************************************************
|
---|
98 | * Class QIStateStatusBarIndicator implementation. *
|
---|
99 | *********************************************************************************************************************************/
|
---|
100 |
|
---|
101 | QIStateStatusBarIndicator::QIStateStatusBarIndicator(QWidget *pParent /* = 0 */)
|
---|
102 | : QIStatusBarIndicator(pParent)
|
---|
103 | , m_iState(0)
|
---|
104 | {
|
---|
105 | }
|
---|
106 |
|
---|
107 | QIcon QIStateStatusBarIndicator::stateIcon(int iState) const
|
---|
108 | {
|
---|
109 | /* Check if state-icon was set before: */
|
---|
110 | return m_icons.value(iState, QIcon());
|
---|
111 | }
|
---|
112 |
|
---|
113 | void QIStateStatusBarIndicator::setStateIcon(int iState, const QIcon &icon)
|
---|
114 | {
|
---|
115 | /* Adjust size-hint: */
|
---|
116 | const QStyle *pStyle = QApplication::style();
|
---|
117 | const int iIconMetric = pStyle->pixelMetric(QStyle::PM_SmallIconSize);
|
---|
118 | m_size = QSize(iIconMetric, iIconMetric);
|
---|
119 | /* Cache passed-icon: */
|
---|
120 | m_icons[iState] = icon;
|
---|
121 | }
|
---|
122 |
|
---|
123 | void QIStateStatusBarIndicator::paintEvent(QPaintEvent *)
|
---|
124 | {
|
---|
125 | QPainter painter(this);
|
---|
126 | drawContents(&painter);
|
---|
127 | }
|
---|
128 |
|
---|
129 | void QIStateStatusBarIndicator::drawContents(QPainter *pPainter)
|
---|
130 | {
|
---|
131 | if (m_icons.contains(m_iState))
|
---|
132 | {
|
---|
133 | if (window())
|
---|
134 | #ifndef VBOX_IS_QT6_OR_LATER /* QIcon::pixmap taking QWindow is deprecated in Qt6 */
|
---|
135 | pPainter->drawPixmap(contentsRect().topLeft(), m_icons.value(m_iState).pixmap(window()->windowHandle(), m_size));
|
---|
136 | #else
|
---|
137 | {
|
---|
138 | const qreal fDevicePixelRatio = window()->windowHandle() ? window()->windowHandle()->devicePixelRatio() : 1;
|
---|
139 | pPainter->drawPixmap(contentsRect().topLeft(), m_icons.value(m_iState).pixmap(m_size, fDevicePixelRatio));
|
---|
140 | }
|
---|
141 | #endif
|
---|
142 | else
|
---|
143 | pPainter->drawPixmap(contentsRect().topLeft(), m_icons.value(m_iState).pixmap(m_size));
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /*********************************************************************************************************************************
|
---|
149 | * Class QITextStatusBarIndicator implementation. *
|
---|
150 | *********************************************************************************************************************************/
|
---|
151 |
|
---|
152 | QITextStatusBarIndicator::QITextStatusBarIndicator(QWidget *pParent /* = 0 */)
|
---|
153 | : QIStatusBarIndicator(pParent)
|
---|
154 | , m_pLabel(0)
|
---|
155 | {
|
---|
156 | /* Create main-layout: */
|
---|
157 | QHBoxLayout *pMainLayout = new QHBoxLayout(this);
|
---|
158 | if (pMainLayout)
|
---|
159 | {
|
---|
160 | /* Configure main-layout: */
|
---|
161 | pMainLayout->setContentsMargins(0, 0, 0, 0);
|
---|
162 | pMainLayout->setSpacing(0);
|
---|
163 | /* Create label: */
|
---|
164 | m_pLabel = new QLabel;
|
---|
165 | if (m_pLabel)
|
---|
166 | {
|
---|
167 | /* Add label into main-layout: */
|
---|
168 | pMainLayout->addWidget(m_pLabel);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | QString QITextStatusBarIndicator::text() const
|
---|
174 | {
|
---|
175 | AssertPtrReturn(m_pLabel, QString());
|
---|
176 | return m_pLabel->text();
|
---|
177 | }
|
---|
178 |
|
---|
179 | void QITextStatusBarIndicator::setText(const QString &strText)
|
---|
180 | {
|
---|
181 | AssertPtrReturnVoid(m_pLabel);
|
---|
182 | m_pLabel->setText(strText);
|
---|
183 | }
|
---|