|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2004 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a configuration dialog for the bookmarked files menu. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import QFileInfo, Qt, pyqtSlot |
|
11 from PyQt5.QtGui import QColor |
|
12 from PyQt5.QtWidgets import QListWidgetItem, QDialog |
|
13 |
|
14 from E5Gui.E5PathPicker import E5PathPickerModes |
|
15 |
|
16 from .Ui_BookmarkedFilesDialog import Ui_BookmarkedFilesDialog |
|
17 |
|
18 |
|
19 class BookmarkedFilesDialog(QDialog, Ui_BookmarkedFilesDialog): |
|
20 """ |
|
21 Class implementing a configuration dialog for the bookmarked files menu. |
|
22 """ |
|
23 def __init__(self, bookmarks, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param bookmarks list of bookmarked files (list of strings) |
|
28 @param parent parent widget (QWidget) |
|
29 """ |
|
30 super().__init__(parent) |
|
31 self.setupUi(self) |
|
32 |
|
33 self.filePicker.setMode(E5PathPickerModes.OpenFileMode) |
|
34 |
|
35 self.bookmarks = bookmarks[:] |
|
36 for bookmark in self.bookmarks: |
|
37 itm = QListWidgetItem(bookmark, self.filesList) |
|
38 if not QFileInfo(bookmark).exists(): |
|
39 itm.setBackground(QColor(Qt.GlobalColor.red)) |
|
40 |
|
41 if len(self.bookmarks): |
|
42 self.filesList.setCurrentRow(0) |
|
43 |
|
44 def on_filePicker_textChanged(self, txt): |
|
45 """ |
|
46 Private slot to handle the textChanged signal of the file edit. |
|
47 |
|
48 @param txt the text of the file edit (string) |
|
49 """ |
|
50 self.addButton.setEnabled(txt != "") |
|
51 self.changeButton.setEnabled( |
|
52 txt != "" and |
|
53 self.filesList.currentRow() != -1) |
|
54 |
|
55 def on_filesList_currentRowChanged(self, row): |
|
56 """ |
|
57 Private slot to set the lineedit depending on the selected entry. |
|
58 |
|
59 @param row the current row (integer) |
|
60 """ |
|
61 if row == -1: |
|
62 self.filePicker.clear() |
|
63 self.downButton.setEnabled(False) |
|
64 self.upButton.setEnabled(False) |
|
65 self.deleteButton.setEnabled(False) |
|
66 self.changeButton.setEnabled(False) |
|
67 else: |
|
68 maxIndex = len(self.bookmarks) - 1 |
|
69 self.upButton.setEnabled(row != 0) |
|
70 self.downButton.setEnabled(row != maxIndex) |
|
71 self.deleteButton.setEnabled(True) |
|
72 self.changeButton.setEnabled(True) |
|
73 |
|
74 bookmark = self.bookmarks[row] |
|
75 self.filePicker.setText(bookmark) |
|
76 |
|
77 @pyqtSlot() |
|
78 def on_addButton_clicked(self): |
|
79 """ |
|
80 Private slot to add a new entry. |
|
81 """ |
|
82 bookmark = self.filePicker.text() |
|
83 if bookmark: |
|
84 itm = QListWidgetItem(bookmark, self.filesList) |
|
85 if not QFileInfo(bookmark).exists(): |
|
86 itm.setBackground(QColor(Qt.GlobalColor.red)) |
|
87 self.filePicker.clear() |
|
88 self.bookmarks.append(bookmark) |
|
89 row = self.filesList.currentRow() |
|
90 self.on_filesList_currentRowChanged(row) |
|
91 |
|
92 @pyqtSlot() |
|
93 def on_changeButton_clicked(self): |
|
94 """ |
|
95 Private slot to change an entry. |
|
96 """ |
|
97 row = self.filesList.currentRow() |
|
98 bookmark = self.filePicker.text() |
|
99 self.bookmarks[row] = bookmark |
|
100 itm = self.filesList.item(row) |
|
101 itm.setText(bookmark) |
|
102 if not QFileInfo(bookmark).exists(): |
|
103 itm.setBackground(QColor(Qt.GlobalColor.red)) |
|
104 else: |
|
105 itm.setBackground(QColor()) |
|
106 |
|
107 @pyqtSlot() |
|
108 def on_deleteButton_clicked(self): |
|
109 """ |
|
110 Private slot to delete the selected entry. |
|
111 """ |
|
112 row = self.filesList.currentRow() |
|
113 itm = self.filesList.takeItem(row) |
|
114 del itm |
|
115 del self.bookmarks[row] |
|
116 row = self.filesList.currentRow() |
|
117 self.on_filesList_currentRowChanged(row) |
|
118 |
|
119 @pyqtSlot() |
|
120 def on_downButton_clicked(self): |
|
121 """ |
|
122 Private slot to move an entry down in the list. |
|
123 """ |
|
124 rows = self.filesList.count() |
|
125 row = self.filesList.currentRow() |
|
126 if row == rows - 1: |
|
127 # we're already at the end |
|
128 return |
|
129 |
|
130 self.__swap(row, row + 1) |
|
131 itm = self.filesList.takeItem(row) |
|
132 self.filesList.insertItem(row + 1, itm) |
|
133 self.filesList.setCurrentItem(itm) |
|
134 self.upButton.setEnabled(True) |
|
135 if row == rows - 2: |
|
136 self.downButton.setEnabled(False) |
|
137 else: |
|
138 self.downButton.setEnabled(True) |
|
139 |
|
140 @pyqtSlot() |
|
141 def on_upButton_clicked(self): |
|
142 """ |
|
143 Private slot to move an entry up in the list. |
|
144 """ |
|
145 row = self.filesList.currentRow() |
|
146 if row == 0: |
|
147 # we're already at the top |
|
148 return |
|
149 |
|
150 self.__swap(row - 1, row) |
|
151 itm = self.filesList.takeItem(row) |
|
152 self.filesList.insertItem(row - 1, itm) |
|
153 self.filesList.setCurrentItem(itm) |
|
154 if row == 1: |
|
155 self.upButton.setEnabled(False) |
|
156 else: |
|
157 self.upButton.setEnabled(True) |
|
158 self.downButton.setEnabled(True) |
|
159 |
|
160 def getBookmarkedFiles(self): |
|
161 """ |
|
162 Public method to retrieve the tools list. |
|
163 |
|
164 @return a list of filenames (list of strings) |
|
165 """ |
|
166 return self.bookmarks |
|
167 |
|
168 def __swap(self, itm1, itm2): |
|
169 """ |
|
170 Private method used two swap two list entries given by their index. |
|
171 |
|
172 @param itm1 index of first entry (int) |
|
173 @param itm2 index of second entry (int) |
|
174 """ |
|
175 tmp = self.bookmarks[itm1] |
|
176 self.bookmarks[itm1] = self.bookmarks[itm2] |
|
177 self.bookmarks[itm2] = tmp |