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