-
-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathprogressbars.py
More file actions
218 lines (183 loc) · 7.75 KB
/
Copy pathprogressbars.py
File metadata and controls
218 lines (183 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
novelWriter – Custom Widget: Progress Bars
==========================================
File History:
Created: 2023-06-07 [2.1b1] NProgressCircle
Created: 2023-06-09 [2.1b1] NProgressSimple
This file is a part of novelWriter
Copyright (C) 2023 Veronica Berglyd Olsen and novelWriter contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import logging
from math import ceil
from PyQt6.QtCore import QRect
from PyQt6.QtGui import QBrush, QColor, QPainter, QPaintEvent, QPen
from PyQt6.QtWidgets import QProgressBar, QWidget
from novelwriter.types import (
QtAlignCenter, QtPaintAntiAlias, QtRoundCap, QtSizeFixed, QtSolidLine,
QtTransparent
)
logger = logging.getLogger(__name__)
class NProgressCircle(QProgressBar):
"""Extension: Circular Progress Widget
A custom widget that paints a circular progress indicator instead of
a straight bar. It is also possible to set custom text for iṫ.
"""
__slots__ = (
"_text", "_point", "_dRect", "_cRect", "_dPen", "_dBrush",
"_cPen", "_bPen", "_tColor"
)
def __init__(self, parent: QWidget, size: int, point: int) -> None:
super().__init__(parent=parent)
self._text = None
self._point = point
self._dRect = QRect(0, 0, size, size)
self._cRect = QRect(point, point, size - 2*point, size - 2*point)
self._dPen = QPen(QtTransparent)
self._dBrush = QBrush(QtTransparent)
self.setColors(
track=self.palette().alternateBase().color(),
bar=self.palette().highlight().color(),
text=self.palette().text().color()
)
self.setSizePolicy(QtSizeFixed, QtSizeFixed)
self.setFixedWidth(size)
self.setFixedHeight(size)
return
def setColors(
self, back: QColor | None = None, track: QColor | None = None,
bar: QColor | None = None, text: QColor | None = None
) -> None:
"""Set the colours of the widget."""
if isinstance(back, QColor):
self._dPen = QPen(back)
self._dBrush = QBrush(back)
if isinstance(bar, QColor):
self._cPen = QPen(QBrush(bar), self._point, QtSolidLine, QtRoundCap)
if isinstance(track, QColor):
self._bPen = QPen(QBrush(track), self._point, QtSolidLine, QtRoundCap)
if isinstance(text, QColor):
self._tColor = text
return
def setCentreText(self, text: str | None) -> None:
"""Replace the progress text with a custom string."""
self._text = text
self.setValue(self.value()) # Triggers a redraw
return
def paintEvent(self, event: QPaintEvent) -> None:
"""Custom painter for the progress bar."""
progress = 100.0*self.value()/self.maximum()
angle = ceil(16*3.6*progress)
painter = QPainter(self)
painter.setRenderHint(QtPaintAntiAlias, True)
painter.setPen(self._dPen)
painter.setBrush(self._dBrush)
painter.drawEllipse(self._dRect)
painter.setPen(self._bPen)
painter.drawArc(self._cRect, 0, 360*16)
painter.setPen(self._cPen)
painter.drawArc(self._cRect, 90*16, -angle)
painter.setPen(self._tColor)
painter.drawText(self._cRect, QtAlignCenter, self._text or f"{progress:.1f} %")
return
class NProgressSimple(QProgressBar):
"""Extension: Simple Progress Widget
A custom widget that paints a plain bar with no other styling.
"""
def __init__(self, parent: QWidget) -> None:
super().__init__(parent=parent)
return
def paintEvent(self, event: QPaintEvent) -> None:
"""Custom painter for the progress bar."""
if (value := self.value()) > 0:
progress = ceil(self.width()*float(value)/self.maximum())
painter = QPainter(self)
painter.setRenderHint(QtPaintAntiAlias, True)
painter.setPen(self.palette().highlight().color())
painter.setBrush(self.palette().highlight())
painter.drawRect(0, 0, progress, self.height())
return
class NProgressGoal(QProgressBar):
"""Extension: Goal Progress Widget
A custom widget that paints a progress bar with custom styling and text.
"""
__slots__ = (
"_text", "_point", "_dRect", "_cRect", "_dPen", "_dBrush",
"_cPen", "_bPen", "_tColor"
)
def __init__(self, parent: QWidget, width: int, height: int, point: int) -> None:
super().__init__(parent=parent)
logger.debug(self.palette())
self._text = None
self._point = point
self._dRect = QRect(0, 0, width, height)
self._cRect = QRect(2 * point, 2 * point, width - 2 * point, height - 2 * point)
self._dPen = QPen(QtTransparent)
self._dBrush = QBrush(QtTransparent)
self.setColors(
track=self.palette().base().color().darker(300),
bar=self.palette().highlight().color(),
text=self.palette().text().color(),
back=self.palette().base().color().darker(300)
)
self.setSizePolicy(QtSizeFixed, QtSizeFixed)
self.setFixedWidth(width)
self.setFixedHeight(height)
return
def resetColors(self) -> None:
"""Reset the colours to the default values."""
self.setColors(
track=self.palette().base().color().darker(300),
bar=self.palette().highlight().color(),
text=self.palette().text().color(),
back=self.palette().base().color().darker(300)
)
return
def setColors(
self, back: QColor | None = None, track: QColor | None = None,
bar: QColor | None = None, text: QColor | None = None
) -> None:
"""Set the colours of the widget."""
if isinstance(back, QColor):
self._dPen = QPen(back)
self._dBrush = QBrush(back)
if isinstance(bar, QColor):
logger.debug(f"Setting bar colour: {bar}")
self._cPen = QPen(QBrush(bar), 2 * self._point, QtSolidLine)
self._cBrush = QBrush(bar)
if isinstance(track, QColor):
logger.debug(f"Setting track colour: {track}")
self._bPen = QPen(QBrush(track), 2 * self._point, QtSolidLine, QtRoundCap)
if isinstance(text, QColor):
self._tColor = text
return
def setCentreText(self, text: str | None) -> None:
"""Replace the progress text with a custom string."""
self._text = text
self.setValue(self.value()) # Triggers a redraw
return
def paintEvent(self, event: QPaintEvent) -> None:
"""Custom painter for the progress bar."""
progress = self.value()/self.maximum()
painter = QPainter(self)
painter.setRenderHint(QtPaintAntiAlias, True)
painter.setPen(self._bPen)
painter.setBrush(self._dBrush)
painter.drawRect(self._dRect)
painter.setPen(self._cPen)
painter.setBrush(self._cBrush)
x, y = self._cRect.topLeft().x(), self._cRect.topLeft().y()
painter.drawRect(x, y, ceil(self._cRect.width() * progress), self._cRect.height())
painter.setPen(self._tColor)
painter.drawText(self._cRect, QtAlignCenter, self._text or f"{(progress*100):.1f} %")
return