1 | /* $Id: UISpecialControls.cpp 100344 2023-07-03 10:09:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UISpecialControls implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 <QHBoxLayout>
|
---|
30 | #ifndef VBOX_DARWIN_USE_NATIVE_CONTROLS
|
---|
31 | # include <QBitmap>
|
---|
32 | # include <QMouseEvent>
|
---|
33 | # include <QPainter>
|
---|
34 | # include <QSignalMapper>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | /* GUI includes: */
|
---|
38 | #include "UIIconPool.h"
|
---|
39 | #include "UISpecialControls.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | #ifdef VBOX_DARWIN_USE_NATIVE_CONTROLS
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Class UIMiniCancelButton implementation. *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 |
|
---|
49 | UIMiniCancelButton::UIMiniCancelButton(QWidget *pParent /* = 0 */)
|
---|
50 | : QAbstractButton(pParent)
|
---|
51 | {
|
---|
52 | setShortcut(QKeySequence(Qt::Key_Escape));
|
---|
53 | m_pButton = new UICocoaButton(this, UICocoaButton::CancelButton);
|
---|
54 | connect(m_pButton, SIGNAL(clicked()), this, SIGNAL(clicked()));
|
---|
55 | setFixedSize(m_pButton->size());
|
---|
56 | }
|
---|
57 |
|
---|
58 | void UIMiniCancelButton::resizeEvent(QResizeEvent *)
|
---|
59 | {
|
---|
60 | m_pButton->resize(size());
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | /*********************************************************************************************************************************
|
---|
65 | * Class UIHelpButton implementation. *
|
---|
66 | *********************************************************************************************************************************/
|
---|
67 |
|
---|
68 | UIHelpButton::UIHelpButton(QWidget *pParent /* = 0 */)
|
---|
69 | : QPushButton(pParent)
|
---|
70 | {
|
---|
71 | setShortcut(QKeySequence(QKeySequence::HelpContents));
|
---|
72 | m_pButton = new UICocoaButton(this, UICocoaButton::HelpButton);
|
---|
73 | connect(m_pButton, SIGNAL(clicked()), this, SIGNAL(clicked()));
|
---|
74 | setFixedSize(m_pButton->size());
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | #else /* !VBOX_DARWIN_USE_NATIVE_CONTROLS */
|
---|
79 |
|
---|
80 |
|
---|
81 | /*********************************************************************************************************************************
|
---|
82 | * Class UIMiniCancelButton implementation. *
|
---|
83 | *********************************************************************************************************************************/
|
---|
84 |
|
---|
85 | UIMiniCancelButton::UIMiniCancelButton(QWidget *pParent /* = 0 */)
|
---|
86 | : QIWithRetranslateUI<QIToolButton>(pParent)
|
---|
87 | {
|
---|
88 | setAutoRaise(true);
|
---|
89 | setFocusPolicy(Qt::TabFocus);
|
---|
90 | setShortcut(QKeySequence(Qt::Key_Escape));
|
---|
91 | setIcon(UIIconPool::defaultIcon(UIIconPool::UIDefaultIconType_DialogCancel));
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /*********************************************************************************************************************************
|
---|
96 | * Class UIHelpButton implementation. *
|
---|
97 | *********************************************************************************************************************************/
|
---|
98 |
|
---|
99 | /* From: src/gui/styles/qmacstyle_mac.cpp */
|
---|
100 | static const int PushButtonLeftOffset = 6;
|
---|
101 | static const int PushButtonTopOffset = 4;
|
---|
102 | static const int PushButtonRightOffset = 12;
|
---|
103 | static const int PushButtonBottomOffset = 4;
|
---|
104 |
|
---|
105 | UIHelpButton::UIHelpButton(QWidget *pParent /* = 0 */)
|
---|
106 | : QIWithRetranslateUI<QPushButton>(pParent)
|
---|
107 | {
|
---|
108 | # ifdef VBOX_WS_MAC
|
---|
109 | m_pButtonPressed = false;
|
---|
110 | m_pNormalPixmap = new QPixmap(":/help_button_normal_mac_24px.png");
|
---|
111 | m_pPressedPixmap = new QPixmap(":/help_button_pressed_mac_24px.png");
|
---|
112 | m_size = m_pNormalPixmap->size();
|
---|
113 | m_pMask = new QImage(m_pNormalPixmap->mask().toImage());
|
---|
114 | m_BRect = QRect(PushButtonLeftOffset,
|
---|
115 | PushButtonTopOffset,
|
---|
116 | m_size.width(),
|
---|
117 | m_size.height());
|
---|
118 | # endif /* VBOX_WS_MAC */
|
---|
119 |
|
---|
120 | /* Apply language settings: */
|
---|
121 | retranslateUi();
|
---|
122 | }
|
---|
123 |
|
---|
124 | void UIHelpButton::initFrom(QPushButton *pOther)
|
---|
125 | {
|
---|
126 | /* Copy settings from pOther: */
|
---|
127 | setIcon(pOther->icon());
|
---|
128 | setText(pOther->text());
|
---|
129 | setShortcut(pOther->shortcut());
|
---|
130 | setFlat(pOther->isFlat());
|
---|
131 | setAutoDefault(pOther->autoDefault());
|
---|
132 | setDefault(pOther->isDefault());
|
---|
133 |
|
---|
134 | /* Apply language settings: */
|
---|
135 | retranslateUi();
|
---|
136 | }
|
---|
137 |
|
---|
138 | void UIHelpButton::retranslateUi()
|
---|
139 | {
|
---|
140 | QPushButton::setText(tr("&Help"));
|
---|
141 | if (QPushButton::shortcut().isEmpty())
|
---|
142 | QPushButton::setShortcut(QKeySequence::HelpContents);
|
---|
143 | }
|
---|
144 |
|
---|
145 | # ifdef VBOX_WS_MAC
|
---|
146 | UIHelpButton::~UIHelpButton()
|
---|
147 | {
|
---|
148 | delete m_pNormalPixmap;
|
---|
149 | delete m_pPressedPixmap;
|
---|
150 | delete m_pMask;
|
---|
151 | }
|
---|
152 |
|
---|
153 | QSize UIHelpButton::sizeHint() const
|
---|
154 | {
|
---|
155 | return QSize(m_size.width() + PushButtonLeftOffset + PushButtonRightOffset,
|
---|
156 | m_size.height() + PushButtonTopOffset + PushButtonBottomOffset);
|
---|
157 | }
|
---|
158 |
|
---|
159 | void UIHelpButton::paintEvent(QPaintEvent *)
|
---|
160 | {
|
---|
161 | QPainter painter(this);
|
---|
162 | painter.drawPixmap(PushButtonLeftOffset, PushButtonTopOffset, m_pButtonPressed ? *m_pPressedPixmap: *m_pNormalPixmap);
|
---|
163 | }
|
---|
164 |
|
---|
165 | bool UIHelpButton::hitButton(const QPoint &position) const
|
---|
166 | {
|
---|
167 | if (m_BRect.contains(position))
|
---|
168 | return m_pMask->pixel(position.x() - PushButtonLeftOffset,
|
---|
169 | position.y() - PushButtonTopOffset) == 0xff000000;
|
---|
170 | else
|
---|
171 | return false;
|
---|
172 | }
|
---|
173 |
|
---|
174 | void UIHelpButton::mousePressEvent(QMouseEvent *pEvent)
|
---|
175 | {
|
---|
176 | #ifndef VBOX_IS_QT6_OR_LATER /* QMouseEvent::pos was replaced with QSinglePointEvent::position in Qt6 */
|
---|
177 | if (hitButton(pEvent->pos()))
|
---|
178 | #else
|
---|
179 | if (hitButton(pEvent->position().toPoint()))
|
---|
180 | #endif
|
---|
181 | m_pButtonPressed = true;
|
---|
182 | QPushButton::mousePressEvent(pEvent);
|
---|
183 | update();
|
---|
184 | }
|
---|
185 |
|
---|
186 | void UIHelpButton::mouseReleaseEvent(QMouseEvent *pEvent)
|
---|
187 | {
|
---|
188 | QPushButton::mouseReleaseEvent(pEvent);
|
---|
189 | m_pButtonPressed = false;
|
---|
190 | update();
|
---|
191 | }
|
---|
192 |
|
---|
193 | void UIHelpButton::leaveEvent(QEvent *pEvent)
|
---|
194 | {
|
---|
195 | QPushButton::leaveEvent(pEvent);
|
---|
196 | m_pButtonPressed = false;
|
---|
197 | update();
|
---|
198 | }
|
---|
199 | # endif /* VBOX_WS_MAC */
|
---|
200 |
|
---|
201 |
|
---|
202 | #endif /* !VBOX_DARWIN_USE_NATIVE_CONTROLS */
|
---|
203 |
|
---|