Changeset View
Changeset View
Standalone View
Standalone View
kstyle/widgets/toolbox.cpp
- This file was added.
1 | #include "breezestyle.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | namespace Breeze | ||||
4 | { | ||||
5 | //______________________________________________________________________________ | ||||
6 | void Helper::renderToolBoxFrame(QPainter *painter, const QRect &rect, int tabWidth, const QColor &outline) const | ||||
7 | { | ||||
8 | if (!outline.isValid()) | ||||
9 | return; | ||||
10 | | ||||
11 | // round radius | ||||
12 | const qreal radius(frameRadius()); | ||||
13 | const QSizeF cornerSize(2 * radius, 2 * radius); | ||||
14 | | ||||
15 | // if rect - tabwidth is even, need to increase tabWidth by 1 unit | ||||
16 | // for anti aliasing | ||||
17 | if (!((rect.width() - tabWidth) % 2)) | ||||
18 | ++tabWidth; | ||||
19 | | ||||
20 | // adjust rect for antialiasing | ||||
21 | QRectF baseRect(rect); | ||||
22 | baseRect.adjust(0.5, 0.5, -0.5, -0.5); | ||||
23 | | ||||
24 | // create path | ||||
25 | QPainterPath path; | ||||
26 | path.moveTo(0, baseRect.height() - 1); | ||||
27 | path.lineTo((baseRect.width() - tabWidth) / 2 - radius, baseRect.height() - 1); | ||||
28 | path.arcTo(QRectF(QPointF((baseRect.width() - tabWidth) / 2 - 2 * radius, baseRect.height() - 1 - 2 * radius), cornerSize), 270, 90); | ||||
29 | path.lineTo((baseRect.width() - tabWidth) / 2, radius); | ||||
30 | path.arcTo(QRectF(QPointF((baseRect.width() - tabWidth) / 2, 0), cornerSize), 180, -90); | ||||
31 | path.lineTo((baseRect.width() + tabWidth) / 2 - 1 - radius, 0); | ||||
32 | path.arcTo(QRectF(QPointF((baseRect.width() + tabWidth) / 2 - 1 - 2 * radius, 0), cornerSize), 90, -90); | ||||
33 | path.lineTo((baseRect.width() + tabWidth) / 2 - 1, baseRect.height() - 1 - radius); | ||||
34 | path.arcTo(QRectF(QPointF((baseRect.width() + tabWidth) / 2 - 1, baseRect.height() - 1 - 2 * radius), cornerSize), 180, 90); | ||||
35 | path.lineTo(baseRect.width() - 1, baseRect.height() - 1); | ||||
36 | | ||||
37 | // render | ||||
38 | painter->setRenderHints(QPainter::Antialiasing); | ||||
39 | painter->setBrush(Qt::NoBrush); | ||||
40 | painter->setPen(outline); | ||||
41 | painter->translate(baseRect.topLeft()); | ||||
42 | painter->drawPath(path); | ||||
43 | } | ||||
44 | | ||||
45 | //____________________________________________________________________ | ||||
46 | QRect Style::toolBoxTabContentsRect(const QStyleOption *option, const QWidget *widget) const | ||||
47 | { | ||||
48 | // cast option and check | ||||
49 | const auto toolBoxOption(qstyleoption_cast<const QStyleOptionToolBox *>(option)); | ||||
50 | if (!toolBoxOption) | ||||
51 | return option->rect; | ||||
52 | | ||||
53 | // copy rect | ||||
54 | const auto &rect(option->rect); | ||||
55 | | ||||
56 | int contentsWidth(0); | ||||
57 | if (!toolBoxOption->icon.isNull()) { | ||||
58 | const int iconSize(pixelMetric(QStyle::PM_SmallIconSize, option, widget)); | ||||
59 | contentsWidth += iconSize; | ||||
60 | | ||||
61 | if (!toolBoxOption->text.isEmpty()) | ||||
62 | contentsWidth += Metrics::ToolBox_TabItemSpacing; | ||||
63 | } | ||||
64 | | ||||
65 | if (!toolBoxOption->text.isEmpty()) { | ||||
66 | const int textWidth = toolBoxOption->fontMetrics.size(_mnemonics->textFlags(), toolBoxOption->text).width(); | ||||
67 | contentsWidth += textWidth; | ||||
68 | } | ||||
69 | | ||||
70 | contentsWidth += 2 * Metrics::ToolBox_TabMarginWidth; | ||||
71 | contentsWidth = qMin(contentsWidth, rect.width()); | ||||
72 | contentsWidth = qMax(contentsWidth, int(Metrics::ToolBox_TabMinWidth)); | ||||
73 | return centerRect(rect, contentsWidth, rect.height()); | ||||
74 | } | ||||
75 | | ||||
76 | //___________________________________________________________________________________ | ||||
77 | bool Style::drawToolBoxTabLabelControl(const QStyleOption *option, QPainter *painter, const QWidget *widget) const | ||||
78 | { | ||||
79 | // rendering is similar to drawPushButtonLabelControl | ||||
80 | // cast option and check | ||||
81 | const auto toolBoxOption(qstyleoption_cast<const QStyleOptionToolBox *>(option)); | ||||
82 | if (!toolBoxOption) | ||||
83 | return true; | ||||
84 | | ||||
85 | // copy palette | ||||
86 | const auto &palette(option->palette); | ||||
87 | | ||||
88 | const State &state(option->state); | ||||
89 | const bool enabled(state & State_Enabled); | ||||
90 | | ||||
91 | // text alignment | ||||
92 | const int textFlags(_mnemonics->textFlags() | Qt::AlignCenter); | ||||
93 | | ||||
94 | // contents rect | ||||
95 | const auto rect(subElementRect(SE_ToolBoxTabContents, option, widget)); | ||||
96 | | ||||
97 | // store icon size | ||||
98 | const int iconSize(pixelMetric(QStyle::PM_SmallIconSize, option, widget)); | ||||
99 | | ||||
100 | // find contents size and rect | ||||
101 | auto contentsRect(rect); | ||||
102 | QSize contentsSize; | ||||
103 | if (!toolBoxOption->text.isEmpty()) { | ||||
104 | contentsSize = option->fontMetrics.size(_mnemonics->textFlags(), toolBoxOption->text); | ||||
105 | if (!toolBoxOption->icon.isNull()) | ||||
106 | contentsSize.rwidth() += Metrics::ToolBox_TabItemSpacing; | ||||
107 | } | ||||
108 | | ||||
109 | // icon size | ||||
110 | if (!toolBoxOption->icon.isNull()) { | ||||
111 | contentsSize.setHeight(qMax(contentsSize.height(), iconSize)); | ||||
112 | contentsSize.rwidth() += iconSize; | ||||
113 | } | ||||
114 | | ||||
115 | // adjust contents rect | ||||
116 | contentsRect = centerRect(contentsRect, contentsSize); | ||||
117 | | ||||
118 | // render icon | ||||
119 | if (!toolBoxOption->icon.isNull()) { | ||||
120 | // icon rect | ||||
121 | QRect iconRect; | ||||
122 | if (toolBoxOption->text.isEmpty()) | ||||
123 | iconRect = centerRect(contentsRect, iconSize, iconSize); | ||||
124 | else { | ||||
125 | iconRect = contentsRect; | ||||
126 | iconRect.setWidth(iconSize); | ||||
127 | iconRect = centerRect(iconRect, iconSize, iconSize); | ||||
128 | contentsRect.setLeft(iconRect.right() + Metrics::ToolBox_TabItemSpacing + 1); | ||||
129 | } | ||||
130 | | ||||
131 | iconRect = visualRect(option, iconRect); | ||||
132 | const QIcon::Mode mode(enabled ? QIcon::Normal : QIcon::Disabled); | ||||
133 | const QPixmap pixmap(toolBoxOption->icon.pixmap(iconSize, mode)); | ||||
134 | drawItemPixmap(painter, iconRect, textFlags, pixmap); | ||||
135 | } | ||||
136 | | ||||
137 | // render text | ||||
138 | if (!toolBoxOption->text.isEmpty()) { | ||||
139 | contentsRect = visualRect(option, contentsRect); | ||||
140 | drawItemText(painter, contentsRect, textFlags, palette, enabled, toolBoxOption->text, QPalette::WindowText); | ||||
141 | } | ||||
142 | | ||||
143 | return true; | ||||
144 | } | ||||
145 | | ||||
146 | //___________________________________________________________________________________ | ||||
147 | bool Style::drawToolBoxTabShapeControl(const QStyleOption *option, QPainter *painter, const QWidget *widget) const | ||||
148 | { | ||||
149 | // cast option and check | ||||
150 | const auto toolBoxOption(qstyleoption_cast<const QStyleOptionToolBox *>(option)); | ||||
151 | if (!toolBoxOption) | ||||
152 | return true; | ||||
153 | | ||||
154 | // copy rect and palette | ||||
155 | const auto &rect(option->rect); | ||||
156 | const auto tabRect(toolBoxTabContentsRect(option, widget)); | ||||
157 | | ||||
158 | /* | ||||
159 | * important: option returns the wrong palette. | ||||
160 | * we use the widget palette instead, when set | ||||
161 | */ | ||||
162 | const auto &palette(widget ? widget->palette() : option->palette); | ||||
163 | | ||||
164 | // store flags | ||||
165 | const State &flags(option->state); | ||||
166 | const bool enabled(flags & State_Enabled); | ||||
167 | const bool selected(flags & State_Selected); | ||||
168 | const bool mouseOver(enabled && !selected && (flags & State_MouseOver)); | ||||
169 | | ||||
170 | // update animation state | ||||
171 | /* | ||||
172 | * the proper widget ( the toolbox tab ) is not passed as argument by Qt. | ||||
173 | * What is passed is the toolbox directly. To implement animations properly, | ||||
174 | *the painter->device() is used instead | ||||
175 | */ | ||||
176 | bool isAnimated(false); | ||||
177 | qreal opacity(AnimationData::OpacityInvalid); | ||||
178 | QPaintDevice *device = painter->device(); | ||||
179 | if (enabled && device) { | ||||
180 | _animations->toolBoxEngine().updateState(device, mouseOver); | ||||
181 | isAnimated = _animations->toolBoxEngine().isAnimated(device); | ||||
182 | opacity = _animations->toolBoxEngine().opacity(device); | ||||
183 | } | ||||
184 | | ||||
185 | // color | ||||
186 | QColor outline; | ||||
187 | if (selected) | ||||
188 | outline = _helper->focusColor(palette); | ||||
189 | else | ||||
190 | outline = _helper->frameOutlineColor(palette, mouseOver, false, opacity, isAnimated ? AnimationHover : AnimationNone); | ||||
191 | | ||||
192 | // render | ||||
193 | _helper->renderToolBoxFrame(painter, rect, tabRect.width(), outline); | ||||
194 | | ||||
195 | return true; | ||||
196 | } | ||||
197 | | ||||
198 | } |