Changeset View
Changeset View
Standalone View
Standalone View
kstyle/widgets/toolbar.cpp
- This file was added.
1 | #include "breezestyle.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | #include <QComboBox> | ||||
4 | | ||||
5 | namespace Breeze | ||||
6 | { | ||||
7 | //___________________________________________________________________________________ | ||||
8 | bool Style::drawIndicatorToolBarHandlePrimitive(const QStyleOption *option, QPainter *painter, const QWidget *) const | ||||
9 | { | ||||
10 | // do nothing if disabled from options | ||||
11 | if (!StyleConfigData::toolBarDrawItemSeparator()) | ||||
12 | return true; | ||||
13 | | ||||
14 | // store rect and palette | ||||
15 | auto rect(option->rect); | ||||
16 | const auto &palette(option->palette); | ||||
17 | | ||||
18 | // store state | ||||
19 | const State &state(option->state); | ||||
20 | const bool separatorIsVertical(state & State_Horizontal); | ||||
21 | | ||||
22 | // define color and render | ||||
23 | const auto color(_helper->separatorColor(palette)); | ||||
24 | if (separatorIsVertical) { | ||||
25 | rect.setWidth(Metrics::ToolBar_HandleWidth); | ||||
26 | rect = centerRect(option->rect, rect.size()); | ||||
27 | rect.setWidth(3); | ||||
28 | _helper->renderSeparator(painter, rect, color, separatorIsVertical); | ||||
29 | | ||||
30 | rect.translate(2, 0); | ||||
31 | _helper->renderSeparator(painter, rect, color, separatorIsVertical); | ||||
32 | | ||||
33 | } else { | ||||
34 | rect.setHeight(Metrics::ToolBar_HandleWidth); | ||||
35 | rect = centerRect(option->rect, rect.size()); | ||||
36 | rect.setHeight(3); | ||||
37 | _helper->renderSeparator(painter, rect, color, separatorIsVertical); | ||||
38 | | ||||
39 | rect.translate(0, 2); | ||||
40 | _helper->renderSeparator(painter, rect, color, separatorIsVertical); | ||||
41 | } | ||||
42 | | ||||
43 | return true; | ||||
44 | } | ||||
45 | | ||||
46 | //___________________________________________________________________________________ | ||||
47 | bool Style::drawIndicatorToolBarSeparatorPrimitive(const QStyleOption *option, QPainter *painter, const QWidget *widget) const | ||||
48 | { | ||||
49 | /* | ||||
50 | * do nothing if disabled from options | ||||
51 | * also need to check if widget is a combobox, because of Qt hack using 'toolbar' separator primitive | ||||
52 | * for rendering separators in comboboxes | ||||
53 | */ | ||||
54 | if (!(StyleConfigData::toolBarDrawItemSeparator() || qobject_cast<const QComboBox *>(widget))) { | ||||
55 | return true; | ||||
56 | } | ||||
57 | | ||||
58 | // store rect and palette | ||||
59 | const auto &rect(option->rect); | ||||
60 | const auto &palette(option->palette); | ||||
61 | | ||||
62 | // store state | ||||
63 | const State &state(option->state); | ||||
64 | const bool separatorIsVertical(state & State_Horizontal); | ||||
65 | | ||||
66 | // define color and render | ||||
67 | const auto color(_helper->separatorColor(palette)); | ||||
68 | _helper->renderSeparator(painter, rect, color, separatorIsVertical); | ||||
69 | | ||||
70 | return true; | ||||
71 | } | ||||
72 | | ||||
73 | //____________________________________________________________________________________ | ||||
74 | QIcon Style::toolBarExtensionIcon(StandardPixmap standardPixmap, const QStyleOption *option, const QWidget *widget) const | ||||
75 | { | ||||
76 | // store palette | ||||
77 | // due to Qt, it is not always safe to assume that either option, nor widget are defined | ||||
78 | QPalette palette; | ||||
79 | if (option) | ||||
80 | palette = option->palette; | ||||
81 | else if (widget) | ||||
82 | palette = widget->palette(); | ||||
83 | else | ||||
84 | palette = QApplication::palette(); | ||||
85 | | ||||
86 | // convenience class to map color to icon mode | ||||
87 | struct IconData { | ||||
88 | QColor _color; | ||||
89 | QIcon::Mode _mode; | ||||
90 | QIcon::State _state; | ||||
91 | }; | ||||
92 | | ||||
93 | // map colors to icon states | ||||
94 | const QList<IconData> iconTypes = {{palette.color(QPalette::Active, QPalette::WindowText), QIcon::Normal, QIcon::Off}, | ||||
95 | {palette.color(QPalette::Active, QPalette::WindowText), QIcon::Selected, QIcon::Off}, | ||||
96 | {palette.color(QPalette::Active, QPalette::WindowText), QIcon::Active, QIcon::Off}, | ||||
97 | {palette.color(QPalette::Disabled, QPalette::WindowText), QIcon::Disabled, QIcon::Off}, | ||||
98 | | ||||
99 | {palette.color(QPalette::Active, QPalette::HighlightedText), QIcon::Normal, QIcon::On}, | ||||
100 | {palette.color(QPalette::Active, QPalette::HighlightedText), QIcon::Selected, QIcon::On}, | ||||
101 | {palette.color(QPalette::Active, QPalette::WindowText), QIcon::Active, QIcon::On}, | ||||
102 | {palette.color(QPalette::Disabled, QPalette::WindowText), QIcon::Disabled, QIcon::On}}; | ||||
103 | | ||||
104 | // default icon sizes | ||||
105 | static const QList<int> iconSizes = {8, 16, 22, 32, 48}; | ||||
106 | | ||||
107 | // decide arrow orientation | ||||
108 | const ArrowOrientation orientation(standardPixmap == SP_ToolBarHorizontalExtensionButton ? ArrowRight : ArrowDown); | ||||
109 | | ||||
110 | // create icon and fill | ||||
111 | QIcon icon; | ||||
112 | foreach (const IconData &iconData, iconTypes) { | ||||
113 | foreach (const int &iconSize, iconSizes) { | ||||
114 | // create pixmap | ||||
115 | QPixmap pixmap(iconSize, iconSize); | ||||
116 | pixmap.fill(Qt::transparent); | ||||
117 | | ||||
118 | // render | ||||
119 | QPainter painter(&pixmap); | ||||
120 | | ||||
121 | // icon size | ||||
122 | const int fixedIconSize(pixelMetric(QStyle::PM_SmallIconSize, option, widget)); | ||||
123 | const QRect fixedRect(0, 0, fixedIconSize, fixedIconSize); | ||||
124 | | ||||
125 | painter.setWindow(fixedRect); | ||||
126 | painter.translate(standardPixmap == SP_ToolBarHorizontalExtensionButton ? QPoint(1, 0) : QPoint(0, 1)); | ||||
127 | _helper->renderArrow(&painter, fixedRect, iconData._color, orientation); | ||||
128 | painter.end(); | ||||
129 | | ||||
130 | // add to icon | ||||
131 | icon.addPixmap(pixmap, iconData._mode, iconData._state); | ||||
132 | } | ||||
133 | } | ||||
134 | | ||||
135 | return icon; | ||||
136 | } | ||||
137 | | ||||
138 | } |