|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2004 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a configuration dialog for the bookmarked files menu. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 from E4Gui.E4Completers import E4FileCompleter |
|
14 |
|
15 from Ui_BookmarkedFilesDialog import Ui_BookmarkedFilesDialog |
|
16 |
|
17 import Utilities |
|
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 QDialog.__init__(self, parent) |
|
31 self.setupUi(self) |
|
32 |
|
33 self.fileCompleter = E4FileCompleter(self.fileEdit) |
|
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.setBackgroundColor(QColor(Qt.red)) |
|
40 |
|
41 if len(self.bookmarks): |
|
42 self.filesList.setCurrentRow(0) |
|
43 |
|
44 def on_fileEdit_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(txt != "" and \ |
|
52 self.filesList.currentRow() != -1) |
|
53 |
|
54 def on_filesList_currentRowChanged(self, row): |
|
55 """ |
|
56 Private slot to set the lineedit depending on the selected entry. |
|
57 |
|
58 @param row the current row (integer) |
|
59 """ |
|
60 if row == -1: |
|
61 self.fileEdit.clear() |
|
62 self.downButton.setEnabled(False) |
|
63 self.upButton.setEnabled(False) |
|
64 self.deleteButton.setEnabled(False) |
|
65 self.changeButton.setEnabled(False) |
|
66 else: |
|
67 maxIndex = len(self.bookmarks) - 1 |
|
68 self.upButton.setEnabled(row != 0) |
|
69 self.downButton.setEnabled(row != maxIndex) |
|
70 self.deleteButton.setEnabled(True) |
|
71 self.changeButton.setEnabled(True) |
|
72 |
|
73 bookmark = self.bookmarks[row] |
|
74 self.fileEdit.setText(bookmark) |
|
75 |
|
76 @pyqtSlot() |
|
77 def on_addButton_clicked(self): |
|
78 """ |
|
79 Private slot to add a new entry. |
|
80 """ |
|
81 bookmark = self.fileEdit.text() |
|
82 if bookmark: |
|
83 bookmark = Utilities.toNativeSeparators(bookmark) |
|
84 itm = QListWidgetItem(bookmark, self.filesList) |
|
85 if not QFileInfo(bookmark).exists(): |
|
86 itm.setBackgroundColor(QColor(Qt.red)) |
|
87 self.fileEdit.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.fileEdit.text() |
|
99 bookmark = Utilities.toNativeSeparators(bookmark) |
|
100 self.bookmarks[row] = bookmark |
|
101 itm = self.filesList.item(row) |
|
102 itm.setText(bookmark) |
|
103 if not QFileInfo(bookmark).exists(): |
|
104 itm.setBackgroundColor(QColor(Qt.red)) |
|
105 else: |
|
106 itm.setBackgroundColor(QColor()) |
|
107 |
|
108 @pyqtSlot() |
|
109 def on_deleteButton_clicked(self): |
|
110 """ |
|
111 Private slot to delete the selected entry. |
|
112 """ |
|
113 row = self.filesList.currentRow() |
|
114 itm = self.filesList.takeItem(row) |
|
115 del itm |
|
116 del self.bookmarks[row] |
|
117 row = self.filesList.currentRow() |
|
118 self.on_filesList_currentRowChanged(row) |
|
119 |
|
120 @pyqtSlot() |
|
121 def on_downButton_clicked(self): |
|
122 """ |
|
123 Private slot to move an entry down in the list. |
|
124 """ |
|
125 rows = self.filesList.count() |
|
126 row = self.filesList.currentRow() |
|
127 if row == rows - 1: |
|
128 # we're already at the end |
|
129 return |
|
130 |
|
131 self.__swap(row, row + 1) |
|
132 itm = self.filesList.takeItem(row) |
|
133 self.filesList.insertItem(row + 1, itm) |
|
134 self.filesList.setCurrentItem(itm) |
|
135 self.upButton.setEnabled(True) |
|
136 if row == rows - 2: |
|
137 self.downButton.setEnabled(False) |
|
138 else: |
|
139 self.downButton.setEnabled(True) |
|
140 |
|
141 @pyqtSlot() |
|
142 def on_upButton_clicked(self): |
|
143 """ |
|
144 Private slot to move an entry up in the list. |
|
145 """ |
|
146 row = self.filesList.currentRow() |
|
147 if row == 0: |
|
148 # we're already at the top |
|
149 return |
|
150 |
|
151 self.__swap(row - 1, row) |
|
152 itm = self.filesList.takeItem(row) |
|
153 self.filesList.insertItem(row - 1, itm) |
|
154 self.filesList.setCurrentItem(itm) |
|
155 if row == 1: |
|
156 self.upButton.setEnabled(False) |
|
157 else: |
|
158 self.upButton.setEnabled(True) |
|
159 self.downButton.setEnabled(True) |
|
160 |
|
161 @pyqtSlot() |
|
162 def on_fileButton_clicked(self): |
|
163 """ |
|
164 Private slot to handle the file selection via a file selection dialog. |
|
165 """ |
|
166 bookmark = QFileDialog.getOpenFileName() |
|
167 if bookmark: |
|
168 bookmark = Utilities.toNativeSeparators(bookmark) |
|
169 self.fileEdit.setText(bookmark) |
|
170 |
|
171 def getBookmarkedFiles(self): |
|
172 """ |
|
173 Public method to retrieve the tools list. |
|
174 |
|
175 @return a list of filenames (list of strings) |
|
176 """ |
|
177 return self.bookmarks |
|
178 |
|
179 def __swap(self, itm1, itm2): |
|
180 """ |
|
181 Private method used two swap two list entries given by their index. |
|
182 |
|
183 @param itm1 index of first entry (int) |
|
184 @param itm2 index of second entry (int) |
|
185 """ |
|
186 tmp = self.bookmarks[itm1] |
|
187 self.bookmarks[itm1] = self.bookmarks[itm2] |
|
188 self.bookmarks[itm2] = tmp |