Menu

[8f66b6]: / dialogs / CheckableItemsDialog.py  Maximize  Restore  History

Download this file

86 lines (73 with data), 3.1 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
# -*- coding: utf-8 -*-
"""
Copyright (C) 2012 Oliver Tengler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from typing import List,Tuple
from PyQt5.QtCore import Qt, pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog, QWidget
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from .Ui_CheckableItemsDialog import Ui_CheckableItemsDialog
class CheckableItemsDialog(QDialog):
def __init__(self, title: str, bCheckAllState: bool, parent: QWidget) -> None:
super().__init__(parent)
self.ui = Ui_CheckableItemsDialog()
self.ui.setupUi(self)
self.ui.buttonOK.clicked.connect(self.accept)
self.ui.buttonCancel.clicked.connect(self.reject)
self.ui.checkToggleAll.toggled.connect(self.checkAll)
self.setWindowTitle(title)
self.setProperty("shadeBackground", True) # fill background with gradient as defined in style sheet
self.model = QStandardItemModel()
self.ui.listViewItems.setModel(self.model)
if bCheckAllState:
self.ui.checkToggleAll.setCheckState(Qt.Checked)
else:
self.ui.checkToggleAll.setCheckState(Qt.Unchecked)
def addItem(self, name: str, bCheck: bool=True) -> None:
item = QStandardItem(name)
item.setFlags(Qt.ItemIsUserCheckable | item.flags())
if bCheck:
item.setData(Qt.Checked, Qt.CheckStateRole)
else:
item.setData(Qt.Unchecked, Qt.CheckStateRole)
self.model.appendRow(item)
def checkedItems(self) -> List[Tuple[int,str]]:
items = []
for i in range(self.model.rowCount()):
index = self.model.index(i, 0)
if index.data(Qt.CheckStateRole) == Qt.Checked:
items.append((i, index.data()))
return items
@pyqtSlot(bool)
def checkAll(self, bCheckAll: bool) -> None:
if bCheckAll:
flag = Qt.Checked
else:
flag = Qt.Unchecked
for i in range(self.model.rowCount()):
index = self.model.index(i, 0)
self.model.setData(index, flag, Qt.CheckStateRole)
def main() -> None:
import sys
QApplication(sys.argv)
w = CheckableItemsDialog("Choose indexes to update", True, None)
w.addItem("Amber")
w.addItem("Silver")
w.addItem("Gold")
w.show()
if w.exec():
items = w.checkedItems()
print(items)
sys.exit(0)
if __name__ == "__main__":
main()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.