|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select the styles to be imported/exported. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot, Qt |
|
11 from PyQt5.QtWidgets import ( |
|
12 QDialog, QDialogButtonBox, QListWidgetItem, QAbstractButton |
|
13 ) |
|
14 |
|
15 from .Ui_EditorHighlightingStylesSelectionDialog import ( |
|
16 Ui_EditorHighlightingStylesSelectionDialog |
|
17 ) |
|
18 |
|
19 |
|
20 class EditorHighlightingStylesSelectionDialog( |
|
21 QDialog, Ui_EditorHighlightingStylesSelectionDialog |
|
22 ): |
|
23 """ |
|
24 Class implementing a dialog to select the styles to be imported/exported. |
|
25 """ |
|
26 def __init__(self, lexerNames, forImport, preselect=None, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param lexerNames list of lexer names to select from |
|
31 @type list of str |
|
32 @param forImport flag indicating a dialog for importing styles |
|
33 @type bool |
|
34 @param preselect list of lexer names to be preselected |
|
35 @type list of str |
|
36 @param parent reference to the parent widget |
|
37 @type QWidget |
|
38 """ |
|
39 super(EditorHighlightingStylesSelectionDialog, self).__init__(parent) |
|
40 self.setupUi(self) |
|
41 |
|
42 self.__selectAllButton = self.buttonBox.addButton( |
|
43 self.tr("Select All"), QDialogButtonBox.ActionRole) |
|
44 |
|
45 if forImport: |
|
46 self.setWindowTitle(self.tr("Import Highlighting Styles")) |
|
47 self.infoLabel.setText(self.tr( |
|
48 "Select the highlighting styles to be imported")) |
|
49 else: |
|
50 self.setWindowTitle(self.tr("Export Highlighting Styles")) |
|
51 self.infoLabel.setText(self.tr( |
|
52 "Select the highlighting styles to be exported")) |
|
53 |
|
54 if preselect is None: |
|
55 preselect = [] |
|
56 |
|
57 for name in lexerNames: |
|
58 itm = QListWidgetItem(name, self.lexersList) |
|
59 itm.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) |
|
60 if name in preselect: |
|
61 itm.setCheckState(Qt.Checked) |
|
62 else: |
|
63 itm.setCheckState(Qt.Unchecked) |
|
64 |
|
65 self.__updateOkButton() |
|
66 |
|
67 @pyqtSlot() |
|
68 def __updateOkButton(self): |
|
69 """ |
|
70 Private slot to update the state of the OK button. |
|
71 """ |
|
72 for row in range(self.lexersList.count()): |
|
73 itm = self.lexersList.item(row) |
|
74 if itm.checkState() == Qt.Checked: |
|
75 enable = True |
|
76 break |
|
77 else: |
|
78 enable = False |
|
79 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable) |
|
80 |
|
81 @pyqtSlot(QListWidgetItem) |
|
82 def on_lexersList_itemChanged(self, item): |
|
83 """ |
|
84 Private slot to react on changes in check state. |
|
85 |
|
86 @param item reference to the changed item |
|
87 @type QListWidgetItem |
|
88 """ |
|
89 self.__updateOkButton() |
|
90 |
|
91 @pyqtSlot(QAbstractButton) |
|
92 def on_buttonBox_clicked(self, button): |
|
93 """ |
|
94 Private slot to handle the user pressing a button. |
|
95 |
|
96 @param button reference to the button pressed |
|
97 @type QAbstractButton |
|
98 """ |
|
99 if button is self.__selectAllButton: |
|
100 for row in range(self.lexersList.count()): |
|
101 itm = self.lexersList.item(row) |
|
102 itm.setCheckState(Qt.Checked) |
|
103 |
|
104 def getLexerNames(self): |
|
105 """ |
|
106 Public method to get the selected lexer names. |
|
107 |
|
108 @return list of selected lexer names |
|
109 @rtype list of str |
|
110 """ |
|
111 lexerNames = [] |
|
112 for row in range(self.lexersList.count()): |
|
113 itm = self.lexersList.item(row) |
|
114 if itm.checkState() == Qt.Checked: |
|
115 lexerNames.append(itm.text()) |
|
116 |
|
117 return lexerNames |