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