Changeset View
Changeset View
Standalone View
Standalone View
kcalc_button.cpp
Show All 22 Lines | |||||
23 | 23 | | |||
24 | #include <QAbstractTextDocumentLayout> | 24 | #include <QAbstractTextDocumentLayout> | ||
25 | #include <QApplication> | 25 | #include <QApplication> | ||
26 | #include <QStyleOptionButton> | 26 | #include <QStyleOptionButton> | ||
27 | #include <QStylePainter> | 27 | #include <QStylePainter> | ||
28 | #include <QTextDocument> | 28 | #include <QTextDocument> | ||
29 | 29 | | |||
30 | 30 | | |||
31 | | ||||
32 | //------------------------------------------------------------------------------ | 31 | //------------------------------------------------------------------------------ | ||
33 | // Name: KCalcButton | 32 | // Name: KCalcButton | ||
34 | // Desc: constructor | 33 | // Desc: constructor | ||
35 | //------------------------------------------------------------------------------ | 34 | //------------------------------------------------------------------------------ | ||
36 | KCalcButton::KCalcButton(QWidget *parent) : QPushButton(parent), show_shortcut_mode_(false), mode_flags_(ModeNormal), size_() { | 35 | KCalcButton::KCalcButton(QWidget *parent) : QPushButton(parent), show_shortcut_mode_(false), mode_flags_(ModeNormal), size_() { | ||
37 | 36 | | |||
38 | setAcceptDrops(true); // allow color drops | 37 | setAcceptDrops(true); // allow color drops | ||
39 | setFocusPolicy(Qt::TabFocus); | 38 | setFocusPolicy(Qt::TabFocus); | ||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Line(s) | 129 | } else { | |||
131 | setText(mode_[mode_flags_].label); | 130 | setText(mode_[mode_flags_].label); | ||
132 | } | 131 | } | ||
133 | 132 | | |||
134 | // restore shortcut | 133 | // restore shortcut | ||
135 | setShortcut(current_shortcut); | 134 | setShortcut(current_shortcut); | ||
136 | update(); | 135 | update(); | ||
137 | } | 136 | } | ||
138 | 137 | | |||
138 | QColor KCalcButton::textColor() const { | ||||
139 | QColor col = text_color_; | ||||
140 | if (!isEnabled()) { | ||||
141 | col.setAlphaF(0.7); | ||||
142 | } | ||||
143 | return col; | ||||
144 | } | ||||
145 | | ||||
139 | //------------------------------------------------------------------------------ | 146 | //------------------------------------------------------------------------------ | ||
140 | // Name: paintEvent | 147 | // Name: paintEvent | ||
141 | // Desc: draws the button | 148 | // Desc: draws the button | ||
142 | //------------------------------------------------------------------------------ | 149 | //------------------------------------------------------------------------------ | ||
143 | void KCalcButton::paintEvent(QPaintEvent *) { | 150 | void KCalcButton::paintEvent(QPaintEvent *) { | ||
144 | 151 | | |||
145 | QPainter p(this); | 152 | QPainter p(this); | ||
146 | QStyleOptionButton option; | 153 | QStyleOptionButton option; | ||
147 | initStyleOption(&option); | 154 | initStyleOption(&option); | ||
148 | const bool is_down = isDown() || isChecked(); | 155 | const bool is_down = isDown() || isChecked(); | ||
149 | const int x_offset = is_down ? style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal, &option, this) : 0; | 156 | const int x_offset = is_down ? style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal, &option, this) : 0; | ||
150 | const int y_offset = is_down ? style()->pixelMetric(QStyle::PM_ButtonShiftVertical, &option, this) : 0; | 157 | const int y_offset = is_down ? style()->pixelMetric(QStyle::PM_ButtonShiftVertical, &option, this) : 0; | ||
151 | 158 | | |||
152 | // draw bevel | 159 | // draw bevel | ||
153 | style()->drawControl(QStyle::CE_PushButtonBevel, &option, &p, this); | 160 | style()->drawControl(QStyle::CE_PushButtonBevel, &option, &p, this); | ||
154 | 161 | | |||
155 | // draw label... | 162 | // draw label... | ||
156 | p.save(); | 163 | p.save(); | ||
157 | 164 | | |||
158 | // rant: Qt4 needs QSimpleRichText, dammit! | 165 | // rant: Qt4 needs QSimpleRichText, dammit! | ||
159 | QTextDocument doc; | 166 | QTextDocument doc; | ||
160 | QAbstractTextDocumentLayout::PaintContext context; | 167 | QAbstractTextDocumentLayout::PaintContext context; | ||
161 | doc.setHtml(QLatin1String("<center>") + text() + QLatin1String("</center>")); | 168 | doc.setHtml(QLatin1String("<center>") + text() + QLatin1String("</center>")); | ||
cfeck: This change can be reverted. The `palette.setColor()` call below is sufficient. | |||||
162 | doc.setDefaultFont(font()); | 169 | doc.setDefaultFont(font()); | ||
163 | context.palette = palette(); | 170 | context.palette = palette(); | ||
164 | context.palette.setColor(QPalette::Text, context.palette.buttonText().color()); | 171 | context.palette.setColor(QPalette::Text, textColor()); | ||
cfeck: textColor() returns a QColor, so the cast is not needed. | |||||
Better modify a local QColor variable before setting. This way you can remove the setEnabled() overload. cfeck: Better modify a local QColor variable before setting. This way you can remove the setEnabled()… | |||||
165 | 172 | | |||
166 | p.translate((width() / 2 - doc.size().width() / 2) + x_offset, (height() / 2 - doc.size().height() / 2) + y_offset); | 173 | p.translate((width() / 2 - doc.size().width() / 2) + x_offset, (height() / 2 - doc.size().height() / 2) + y_offset); | ||
167 | doc.documentLayout()->draw(&p, context); | 174 | doc.documentLayout()->draw(&p, context); | ||
168 | p.restore(); | 175 | p.restore(); | ||
169 | 176 | | |||
170 | // draw focus | 177 | // draw focus | ||
171 | if (hasFocus()) { | 178 | if (hasFocus()) { | ||
172 | QStyleOptionFocusRect fropt; | 179 | QStyleOptionFocusRect fropt; | ||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Line(s) | |||||
215 | // Desc: | 222 | // Desc: | ||
216 | //------------------------------------------------------------------------------ | 223 | //------------------------------------------------------------------------------ | ||
217 | void KCalcButton::setFont(const QFont &fnt) { | 224 | void KCalcButton::setFont(const QFont &fnt) { | ||
218 | 225 | | |||
219 | QPushButton::setFont(fnt); | 226 | QPushButton::setFont(fnt); | ||
220 | calcSizeHint(); | 227 | calcSizeHint(); | ||
221 | } | 228 | } | ||
222 | 229 | | |||
230 | void KCalcButton::setTextColor(const QColor &color) { | ||||
231 | text_color_ = color; | ||||
232 | } | ||||
cfeck: Color does not affect the size. Can be removed. | |||||
233 | | ||||
223 | //------------------------------------------------------------------------------ | 234 | //------------------------------------------------------------------------------ | ||
224 | // Name: setText | 235 | // Name: setText | ||
225 | // Desc: | 236 | // Desc: | ||
226 | //------------------------------------------------------------------------------ | 237 | //------------------------------------------------------------------------------ | ||
227 | void KCalcButton::setText(const QString &text) { | 238 | void KCalcButton::setText(const QString &text) { | ||
228 | 239 | | |||
229 | QPushButton::setText(text); | 240 | QPushButton::setText(text); | ||
230 | 241 | | |||
231 | // normal mode may not have been explicitly set | 242 | // normal mode may not have been explicitly set | ||
232 | if (mode_[ModeNormal].label.isEmpty()) { | 243 | if (mode_[ModeNormal].label.isEmpty()) { | ||
233 | mode_[ModeNormal].label = text; | 244 | mode_[ModeNormal].label = text; | ||
234 | } | 245 | } | ||
235 | 246 | | |||
236 | calcSizeHint(); | 247 | calcSizeHint(); | ||
237 | } | 248 | } | ||
238 | 249 | | |||
250 | void KCalcButton::setEnabled(const bool& enabled) { | ||||
251 | QPushButton::setEnabled(enabled); | ||||
252 | | ||||
253 | if (!enabled) { | ||||
254 | text_color_.setAlphaF(0.6); | ||||
255 | } | ||||
256 | } | ||||
257 | | ||||
239 | //------------------------------------------------------------------------------ | 258 | //------------------------------------------------------------------------------ | ||
240 | // Name: setToolTip | 259 | // Name: setToolTip | ||
241 | // Desc: | 260 | // Desc: | ||
242 | //------------------------------------------------------------------------------ | 261 | //------------------------------------------------------------------------------ | ||
243 | void KCalcButton::setToolTip(const QString &tip) { | 262 | void KCalcButton::setToolTip(const QString &tip) { | ||
244 | 263 | | |||
245 | QPushButton::setToolTip(tip); | 264 | QPushButton::setToolTip(tip); | ||
246 | 265 | | |||
247 | // normal mode may not have been explicitly set | 266 | // normal mode may not have been explicitly set | ||
248 | if (mode_[ModeNormal].tooltip.isEmpty()) { | 267 | if (mode_[ModeNormal].tooltip.isEmpty()) { | ||
249 | mode_[ModeNormal].tooltip = tip; | 268 | mode_[ModeNormal].tooltip = tip; | ||
250 | } | 269 | } | ||
251 | } | 270 | } |
This change can be reverted. The palette.setColor() call below is sufficient.