[26714] | 1 | /* $Id: QIRichToolButton.cpp 71900 2018-04-18 14:40:43Z vboxsync $ */
|
---|
[25177] | 2 | /** @file
|
---|
[71900] | 3 | * VBox Qt GUI - Qt extensions: QIRichToolButton class declaration.
|
---|
[25177] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[71404] | 7 | * Copyright (C) 2006-2018 Oracle Corporation
|
---|
[25177] | 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 |
|
---|
[52730] | 18 | #ifdef VBOX_WITH_PRECOMPILED_HEADERS
|
---|
| 19 | # include <precomp.h>
|
---|
| 20 | #else /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
| 21 |
|
---|
[51873] | 22 | /* Qt includes: */
|
---|
[52730] | 23 | # include <QHBoxLayout>
|
---|
[71404] | 24 | # include <QKeyEvent>
|
---|
[52730] | 25 | # include <QLabel>
|
---|
[71404] | 26 | # include <QStyleOptionFocusRect>
|
---|
[52730] | 27 | # include <QStylePainter>
|
---|
[25177] | 28 |
|
---|
[51873] | 29 | /* GUI includes: */
|
---|
[52730] | 30 | # include "QIRichToolButton.h"
|
---|
| 31 | # include "QIToolButton.h"
|
---|
[51873] | 32 |
|
---|
| 33 | /* Other VBox includes: */
|
---|
[52730] | 34 | # include "iprt/assert.h"
|
---|
[51873] | 35 |
|
---|
[52730] | 36 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
| 37 |
|
---|
| 38 |
|
---|
[51873] | 39 | QIRichToolButton::QIRichToolButton(QWidget *pParent)
|
---|
| 40 | : QWidget(pParent)
|
---|
| 41 | , m_pButton(0)
|
---|
| 42 | , m_pLabel(0)
|
---|
[25177] | 43 | {
|
---|
[51873] | 44 | /* Prepare: */
|
---|
| 45 | prepare();
|
---|
[25177] | 46 | }
|
---|
| 47 |
|
---|
[51873] | 48 | void QIRichToolButton::setIconSize(const QSize &iconSize)
|
---|
[25177] | 49 | {
|
---|
[51873] | 50 | m_pButton->setIconSize(iconSize);
|
---|
[25177] | 51 | }
|
---|
| 52 |
|
---|
[51873] | 53 | void QIRichToolButton::setIcon(const QIcon &icon)
|
---|
[25177] | 54 | {
|
---|
[51873] | 55 | m_pButton->setIcon(icon);
|
---|
| 56 | }
|
---|
[25177] | 57 |
|
---|
[51873] | 58 | void QIRichToolButton::animateClick()
|
---|
| 59 | {
|
---|
| 60 | m_pButton->animateClick();
|
---|
| 61 | }
|
---|
[25177] | 62 |
|
---|
[51873] | 63 | void QIRichToolButton::setText(const QString &strText)
|
---|
| 64 | {
|
---|
| 65 | m_pLabel->setText(strText);
|
---|
[25177] | 66 | }
|
---|
| 67 |
|
---|
[51873] | 68 | void QIRichToolButton::paintEvent(QPaintEvent *pEvent)
|
---|
[25177] | 69 | {
|
---|
[51873] | 70 | /* Draw focus around whole button if focused: */
|
---|
| 71 | if (hasFocus())
|
---|
[25177] | 72 | {
|
---|
[51873] | 73 | QStylePainter painter(this);
|
---|
| 74 | QStyleOptionFocusRect option;
|
---|
| 75 | option.initFrom(this);
|
---|
| 76 | option.rect = geometry();
|
---|
| 77 | painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);
|
---|
[25177] | 78 | }
|
---|
[51873] | 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())
|
---|
[25177] | 87 | {
|
---|
[51873] | 88 | /* Animate-click for the Space key: */
|
---|
| 89 | case Qt::Key_Space: return animateClick();
|
---|
| 90 | default: break;
|
---|
[25177] | 91 | }
|
---|
[51873] | 92 | /* Call to base-class: */
|
---|
| 93 | QWidget::keyPressEvent(pEvent);
|
---|
| 94 | }
|
---|
[25177] | 95 |
|
---|
[51873] | 96 | void QIRichToolButton::mousePressEvent(QMouseEvent *pEvent)
|
---|
| 97 | {
|
---|
[52267] | 98 | NOREF(pEvent);
|
---|
[51873] | 99 | /* Animate-click: */
|
---|
| 100 | animateClick();
|
---|
[25177] | 101 | }
|
---|
| 102 |
|
---|
[51873] | 103 | void QIRichToolButton::prepare()
|
---|
[25177] | 104 | {
|
---|
[51873] | 105 | /* Enable string focus: */
|
---|
| 106 | setFocusPolicy(Qt::StrongFocus);
|
---|
| 107 |
|
---|
| 108 | /* Create main-layout: */
|
---|
| 109 | QHBoxLayout *pMainLayout = new QHBoxLayout(this);
|
---|
| 110 | AssertPtrReturnVoid(pMainLayout);
|
---|
[25177] | 111 | {
|
---|
[51873] | 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);
|
---|
[68079] | 122 | connect(m_pButton, &QIToolButton::clicked, this, &QIRichToolButton::sltButtonClicked);
|
---|
| 123 | connect(m_pButton, &QIToolButton::clicked, this, &QIRichToolButton::sigClicked);
|
---|
[51873] | 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 | }
|
---|
[25177] | 137 | }
|
---|
| 138 | }
|
---|