21 |
21 |
22 class SessionManagerDialog(QDialog, Ui_SessionManagerDialog): |
22 class SessionManagerDialog(QDialog, Ui_SessionManagerDialog): |
23 """ |
23 """ |
24 Class implementing a dialog to manage sessions. |
24 Class implementing a dialog to manage sessions. |
25 """ |
25 """ |
|
26 |
26 SessionFileRole = Qt.ItemDataRole.UserRole |
27 SessionFileRole = Qt.ItemDataRole.UserRole |
27 BackupSessionRole = Qt.ItemDataRole.UserRole + 1 |
28 BackupSessionRole = Qt.ItemDataRole.UserRole + 1 |
28 ActiveSessionRole = Qt.ItemDataRole.UserRole + 2 |
29 ActiveSessionRole = Qt.ItemDataRole.UserRole + 2 |
29 DefaultSessionRole = Qt.ItemDataRole.UserRole + 3 |
30 DefaultSessionRole = Qt.ItemDataRole.UserRole + 3 |
30 |
31 |
31 def __init__(self, parent=None): |
32 def __init__(self, parent=None): |
32 """ |
33 """ |
33 Constructor |
34 Constructor |
34 |
35 |
35 @param parent reference to the parent widget |
36 @param parent reference to the parent widget |
36 @type QWidget |
37 @type QWidget |
37 """ |
38 """ |
38 super().__init__(parent) |
39 super().__init__(parent) |
39 self.setupUi(self) |
40 self.setupUi(self) |
40 self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose) |
41 self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose) |
41 |
42 |
42 self.newButton.clicked.connect(self.__newSession) |
43 self.newButton.clicked.connect(self.__newSession) |
43 self.renameButton.clicked.connect(self.__renameSession) |
44 self.renameButton.clicked.connect(self.__renameSession) |
44 self.cloneButton.clicked.connect(self.__cloneSession) |
45 self.cloneButton.clicked.connect(self.__cloneSession) |
45 self.deleteButton.clicked.connect(self.__deleteSession) |
46 self.deleteButton.clicked.connect(self.__deleteSession) |
46 self.switchButton.clicked.connect(self.__switchToSession) |
47 self.switchButton.clicked.connect(self.__switchToSession) |
47 self.sessionsList.currentItemChanged.connect(self.__updateButtons) |
48 self.sessionsList.currentItemChanged.connect(self.__updateButtons) |
48 |
49 |
49 self.__refresh() |
50 self.__refresh() |
50 WebBrowserWindow.sessionManager().sessionsMetaDataChanged.connect( |
51 WebBrowserWindow.sessionManager().sessionsMetaDataChanged.connect( |
51 self.__refresh) |
52 self.__refresh |
52 |
53 ) |
|
54 |
53 @pyqtSlot() |
55 @pyqtSlot() |
54 def __refresh(self): |
56 def __refresh(self): |
55 """ |
57 """ |
56 Private slot to refresh the list of sessions. |
58 Private slot to refresh the list of sessions. |
57 """ |
59 """ |
58 self.sessionsList.clear() |
60 self.sessionsList.clear() |
59 |
61 |
60 sessions = WebBrowserWindow.sessionManager().sessionMetaData( |
62 sessions = WebBrowserWindow.sessionManager().sessionMetaData( |
61 includeBackups=True) |
63 includeBackups=True |
|
64 ) |
62 for session in sessions: |
65 for session in sessions: |
63 if pathlib.Path(session.filePath).exists(): |
66 if pathlib.Path(session.filePath).exists(): |
64 itm = QTreeWidgetItem() |
67 itm = QTreeWidgetItem() |
65 itm.setText(0, session.name) |
68 itm.setText(0, session.name) |
66 itm.setText(1, datetime.datetime.fromtimestamp( |
69 itm.setText( |
67 pathlib.Path(session.filePath).stat().st_mtime) |
70 1, |
68 .isoformat(sep=" ", timespec="minutes")) |
71 datetime.datetime.fromtimestamp( |
69 itm.setData(0, SessionManagerDialog.SessionFileRole, |
72 pathlib.Path(session.filePath).stat().st_mtime |
70 session.filePath) |
73 ).isoformat(sep=" ", timespec="minutes"), |
71 itm.setData(0, SessionManagerDialog.BackupSessionRole, |
74 ) |
72 session.isBackup) |
75 itm.setData(0, SessionManagerDialog.SessionFileRole, session.filePath) |
73 itm.setData(0, SessionManagerDialog.ActiveSessionRole, |
76 itm.setData(0, SessionManagerDialog.BackupSessionRole, session.isBackup) |
74 session.isActive) |
77 itm.setData(0, SessionManagerDialog.ActiveSessionRole, session.isActive) |
75 itm.setData(0, SessionManagerDialog.DefaultSessionRole, |
78 itm.setData( |
76 session.isDefault) |
79 0, SessionManagerDialog.DefaultSessionRole, session.isDefault |
|
80 ) |
77 self.__updateSessionItem(itm) |
81 self.__updateSessionItem(itm) |
78 self.sessionsList.addTopLevelItem(itm) |
82 self.sessionsList.addTopLevelItem(itm) |
79 |
83 |
80 self.__updateButtons() |
84 self.__updateButtons() |
81 |
85 |
82 def __updateButtons(self): |
86 def __updateButtons(self): |
83 """ |
87 """ |
84 Private method to update the button state. |
88 Private method to update the button state. |
85 """ |
89 """ |
86 itm = self.sessionsList.currentItem() |
90 itm = self.sessionsList.currentItem() |
87 if itm: |
91 if itm: |
88 isBackup = itm.data(0, SessionManagerDialog.BackupSessionRole) |
92 isBackup = itm.data(0, SessionManagerDialog.BackupSessionRole) |
89 isActive = itm.data(0, SessionManagerDialog.ActiveSessionRole) |
93 isActive = itm.data(0, SessionManagerDialog.ActiveSessionRole) |
90 isDefault = itm.data(0, SessionManagerDialog.DefaultSessionRole) |
94 isDefault = itm.data(0, SessionManagerDialog.DefaultSessionRole) |
91 |
95 |
92 self.renameButton.setEnabled(not isDefault and not isBackup) |
96 self.renameButton.setEnabled(not isDefault and not isBackup) |
93 self.cloneButton.setEnabled(not isBackup) |
97 self.cloneButton.setEnabled(not isBackup) |
94 self.deleteButton.setEnabled(not isBackup and not isDefault and |
98 self.deleteButton.setEnabled( |
95 not isActive) |
99 not isBackup and not isDefault and not isActive |
|
100 ) |
96 self.switchButton.setEnabled(not isActive) |
101 self.switchButton.setEnabled(not isActive) |
97 if isBackup: |
102 if isBackup: |
98 self.switchButton.setText(self.tr("Restore")) |
103 self.switchButton.setText(self.tr("Restore")) |
99 else: |
104 else: |
100 self.switchButton.setText(self.tr("Switch To")) |
105 self.switchButton.setText(self.tr("Switch To")) |
102 self.renameButton.setEnabled(False) |
107 self.renameButton.setEnabled(False) |
103 self.cloneButton.setEnabled(False) |
108 self.cloneButton.setEnabled(False) |
104 self.deleteButton.setEnabled(False) |
109 self.deleteButton.setEnabled(False) |
105 self.switchButton.setEnabled(False) |
110 self.switchButton.setEnabled(False) |
106 self.switchButton.setText(self.tr("Switch To")) |
111 self.switchButton.setText(self.tr("Switch To")) |
107 |
112 |
108 def __updateSessionItem(self, itm): |
113 def __updateSessionItem(self, itm): |
109 """ |
114 """ |
110 Private method to set various item properties. |
115 Private method to set various item properties. |
111 |
116 |
112 @param itm reference to the item to be updated |
117 @param itm reference to the item to be updated |
113 @type QTreeWidgetItem |
118 @type QTreeWidgetItem |
114 """ |
119 """ |
115 isBackup = itm.data(0, SessionManagerDialog.BackupSessionRole) |
120 isBackup = itm.data(0, SessionManagerDialog.BackupSessionRole) |
116 isActive = itm.data(0, SessionManagerDialog.ActiveSessionRole) |
121 isActive = itm.data(0, SessionManagerDialog.ActiveSessionRole) |
117 isDefault = itm.data(0, SessionManagerDialog.DefaultSessionRole) |
122 isDefault = itm.data(0, SessionManagerDialog.DefaultSessionRole) |
118 |
123 |
119 font = itm.font(0) |
124 font = itm.font(0) |
120 |
125 |
121 if isBackup: |
126 if isBackup: |
122 color = self.palette().color(QPalette.ColorGroup.Disabled, |
127 color = self.palette().color( |
123 QPalette.ColorRole.WindowText) |
128 QPalette.ColorGroup.Disabled, QPalette.ColorRole.WindowText |
|
129 ) |
124 itm.setForeground(0, color) |
130 itm.setForeground(0, color) |
125 itm.setForeground(1, color) |
131 itm.setForeground(1, color) |
126 |
132 |
127 if isActive: |
133 if isActive: |
128 font.setBold(True) |
134 font.setBold(True) |
129 itm.setFont(0, font) |
135 itm.setFont(0, font) |
130 itm.setFont(1, font) |
136 itm.setFont(1, font) |
131 |
137 |
132 if isDefault: |
138 if isDefault: |
133 font.setItalic(True) |
139 font.setItalic(True) |
134 itm.setFont(0, font) |
140 itm.setFont(0, font) |
135 itm.setFont(1, font) |
141 itm.setFont(1, font) |
136 |
142 |
137 def showEvent(self, evt): |
143 def showEvent(self, evt): |
138 """ |
144 """ |
139 Protected method handling the dialog being shown. |
145 Protected method handling the dialog being shown. |
140 |
146 |
141 @param evt reference to the event object |
147 @param evt reference to the event object |
142 @type QShowEvent |
148 @type QShowEvent |
143 """ |
149 """ |
144 super().showEvent(evt) |
150 super().showEvent(evt) |
145 self.__resizeViewHeader() |
151 self.__resizeViewHeader() |
146 |
152 |
147 def resizeEvent(self, evt): |
153 def resizeEvent(self, evt): |
148 """ |
154 """ |
149 Protected method handling the dialog being resized. |
155 Protected method handling the dialog being resized. |
150 |
156 |
151 @param evt reference to the event object |
157 @param evt reference to the event object |
152 @type QResizeEvent |
158 @type QResizeEvent |
153 """ |
159 """ |
154 super().resizeEvent(evt) |
160 super().resizeEvent(evt) |
155 self.__resizeViewHeader() |
161 self.__resizeViewHeader() |
156 |
162 |
157 def __resizeViewHeader(self): |
163 def __resizeViewHeader(self): |
158 """ |
164 """ |
159 Private method to resize the session column of the list. |
165 Private method to resize the session column of the list. |
160 """ |
166 """ |
161 headerWidth = self.sessionsList.header().width() |
167 headerWidth = self.sessionsList.header().width() |
162 self.sessionsList.header().resizeSection( |
168 self.sessionsList.header().resizeSection( |
163 0, int(headerWidth - headerWidth / 2.5)) |
169 0, int(headerWidth - headerWidth / 2.5) |
164 |
170 ) |
|
171 |
165 @pyqtSlot() |
172 @pyqtSlot() |
166 def __newSession(self): |
173 def __newSession(self): |
167 """ |
174 """ |
168 Private slot to create a new session. |
175 Private slot to create a new session. |
169 """ |
176 """ |
170 WebBrowserWindow.sessionManager().newSession() |
177 WebBrowserWindow.sessionManager().newSession() |
171 |
178 |
172 @pyqtSlot() |
179 @pyqtSlot() |
173 def __renameSession(self): |
180 def __renameSession(self): |
174 """ |
181 """ |
175 Private slot to rename the selected session. |
182 Private slot to rename the selected session. |
176 """ |
183 """ |
177 itm = self.sessionsList.currentItem() |
184 itm = self.sessionsList.currentItem() |
178 if itm is None: |
185 if itm is None: |
179 return |
186 return |
180 |
187 |
181 filePath = itm.data(0, SessionManagerDialog.SessionFileRole) |
188 filePath = itm.data(0, SessionManagerDialog.SessionFileRole) |
182 if filePath: |
189 if filePath: |
183 WebBrowserWindow.sessionManager().renameSession(filePath) |
190 WebBrowserWindow.sessionManager().renameSession(filePath) |
184 |
191 |
185 @pyqtSlot() |
192 @pyqtSlot() |
186 def __cloneSession(self): |
193 def __cloneSession(self): |
187 """ |
194 """ |
188 Private slot to clone the selected session. |
195 Private slot to clone the selected session. |
189 """ |
196 """ |
190 itm = self.sessionsList.currentItem() |
197 itm = self.sessionsList.currentItem() |
191 if itm is None: |
198 if itm is None: |
192 return |
199 return |
193 |
200 |
194 filePath = itm.data(0, SessionManagerDialog.SessionFileRole) |
201 filePath = itm.data(0, SessionManagerDialog.SessionFileRole) |
195 if filePath: |
202 if filePath: |
196 WebBrowserWindow.sessionManager().cloneSession(filePath) |
203 WebBrowserWindow.sessionManager().cloneSession(filePath) |
197 |
204 |
198 @pyqtSlot() |
205 @pyqtSlot() |
199 def __deleteSession(self): |
206 def __deleteSession(self): |
200 """ |
207 """ |
201 Private slot to delete the selected session. |
208 Private slot to delete the selected session. |
202 """ |
209 """ |
203 itm = self.sessionsList.currentItem() |
210 itm = self.sessionsList.currentItem() |
204 if itm is None: |
211 if itm is None: |
205 return |
212 return |
206 |
213 |
207 filePath = itm.data(0, SessionManagerDialog.SessionFileRole) |
214 filePath = itm.data(0, SessionManagerDialog.SessionFileRole) |
208 if filePath: |
215 if filePath: |
209 WebBrowserWindow.sessionManager().deleteSession(filePath) |
216 WebBrowserWindow.sessionManager().deleteSession(filePath) |
210 |
217 |
211 @pyqtSlot() |
218 @pyqtSlot() |
212 def __switchToSession(self): |
219 def __switchToSession(self): |
213 """ |
220 """ |
214 Private slot to switch to the selected session. |
221 Private slot to switch to the selected session. |
215 """ |
222 """ |
216 itm = self.sessionsList.currentItem() |
223 itm = self.sessionsList.currentItem() |
217 if itm is None: |
224 if itm is None: |
218 return |
225 return |
219 |
226 |
220 filePath = itm.data(0, SessionManagerDialog.SessionFileRole) |
227 filePath = itm.data(0, SessionManagerDialog.SessionFileRole) |
221 if filePath: |
228 if filePath: |
222 if itm.data(0, SessionManagerDialog.BackupSessionRole): |
229 if itm.data(0, SessionManagerDialog.BackupSessionRole): |
223 res = ( |
230 res = WebBrowserWindow.sessionManager().replaceSession(filePath) |
224 WebBrowserWindow.sessionManager().replaceSession(filePath) |
|
225 ) |
|
226 else: |
231 else: |
227 res = ( |
232 res = WebBrowserWindow.sessionManager().switchToSession(filePath) |
228 WebBrowserWindow.sessionManager().switchToSession(filePath) |
233 |
229 ) |
|
230 |
|
231 if res: |
234 if res: |
232 self.close() |
235 self.close() |