1 | /* $Id: QIRichToolButton.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIRichToolButton class declaration.
|
---|
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 <QHBoxLayout>
|
---|
30 | #include <QKeyEvent>
|
---|
31 | #include <QLabel>
|
---|
32 | #include <QStyleOptionFocusRect>
|
---|
33 | #include <QStylePainter>
|
---|
34 |
|
---|
35 | /* GUI includes: */
|
---|
36 | #include "QIRichToolButton.h"
|
---|
37 | #include "QIToolButton.h"
|
---|
38 |
|
---|
39 | /* Other VBox includes: */
|
---|
40 | #include "iprt/assert.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | QIRichToolButton::QIRichToolButton(QWidget *pParent)
|
---|
44 | : QWidget(pParent)
|
---|
45 | , m_pButton(0)
|
---|
46 | , m_pLabel(0)
|
---|
47 | {
|
---|
48 | /* Prepare: */
|
---|
49 | prepare();
|
---|
50 | }
|
---|
51 |
|
---|
52 | void QIRichToolButton::setIconSize(const QSize &iconSize)
|
---|
53 | {
|
---|
54 | m_pButton->setIconSize(iconSize);
|
---|
55 | }
|
---|
56 |
|
---|
57 | void QIRichToolButton::setIcon(const QIcon &icon)
|
---|
58 | {
|
---|
59 | m_pButton->setIcon(icon);
|
---|
60 | }
|
---|
61 |
|
---|
62 | void QIRichToolButton::animateClick()
|
---|
63 | {
|
---|
64 | m_pButton->animateClick();
|
---|
65 | }
|
---|
66 |
|
---|
67 | void QIRichToolButton::setText(const QString &strText)
|
---|
68 | {
|
---|
69 | m_pLabel->setText(strText);
|
---|
70 | }
|
---|
71 |
|
---|
72 | void QIRichToolButton::paintEvent(QPaintEvent *pEvent)
|
---|
73 | {
|
---|
74 | /* Draw focus around whole button if focused: */
|
---|
75 | if (hasFocus())
|
---|
76 | {
|
---|
77 | QStylePainter painter(this);
|
---|
78 | QStyleOptionFocusRect option;
|
---|
79 | option.initFrom(this);
|
---|
80 | option.rect = geometry();
|
---|
81 | painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);
|
---|
82 | }
|
---|
83 | /* Call to base-class: */
|
---|
84 | QWidget::paintEvent(pEvent);
|
---|
85 | }
|
---|
86 |
|
---|
87 | void QIRichToolButton::keyPressEvent(QKeyEvent *pEvent)
|
---|
88 | {
|
---|
89 | /* Handle different keys: */
|
---|
90 | switch (pEvent->key())
|
---|
91 | {
|
---|
92 | /* Animate-click for the Space key: */
|
---|
93 | case Qt::Key_Space: return animateClick();
|
---|
94 | default: break;
|
---|
95 | }
|
---|
96 | /* Call to base-class: */
|
---|
97 | QWidget::keyPressEvent(pEvent);
|
---|
98 | }
|
---|
99 |
|
---|
100 | void QIRichToolButton::mousePressEvent(QMouseEvent *pEvent)
|
---|
101 | {
|
---|
102 | NOREF(pEvent);
|
---|
103 | /* Animate-click: */
|
---|
104 | animateClick();
|
---|
105 | }
|
---|
106 |
|
---|
107 | void QIRichToolButton::prepare()
|
---|
108 | {
|
---|
109 | /* Enable string focus: */
|
---|
110 | setFocusPolicy(Qt::StrongFocus);
|
---|
111 |
|
---|
112 | /* Create main-layout: */
|
---|
113 | QHBoxLayout *pMainLayout = new QHBoxLayout(this);
|
---|
114 | AssertPtrReturnVoid(pMainLayout);
|
---|
115 | {
|
---|
116 | /* Configure main-layout: */
|
---|
117 | pMainLayout->setContentsMargins(0, 0, 0, 0);
|
---|
118 | pMainLayout->setSpacing(0);
|
---|
119 | /* Create tool-button: */
|
---|
120 | m_pButton = new QIToolButton;
|
---|
121 | AssertPtrReturnVoid(m_pButton);
|
---|
122 | {
|
---|
123 | /* Configure tool-button: */
|
---|
124 | m_pButton->removeBorder();
|
---|
125 | m_pButton->setFocusPolicy(Qt::NoFocus);
|
---|
126 | connect(m_pButton, &QIToolButton::clicked, this, &QIRichToolButton::sltButtonClicked);
|
---|
127 | connect(m_pButton, &QIToolButton::clicked, this, &QIRichToolButton::sigClicked);
|
---|
128 | /* Add tool-button into main-layout: */
|
---|
129 | pMainLayout->addWidget(m_pButton);
|
---|
130 | }
|
---|
131 | /* Create text-label: */
|
---|
132 | m_pLabel = new QLabel;
|
---|
133 | AssertPtrReturnVoid(m_pLabel);
|
---|
134 | {
|
---|
135 | /* Configure text-label: */
|
---|
136 | m_pLabel->setBuddy(m_pButton);
|
---|
137 | m_pLabel->setStyleSheet("QLabel {padding: 2px 0px 2px 0px;}");
|
---|
138 | /* Add text-label into main-layout: */
|
---|
139 | pMainLayout->addWidget(m_pLabel);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|