Changeset View
Changeset View
Standalone View
Standalone View
src/kmessagewidget.cpp
Show First 20 Lines • Show All 48 Lines • ▼ Show 20 Line(s) | 40 | public: | |||
---|---|---|---|---|---|
49 | QIcon icon; | 49 | QIcon icon; | ||
50 | 50 | | |||
51 | KMessageWidget::MessageType messageType; | 51 | KMessageWidget::MessageType messageType; | ||
52 | bool wordWrap; | 52 | bool wordWrap; | ||
53 | QList<QToolButton *> buttons; | 53 | QList<QToolButton *> buttons; | ||
54 | QPixmap contentSnapShot; | 54 | QPixmap contentSnapShot; | ||
55 | 55 | | |||
56 | void createLayout(); | 56 | void createLayout(); | ||
57 | void applyStyleSheet(); | ||||
rjvbb: Nitpick: shouldn't this be called `setStyleSheet` or something similar that indicates a bit… | |||||
broulik: It doesn't take any arguments, so I explicitly did not call it `set` | |||||
57 | void updateSnapShot(); | 58 | void updateSnapShot(); | ||
58 | void updateLayout(); | 59 | void updateLayout(); | ||
59 | void slotTimeLineChanged(qreal); | 60 | void slotTimeLineChanged(qreal); | ||
60 | void slotTimeLineFinished(); | 61 | void slotTimeLineFinished(); | ||
61 | 62 | | |||
62 | int bestContentHeight() const; | 63 | int bestContentHeight() const; | ||
63 | }; | 64 | }; | ||
64 | 65 | | |||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Line(s) | 107 | { | |||
159 | }; | 160 | }; | ||
160 | 161 | | |||
161 | if (q->isVisible()) { | 162 | if (q->isVisible()) { | ||
162 | q->setFixedHeight(content->sizeHint().height()); | 163 | q->setFixedHeight(content->sizeHint().height()); | ||
163 | } | 164 | } | ||
164 | q->updateGeometry(); | 165 | q->updateGeometry(); | ||
165 | } | 166 | } | ||
166 | 167 | | |||
168 | void KMessageWidgetPrivate::applyStyleSheet() | ||||
169 | { | ||||
170 | QColor bgBaseColor; | ||||
171 | | ||||
172 | // We have to hardcode colors here because KWidgetsAddons is a tier 1 framework | ||||
173 | // and therefore can't depend on any other KDE Frameworks | ||||
174 | // The following RGB color values come from the "default" scheme in kcolorscheme.cpp | ||||
175 | switch (messageType) { | ||||
176 | case KMessageWidget::Positive: | ||||
177 | bgBaseColor.setRgb(39, 174, 96); // Window: ForegroundPositive | ||||
178 | break; | ||||
179 | case KMessageWidget::Information: | ||||
180 | bgBaseColor.setRgb(61, 174, 233); // Window: ForegroundActive | ||||
181 | break; | ||||
182 | case KMessageWidget::Warning: | ||||
183 | bgBaseColor.setRgb(246, 116, 0); // Window: ForegroundNeutral | ||||
184 | break; | ||||
185 | case KMessageWidget::Error: | ||||
186 | bgBaseColor.setRgb(218, 68, 83); // Window: ForegroundNegative | ||||
187 | break; | ||||
188 | } | ||||
189 | const qreal bgBaseColorAlpha = 0.2; | ||||
190 | bgBaseColor.setAlphaF(bgBaseColorAlpha); | ||||
191 | | ||||
192 | const QPalette palette = QGuiApplication::palette(); | ||||
193 | const QColor windowColor = palette.window().color(); | ||||
194 | const QColor textColor = palette.text().color(); | ||||
195 | const QColor border = bgBaseColor; | ||||
196 | | ||||
197 | // Generate a final background color from overlaying bgBaseColor over windowColor | ||||
198 | const int newRed = (bgBaseColor.red() * bgBaseColorAlpha) + (windowColor.red() * (1 - bgBaseColorAlpha)); | ||||
199 | const int newGreen = (bgBaseColor.green() * bgBaseColorAlpha) + (windowColor.green() * (1 - bgBaseColorAlpha)); | ||||
200 | const int newBlue = (bgBaseColor.blue() * bgBaseColorAlpha) + (windowColor.blue() * (1 - bgBaseColorAlpha)); | ||||
201 | | ||||
202 | const QColor bgFinalColor = QColor(newRed, newGreen, newBlue); | ||||
203 | | ||||
204 | content->setStyleSheet( | ||||
205 | QString::fromLatin1(".QFrame {" | ||||
206 | "background-color: %1;" | ||||
207 | "border-radius: 4px;" | ||||
208 | "border: 2px solid %2;" | ||||
209 | "margin: %3px;" | ||||
210 | "}" | ||||
211 | ".QLabel { color: %4; }" | ||||
212 | ) | ||||
213 | .arg(bgFinalColor.name()) | ||||
214 | .arg(border.name()) | ||||
215 | // DefaultFrameWidth returns the size of the external margin + border width. We know our border is 1px, so we subtract this from the frame normal QStyle FrameWidth to get our margin | ||||
216 | .arg(q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, q) - 1) | ||||
217 | .arg(textColor.name()) | ||||
218 | ); | ||||
219 | } | ||||
220 | | ||||
167 | void KMessageWidgetPrivate::updateLayout() | 221 | void KMessageWidgetPrivate::updateLayout() | ||
168 | { | 222 | { | ||
169 | if (content->layout()) { | 223 | if (content->layout()) { | ||
170 | createLayout(); | 224 | createLayout(); | ||
171 | } | 225 | } | ||
172 | } | 226 | } | ||
173 | 227 | | |||
174 | void KMessageWidgetPrivate::updateSnapShot() | 228 | void KMessageWidgetPrivate::updateSnapShot() | ||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Line(s) | |||||
252 | KMessageWidget::MessageType KMessageWidget::messageType() const | 306 | KMessageWidget::MessageType KMessageWidget::messageType() const | ||
253 | { | 307 | { | ||
254 | return d->messageType; | 308 | return d->messageType; | ||
255 | } | 309 | } | ||
256 | 310 | | |||
257 | void KMessageWidget::setMessageType(KMessageWidget::MessageType type) | 311 | void KMessageWidget::setMessageType(KMessageWidget::MessageType type) | ||
258 | { | 312 | { | ||
259 | d->messageType = type; | 313 | d->messageType = type; | ||
260 | QColor bgBaseColor; | 314 | d->applyStyleSheet(); | ||
261 | | ||||
262 | // We have to hardcode colors here because KWidgetsAddons is a tier 1 framework | | |||
263 | // and therefore can't depend on any other KDE Frameworks | | |||
264 | // The following RGB color values come from the "default" scheme in kcolorscheme.cpp | | |||
265 | switch (type) { | | |||
266 | case Positive: | | |||
267 | bgBaseColor.setRgb(39, 174, 96); // Window: ForegroundPositive | | |||
268 | break; | | |||
269 | case Information: | | |||
270 | bgBaseColor.setRgb(61, 174, 233); // Window: ForegroundActive | | |||
271 | break; | | |||
272 | case Warning: | | |||
273 | bgBaseColor.setRgb(246, 116, 0); // Window: ForegroundNeutral | | |||
274 | break; | | |||
275 | case Error: | | |||
276 | bgBaseColor.setRgb(218, 68, 83); // Window: ForegroundNegative | | |||
277 | break; | | |||
278 | } | | |||
279 | const qreal bgBaseColorAlpha = 0.2; | | |||
280 | bgBaseColor.setAlphaF(bgBaseColorAlpha); | | |||
281 | | ||||
282 | const QPalette palette = QGuiApplication::palette(); | | |||
283 | const QColor windowColor = palette.window().color(); | | |||
284 | const QColor textColor = palette.text().color(); | | |||
285 | const QColor border = bgBaseColor; | | |||
286 | | ||||
287 | // Generate a final background color from overlaying bgBaseColor over windowColor | | |||
288 | const int newRed = (bgBaseColor.red() * bgBaseColorAlpha) + (windowColor.red() * (1 - bgBaseColorAlpha)); | | |||
289 | const int newGreen = (bgBaseColor.green() * bgBaseColorAlpha) + (windowColor.green() * (1 - bgBaseColorAlpha)); | | |||
290 | const int newBlue = (bgBaseColor.blue() * bgBaseColorAlpha) + (windowColor.blue() * (1 - bgBaseColorAlpha)); | | |||
291 | | ||||
292 | const QColor bgFinalColor = QColor(newRed, newGreen, newBlue); | | |||
293 | | ||||
294 | d->content->setStyleSheet( | | |||
295 | QString::fromLatin1(".QFrame {" | | |||
296 | "background-color: %1;" | | |||
297 | "border-radius: 4px;" | | |||
298 | "border: 2px solid %2;" | | |||
299 | "margin: %3px;" | | |||
300 | "}" | | |||
301 | ".QLabel { color: %4; }" | | |||
302 | ) | | |||
303 | .arg(bgFinalColor.name()) | | |||
304 | .arg(border.name()) | | |||
305 | // DefaultFrameWidth returns the size of the external margin + border width. We know our border is 1px, so we subtract this from the frame normal QStyle FrameWidth to get our margin | | |||
306 | .arg(style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this) - 1) | | |||
307 | .arg(textColor.name()) | | |||
308 | ); | | |||
309 | } | 315 | } | ||
310 | 316 | | |||
311 | QSize KMessageWidget::sizeHint() const | 317 | QSize KMessageWidget::sizeHint() const | ||
312 | { | 318 | { | ||
313 | ensurePolished(); | 319 | ensurePolished(); | ||
314 | return d->content->sizeHint(); | 320 | return d->content->sizeHint(); | ||
315 | } | 321 | } | ||
316 | 322 | | |||
317 | QSize KMessageWidget::minimumSizeHint() const | 323 | QSize KMessageWidget::minimumSizeHint() const | ||
318 | { | 324 | { | ||
319 | ensurePolished(); | 325 | ensurePolished(); | ||
320 | return d->content->minimumSizeHint(); | 326 | return d->content->minimumSizeHint(); | ||
321 | } | 327 | } | ||
322 | 328 | | |||
323 | bool KMessageWidget::event(QEvent *event) | 329 | bool KMessageWidget::event(QEvent *event) | ||
324 | { | 330 | { | ||
325 | if (event->type() == QEvent::Polish && !d->content->layout()) { | 331 | if (event->type() == QEvent::Polish && !d->content->layout()) { | ||
326 | d->createLayout(); | 332 | d->createLayout(); | ||
333 | } else if (event->type() == QEvent::PaletteChange) { | ||||
334 | d->applyStyleSheet(); | ||||
327 | } | 335 | } | ||
328 | return QFrame::event(event); | 336 | return QFrame::event(event); | ||
329 | } | 337 | } | ||
330 | 338 | | |||
331 | void KMessageWidget::resizeEvent(QResizeEvent *event) | 339 | void KMessageWidget::resizeEvent(QResizeEvent *event) | ||
332 | { | 340 | { | ||
333 | QFrame::resizeEvent(event); | 341 | QFrame::resizeEvent(event); | ||
334 | 342 | | |||
▲ Show 20 Lines • Show All 161 Lines • Show Last 20 Lines |
Nitpick: shouldn't this be called setStyleSheet or something similar that indicates a bit better that it defines the stylesheet itself?