Changeset View
Changeset View
Standalone View
Standalone View
kstyle/widgets/spinbox.cpp
- This file was added.
1 | #include "breezestyle.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | namespace Breeze | ||||
4 | { | ||||
5 | //___________________________________________________________________________________________________________________ | ||||
6 | QRect Style::spinBoxSubControlRect(const QStyleOptionComplex *option, SubControl subControl, const QWidget *widget) const | ||||
7 | { | ||||
8 | // cast option and check | ||||
9 | const auto spinBoxOption(qstyleoption_cast<const QStyleOptionSpinBox *>(option)); | ||||
10 | if (!spinBoxOption) | ||||
11 | return ParentStyleClass::subControlRect(CC_SpinBox, option, subControl, widget); | ||||
12 | const bool flat(!spinBoxOption->frame); | ||||
13 | | ||||
14 | // copy rect | ||||
15 | auto rect(option->rect); | ||||
16 | | ||||
17 | switch (subControl) { | ||||
18 | case SC_SpinBoxFrame: | ||||
19 | return flat ? QRect() : rect; | ||||
20 | | ||||
21 | case SC_SpinBoxUp: | ||||
22 | case SC_SpinBoxDown: { | ||||
23 | // take out frame width | ||||
24 | if (!flat && rect.height() >= 2 * Metrics::Frame_FrameWidth + Metrics::SpinBox_ArrowButtonWidth) | ||||
25 | rect = insideMargin(rect, Metrics::Frame_FrameWidth); | ||||
26 | | ||||
27 | QRect arrowRect; | ||||
28 | arrowRect = QRect(rect.right() - Metrics::SpinBox_ArrowButtonWidth + 1, rect.top(), Metrics::SpinBox_ArrowButtonWidth, rect.height()); | ||||
29 | | ||||
30 | const int arrowHeight(qMin(rect.height(), int(Metrics::SpinBox_ArrowButtonWidth))); | ||||
31 | arrowRect = centerRect(arrowRect, Metrics::SpinBox_ArrowButtonWidth, arrowHeight); | ||||
32 | arrowRect.setHeight(arrowHeight / 2); | ||||
33 | if (subControl == SC_SpinBoxDown) | ||||
34 | arrowRect.translate(0, arrowHeight / 2); | ||||
35 | | ||||
36 | return visualRect(option, arrowRect); | ||||
37 | } | ||||
38 | | ||||
39 | case SC_SpinBoxEditField: { | ||||
40 | QRect labelRect; | ||||
41 | labelRect = QRect(rect.left(), rect.top(), rect.width() - Metrics::SpinBox_ArrowButtonWidth, rect.height()); | ||||
42 | | ||||
43 | // remove right side line editor margins | ||||
44 | const int frameWidth(pixelMetric(PM_SpinBoxFrameWidth, option, widget)); | ||||
45 | if (!flat && labelRect.height() >= option->fontMetrics.height() + 2 * frameWidth) { | ||||
46 | labelRect.adjust(frameWidth, frameWidth, 0, -frameWidth); | ||||
47 | } | ||||
48 | | ||||
49 | return visualRect(option, labelRect); | ||||
50 | } | ||||
51 | | ||||
52 | default: | ||||
53 | break; | ||||
54 | } | ||||
55 | | ||||
56 | return ParentStyleClass::subControlRect(CC_SpinBox, option, subControl, widget); | ||||
57 | } | ||||
58 | | ||||
59 | //______________________________________________________________ | ||||
60 | QSize Style::spinBoxSizeFromContents(const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const | ||||
61 | { | ||||
62 | // cast option and check | ||||
63 | const auto spinBoxOption(qstyleoption_cast<const QStyleOptionSpinBox *>(option)); | ||||
64 | if (!spinBoxOption) | ||||
65 | return contentsSize; | ||||
66 | | ||||
67 | const bool flat(!spinBoxOption->frame); | ||||
68 | | ||||
69 | // copy size | ||||
70 | QSize size(contentsSize); | ||||
71 | | ||||
72 | // add editor margins | ||||
73 | const int frameWidth(pixelMetric(PM_SpinBoxFrameWidth, option, widget)); | ||||
74 | if (!flat) | ||||
75 | size = expandSize(size, frameWidth); | ||||
76 | | ||||
77 | // make sure there is enough height for the button | ||||
78 | size.setHeight(qMax(size.height(), int(Metrics::SpinBox_ArrowButtonWidth))); | ||||
79 | | ||||
80 | // add button width and spacing | ||||
81 | size.rwidth() += Metrics::SpinBox_ArrowButtonWidth; | ||||
82 | | ||||
83 | return size; | ||||
84 | } | ||||
85 | | ||||
86 | //______________________________________________________________ | ||||
87 | bool Style::drawSpinBoxComplexControl(const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const | ||||
88 | { | ||||
89 | const auto spinBoxOption(qstyleoption_cast<const QStyleOptionSpinBox *>(option)); | ||||
90 | if (!spinBoxOption) | ||||
91 | return true; | ||||
92 | | ||||
93 | // store palette and rect | ||||
94 | const auto &palette(option->palette); | ||||
95 | const auto &rect(option->rect); | ||||
96 | | ||||
97 | if (option->subControls & SC_SpinBoxFrame) { | ||||
98 | // detect flat spinboxes | ||||
99 | bool flat(!spinBoxOption->frame); | ||||
100 | flat |= (rect.height() < 2 * Metrics::Frame_FrameWidth + Metrics::SpinBox_ArrowButtonWidth); | ||||
101 | if (flat) { | ||||
102 | const auto &background = palette.color(QPalette::Base); | ||||
103 | | ||||
104 | painter->setBrush(background); | ||||
105 | painter->setPen(Qt::NoPen); | ||||
106 | painter->drawRect(rect); | ||||
107 | | ||||
108 | } else { | ||||
109 | drawPrimitive(PE_FrameLineEdit, option, painter, widget); | ||||
110 | } | ||||
111 | } | ||||
112 | | ||||
113 | if (option->subControls & SC_SpinBoxUp) | ||||
114 | renderSpinBoxArrow(SC_SpinBoxUp, spinBoxOption, painter, widget); | ||||
115 | if (option->subControls & SC_SpinBoxDown) | ||||
116 | renderSpinBoxArrow(SC_SpinBoxDown, spinBoxOption, painter, widget); | ||||
117 | | ||||
118 | return true; | ||||
119 | } | ||||
120 | | ||||
121 | //____________________________________________________________________________________________________ | ||||
122 | void Style::renderSpinBoxArrow(const SubControl &subControl, const QStyleOptionSpinBox *option, QPainter *painter, const QWidget *widget) const | ||||
123 | { | ||||
124 | const auto &palette(option->palette); | ||||
125 | const State &state(option->state); | ||||
126 | | ||||
127 | // enable state | ||||
128 | bool enabled(state & State_Enabled); | ||||
129 | | ||||
130 | // check steps enable step | ||||
131 | const bool atLimit((subControl == SC_SpinBoxUp && !(option->stepEnabled & QAbstractSpinBox::StepUpEnabled)) || (subControl == SC_SpinBoxDown && !(option->stepEnabled & QAbstractSpinBox::StepDownEnabled))); | ||||
132 | | ||||
133 | // update enabled state accordingly | ||||
134 | enabled &= !atLimit; | ||||
135 | | ||||
136 | // update mouse-over effect | ||||
137 | const bool mouseOver(enabled && (state & State_MouseOver)); | ||||
138 | | ||||
139 | // check animation state | ||||
140 | const bool subControlHover(enabled && mouseOver && (option->activeSubControls & subControl)); | ||||
141 | _animations->spinBoxEngine().updateState(widget, subControl, subControlHover); | ||||
142 | | ||||
143 | const bool animated(enabled && _animations->spinBoxEngine().isAnimated(widget, subControl)); | ||||
144 | const qreal opacity(_animations->spinBoxEngine().opacity(widget, subControl)); | ||||
145 | | ||||
146 | auto color = _helper->arrowColor(palette, QPalette::Text); | ||||
147 | if (animated) { | ||||
148 | auto highlight = _helper->hoverColor(palette); | ||||
149 | color = KColorUtils::mix(color, highlight, opacity); | ||||
150 | | ||||
151 | } else if (subControlHover) { | ||||
152 | color = _helper->hoverColor(palette); | ||||
153 | | ||||
154 | } else if (atLimit) { | ||||
155 | color = _helper->arrowColor(palette, QPalette::Disabled, QPalette::Text); | ||||
156 | } | ||||
157 | | ||||
158 | // arrow orientation | ||||
159 | ArrowOrientation orientation((subControl == SC_SpinBoxUp) ? ArrowUp : ArrowDown); | ||||
160 | | ||||
161 | // arrow rect | ||||
162 | const auto arrowRect(subControlRect(CC_SpinBox, option, subControl, widget)); | ||||
163 | | ||||
164 | // render | ||||
165 | _helper->renderArrow(painter, arrowRect, color, orientation); | ||||
166 | } | ||||
167 | | ||||
168 | } |