-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget.py
66 lines (49 loc) · 1.99 KB
/
widget.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
from typing import List, Union
from PySide2.QtGui import QMovie
from PySide2.QtWidgets import QTableWidget, QTableWidgetItem, QListWidget, QLabel
class QMovieWidget(QLabel):
def __init__(self, *args, **kwargs):
super(QMovieWidget, self).__init__(*args, **kwargs)
def is_running(self) -> bool:
return self.movie().MovieState == QMovie.Running
def set_running(self, run: bool) -> None:
if run:
self.movie().start()
else:
self.movie().stop()
class BindableQListWidget(QListWidget):
def __init__(self, *args, **kwargs):
super(BindableQListWidget, self).__init__(*args, **kwargs)
def text_items(self) -> List[str]:
return [self.item(i).text() for i in range(self.count())]
def set_text_items(self, text_items: List[str]):
self.addItems(text_items)
class BindableQTableWidget(QTableWidget):
"""
Table widget that exposes its data for updating, and clears and repopulates the table on data change.
Intended to be used for simple one-way (VM->Table) src for small datasets that don't change often.
"""
def __init__(self, *args, **kwargs):
super(BindableQTableWidget, self).__init__(*args, **kwargs)
def text_items(self):
n_rows = self.rowCount()
n_cols = self.columnCount()
return [
[self.item(i, j).text() for j in range(n_cols)]
for i in range(n_rows)
]
def QT_items(self):
n_rows = self.rowCount()
n_cols = self.columnCount()
return [
[self.item(i, j) for j in range(n_cols)]
for i in range(n_rows)
]
def set_data(self, data: Union[List[List[str]], List[List[QTableWidgetItem]]]):
self.setRowCount(0)
self.setRowCount(len(data))
for i, row in enumerate(data):
for j, element in enumerate(row):
if type(element) is str:
element = QTableWidgetItem(element)
self.setItem(i, j, element)