Changeset View
Changeset View
Standalone View
Standalone View
kstyle/widgets/mdi.cpp
- This file was added.
1 | #include "breezestyle.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | namespace Breeze | ||||
4 | { | ||||
5 | //____________________________________________________________________________ | ||||
6 | bool Style::eventFilterMdiSubWindow(QMdiSubWindow *subWindow, QEvent *event) | ||||
7 | { | ||||
8 | if (event->type() == QEvent::Paint) { | ||||
9 | QPainter painter(subWindow); | ||||
10 | QPaintEvent *paintEvent(static_cast<QPaintEvent *>(event)); | ||||
11 | painter.setClipRegion(paintEvent->region()); | ||||
12 | | ||||
13 | const auto rect(subWindow->rect()); | ||||
14 | const auto background(subWindow->palette().color(QPalette::Window)); | ||||
15 | | ||||
16 | if (subWindow->isMaximized()) { | ||||
17 | // full painting | ||||
18 | painter.setPen(Qt::NoPen); | ||||
19 | painter.setBrush(background); | ||||
20 | painter.drawRect(rect); | ||||
21 | | ||||
22 | } else { | ||||
23 | // framed painting | ||||
24 | _helper->renderMenuFrame(&painter, rect, background, QColor()); | ||||
25 | } | ||||
26 | } | ||||
27 | | ||||
28 | // continue with normal painting | ||||
29 | return false; | ||||
30 | } | ||||
31 | | ||||
32 | //______________________________________________________________ | ||||
33 | bool Style::drawTitleBarComplexControl(const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const | ||||
34 | { | ||||
35 | // cast option and check | ||||
36 | const auto titleBarOption(qstyleoption_cast<const QStyleOptionTitleBar *>(option)); | ||||
37 | if (!titleBarOption) | ||||
38 | return true; | ||||
39 | | ||||
40 | // store palette and rect | ||||
41 | auto palette(option->palette); | ||||
42 | const auto &rect(option->rect); | ||||
43 | | ||||
44 | const State &flags(option->state); | ||||
45 | const bool enabled(flags & State_Enabled); | ||||
46 | const bool active(enabled && (titleBarOption->titleBarState & Qt::WindowActive)); | ||||
47 | | ||||
48 | if (titleBarOption->subControls & SC_TitleBarLabel) { | ||||
49 | // render background | ||||
50 | painter->setClipRect(rect); | ||||
51 | const auto outline(active ? QColor() : _helper->frameOutlineColor(palette, false, false)); | ||||
52 | const auto background(_helper->titleBarColor(active)); | ||||
53 | _helper->renderTabWidgetFrame(painter, rect.adjusted(-1, -1, 1, 3), background, outline, CornersTop); | ||||
54 | | ||||
55 | const bool useSeparator(active && _helper->titleBarColor(active) != palette.color(QPalette::Window) && !(titleBarOption->titleBarState & Qt::WindowMinimized)); | ||||
56 | | ||||
57 | if (useSeparator) { | ||||
58 | painter->setRenderHint(QPainter::Antialiasing, false); | ||||
59 | painter->setBrush(Qt::NoBrush); | ||||
60 | painter->setPen(palette.color(QPalette::Highlight)); | ||||
61 | painter->drawLine(rect.bottomLeft(), rect.bottomRight()); | ||||
62 | } | ||||
63 | | ||||
64 | // render text | ||||
65 | palette.setColor(QPalette::WindowText, _helper->titleBarTextColor(active)); | ||||
66 | const auto textRect(subControlRect(CC_TitleBar, option, SC_TitleBarLabel, widget)); | ||||
67 | ParentStyleClass::drawItemText(painter, textRect, Qt::AlignCenter, palette, active, titleBarOption->text, QPalette::WindowText); | ||||
68 | } | ||||
69 | | ||||
70 | // buttons | ||||
71 | static const QList<SubControl> subControls = {SC_TitleBarMinButton, SC_TitleBarMaxButton, SC_TitleBarCloseButton, SC_TitleBarNormalButton, SC_TitleBarSysMenu}; | ||||
72 | | ||||
73 | // loop over supported buttons | ||||
74 | foreach (const SubControl &subControl, subControls) { | ||||
75 | // skip if not requested | ||||
76 | if (!(titleBarOption->subControls & subControl)) | ||||
77 | continue; | ||||
78 | | ||||
79 | // find matching icon | ||||
80 | QIcon icon; | ||||
81 | switch (subControl) { | ||||
82 | case SC_TitleBarMinButton: | ||||
83 | icon = standardIcon(SP_TitleBarMinButton, option, widget); | ||||
84 | break; | ||||
85 | case SC_TitleBarMaxButton: | ||||
86 | icon = standardIcon(SP_TitleBarMaxButton, option, widget); | ||||
87 | break; | ||||
88 | case SC_TitleBarCloseButton: | ||||
89 | icon = standardIcon(SP_TitleBarCloseButton, option, widget); | ||||
90 | break; | ||||
91 | case SC_TitleBarNormalButton: | ||||
92 | icon = standardIcon(SP_TitleBarNormalButton, option, widget); | ||||
93 | break; | ||||
94 | case SC_TitleBarSysMenu: | ||||
95 | icon = titleBarOption->icon; | ||||
96 | break; | ||||
97 | default: | ||||
98 | break; | ||||
99 | } | ||||
100 | | ||||
101 | // check icon | ||||
102 | if (icon.isNull()) | ||||
103 | continue; | ||||
104 | | ||||
105 | // define icon rect | ||||
106 | auto iconRect(subControlRect(CC_TitleBar, option, subControl, widget)); | ||||
107 | if (iconRect.isEmpty()) | ||||
108 | continue; | ||||
109 | | ||||
110 | // active state | ||||
111 | const bool subControlActive(titleBarOption->activeSubControls & subControl); | ||||
112 | | ||||
113 | // mouse over state | ||||
114 | const bool mouseOver(!subControlActive && widget && iconRect.translated(widget->mapToGlobal(QPoint(0, 0))).contains(QCursor::pos())); | ||||
115 | | ||||
116 | // adjust iconRect | ||||
117 | const int iconWidth(pixelMetric(PM_SmallIconSize, option, widget)); | ||||
118 | const QSize iconSize(iconWidth, iconWidth); | ||||
119 | iconRect = centerRect(iconRect, iconSize); | ||||
120 | | ||||
121 | // set icon mode and state | ||||
122 | QIcon::Mode iconMode; | ||||
123 | QIcon::State iconState; | ||||
124 | | ||||
125 | if (!enabled) { | ||||
126 | iconMode = QIcon::Disabled; | ||||
127 | iconState = QIcon::Off; | ||||
128 | | ||||
129 | } else { | ||||
130 | if (mouseOver) | ||||
131 | iconMode = QIcon::Active; | ||||
132 | else if (active) | ||||
133 | iconMode = QIcon::Selected; | ||||
134 | else | ||||
135 | iconMode = QIcon::Normal; | ||||
136 | | ||||
137 | iconState = subControlActive ? QIcon::On : QIcon::Off; | ||||
138 | } | ||||
139 | | ||||
140 | // get pixmap and render | ||||
141 | const QPixmap pixmap = icon.pixmap(iconSize, iconMode, iconState); | ||||
142 | painter->drawPixmap(iconRect, pixmap); | ||||
143 | } | ||||
144 | | ||||
145 | return true; | ||||
146 | } | ||||
147 | | ||||
148 | //____________________________________________________________________________________ | ||||
149 | QIcon Style::titleBarButtonIcon(StandardPixmap standardPixmap, const QStyleOption *option, const QWidget *widget) const | ||||
150 | { | ||||
151 | // map standardPixmap to button type | ||||
152 | ButtonType buttonType; | ||||
153 | switch (standardPixmap) { | ||||
154 | case SP_TitleBarNormalButton: | ||||
155 | buttonType = ButtonRestore; | ||||
156 | break; | ||||
157 | case SP_TitleBarMinButton: | ||||
158 | buttonType = ButtonMinimize; | ||||
159 | break; | ||||
160 | case SP_TitleBarMaxButton: | ||||
161 | buttonType = ButtonMaximize; | ||||
162 | break; | ||||
163 | case SP_TitleBarCloseButton: | ||||
164 | case SP_DockWidgetCloseButton: | ||||
165 | buttonType = ButtonClose; | ||||
166 | break; | ||||
167 | | ||||
168 | default: | ||||
169 | return QIcon(); | ||||
170 | } | ||||
171 | | ||||
172 | // store palette | ||||
173 | // due to Qt, it is not always safe to assume that either option, nor widget are defined | ||||
174 | QPalette palette; | ||||
175 | if (option) | ||||
176 | palette = option->palette; | ||||
177 | else if (widget) | ||||
178 | palette = widget->palette(); | ||||
179 | else | ||||
180 | palette = QApplication::palette(); | ||||
181 | | ||||
182 | const bool isCloseButton(buttonType == ButtonClose && StyleConfigData::outlineCloseButton()); | ||||
183 | | ||||
184 | palette.setCurrentColorGroup(QPalette::Active); | ||||
185 | const auto base(palette.color(QPalette::WindowText)); | ||||
186 | const auto selected(palette.color(QPalette::HighlightedText)); | ||||
187 | const auto negative(buttonType == ButtonClose ? _helper->negativeText(palette) : base); | ||||
188 | const auto negativeSelected(buttonType == ButtonClose ? _helper->negativeText(palette) : selected); | ||||
189 | | ||||
190 | const bool invertNormalState(isCloseButton); | ||||
191 | | ||||
192 | // convenience class to map color to icon mode | ||||
193 | struct IconData { | ||||
194 | QColor _color; | ||||
195 | bool _inverted; | ||||
196 | QIcon::Mode _mode; | ||||
197 | QIcon::State _state; | ||||
198 | }; | ||||
199 | | ||||
200 | // map colors to icon states | ||||
201 | const QList<IconData> iconTypes = { | ||||
202 | | ||||
203 | // state off icons | ||||
204 | {KColorUtils::mix(palette.color(QPalette::Window), base, 0.5), invertNormalState, QIcon::Normal, QIcon::Off}, | ||||
205 | {KColorUtils::mix(palette.color(QPalette::Window), selected, 0.5), invertNormalState, QIcon::Selected, QIcon::Off}, | ||||
206 | {KColorUtils::mix(palette.color(QPalette::Window), negative, 0.5), true, QIcon::Active, QIcon::Off}, | ||||
207 | {KColorUtils::mix(palette.color(QPalette::Window), base, 0.2), invertNormalState, QIcon::Disabled, QIcon::Off}, | ||||
208 | | ||||
209 | // state on icons | ||||
210 | {KColorUtils::mix(palette.color(QPalette::Window), negative, 0.7), true, QIcon::Normal, QIcon::On}, | ||||
211 | {KColorUtils::mix(palette.color(QPalette::Window), negativeSelected, 0.7), true, QIcon::Selected, QIcon::On}, | ||||
212 | {KColorUtils::mix(palette.color(QPalette::Window), negative, 0.7), true, QIcon::Active, QIcon::On}, | ||||
213 | {KColorUtils::mix(palette.color(QPalette::Window), base, 0.2), invertNormalState, QIcon::Disabled, QIcon::On} | ||||
214 | | ||||
215 | }; | ||||
216 | | ||||
217 | // default icon sizes | ||||
218 | static const QList<int> iconSizes = {8, 16, 22, 32, 48}; | ||||
219 | | ||||
220 | // output icon | ||||
221 | QIcon icon; | ||||
222 | | ||||
223 | foreach (const IconData &iconData, iconTypes) { | ||||
224 | foreach (const int &iconSize, iconSizes) { | ||||
225 | // create pixmap | ||||
226 | QPixmap pixmap(iconSize, iconSize); | ||||
227 | pixmap.fill(Qt::transparent); | ||||
228 | | ||||
229 | // create painter and render | ||||
230 | QPainter painter(&pixmap); | ||||
231 | _helper->renderDecorationButton(&painter, pixmap.rect(), iconData._color, buttonType, iconData._inverted); | ||||
232 | | ||||
233 | painter.end(); | ||||
234 | | ||||
235 | // store | ||||
236 | icon.addPixmap(pixmap, iconData._mode, iconData._state); | ||||
237 | } | ||||
238 | } | ||||
239 | | ||||
240 | return icon; | ||||
241 | } | ||||
242 | | ||||
243 | } |