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