1 | /* $Id: QIRichToolButton.cpp 76606 2019-01-02 05:40:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIRichToolButton class declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /* Qt includes: */
|
---|
19 | #include <QHBoxLayout>
|
---|
20 | #include <QKeyEvent>
|
---|
21 | #include <QLabel>
|
---|
22 | #include <QStyleOptionFocusRect>
|
---|
23 | #include <QStylePainter>
|
---|
24 |
|
---|
25 | /* GUI includes: */
|
---|
26 | #include "QIRichToolButton.h"
|
---|
27 | #include "QIToolButton.h"
|
---|
28 |
|
---|
29 | /* Other VBox includes: */
|
---|
30 | #include "iprt/assert.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | QIRichToolButton::QIRichToolButton(QWidget *pParent)
|
---|
34 | : QWidget(pParent)
|
---|
35 | , m_pButton(0)
|
---|
36 | , m_pLabel(0)
|
---|
37 | {
|
---|
38 | /* Prepare: */
|
---|
39 | prepare();
|
---|
40 | }
|
---|
41 |
|
---|
42 | void QIRichToolButton::setIconSize(const QSize &iconSize)
|
---|
43 | {
|
---|
44 | m_pButton->setIconSize(iconSize);
|
---|
45 | }
|
---|
46 |
|
---|
47 | void QIRichToolButton::setIcon(const QIcon &icon)
|
---|
48 | {
|
---|
49 | m_pButton->setIcon(icon);
|
---|
50 | }
|
---|
51 |
|
---|
52 | void QIRichToolButton::animateClick()
|
---|
53 | {
|
---|
54 | m_pButton->animateClick();
|
---|
55 | }
|
---|
56 |
|
---|
57 | void QIRichToolButton::setText(const QString &strText)
|
---|
58 | {
|
---|
59 | m_pLabel->setText(strText);
|
---|
60 | }
|
---|
61 |
|
---|
62 | void QIRichToolButton::paintEvent(QPaintEvent *pEvent)
|
---|
63 | {
|
---|
64 | /* Draw focus around whole button if focused: */
|
---|
65 | if (hasFocus())
|
---|
66 | {
|
---|
67 | QStylePainter painter(this);
|
---|
68 | QStyleOptionFocusRect option;
|
---|
69 | option.initFrom(this);
|
---|
70 | option.rect = geometry();
|
---|
71 | painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);
|
---|
72 | }
|
---|
73 | /* Call to base-class: */
|
---|
74 | QWidget::paintEvent(pEvent);
|
---|
75 | }
|
---|
76 |
|
---|
77 | void QIRichToolButton::keyPressEvent(QKeyEvent *pEvent)
|
---|
78 | {
|
---|
79 | /* Handle different keys: */
|
---|
80 | switch (pEvent->key())
|
---|
81 | {
|
---|
82 | /* Animate-click for the Space key: */
|
---|
83 | case Qt::Key_Space: return animateClick();
|
---|
84 | default: break;
|
---|
85 | }
|
---|
86 | /* Call to base-class: */
|
---|
87 | QWidget::keyPressEvent(pEvent);
|
---|
88 | }
|
---|
89 |
|
---|
90 | void QIRichToolButton::mousePressEvent(QMouseEvent *pEvent)
|
---|
91 | {
|
---|
92 | NOREF(pEvent);
|
---|
93 | /* Animate-click: */
|
---|
94 | animateClick();
|
---|
95 | }
|
---|
96 |
|
---|
97 | void QIRichToolButton::prepare()
|
---|
98 | {
|
---|
99 | /* Enable string focus: */
|
---|
100 | setFocusPolicy(Qt::StrongFocus);
|
---|
101 |
|
---|
102 | /* Create main-layout: */
|
---|
103 | QHBoxLayout *pMainLayout = new QHBoxLayout(this);
|
---|
104 | AssertPtrReturnVoid(pMainLayout);
|
---|
105 | {
|
---|
106 | /* Configure main-layout: */
|
---|
107 | pMainLayout->setContentsMargins(0, 0, 0, 0);
|
---|
108 | pMainLayout->setSpacing(0);
|
---|
109 | /* Create tool-button: */
|
---|
110 | m_pButton = new QIToolButton;
|
---|
111 | AssertPtrReturnVoid(m_pButton);
|
---|
112 | {
|
---|
113 | /* Configure tool-button: */
|
---|
114 | m_pButton->removeBorder();
|
---|
115 | m_pButton->setFocusPolicy(Qt::NoFocus);
|
---|
116 | connect(m_pButton, &QIToolButton::clicked, this, &QIRichToolButton::sltButtonClicked);
|
---|
117 | connect(m_pButton, &QIToolButton::clicked, this, &QIRichToolButton::sigClicked);
|
---|
118 | /* Add tool-button into main-layout: */
|
---|
119 | pMainLayout->addWidget(m_pButton);
|
---|
120 | }
|
---|
121 | /* Create text-label: */
|
---|
122 | m_pLabel = new QLabel;
|
---|
123 | AssertPtrReturnVoid(m_pLabel);
|
---|
124 | {
|
---|
125 | /* Configure text-label: */
|
---|
126 | m_pLabel->setBuddy(m_pButton);
|
---|
127 | m_pLabel->setStyleSheet("QLabel {padding: 2px 0px 2px 0px;}");
|
---|
128 | /* Add text-label into main-layout: */
|
---|
129 | pMainLayout->addWidget(m_pLabel);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|