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