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