PeriDyno 1.0.0
Loading...
Searching...
No Matches
ToolButtonStyle.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 <QPainter>
19#include <QStyleOptionToolButton>
20#include "ToolButtonStyle.h"
21
22using namespace tt;
23
24//redefine text alignment
25void TTToolButtonStyle::drawControl(ControlElement element, const QStyleOption* opt, QPainter* p, const QWidget* widget) const
26{
27 if(element == CE_ToolButtonLabel)
28 {
29 if (const QStyleOptionToolButton* toolbutton = static_cast<const QStyleOptionToolButton*>(opt))
30 {
31 QRect rect = toolbutton->rect;
32 int shiftX = 0;
33 int shiftY = 0;
34 if (toolbutton->state & (State_Sunken | State_On)) {
35 shiftX = proxy()->pixelMetric(PM_ButtonShiftHorizontal, toolbutton, widget);
36 shiftY = proxy()->pixelMetric(PM_ButtonShiftVertical, toolbutton, widget);
37 }
38 // Arrow type always overrules and is always shown
39 bool hasArrow = toolbutton->features & QStyleOptionToolButton::Arrow;
40 if (((!hasArrow && toolbutton->icon.isNull()) && !toolbutton->text.isEmpty())
41 || toolbutton->toolButtonStyle == Qt::ToolButtonTextOnly) {
42 int alignment = Qt::AlignTop | Qt::AlignHCenter | Qt::TextShowMnemonic;
43 if (!proxy()->styleHint(SH_UnderlineShortcut, opt, widget))
44 alignment |= Qt::TextHideMnemonic;
45 rect.translate(shiftX, shiftY);
46 p->setFont(toolbutton->font);
47 proxy()->drawItemText(p, rect, alignment, toolbutton->palette,
48 opt->state & State_Enabled, toolbutton->text,
49 QPalette::ButtonText);
50 } else {
51 QPixmap pm;
52 QSize pmSize = toolbutton->iconSize;
53 if (!toolbutton->icon.isNull()) {
54 QIcon::State state = toolbutton->state & State_On ? QIcon::On : QIcon::Off;
55 QIcon::Mode mode;
56 if (!(toolbutton->state & State_Enabled))
57 mode = QIcon::Disabled;
58 else if ((opt->state & State_MouseOver) && (opt->state & State_AutoRaise))
59 mode = QIcon::Active;
60 else
61 mode = QIcon::Normal;
62 pm = toolbutton->icon.pixmap(toolbutton->rect.size().boundedTo(toolbutton->iconSize),
63 mode, state);
64 pmSize = pm.size();
65 }
66
67 if (toolbutton->toolButtonStyle != Qt::ToolButtonIconOnly) {
68 p->setFont(toolbutton->font);
69 QRect pr = rect,
70 tr = rect;
71 int alignment = Qt::TextShowMnemonic;
72 if (!proxy()->styleHint(SH_UnderlineShortcut, opt, widget))
73 alignment |= Qt::TextHideMnemonic;
74
75 if (toolbutton->toolButtonStyle == Qt::ToolButtonTextUnderIcon) {
76 pr.setHeight(pmSize.height() + 6);
77 tr.adjust(0, pr.height() - 1, 0, -2);
78 pr.translate(shiftX, shiftY);
79 if (!hasArrow)
80 proxy()->drawItemPixmap(p, pr, Qt::AlignTop | Qt::AlignHCenter, pm);
81 alignment |= Qt::AlignTop | Qt::AlignHCenter;
82 } else {
83 pr.setWidth(pmSize.width() + 8);
84 tr.adjust(pr.width(), 0, 0, 0);
85 pr.translate(shiftX, shiftY);
86 if (!hasArrow)
87 proxy()->drawItemPixmap(p, QStyle::visualRect(opt->direction, rect, pr), Qt::AlignTop | Qt::AlignHCenter, pm);
88 alignment |= Qt::AlignTop | Qt::AlignHCenter;
89 }
90 tr.translate(shiftX, shiftY);
91 proxy()->drawItemText(p, QStyle::visualRect(opt->direction, rect, tr), alignment, toolbutton->palette,
92 toolbutton->state & State_Enabled, toolbutton->text,
93 QPalette::ButtonText);
94 } else {
95 rect.translate(shiftX, shiftY);
96 if (!hasArrow)
97 proxy()->drawItemPixmap(p, rect, Qt::AlignTop | Qt::AlignHCenter, pm);
98 }
99 }
100 }
101 return;
102 }
103 return QProxyStyle::drawControl(element, opt, p, widget);
104}
105
106//redefine arrow alignment
107void TTToolButtonStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex* opt, QPainter* p, const QWidget* widget) const
108{
109 if(cc == CC_ToolButton)
110 {
111 if (const QStyleOptionToolButton *toolbutton
112 = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
113 QRect button, menuarea;
114 button = proxy()->subControlRect(cc, toolbutton, SC_ToolButton, widget);
115 menuarea = proxy()->subControlRect(cc, toolbutton, SC_ToolButtonMenu, widget);
116
117 State bflags = toolbutton->state & ~State_Sunken;
118
119 if (bflags & State_AutoRaise) {
120 if (!(bflags & State_MouseOver) || !(bflags & State_Enabled)) {
121 bflags &= ~State_Raised;
122 }
123 }
124 State mflags = bflags;
125 if (toolbutton->state & State_Sunken) {
126 if (toolbutton->activeSubControls & SC_ToolButton)
127 bflags |= State_Sunken;
128 mflags |= State_Sunken;
129 }
130
131 QStyleOption tool(0);
132 tool.palette = toolbutton->palette;
133 if (toolbutton->subControls & SC_ToolButton) {
134 if (bflags & (State_Sunken | State_On | State_Raised)) {
135 tool.rect = button;
136 tool.state = bflags;
137 proxy()->drawPrimitive(PE_PanelButtonTool, &tool, p, widget);
138 }
139 }
140
141 if (toolbutton->state & State_HasFocus) {
142 QStyleOptionFocusRect fr;
143 fr.QStyleOption::operator=(*toolbutton);
144 fr.rect.adjust(3, 3, -3, -3);
145 if (toolbutton->features & QStyleOptionToolButton::MenuButtonPopup)
146 fr.rect.adjust(0, 0, -proxy()->pixelMetric(QStyle::PM_MenuButtonIndicator,
147 toolbutton, widget), 0);
148 proxy()->drawPrimitive(PE_FrameFocusRect, &fr, p, widget);
149 }
150 QStyleOptionToolButton label = *toolbutton;
151 label.state = bflags;
152 int fw = proxy()->pixelMetric(PM_DefaultFrameWidth, opt, widget);
153 label.rect = button.adjusted(fw, fw, -fw, -fw);
154 proxy()->drawControl(CE_ToolButtonLabel, &label, p, widget);
155
156 if (toolbutton->subControls & SC_ToolButtonMenu) {
157 tool.rect = menuarea;
158 tool.state = mflags;
159 if (mflags & (State_Sunken | State_On | State_Raised))
160 proxy()->drawPrimitive(PE_IndicatorButtonDropDown, &tool, p, widget);
161 proxy()->drawPrimitive(PE_IndicatorArrowDown, &tool, p, widget);
162 } else if (toolbutton->features & QStyleOptionToolButton::HasMenu) {
163 int mbi = proxy()->pixelMetric(PM_MenuButtonIndicator, toolbutton, widget);
164 QRect ir = toolbutton->rect;
165 QStyleOptionToolButton newBtn = *toolbutton;
166 newBtn.rect = QRect(ir.center().x() + 1 - (mbi - 6)/2, ir.y() + ir.height() - mbi + 4, mbi - 6, mbi - 6);
167 proxy()->drawPrimitive(PE_IndicatorArrowDown, &newBtn, p, widget);
168 }
169 }
170 return;
171 }
172 return QProxyStyle::drawComplexControl(cc, opt, p, widget);
173}
void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const override
void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *widget) const override
Definition Builder.h:33