Changeset View
Changeset View
Standalone View
Standalone View
kstyle/widgets/dial.cpp
- This file was added.
1 | #include "breezestyle.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | namespace Breeze | ||||
4 | { | ||||
5 | //______________________________________________________________________________ | ||||
6 | void Helper::renderDialGroove(QPainter *painter, const QRect &rect, const QColor &color, qreal first, qreal last) const | ||||
7 | { | ||||
8 | // setup painter | ||||
9 | painter->setRenderHint(QPainter::Antialiasing, true); | ||||
10 | | ||||
11 | const QRectF baseRect(rect); | ||||
12 | | ||||
13 | // content | ||||
14 | if (color.isValid()) { | ||||
15 | const qreal penWidth(Metrics::Slider_GrooveThickness); | ||||
16 | const QRectF grooveRect(rect.adjusted(penWidth / 2, penWidth / 2, -penWidth / 2, -penWidth / 2)); | ||||
17 | | ||||
18 | // setup angles | ||||
19 | const int angleStart(first * 180 * 16 / M_PI); | ||||
20 | const int angleSpan((last - first) * 180 * 16 / M_PI); | ||||
21 | | ||||
22 | // setup pen | ||||
23 | if (angleSpan != 0) { | ||||
24 | QPen pen(color, penWidth); | ||||
25 | pen.setCapStyle(Qt::RoundCap); | ||||
26 | painter->setPen(pen); | ||||
27 | painter->setBrush(Qt::NoBrush); | ||||
28 | painter->drawArc(grooveRect, angleStart, angleSpan); | ||||
29 | } | ||||
30 | } | ||||
31 | } | ||||
32 | | ||||
33 | //______________________________________________________________________________ | ||||
34 | void Helper::renderDialContents(QPainter *painter, const QRect &rect, const QColor &color, qreal first, qreal second) const | ||||
35 | { | ||||
36 | // setup painter | ||||
37 | painter->setRenderHint(QPainter::Antialiasing, true); | ||||
38 | | ||||
39 | const QRectF baseRect(rect); | ||||
40 | | ||||
41 | // content | ||||
42 | if (color.isValid()) { | ||||
43 | // setup groove rect | ||||
44 | const qreal penWidth(Metrics::Slider_GrooveThickness); | ||||
45 | const QRectF grooveRect(rect.adjusted(penWidth / 2, penWidth / 2, -penWidth / 2, -penWidth / 2)); | ||||
46 | | ||||
47 | // setup angles | ||||
48 | const int angleStart(first * 180 * 16 / M_PI); | ||||
49 | const int angleSpan((second - first) * 180 * 16 / M_PI); | ||||
50 | | ||||
51 | // setup pen | ||||
52 | if (angleSpan != 0) { | ||||
53 | QPen pen(color, penWidth); | ||||
54 | pen.setCapStyle(Qt::RoundCap); | ||||
55 | painter->setPen(pen); | ||||
56 | painter->setBrush(Qt::NoBrush); | ||||
57 | painter->drawArc(grooveRect, angleStart, angleSpan); | ||||
58 | } | ||||
59 | } | ||||
60 | } | ||||
61 | | ||||
62 | //___________________________________________________________________________________________________________________ | ||||
63 | QRect Style::dialSubControlRect(const QStyleOptionComplex *option, SubControl subControl, const QWidget *widget) const | ||||
64 | { | ||||
65 | // cast option and check | ||||
66 | const auto sliderOption(qstyleoption_cast<const QStyleOptionSlider *>(option)); | ||||
67 | if (!sliderOption) | ||||
68 | return ParentStyleClass::subControlRect(CC_Dial, option, subControl, widget); | ||||
69 | | ||||
70 | // adjust rect to be square, and centered | ||||
71 | auto rect(option->rect); | ||||
72 | const int dimension(qMin(rect.width(), rect.height())); | ||||
73 | rect = centerRect(rect, dimension, dimension); | ||||
74 | | ||||
75 | switch (subControl) { | ||||
76 | case QStyle::SC_DialGroove: | ||||
77 | return insideMargin(rect, (Metrics::Slider_ControlThickness - Metrics::Slider_GrooveThickness) / 2); | ||||
78 | case QStyle::SC_DialHandle: { | ||||
79 | // calculate angle at which handle needs to be drawn | ||||
80 | const qreal angle(dialAngle(sliderOption, sliderOption->sliderPosition)); | ||||
81 | | ||||
82 | // groove rect | ||||
83 | const QRectF grooveRect(insideMargin(rect, Metrics::Slider_ControlThickness / 2)); | ||||
84 | qreal radius(grooveRect.width() / 2); | ||||
85 | | ||||
86 | // slider center | ||||
87 | QPointF center(grooveRect.center() + QPointF(radius * std::cos(angle), -radius * std::sin(angle))); | ||||
88 | | ||||
89 | // slider rect | ||||
90 | QRect handleRect(0, 0, Metrics::Slider_ControlThickness, Metrics::Slider_ControlThickness); | ||||
91 | handleRect.moveCenter(center.toPoint()); | ||||
92 | return handleRect; | ||||
93 | } | ||||
94 | | ||||
95 | default: | ||||
96 | return ParentStyleClass::subControlRect(CC_Dial, option, subControl, widget); | ||||
97 | ; | ||||
98 | } | ||||
99 | } | ||||
100 | | ||||
101 | //______________________________________________________________ | ||||
102 | bool Style::drawDialComplexControl(const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const | ||||
103 | { | ||||
104 | // cast option and check | ||||
105 | const auto sliderOption(qstyleoption_cast<const QStyleOptionSlider *>(option)); | ||||
106 | if (!sliderOption) | ||||
107 | return true; | ||||
108 | | ||||
109 | const auto &palette(option->palette); | ||||
110 | const State &state(option->state); | ||||
111 | const bool enabled(state & State_Enabled); | ||||
112 | const bool mouseOver(enabled && (state & State_MouseOver)); | ||||
113 | const bool hasFocus(enabled && (state & State_HasFocus)); | ||||
114 | | ||||
115 | // do not render tickmarks | ||||
116 | if (sliderOption->subControls & SC_DialTickmarks) { | ||||
117 | } | ||||
118 | | ||||
119 | // groove | ||||
120 | if (sliderOption->subControls & SC_DialGroove) { | ||||
121 | // groove rect | ||||
122 | auto grooveRect(subControlRect(CC_Dial, sliderOption, SC_SliderGroove, widget)); | ||||
123 | | ||||
124 | // groove | ||||
125 | const auto grooveColor(KColorUtils::mix(palette.color(QPalette::Window), palette.color(QPalette::WindowText), 0.3)); | ||||
126 | | ||||
127 | // angles | ||||
128 | const qreal first(dialAngle(sliderOption, sliderOption->minimum)); | ||||
129 | const qreal last(dialAngle(sliderOption, sliderOption->maximum)); | ||||
130 | | ||||
131 | // render groove | ||||
132 | _helper->renderDialGroove(painter, grooveRect, grooveColor, first, last); | ||||
133 | | ||||
134 | if (enabled) { | ||||
135 | // highlight | ||||
136 | const auto &highlight = palette.color(QPalette::Highlight); | ||||
137 | | ||||
138 | // angles | ||||
139 | const qreal second(dialAngle(sliderOption, sliderOption->sliderPosition)); | ||||
140 | | ||||
141 | // render contents | ||||
142 | _helper->renderDialContents(painter, grooveRect, highlight, first, second); | ||||
143 | } | ||||
144 | } | ||||
145 | | ||||
146 | // handle | ||||
147 | if (sliderOption->subControls & SC_DialHandle) { | ||||
148 | // get handle rect | ||||
149 | auto handleRect(subControlRect(CC_Dial, sliderOption, SC_DialHandle, widget)); | ||||
150 | handleRect = centerRect(handleRect, Metrics::Slider_ControlThickness, Metrics::Slider_ControlThickness); | ||||
151 | | ||||
152 | // handle state | ||||
153 | const bool handleActive(mouseOver && handleRect.contains(_animations->dialEngine().position(widget))); | ||||
154 | const bool sunken(state & (State_On | State_Sunken)); | ||||
155 | | ||||
156 | // animation state | ||||
157 | _animations->dialEngine().setHandleRect(widget, handleRect); | ||||
158 | _animations->dialEngine().updateState(widget, AnimationHover, handleActive && mouseOver); | ||||
159 | _animations->dialEngine().updateState(widget, AnimationFocus, hasFocus); | ||||
160 | const auto mode(_animations->dialEngine().buttonAnimationMode(widget)); | ||||
161 | const qreal opacity(_animations->dialEngine().buttonOpacity(widget)); | ||||
162 | | ||||
163 | // define colors | ||||
164 | const auto &background = palette.color(QPalette::Button); | ||||
165 | const auto outline(_helper->sliderOutlineColor(palette, handleActive && mouseOver, hasFocus, opacity, mode)); | ||||
166 | const auto shadow(_helper->shadowColor(palette)); | ||||
167 | | ||||
168 | // render | ||||
169 | _helper->renderSliderHandle(painter, handleRect, background, outline, shadow, sunken); | ||||
170 | } | ||||
171 | | ||||
172 | return true; | ||||
173 | } | ||||
174 | | ||||
175 | //______________________________________________________________________________ | ||||
176 | qreal Style::dialAngle(const QStyleOptionSlider *sliderOption, int value) const | ||||
177 | { | ||||
178 | // calculate angle at which handle needs to be drawn | ||||
179 | qreal angle(0); | ||||
180 | if (sliderOption->maximum == sliderOption->minimum) | ||||
181 | angle = M_PI / 2; | ||||
182 | else { | ||||
183 | qreal fraction(qreal(value - sliderOption->minimum) / qreal(sliderOption->maximum - sliderOption->minimum)); | ||||
184 | if (!sliderOption->upsideDown) | ||||
185 | fraction = 1 - fraction; | ||||
186 | | ||||
187 | if (sliderOption->dialWrapping) | ||||
188 | angle = 1.5 * M_PI - fraction * 2 * M_PI; | ||||
189 | else | ||||
190 | angle = (M_PI * 8 - fraction * 10 * M_PI) / 6; | ||||
191 | } | ||||
192 | | ||||
193 | return angle; | ||||
194 | } | ||||
195 | | ||||
196 | } |