-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.py
260 lines (249 loc) · 9.82 KB
/
components.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from PySide6 import QtCore
import PySide6.QtGui
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from apscheduler.schedulers.qt import QtScheduler
from datetime import timedelta
from PySide6.QtWidgets import QWidget
class taskRecorder(QWidget):
renameTask = QtCore.Signal()
def __init__(self, parent=None):
super().__init__()
self.parent = parent
hlay = QHBoxLayout(self)
hlay.setContentsMargins(0,0,0,0)
hlay.setSpacing(0)
self.label = QLabel("__")
self.label.setFixedWidth(100)
self.progress = QProgressBar()
self.progress.setMinimum(0)
self.progress.setMaximum(100)
# print(self.metaObject().className())
# self.setProperty("class", "recorder")
self.progress.setStyleSheet("""
QProgressBar {
border: 0px solid grey;
border-radius: 0px;
text-align: center;
background-color: transparent;
padding: 0 10px;
}
QProgressBar::chunk {background-color: #63cf80; width: 1px;}
QProgressBar::chunk:hover { background-color: #6ee68e; }
""")
# for timing
self.step = 0
self.recording = False
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_time)
self.duration = QLabel("0")
self.duration.setFixedWidth(50)
self.btn = QPushButton("run")
self.btn.setFlat(True)
self.btn.setStyleSheet("""
QPushButton {
background-color:transparent;
width:40px;
border:0px solid rgba(0,0,0,0.2);
border-radius:2px;
padding:0 5px;
}
QPushButton:pressed {
border:0px solid rgba(0,0,0,0.5)
}
QPushButton:hover {
background-color:rgba(0,0,0,0.1);
}
""")
hlay.addWidget(self.label)
hlay.addWidget(self.progress)
hlay.addWidget(self.duration)
hlay.addWidget(self.btn)
self.setLayout(hlay)
# self.setContextMenuPolicy(Qt.CustomContextMenu)
# self.customContextMenuRequested.connect(self.contextMenuEvent)
def get_step(self):
return self.step
def set_step(self, step):
self.step = step
m, s = divmod(self.step, 60)
h, m = divmod(m, 60)
self.duration.setText(f'{h:02d}:{m:02d}:{s:02d}')
def get_name(self):
return self.label.text()
def set_name(self, name):
print('name',name)
self.label.setText(name)
def update_time(self):
self.step += 1
m, s = divmod(self.step, 60)
h, m = divmod(m, 60)
self.duration.setText(f'{h:02d}:{m:02d}:{s:02d}')
def contextMenuEvent(self, event):
print('menu')
self.menu = QMenu(self)
action1 = self.menu.addAction('Rename')
action1.triggered.connect(self.renameTask.emit)
self.menu.exec(event.globalPos())
# return super().contextMenuEvent(event)
class CheckableComboBox(QComboBox):
popupAboutToBeShown = QtCore.Signal()
# Subclass Delegate to increase item height
class Delegate(QStyledItemDelegate):
def sizeHint(self, option, index):
size = super().sizeHint(option, index)
size.setHeight(20)
return size
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Make the combo editable to set a custom text, but readonly
self.setEditable(True)
self.lineEdit().setReadOnly(True)
# Make the lineedit the same color as QPushButton
palette = QApplication.palette()
palette.setBrush(QPalette.Base, palette.button())
self.lineEdit().setPalette(palette)
# Use custom delegate
self.setItemDelegate(CheckableComboBox.Delegate())
# Update the text when an item is toggled
self.model().dataChanged.connect(self.updateText)
# Hide and show popup when clicking the line edit
self.lineEdit().installEventFilter(self)
self.closeOnLineEditClick = False
# Prevent popup from closing when clicking on an item
self.view().viewport().installEventFilter(self)
# menu for clean or select all
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.showMenu)
def showMenu(self, pos):
print('showmenu')
# self.hidePopup()
menu = QMenu()
clear = menu.addAction("Clear", self.clearSelection)
all = menu.addAction("All", self.selectAll)
action = menu.exec(self.mapToGlobal(pos))
def clearSelection(self):
print('clear')
for i in range(self.model().rowCount()):
self.model().item(i).setCheckState(Qt.Unchecked)
def selectAll(self):
print('all')
self.popupAboutToBeShown.emit() # to load the tasks for the first time
cnt = self.model().rowCount()
for i in range(cnt):
self.model().item(i).setCheckState(Qt.Checked)
def resizeEvent(self, event):
# Recompute text to elide as needed
self.updateText()
super().resizeEvent(event)
def eventFilter(self, object, event):
if object == self.lineEdit():
if event.type() == QEvent.MouseButtonRelease:
# if self.closeOnLineEditClick:
if self.closeOnLineEditClick or event.button() == Qt.RightButton:
self.hidePopup()
else:
self.showPopup()
return True
return False
if object == self.view().viewport():
if event.type() == QEvent.MouseButtonRelease:
if event.button() == Qt.RightButton: # added for menu
self.showMenu(event.pos())
return True
index = self.view().indexAt(event.pos())
item = self.model().item(index.row())
if item.checkState() == Qt.Checked:
item.setCheckState(Qt.Unchecked)
else:
item.setCheckState(Qt.Checked)
return True
return False
def showPopup(self):
self.popupAboutToBeShown.emit()
super().showPopup()
# When the popup is displayed, a click on the lineedit should close it
self.closeOnLineEditClick = True
def hidePopup(self):
super().hidePopup()
# Used to prevent immediate reopening when clicking on the lineEdit
self.startTimer(100)
# Refresh the display text when closing
self.updateText()
def timerEvent(self, event):
# After timeout, kill timer, and reenable click on line edit
self.killTimer(event.timerId())
self.closeOnLineEditClick = False
def updateText(self):
texts = []
for i in range(self.model().rowCount()):
if self.model().item(i).checkState() == Qt.Checked:
texts.append(self.model().item(i).text())
text = ", ".join(texts)
# Compute elided text (with "...")
metrics = QFontMetrics(self.lineEdit().font())
elidedText = metrics.elidedText(text, Qt.ElideRight, self.lineEdit().width())
self.lineEdit().setText(elidedText)
def addItem(self, text, data=None):
item = QStandardItem()
item.setText(text)
if data is None:
item.setData(text)
else:
item.setData(data)
item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
item.setData(Qt.Unchecked, Qt.CheckStateRole)
self.model().appendRow(item)
def addItems(self, texts, datalist=None):
for i, text in enumerate(texts):
try:
data = datalist[i]
except (TypeError, IndexError):
data = None
self.addItem(text, data)
def currentData(self):
# Return the list of selected items data
res = []
for i in range(self.model().rowCount()):
if self.model().item(i).checkState() == Qt.Checked:
res.append(self.model().item(i).data())
return res
class ProgressDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
progress = index.data(QtCore.Qt.UserRole+1000)
# progress = .3
opt = QStyleOptionProgressBar()
opt.rect = option.rect
opt.minimum = 0
opt.maximum = 100
opt.progress = progress
opt.text = format(progress, '.2f')
opt.textVisible = True
opt.textAlignment = QtCore.Qt.AlignCenter
# opt.palette.setBrush(QPalette.Disabled, QPalette.Base, QBrush(Qt.red))
# QApplication.style().drawControl(QStyle.CE_ProgressBar, opt, painter)
# QApplication.style().drawControl(QStyle.CE_ProgressBarContents, opt, painter)
if (progress > 0):
progBarWidth = float((option.rect.width() * progress) / 100)
painter.save()
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setBrush(QColor(99,207,128,200)) #grigio molto scuro
painter.setPen(QColor("transparent"))
rect2=QtCore.QRect(option.rect.x(), option.rect.y(), progBarWidth,
option.rect.height())
painter.drawRect(rect2)
painter.setPen(QColor(QtCore.Qt.white))
painter.restore()
QApplication.style().drawControl(QStyle.CE_ProgressBarLabel, opt, painter)
class Scheduler(QtCore.QObject):
dateChanges = QtCore.Signal()
def __init__(self):
super().__init__()
self.id = 'sched'
self.sched = QtScheduler()
def start(self, hour, minute):
self.sched.add_job(self.date_changes, 'cron', hour=hour, minute=minute)
self.sched.start()
def date_changes(self):
self.dateChanges.emit()