PeriDyno 1.0.0
Loading...
Searching...
No Matches
CompactToolButton.cpp
Go to the documentation of this file.
1/*
2 TabToolbar - a small utility library for Qt, providing tabbed toolbars
3 Copyright (C) 2018 Oleksii Sierov
4
5 TabToolbar is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TabToolbar is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with TabToolbar. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include <QAction>
19#include <QMenu>
20#include <QToolButton>
21#include <QPainter>
22#include <QVBoxLayout>
23#include <QStyle>
24#include <QApplication>
25#include <QLinearGradient>
26#include <QStylePainter>
27#include <QStyleOptionToolButton>
28#include <QResizeEvent>
29#include <QScreen>
30
31#include "StyleTools.h"
32#include "TabToolbar.h"
33#include "CompactToolButton.h"
34#include "ToolButtonStyle.h"
35
36namespace
37{
38class TTOverlayToolButton : public QToolButton
39{
40public:
41 TTOverlayToolButton(QWidget* parent) : QToolButton(parent)
42 {
43 setAttribute(Qt::WA_NoSystemBackground);
44 setAttribute(Qt::WA_TranslucentBackground);
45 setAttribute(Qt::WA_TransparentForMouseEvents);
46 parent->installEventFilter(this);
47 lower();
48 }
49
50 bool paint = false;
51
52protected:
53 bool eventFilter(QObject* obj, QEvent* ev) override
54 {
55 if (obj == parent())
56 {
57 if (ev->type() == QEvent::Resize)
58 resize(static_cast<QResizeEvent*>(ev)->size());
59 else if (ev->type() == QEvent::ChildAdded)
60 lower();
61 }
62 return QWidget::eventFilter(obj, ev);
63 }
64
65 void paintEvent(QPaintEvent*) override
66 {
67 if(!paint)
68 return;
69
70 QStylePainter sp(this);
71 QStyleOptionToolButton opt;
72 initStyleOption(&opt);
73 opt.state |= QStyle::State_MouseOver | QStyle::State_AutoRaise | QStyle::State_Raised;
74 opt.activeSubControls |= QStyle::SC_ToolButton;
75 sp.drawComplexControl(QStyle::CC_ToolButton, opt);
76 }
77};
78
79class TTHover : public QObject
80{
81public:
82 TTHover(tt::CompactToolButton* parent, QToolButton* up, QToolButton* down) :
83 QObject(parent),
84 toolButton(parent),
85 upButton(up),
86 downButton(down)
87 {
88 }
89
90protected:
91 bool eventFilter(QObject* watched, QEvent* event) override
92 {
93 if(event->type() == QEvent::HoverLeave)
94 {
95 toolButton->SetHover(false);
96 }
97 else if(event->type() == QEvent::HoverEnter)
98 {
99 if(watched == upButton || watched == downButton)
100 toolButton->SetHover(upButton->isEnabled());
101 }
102 if(watched == upButton)
103 {
104 if(event->type() == QEvent::Hide)
105 downButton->hide();
106 else if(event->type() == QEvent::Show)
107 downButton->show();
108 else if(event->type() == QEvent::EnabledChange)
109 {
110 downButton->setEnabled(upButton->isEnabled());
111 toolButton->SetHover(upButton->isEnabled() && upButton->underMouse());
112 }
113 }
114 return QObject::eventFilter(watched, event);
115 }
116
117private:
118 tt::CompactToolButton* toolButton;
119 QToolButton* upButton;
120 QToolButton* downButton;
121};
122}
123
124namespace tt
125{
126
127CompactToolButton::CompactToolButton(QAction* action, QMenu* menu, QWidget* parent) :
128 QFrame(parent)
129{
130 overlay = new TTOverlayToolButton(this);
131
132 const int iconSize = GetPixelMetric(QStyle::PM_LargeIconSize) * GetScaleFactor(*this);
133 upButton = new QToolButton(this);
134 upButton->setProperty("TTInternal", QVariant(true));
135 upButton->setAutoRaise(true);
136 upButton->setDefaultAction(action);
137 upButton->setIconSize(QSize(iconSize, iconSize));
138 upButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
139 upButton->setStyle(new TTToolButtonStyle());
140 upButton->setMaximumHeight(iconSize + 5);
141
142 QVBoxLayout* l = new QVBoxLayout(this);
143 l->setContentsMargins(0, 0, 0, 0);
144 l->setSpacing(0);
145 l->setDirection(QBoxLayout::TopToBottom);
146
147 upButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
148 upButton->setPopupMode(QToolButton::DelayedPopup);
149 l->addWidget(upButton);
150
151 downButton = new QToolButton(this);
152 downButton->setProperty("TTInternal", QVariant(true));
153 downButton->setAutoRaise(true);
154 downButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
155 downButton->setPopupMode(QToolButton::InstantPopup);
156 downButton->setMinimumHeight(25);
157 downButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
158 downButton->setText(action->text());
159 downButton->setToolTip(action->toolTip());
160 downButton->setStyle(new TTToolButtonStyle());
161
162 if(menu)
163 {
164 downButton->setMenu(menu);
165 QObject::connect(menu, &QMenu::aboutToHide, this, [this]{ SetHover(false); });
166 }
167 l->addWidget(downButton);
168 setLayout(l);
169
170 TTHover* hover = new TTHover(this, upButton, downButton);
171 upButton->installEventFilter(hover);
172 downButton->installEventFilter(hover);
173}
174
176{
177 static_cast<TTOverlayToolButton*>(overlay)->paint = hover;
178 update();
179}
180
181}
CompactToolButton(QAction *action, QMenu *menu, QWidget *parent=nullptr)
Definition Builder.h:33
int GetPixelMetric(QStyle::PixelMetric metric)
float GetScaleFactor(const QWidget &widget)