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 <QIcon> | ||||
24 | #include <QPainter> | ||||
25 | #include <QPropertyAnimation> | ||||
26 | #include <QResizeEvent> | ||||
27 | | ||||
28 | class KBusyIndicatorWidget::Private : public QObject | ||||
broulik: `Q_DECL_HIDDEN` | |||||
29 | { | ||||
30 | Q_OBJECT | ||||
31 | Q_PROPERTY(qreal rotation MEMBER rotation WRITE setRotation) | ||||
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… | |||||
32 | public: | ||||
33 | Private(KBusyIndicatorWidget *parent) | ||||
34 | : q(parent) | ||||
35 | , animation(new QPropertyAnimation(this, "rotation", q)) | ||||
36 | { | ||||
37 | animation->setLoopCount(-1); | ||||
38 | animation->setDuration(1000); | ||||
broulik: Plasma's `BusyIndicator` uses 1500ms | |||||
39 | animation->setStartValue(0); | ||||
40 | animation->setEndValue(360); | ||||
41 | } | ||||
42 | | ||||
43 | void setRotation(qreal newRotation) | ||||
44 | { | ||||
45 | rotation = newRotation; | ||||
46 | q->update(); // repaint new rotation | ||||
47 | } | ||||
48 | | ||||
49 | // Called when running and/or visibility changes to start/pause the | ||||
50 | // animation as necessary. | ||||
51 | void maybeToggleAnimation() | ||||
52 | { | ||||
53 | if (running && q->isVisible()) { | ||||
54 | animation->start(); | ||||
55 | return; | ||||
56 | } | ||||
57 | animation->pause(); | ||||
58 | } | ||||
59 | | ||||
60 | KBusyIndicatorWidget *q = nullptr; | ||||
61 | QPropertyAnimation *animation = nullptr; | ||||
62 | QIcon icon = QIcon::fromTheme(QStringLiteral("view-refresh")); | ||||
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… | |||||
63 | qreal rotation = 0; | ||||
64 | bool running = true; | ||||
65 | QPointF paintCenter; | ||||
66 | }; | ||||
67 | | ||||
68 | KBusyIndicatorWidget::KBusyIndicatorWidget(QWidget *parent) | ||||
69 | : QWidget(parent) | ||||
70 | , d(new Private(this)) | ||||
71 | { | ||||
72 | } | ||||
73 | | ||||
74 | KBusyIndicatorWidget::~KBusyIndicatorWidget() | ||||
75 | { | ||||
76 | delete d; | ||||
77 | } | ||||
78 | | ||||
79 | bool KBusyIndicatorWidget::running() const | ||||
80 | { | ||||
81 | return d->running; | ||||
82 | } | ||||
83 | | ||||
84 | void KBusyIndicatorWidget::setRunning(bool running) | ||||
85 | { | ||||
86 | if (d->running == running) { | ||||
87 | return; | ||||
88 | } | ||||
89 | | ||||
90 | d->running = running; | ||||
91 | d->maybeToggleAnimation(); | ||||
92 | emit runningChanged(d->running); | ||||
93 | } | ||||
94 | | ||||
95 | void KBusyIndicatorWidget::showEvent(QShowEvent *event) | ||||
96 | { | ||||
97 | d->maybeToggleAnimation(); | ||||
98 | QWidget::showEvent(event); | ||||
99 | } | ||||
100 | | ||||
101 | void KBusyIndicatorWidget::hideEvent(QHideEvent *event) | ||||
102 | { | ||||
103 | d->maybeToggleAnimation(); | ||||
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… | |||||
104 | QWidget::hideEvent(event); | ||||
105 | } | ||||
106 | | ||||
107 | void KBusyIndicatorWidget::resizeEvent(QResizeEvent *event) | ||||
108 | { | ||||
109 | QWidget::resizeEvent(event); | ||||
110 | | ||||
111 | d->paintCenter = QPointF(event->size().width() / 2.0, | ||||
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! | |||||
112 | event->size().height() / 2.0); | ||||
113 | } | ||||
114 | | ||||
115 | void KBusyIndicatorWidget::paintEvent(QPaintEvent *) | ||||
116 | { | ||||
117 | QPainter painter(this); | ||||
118 | painter.setRenderHint(QPainter::SmoothPixmapTransform); | ||||
119 | | ||||
120 | // Rotate around the center and then reset back to origin for icon painting. | ||||
121 | painter.translate(d->paintCenter); | ||||
122 | painter.rotate(d->rotation); | ||||
123 | painter.translate(-d->paintCenter); | ||||
124 | | ||||
125 | d->icon.paint(&painter, rect()); | ||||
126 | } | ||||
127 | | ||||
128 | #include "kbusyindicatorwidget.moc" |
Q_DECL_HIDDEN