-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.py
More file actions
426 lines (324 loc) · 18.6 KB
/
window.py
File metadata and controls
426 lines (324 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# This Python file uses the following encoding: utf-8
#
# Copyright 2022 naracanto, <https://naracanto.github.io>.
#
# This file is part of dwStudio, <https://github.com/naracanto/dwstudio>.
#
# dwStudio is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# dwStudio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dwStudio. If not, see <https://www.gnu.org/licenses/>.
#
from PySide6.QtCore import QByteArray, QSettings, Qt
from PySide6.QtGui import QAction, QActionGroup, QIcon, QKeySequence
from PySide6.QtWidgets import QApplication, QMainWindow, QMenu
from about_dialog import AboutDialog
from colophon_dialog import ColophonDialog
from preferences_dialog import PreferencesDialog
import icons_rc
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowIcon(QIcon(":/icons/apps/16/dwstudio.svg"))
self._createActions()
self._createMenuBar()
self._createStatusBar()
self._createToolBars()
self._loadSettings()
self._updateActionFullScreen()
def closeEvent(self, event):
if True:
# Store application properties
self._saveSettings()
event.accept()
else:
event.ignore()
def _loadSettings(self):
settings = QSettings()
# Application property: Geometry
geometry = settings.value("Application/Geometry", QByteArray())
if not geometry.isEmpty():
self.restoreGeometry(geometry)
else:
# Default: Center window
availableGeometry = self.screen().availableGeometry()
self.resize(availableGeometry.width() * 2/3, availableGeometry.height() * 2/3)
self.move((availableGeometry.width() - self.width()) / 2, (availableGeometry.height() - self.height()) / 2)
# Application property: State
state = settings.value("Application/State", QByteArray())
if not state.isEmpty():
self.restoreState(state)
else:
# Default: Show/hide toolbars
self._toolbarApplication.setVisible(True)
self._toolbarFile.setVisible(True)
self._toolbarEdit.setVisible(True)
self._toolbarView.setVisible(True)
self._toolbarAppearance.setVisible(False)
self._toolbarHelp.setVisible(False)
# Application property: Menu Bar
visibleMenuBar = settings.value("Application/MenuBar", True, type=bool)
self.menuBar().setVisible(visibleMenuBar)
self._actionMenubar.setChecked(visibleMenuBar)
# Application property: Status Bar
visible = settings.value("Application/StatusBar", True, type=bool)
self._statusbar.setVisible(visible)
self._actionStatusbar.setChecked(visible)
# Application property: Tool Button Style
style = settings.value("Application/ToolButtonStyle", Qt.ToolButtonFollowStyle, type=int)
self._updateActionsToolButtonStyle(Qt.ToolButtonStyle(style))
def _saveSettings(self):
settings = QSettings()
# Application property: Geometry
geometry = self.saveGeometry()
settings.setValue("Application/Geometry", geometry)
# Application property: State
state = self.saveState()
settings.setValue("Application/State", state)
# Application property: Menu Bar
visibleMenuBar = self.menuBar().isVisible()
settings.setValue("Application/MenuBar", visibleMenuBar)
# Application property: Status Bar
visible = self._statusbar.isVisible()
settings.setValue("Application/StatusBar", visible)
# Application property: Tool Button Style
style = self._actionsToolButtonStyle.checkedAction().data()
settings.setValue("Application/ToolButtonStyle", style)
def _createActions(self):
#
# Actions: Application
self._actionAbout = QAction(self.tr("About {0}").format(QApplication.applicationName()), self)
self._actionAbout.setObjectName("actionAbout")
self._actionAbout.setIcon(QIcon(":/icons/apps/16/dwstudio.svg"))
self._actionAbout.setIconText(self.tr("About"))
self._actionAbout.setToolTip(self.tr("Brief description of the application"))
self._actionAbout.triggered.connect(self._onActionAboutTriggered)
self._actionColophon = QAction(self.tr("Colophon"), self)
self._actionColophon.setObjectName("actionColophon")
self._actionColophon.setToolTip(self.tr("Lengthy description of the application"))
self._actionColophon.triggered.connect(self._onActionColophonTriggered)
self._actionPreferences = QAction(self.tr("Preferences…"), self)
self._actionPreferences.setObjectName("actionPreferences")
self._actionPreferences.setIcon(QIcon.fromTheme("configure", QIcon(":/icons/actions/16/configure.svg")))
self._actionPreferences.setToolTip(self.tr("Customize the appearance and behavior of the application"))
self._actionPreferences.triggered.connect(self._onActionPreferencesTriggered)
self._actionQuit = QAction(self.tr("Quit"), self)
self._actionQuit.setObjectName("actionQuit")
self._actionQuit.setIcon(QIcon.fromTheme("application-exit", QIcon(":/icons/actions/16/application-exit.svg")))
self._actionQuit.setShortcut(QKeySequence.Quit)
self._actionQuit.setToolTip(self.tr("Quit the application"))
self._actionQuit.triggered.connect(self.close)
self.addAction(self._actionQuit)
#
# Actions: Appearance
self._actionMenubar = QAction(self.tr("Show Menu Bar"), self)
self._actionMenubar.setObjectName("actionMenubar")
self._actionMenubar.setCheckable(True)
self._actionMenubar.setIcon(QIcon.fromTheme("show-menu", QIcon(":/icons/actions/16/show-menu.svg")))
self._actionMenubar.setIconText("Menu Bar")
self._actionMenubar.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_M))
self._actionMenubar.setToolTip(self.tr("Display the Menu bar"))
self._actionMenubar.toggled.connect(lambda checked: self.menuBar().setVisible(checked))
self.addAction(self._actionMenubar)
self._actionToolbarApplication = QAction(self.tr("Show Application Toolbar"), self)
self._actionToolbarApplication.setObjectName("actionToolbarApplication")
self._actionToolbarApplication.setCheckable(True)
self._actionToolbarApplication.setToolTip(self.tr("Display the Application toolbar"))
self._actionToolbarApplication.toggled.connect(lambda checked: self._toolbarApplication.setVisible(checked))
self._actionToolbarFile = QAction(self.tr("Show File Toolbar"), self)
self._actionToolbarFile.setObjectName("actionToolbarFile")
self._actionToolbarFile.setCheckable(True)
self._actionToolbarFile.setToolTip(self.tr("Display the File toolbar"))
self._actionToolbarFile.toggled.connect(lambda checked: self._toolbarFile.setVisible(checked))
self._actionToolbarEdit = QAction(self.tr("Show Edit Toolbar"), self)
self._actionToolbarEdit.setObjectName("actionToolbarEdit")
self._actionToolbarEdit.setCheckable(True)
self._actionToolbarEdit.setToolTip(self.tr("Display the Edit toolbar"))
self._actionToolbarEdit.toggled.connect(lambda checked: self._toolbarEdit.setVisible(checked))
self._actionToolbarView = QAction(self.tr("Show View Toolbar"), self)
self._actionToolbarView.setObjectName("actionToolbarView")
self._actionToolbarView.setCheckable(True)
self._actionToolbarView.setToolTip(self.tr("Display the View toolbar"))
self._actionToolbarView.toggled.connect(lambda checked: self._toolbarView.setVisible(checked))
self._actionToolbarAppearance = QAction(self.tr("Show Appearance Toolbar"), self)
self._actionToolbarAppearance.setObjectName("actionToolbarAppearance")
self._actionToolbarAppearance.setCheckable(True)
self._actionToolbarAppearance.setToolTip(self.tr("Display the Appearance toolbar"))
self._actionToolbarAppearance.toggled.connect(lambda checked: self._toolbarAppearance.setVisible(checked))
self._actionToolbarHelp = QAction(self.tr("Show Help Toolbar"), self)
self._actionToolbarHelp.setObjectName("actionToolbarHelp")
self._actionToolbarHelp.setCheckable(True)
self._actionToolbarHelp.setToolTip(self.tr("Display the Help toolbar"))
self._actionToolbarHelp.toggled.connect(lambda checked: self._toolbarHelp.setVisible(checked))
self._actionStatusbar = QAction(self.tr("Show Status Bar"), self)
self._actionStatusbar.setObjectName("actionStatusbar")
self._actionStatusbar.setCheckable(True)
self._actionStatusbar.setToolTip(self.tr("Display the Status bar"))
self._actionStatusbar.toggled.connect(lambda checked: self._statusbar.setVisible(checked))
self._actionFullScreen = QAction(self)
self._actionFullScreen.setObjectName("actionFullScreen")
self._actionFullScreen.setCheckable(True)
self._actionFullScreen.setIconText(self.tr("Full Screen"))
self._actionFullScreen.setShortcut(QKeySequence.FullScreen)
self._actionFullScreen.triggered.connect(self._onActionFullScreenTriggered)
self.addAction(self._actionFullScreen)
#
# Action group: Tool Button Style
actionToolButtonStyleIconOnly = QAction(self.tr("Icon Only"), self)
actionToolButtonStyleIconOnly.setObjectName("actionToolButtonStyleIconOnly")
actionToolButtonStyleIconOnly.setCheckable(True)
actionToolButtonStyleIconOnly.setToolTip(self.tr("Only display the icon"))
actionToolButtonStyleIconOnly.setData(Qt.ToolButtonIconOnly)
actionToolButtonStyleTextOnly = QAction(self.tr("Text Only"), self)
actionToolButtonStyleTextOnly.setObjectName("actionToolButtonStyleTextOnly")
actionToolButtonStyleTextOnly.setCheckable(True)
actionToolButtonStyleTextOnly.setToolTip(self.tr("Only display the text"))
actionToolButtonStyleTextOnly.setData(Qt.ToolButtonTextOnly)
actionToolButtonStyleTextBesideIcon = QAction(self.tr("Text Beside Icon"), self)
actionToolButtonStyleTextBesideIcon.setObjectName("actionToolButtonStyleTextBesideIcon")
actionToolButtonStyleTextBesideIcon.setCheckable(True)
actionToolButtonStyleTextBesideIcon.setToolTip(self.tr("The text appears beside the icon"))
actionToolButtonStyleTextBesideIcon.setData(Qt.ToolButtonTextBesideIcon)
actionToolButtonStyleTextUnderIcon = QAction(self.tr("Text Under Icon"), self)
actionToolButtonStyleTextUnderIcon.setObjectName("actionToolButtonStyleTextUnderIcon")
actionToolButtonStyleTextUnderIcon.setCheckable(True)
actionToolButtonStyleTextUnderIcon.setToolTip(self.tr("The text appears under the icon"))
actionToolButtonStyleTextUnderIcon.setData(Qt.ToolButtonTextUnderIcon)
actionToolButtonStyleFollowStyle = QAction(self.tr("Follow Style"), self)
actionToolButtonStyleFollowStyle.setObjectName("actionToolButtonStyleFollowStyle")
actionToolButtonStyleFollowStyle.setCheckable(True)
actionToolButtonStyleFollowStyle.setToolTip(self.tr("Follow the style"))
actionToolButtonStyleFollowStyle.setData(Qt.ToolButtonFollowStyle)
self._actionsToolButtonStyle = QActionGroup(self)
self._actionsToolButtonStyle.setObjectName("actionsToolButtonStyle")
self._actionsToolButtonStyle.addAction(actionToolButtonStyleIconOnly)
self._actionsToolButtonStyle.addAction(actionToolButtonStyleTextOnly)
self._actionsToolButtonStyle.addAction(actionToolButtonStyleTextBesideIcon)
self._actionsToolButtonStyle.addAction(actionToolButtonStyleTextUnderIcon)
self._actionsToolButtonStyle.addAction(actionToolButtonStyleFollowStyle)
self._actionsToolButtonStyle.triggered.connect(self._onActionsToolButtonStyleTriggered)
def _createMenuBar(self):
# Menu: Application
menuApplication = self.menuBar().addMenu(self.tr("Application"))
menuApplication.setObjectName("menuApplication")
menuApplication.addAction(self._actionAbout)
menuApplication.addAction(self._actionColophon)
menuApplication.addSeparator()
menuApplication.addAction(self._actionPreferences)
menuApplication.addSeparator()
menuApplication.addAction(self._actionQuit)
# Menu: File
menuFile = self.menuBar().addMenu(self.tr("File"))
menuFile.setObjectName("menuFile")
# Menu: Edit
menuEdit = self.menuBar().addMenu(self.tr("Edit"))
menuEdit.setObjectName("menuEdit")
# Menu: View
menuView = self.menuBar().addMenu(self.tr("View"))
menuView.setObjectName("menuView")
#
# Menus: Appearance
menuToolButtonStyle = QMenu(self.tr("Tool Button Style"), self)
menuToolButtonStyle.setObjectName("menuToolButtonStyle")
menuToolButtonStyle.addActions(self._actionsToolButtonStyle.actions())
menuAppearance = self.menuBar().addMenu(self.tr("Appearance"))
menuAppearance.setObjectName("menuAppearance")
menuAppearance.addAction(self._actionMenubar)
menuAppearance.addSeparator()
menuAppearance.addAction(self._actionToolbarApplication)
menuAppearance.addAction(self._actionToolbarFile)
menuAppearance.addAction(self._actionToolbarEdit)
menuAppearance.addAction(self._actionToolbarView)
menuAppearance.addAction(self._actionToolbarAppearance)
menuAppearance.addAction(self._actionToolbarHelp)
menuAppearance.addMenu(menuToolButtonStyle)
menuAppearance.addSeparator()
menuAppearance.addAction(self._actionStatusbar)
menuAppearance.addSeparator()
menuAppearance.addAction(self._actionFullScreen)
# Menu: Help
menuHelp = self.menuBar().addMenu(self.tr("Help"))
menuHelp.setObjectName("menuHelp")
def _createStatusBar(self):
self._statusbar = self.statusBar()
self._statusbar.showMessage(self.tr("Ready"), 3000)
def _createToolBars(self):
# Toolbar: Application
self._toolbarApplication = self.addToolBar(self.tr("Application Toolbar"))
self._toolbarApplication.setObjectName("toolbarApplication")
self._toolbarApplication.addAction(self._actionAbout)
self._toolbarApplication.addAction(self._actionPreferences)
self._toolbarApplication.addSeparator()
self._toolbarApplication.addAction(self._actionQuit)
self._toolbarApplication.visibilityChanged.connect(lambda visible: self._actionToolbarApplication.setChecked(visible))
# Toolbar: File
self._toolbarFile = self.addToolBar(self.tr("File Toolbar"))
self._toolbarFile.setObjectName("toolbarFile")
self._toolbarFile.visibilityChanged.connect(lambda visible: self._actionToolbarFile.setChecked(visible))
# Toolbar: Edit
self._toolbarEdit = self.addToolBar(self.tr("Edit Toolbar"))
self._toolbarEdit.setObjectName("toolbarEdit")
self._toolbarEdit.visibilityChanged.connect(lambda visible: self._actionToolbarEdit.setChecked(visible))
# Toolbar: View
self._toolbarView = self.addToolBar(self.tr("View Toolbar"))
self._toolbarView.setObjectName("toolbarView")
self._toolbarView.visibilityChanged.connect(lambda visible: self._actionToolbarView.setChecked(visible))
# Toolbar: Appearance
self._toolbarAppearance = self.addToolBar(self.tr("Appearance Toolbar"))
self._toolbarAppearance.setObjectName("toolbarAppearance")
self._toolbarAppearance.addAction(self._actionMenubar)
self._toolbarAppearance.addSeparator()
self._toolbarAppearance.addAction(self._actionFullScreen)
self._toolbarAppearance.visibilityChanged.connect(lambda visible: self._actionToolbarAppearance.setChecked(visible))
# Toolbar: Help
self._toolbarHelp = self.addToolBar(self.tr("Help Toolbar"))
self._toolbarHelp.setObjectName("toolbarHelp")
self._toolbarHelp.visibilityChanged.connect(lambda visible: self._actionToolbarHelp.setChecked(visible))
def _updateActionsToolButtonStyle(self, toolButtonStyle):
for action in self._actionsToolButtonStyle.actions():
if Qt.ToolButtonStyle(action.data()) == toolButtonStyle:
action.setChecked(True)
self._onActionsToolButtonStyleTriggered(action)
break
def _updateActionFullScreen(self):
if not self.isFullScreen():
self._actionFullScreen.setText(self.tr("Full Screen Mode"))
self._actionFullScreen.setIcon(QIcon.fromTheme("view-fullscreen", QIcon(":/icons/actions/16/view-fullscreen.svg")))
self._actionFullScreen.setChecked(False)
self._actionFullScreen.setToolTip(self.tr("Display the window in full screen"))
else:
self._actionFullScreen.setText(self.tr("Exit Full Screen Mode"))
self._actionFullScreen.setIcon(QIcon.fromTheme("view-restore", QIcon(":/icons/actions/16/view-restore.svg")))
self._actionFullScreen.setChecked(True)
self._actionFullScreen.setToolTip(self.tr("Exit the full screen mode"))
def _onActionAboutTriggered(self):
dialog = AboutDialog(self)
dialog.open()
def _onActionColophonTriggered(self):
dialog = ColophonDialog(self)
dialog.open()
def _onActionPreferencesTriggered(self):
dialog = PreferencesDialog(self)
dialog.open()
def _onActionsToolButtonStyleTriggered(self, actionToolButtonStyle):
style = Qt.ToolButtonStyle(actionToolButtonStyle.data())
self._toolbarApplication.setToolButtonStyle(style)
self._toolbarFile.setToolButtonStyle(style)
self._toolbarEdit.setToolButtonStyle(style)
self._toolbarView.setToolButtonStyle(style)
self._toolbarAppearance.setToolButtonStyle(style)
self._toolbarHelp.setToolButtonStyle(style)
def _onActionFullScreenTriggered(self):
if not self.isFullScreen():
self.setWindowState(self.windowState() | Qt.WindowFullScreen)
else:
self.setWindowState(self.windowState() & ~Qt.WindowFullScreen)
self._updateActionFullScreen()