Changeset View
Changeset View
Standalone View
Standalone View
src/kbusyindicatorwidget.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <https://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "kbusyindicatorwidget.h" | ||||
22 | | ||||
23 | #include <QApplication> | ||||
24 | #include <QIcon> | ||||
25 | #include <QPainter> | ||||
26 | #include <QPropertyAnimation> | ||||
27 | #include <QResizeEvent> | ||||
28 | #include <QStyle> | ||||
29 | | ||||
broulik: `Q_DECL_HIDDEN` | |||||
30 | class KBusyIndicatorWidgetPrivate | ||||
31 | { | ||||
32 | public: | ||||
You probably want to be using QVariantAnimation instead of going through the property system for that. Then your private also probably doesn't need to be a QObject broulik: You probably want to be using `QVariantAnimation` instead of going through the property system… | |||||
33 | KBusyIndicatorWidgetPrivate(KBusyIndicatorWidget *parent) | ||||
34 | : q(parent) | ||||
35 | { | ||||
36 | animation.setLoopCount(-1); | ||||
37 | animation.setDuration(1500); | ||||
38 | animation.setStartValue(0); | ||||
39 | animation.setEndValue(360); | ||||
broulik: Plasma's `BusyIndicator` uses 1500ms | |||||
40 | QObject::connect(&animation, &QVariantAnimation::valueChanged, | ||||
41 | q, [=](QVariant value) { | ||||
42 | rotation = value.toReal(); | ||||
43 | q->update(); // repaint new rotation | ||||
44 | }); | ||||
45 | } | ||||
46 | | ||||
47 | KBusyIndicatorWidget *q = nullptr; | ||||
48 | QVariantAnimation animation; | ||||
49 | QIcon icon = QIcon::fromTheme(QStringLiteral("view-refresh")); | ||||
50 | qreal rotation = 0; | ||||
51 | QPointF paintCenter; | ||||
52 | }; | ||||
53 | | ||||
54 | KBusyIndicatorWidget::KBusyIndicatorWidget(QWidget *parent) | ||||
55 | : QWidget(parent) | ||||
56 | , d(new KBusyIndicatorWidgetPrivate(this)) | ||||
57 | { | ||||
58 | } | ||||
59 | | ||||
60 | KBusyIndicatorWidget::~KBusyIndicatorWidget() | ||||
61 | { | ||||
62 | delete d; | ||||
63 | } | ||||
broulik: Is that legal in the C++ standard allowed by frameworks? | |||||
Well, it builds at least under c++11 which is what kf5 is compatible with. According to this page it should be fine (assuming the caveats mentioned are exhaustive anyway). sitter: Well, it builds at least under c++11 which is what kf5 is compatible with.
According to… | |||||
64 | | ||||
65 | QSize KBusyIndicatorWidget::minimumSizeHint() const | ||||
66 | { | ||||
67 | const auto extent = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize); | ||||
68 | return QSize(extent, extent); | ||||
69 | } | ||||
70 | | ||||
71 | void KBusyIndicatorWidget::showEvent(QShowEvent *event) | ||||
72 | { | ||||
73 | QWidget::showEvent(event); | ||||
74 | d->animation.start(); | ||||
75 | } | ||||
76 | | ||||
77 | void KBusyIndicatorWidget::hideEvent(QHideEvent *event) | ||||
78 | { | ||||
79 | QWidget::hideEvent(event); | ||||
80 | d->animation.pause(); | ||||
81 | } | ||||
82 | | ||||
83 | void KBusyIndicatorWidget::resizeEvent(QResizeEvent *event) | ||||
84 | { | ||||
85 | QWidget::resizeEvent(event); | ||||
86 | d->paintCenter = QPointF(event->size().width() / 2.0, | ||||
87 | event->size().height() / 2.0); | ||||
88 | } | ||||
89 | | ||||
90 | void KBusyIndicatorWidget::paintEvent(QPaintEvent *) | ||||
91 | { | ||||
92 | QPainter painter(this); | ||||
93 | painter.setRenderHint(QPainter::SmoothPixmapTransform); | ||||
94 | | ||||
95 | // Rotate around the center and then reset back to origin for icon painting. | ||||
96 | painter.translate(d->paintCenter); | ||||
97 | painter.rotate(d->rotation); | ||||
98 | painter.translate(-d->paintCenter); | ||||
99 | | ||||
100 | d->icon.paint(&painter, rect()); | ||||
101 | } | ||||
102 | | ||||
103 | bool KBusyIndicatorWidget::event(QEvent *event) | ||||
104 | { | ||||
is q->isVisible() checked in this method already updated at this point or should the hideEvent be processed before? Likewise for showEvent broulik: is `q->isVisible()` checked in this method already updated at this point or should the… | |||||
105 | // Only overridden to be flexible WRT binary compatible in the future. | ||||
106 | // Overriding later has potential to change the call going through | ||||
107 | // the vtable or not. | ||||
108 | return QWidget::event(event); | ||||
109 | } | ||||
Does this widget need a sizeHint of a button or default icon size or something? broulik: Does this widget need a `sizeHint` of a button or default icon size or something? | |||||
I was thinking about it, and honestly I am not sure. I don't think we have access to anything to do with icon units, so we could hardcode some dimensions (22,22 I guess) but that's about it. sitter: I was thinking about it, and honestly I am not sure. I don't think we have access to anything… | |||||
broulik: How about `PM_LargeIconSize` | |||||
sitter: Oh, that would work! |
Q_DECL_HIDDEN