-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainwindow.py
709 lines (577 loc) · 24.2 KB
/
mainwindow.py
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#!/usr/bin/python3
from argparse import ArgumentParser
import ctypes
from enum import Enum
import os
from os.path import basename
import sys
import traceback
from signal import signal, SIGINT, SIG_DFL
from PySide6.QtCore import QCoreApplication, Qt, Slot, QUrl
from PySide6.QtGui import QCloseEvent, QIcon, QDesktopServices, QImage, QShortcut
from PySide6.QtWidgets import (QApplication, QMainWindow, QMessageBox, QComboBox,
QFileDialog, QToolButton, QWidget, QDialog, QDialogButtonBox, QAbstractButton)
from ui.ui_mainwindow import Ui_MainWindow
from dialogs.errordialog import ErrorDialog
from dialogs.modifiedfilesdialog import ModifiedFilesDialog
from dialogs.newimagedialog import NewImageDialog
from dialogs.newmapdialog import NewMapDialog
from dialogs.projectpropertiesdialog import ProjectPropertiesDialog
from dialogs.settingswindow import SettingsWindow
from formats.spheremap import SphereMap
from qsiproject import QSIProject
from spherelauncher import SphereLauncher
from widgets.image.drawingview import DrawingView
from widgets.image.imageeditor import ImageEditor
from widgets.map.mapeditor import MapEditor
from widgets.sphereeditor import SphereEditor
from widgets.spriteset.spriteseteditor import SpritesetEditor
from widgets.startpage import StartPage
from widgets.textedit import TextEdit
from settings import Settings
_VERSION = "0.10"
_APPLICATION_NAME = "QtSphere IDE"
_ORG_NAME = "Spherical"
_ABOUT_STRING = f"""QtSphere IDE v{_VERSION}<br />
Copyright 2024 by <a href=\"https://github.com/eggbertx\">Eggbertx</a><br /><br />
See <a href=\"https://github.com/Eggbertx/QtSphere-IDE/blob/master/LICENSE.txt\">LICENSE.txt</a> for more information.
"""
_OPEN_DIALOG_FILTER = (
"All supported files (*.sgm *.txt *.js *.mjs *.cjs *.rmp *.rss *.rws)",
"Sphere projects (*.sgm)",
"Script files (*.js *.mjs, *.cjs, *.ts)",
"Text files (*.txt *.md)",
"Images (*.bmp *.gif *.jpeg *.jpg *.png)",
"Audio files (*.wav *.ogg *.mp3 *.flac *.it *.mod *.s3m *.xm)",
"Sphere fonts (*.rfn)",
"Sphere maps (*.rmp)",
"Spritesets (*.rss)",
"Windowstyles (*.rws)",
"All files (*.*)"
)
class FileType(Enum):
AllSupported = 0
Projects = 1
Script = 2
Text = 3
Image = 4
Audio = 5
SphereFont = 6
SphereMap = 7
Spriteset = 8
WindowStyle = 9
All = 10
class SidebarTab(Enum):
FileTree = 0
TaskList = 1
SoundTest = 2
class MainWindow(QMainWindow):
startPage: StartPage
engineSelector: QComboBox
settingsWindow: SettingsWindow
loadedProject: QSIProject
newImageDialog: NewImageDialog
newMapDialog: NewMapDialog
launcher: SphereLauncher
verbose: bool
projectPropertiesDialog: ProjectPropertiesDialog
modifiedFilesDialog: ModifiedFilesDialog
newButton: QToolButton
openFilePaths: list[str] # each element should correspond to the respective tab index, with Start Page (and any other non-file tabs) being None
@property
def currentTabWidget(self):
return self.ui.openFileTabs.currentWidget()
@property
def currentTabIndex(self):
return self.ui.openFileTabs.currentIndex()
def __init__(self, parent=None, verbose=False):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.installEventFilter(self)
self.switchSidebarTab(SidebarTab.FileTree)
self.verbose = verbose
self.settingsWindow = SettingsWindow(self)
self.ui.splitter.setStretchFactor(1, 4)
self.startPage = StartPage(self.ui.openFileTabs, printWarnings=self.verbose)
self.ui.openFileTabs.addTab(self.startPage, "Start Page")
self.openFilePaths = [None]
self.engineSelector = QComboBox(self.ui.mainToolBar)
self.engineSelector.setMinimumWidth(120)
self.engineSelector.setToolTip("Sphere engine")
self.engineSelector.addItem(QIcon(":/res/neosphere.png"), "neoSphere")
self.engineSelector.addItem(QIcon(":/res/legacyengine.png"),"Sphere 1.x")
self.ui.mainToolBar.addWidget(self.engineSelector)
self.engineSelector.setEnabled(False)
self.newButton = QToolButton()
self.newButton.setIcon(QIcon.fromTheme("document-new"))
self.newButton.setText("New file")
self.newButton.setPopupMode(QToolButton.ToolButtonPopupMode.InstantPopup)
self.newButton.setMenu(self.ui.menuNew)
self.ui.mainToolBar.insertWidget(self.ui.actionOpenFile, self.newButton)
self.loadedProject = None
self.launcher = SphereLauncher(self)
self.newImageDialog = NewImageDialog(self)
self.newMapDialog = NewMapDialog(self)
self.modifiedFilesDialog = ModifiedFilesDialog(self)
self.projectPropertiesDialog = ProjectPropertiesDialog(self, self.loadedProject)
self.ui.treeView.updateProject(self.loadedProject)
self._setupSettings()
self._connectActions()
if os.name == "nt":
# makes it so that our icon shows up correctly in the task bar instead of using pythonw.exe's icon
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(f"spherical.QtSphereIDE{_VERSION}")
def _setupSettings(self):
settings = Settings()
match settings.defaultEngine:
case "legacy":
self.engineSelector.setCurrentText("Sphere 1.x")
self.ui.actionLegacyConfig.setEnabled(True)
case _:
self.engineSelector.setCurrentText("neoSphere")
self.ui.actionLegacyConfig.setEnabled(False)
def _connectActions(self):
self.ui.actionExit.triggered.connect(sys.exit)
self.ui.actionAbout_Qt.triggered.connect(lambda: QMessageBox.aboutQt(self, "About Qt"))
self.ui.actionAbout.triggered.connect(lambda: QMessageBox.about(self, "About QtSphere IDE", _ABOUT_STRING))
self.ui.openFileTabs.tabCloseRequested.connect(self.onTabCloseRequested)
self.ui.openFileTabs.tabBar().tabMoved.connect(self.onTabMoved)
self.ui.actionConfigure_QtSphere_IDE.triggered.connect(self.onOpenSettingsWindowTriggered)
self.settingsWindow.settingsSaved.connect(self.onSettingsSaved)
self.startPage.projectLoaded.connect(lambda proj: self.loadProject(proj))
self.startPage.loadProjectAction.triggered.connect(self.onStartPageProjectSelected)
self.startPage.startGameAction.triggered.connect(self.onStartGameTriggered)
self.startPage.openProjectDirAction.triggered.connect(self.onOpenSelectedProjectDir)
self.engineSelector.currentIndexChanged.connect(self.onEngineChanged)
self.ui.actionClose.triggered.connect(self.closeProject)
self.ui.actionRefresh.triggered.connect(lambda: self.ui.treeView.updateProject(self.loadedProject))
self.ui.actionProject_Explorer.triggered.connect(lambda: self.switchSidebarTab(SidebarTab.FileTree))
self.ui.actionProject_Task_List.triggered.connect(lambda: self.switchSidebarTab(SidebarTab.TaskList))
self.ui.actionSound_Test.triggered.connect(lambda: self.switchSidebarTab(SidebarTab.SoundTest))
self.ui.actionSpherical_community.triggered.connect(lambda: QDesktopServices.openUrl("https://spheredev.org/"))
self.ui.actionQSIGithub.triggered.connect(lambda: QDesktopServices.openUrl("https://github.com/Eggbertx/QtSphere-IDE"))
self.ui.actionNSGithub.triggered.connect(lambda: QDesktopServices.openUrl("https://github.com/spheredev/neoSphere"))
self.ui.actionOpen_Game_Directory.triggered.connect(self._openCurrentProjectDir)
self.ui.treeView.fileItemActivated.connect(self.openFile)
self.ui.actionOpenFile.triggered.connect(self.onOpenFileTriggered)
self.ui.actionOpenProject.triggered.connect(self.onOpenProjectTriggered)
self.ui.openFileTabs.currentChanged.connect(self.onTabChanged)
self.ui.actionUndo.triggered.connect(self.onUndoTriggered)
self.ui.actionRedo.triggered.connect(self.onRedoTriggered)
self.ui.actionCut.triggered.connect(self.onCutTriggered)
self.ui.actionCopy.triggered.connect(self.onCopyTriggered)
self.ui.actionPaste.triggered.connect(self.onPasteTriggered)
self.ui.actionSelect_All.triggered.connect(self.onSelectAllTriggered)
self.ui.actionLegacyConfig.triggered.connect(self.launcher.runLegacyConfig)
self.ui.toolbarPlayGame.triggered.connect(self.onGameLaunched)
self.ui.newProject.triggered.connect(self.projectPropertiesDialog.show)
self.ui.newImage.triggered.connect(self.newImageDialog.show)
self.newImageDialog.accepted.connect(self.onNewImageAccepted)
self.ui.newMap.triggered.connect(self.newMapDialog.show)
self.newMapDialog.accepted.connect(self.onNewMapAccepted)
self.ui.actionProject_Properties.triggered.connect(self.projectPropertiesDialog.show)
self.ui.actionSave.triggered.connect(lambda: self.saveCurrentTab(False))
self.ui.actionSave_As.triggered.connect(lambda: self.saveCurrentTab(True))
self.ui.newPlainTextFile.triggered.connect(self.newTextFile)
self.modifiedFilesDialog.buttonBox.clicked.connect(self.onModifiedFileDialogButtonClicked)
QShortcut(Qt.Modifier.CTRL | Qt.Key.Key_PageDown, self, self.onNextTabTriggered)
QShortcut(Qt.Modifier.CTRL | Qt.Key.Key_PageUp, self, self.onPrevTabTriggered)
QShortcut(Qt.Modifier.CTRL | Qt.Key.Key_Tab, self, self.onNextTabTriggered)
QShortcut(Qt.Modifier.CTRL | Qt.Modifier.SHIFT | Qt.Key.Key_Tab, self, self.onPrevTabTriggered)
QShortcut(Qt.Modifier.CTRL | Qt.Key.Key_F4, self, self.onCurrentTabCloseRequested)
QShortcut(Qt.Modifier.CTRL | Qt.Key.Key_W, self, self.onCurrentTabCloseRequested)
def _openCurrentProjectDir(self):
if self.loadedProject is not None:
QDesktopServices.openUrl(self.loadedProject.projectDir)
def launchGame(self, game:QSIProject):
if game is None or game.buildDir is None or game.buildDir == "":
ErrorDialog.showError(self, "A game does not appear to be loaded", "Error launching game")
return
try:
self.launcher.launchGame(game)
except Exception as e:
ErrorDialog.showError(self, f"Unable to run game: {e}", "Error launching game")
def newTextFile(self):
editor = TextEdit(self.ui.openFileTabs)
editor.setModified(True)
self.openAndGoToNewEditorWidget(editor, "")
def openAndGoToNewEditorWidget(self, editor:QWidget, filePath:str):
self.openFilePaths.append(filePath)
t = self.ui.openFileTabs.addTab(editor, "<new file>" if filePath == "" else basename(filePath))
self.ui.openFileTabs.setCurrentIndex(t)
def openFile(self, filePath:str):
ext = ""
if filePath.count(".") > 0:
ext = filePath[filePath.rindex("."):]
try:
editor:SphereEditor = None
settings = Settings()
match ext.lower():
case ".rmp":
editor = MapEditor.openAndAttach(self.ui.openFileTabs, filePath)
case ".rss":
editor = SpritesetEditor.openAndAttach(self.ui.openFileTabs, filePath)
case ".rts":
self.newMapDialog.tilesetPath = filePath
if self.newMapDialog.exec() == QDialog.DialogCode.Rejected:
return
rmp = SphereMap.create(self.newMapDialog.tilesW, self.newMapDialog.tilesH, self.newMapDialog.tilesetPath)
editor = MapEditor(self.ui.openFileTabs)
editor.attachMap(rmp)
case ".txt"|".js"|".cjs"|".mjs"|".ts"|".md"|".sgm":
editor = TextEdit.openAndAttach(self.ui.openFileTabs, filePath)
case ".bmp"|".gif"|".jpeg"|".jpg"|".png":
editor = ImageEditor.openAndAttach(self.ui.openFileTabs, filePath)
case ".wav"|".ogg"|".mp3"|".flac"|".it"|".mod"|".s3m"|".xm":
self.ui.soundPlayer.load(filePath)
self.switchSidebarTab(SidebarTab.SoundTest)
return
case _:
if settings.unrecognizedFileEditor == "external":
QDesktopServices.openUrl(QUrl.fromLocalFile(filePath))
return
else:
editor = TextEdit.openAndAttach(self.ui.openFileTabs, filePath)
editor.modificationChanged.connect(self.onCurrentFileModificationChanged)
self.openAndGoToNewEditorWidget(editor, filePath)
except Exception as e:
errorDialog = QMessageBox(QMessageBox.Icon.Critical, "Error", str(e))
errorDialog.setInformativeText(traceback.format_exc())
errorDialog.show()
raise
def closeProject(self):
self.loadedProject = None
self.ui.treeView.updateProject(None)
self.setWindowTitle(f"QtSphere IDE {_VERSION}")
self.ui.menuProject.setEnabled(False)
self.engineSelector.setEnabled(False)
self.ui.actionProject_Properties.setEnabled(False)
self.ui.toolbarPlayGame.setEnabled(False)
self.newMapDialog.projectPath = None
def switchSidebarTab(self, tab:SidebarTab):
match tab:
case SidebarTab.FileTree:
self.ui.sideBar.tabBar().setCurrentIndex(0)
case SidebarTab.TaskList:
self.ui.sideBar.tabBar().setCurrentIndex(1)
case SidebarTab.SoundTest:
self.ui.sideBar.tabBar().setCurrentIndex(2)
def showOpenFileDialog(self, fileType: FileType = FileType.AllSupported, startDir:str = ".", title:str = "Open File") -> str|None:
result = QFileDialog.getOpenFileName(self, title, startDir,
";;".join(_OPEN_DIALOG_FILTER), _OPEN_DIALOG_FILTER[fileType.value])
return result[0] if len(result) > 0 and result[0] != "" else None
def showSaveFileDialog(self, fileType: FileType = FileType.AllSupported, startDir:str = ".", title:str = "Save File"):
result = QFileDialog.getSaveFileName(self, title, startDir,
";;".join(_OPEN_DIALOG_FILTER), _OPEN_DIALOG_FILTER[fileType.value])
return result[0] if len(result) > 0 and result[0] != "" else None
def saveCurrentTab(self, saveAs:bool):
index = self.currentTabIndex
filePath = self.openFilePaths[index]
if saveAs or filePath == "":
newFilePath = self.showSaveFileDialog()
if newFilePath is None:
return # cancelled
filePath = newFilePath
try:
activeWidget = self.currentTabWidget
if hasattr(activeWidget, "save"):
activeWidget.save(filePath)
elif activeWidget is not StartPage:
raise NotImplementedError(f"Saving has not been implemented yet for this editor ({type(activeWidget)})")
except Exception as e:
errorDialog = QMessageBox(QMessageBox.Icon.Critical, "Error", str(e))
errorDialog.setInformativeText(traceback.format_exc())
errorDialog.show()
raise
def currentTabIsModified(self):
active = self.currentTabWidget
return hasattr(active, "isModified") and active.isModified()
def setCurrentTabSetModified(self, modified:bool):
active = self.currentTabWidget
if hasattr(active, "setModified"):
active.setModified(modified)
def currentTabCanCutCopyPaste(self):
active = self.currentTabWidget
return hasattr(active, "cut") and hasattr(active, "copy") and hasattr(active, "paste")
def currentTabHasUndoRedo(self):
active = self.currentTabWidget
return hasattr(active, "undo") and hasattr(active, "redo")
def currentTabHasSelectAll(self):
return hasattr(self.currentTabWidget, "selectAll")
def showSaveConfirmationDialog(self):
msgbox = QMessageBox(self)
msgbox.setStandardButtons(QMessageBox.StandardButton.Save|QMessageBox.StandardButton.Discard|QMessageBox.StandardButton.Cancel)
QIcon.fromTheme("")
filename = basename(self.openFilePaths[self.currentTabIndex])
if filename == "":
filename = "The current file"
msgbox.setText(f"{filename} has been modified")
msgbox.setIcon(QMessageBox.Icon.Question)
msgbox.setInformativeText("Do you want to save your changes?")
return msgbox.exec()
def setStatus(self, status:str, timeout:int = 0):
if status is None or status == "":
self.ui.statusBar.clearMessage()
else:
self.ui.statusBar.showMessage(status, timeout)
def closeEvent(self, event: QCloseEvent):
tabs = self.ui.openFileTabs.count()
self.modifiedFilesDialog.clearPaths()
for t in range(tabs):
widget:SphereEditor = self.ui.openFileTabs.widget(t)
filePath = self.openFilePaths[t]
if filePath is not None and hasattr(widget, "isModified") and widget.isModified():
self.modifiedFilesDialog.addPath(filePath if filePath != "" else "<new file>")
if len(self.modifiedFilesDialog.paths) > 0:
self.modifiedFilesDialog.show()
event.ignore()
else:
self.launcher.process.kill()
super().closeEvent(event)
def getExtensionFilterIndex(self, ext:str) -> int:
match ext:
case ".js"|".cjs"|".mjs"|".ts":
return FileType.Script.value
case ".txt"|".md":
return FileType.Text.value
case ".wav"|".ogg"|".mp3"|".flac"|".it"|".mod"|".s3m"|".xm":
return FileType.Audio.value
case ".rfn":
return FileType.SphereFont.value
case ".rmp":
return FileType.SphereMap.value
case ".rss":
return FileType.Spriteset.value
case ".rws":
return FileType.WindowStyle.value
return FileType.All.value
def saveAllModified(self):
tabs = self.ui.openFileTabs.count()
for t in range(tabs):
editor:SphereEditor = self.ui.openFileTabs.widget(t)
filePath = self.openFilePaths[t]
if filePath is None or not hasattr(editor, "isModified") or not editor.isModified():
continue
ext = filePath[filePath.rindex("."):]
filter = self.getExtensionFilterIndex(ext)
saveFilename = self.showSaveFileDialog(filter, "." if self.loadedProject is None else self.loadedProject.projectDir)
if saveFilename is None:
# user clicked cancel in save dialog, abort exiting
return False
editor.save(saveFilename)
return True
def loadProject(self, project:QSIProject):
print("Loading project:", project.projectDir)
self.setWindowTitle(f"QtSphereIDE {_VERSION} - {project.name}")
self.ui.menuProject.setEnabled(True)
self.engineSelector.setEnabled(True)
self.ui.actionProject_Properties.setEnabled(True)
self.loadedProject = project
self.ui.toolbarPlayGame.setEnabled(True)
self.newMapDialog.projectPath = project.projectDir
self.projectPropertiesDialog.project = self.loadedProject
self.ui.treeView.updateProject(project)
self.switchSidebarTab(SidebarTab.FileTree)
#region Slots
@Slot(QAbstractButton)
def onModifiedFileDialogButtonClicked(self, btn:QAbstractButton):
match self.modifiedFilesDialog.buttonBox.standardButton(btn):
case QDialogButtonBox.StandardButton.SaveAll:
if self.saveAllModified():
QApplication.exit(0)
case QDialogButtonBox.StandardButton.Discard:
self.modifiedFilesDialog.close()
QApplication.exit(0)
case QDialogButtonBox.StandardButton.Cancel:
self.modifiedFilesDialog.close()
@Slot()
def onNewImageAccepted(self):
image = QImage(self.newImageDialog.imageWidth, self.newImageDialog.imageHeight,
QImage.Format.Format_RGBA8888)
color = self.newImageDialog.fillColor
color.setAlpha(self.newImageDialog.fillOpacity)
image.fill(self.newImageDialog.fillColor)
editor = ImageEditor(self.ui.openFileTabs, image)
self.openAndGoToNewEditorWidget(editor, "")
editor.modificationChanged.connect(self.onCurrentFileModificationChanged)
@Slot()
def onNewMapAccepted(self):
rmp = SphereMap.create(self.newMapDialog.tilesW, self.newMapDialog.tilesH, self.newMapDialog.tilesetPath)
editor = MapEditor(self.ui.openFileTabs)
editor.attachMap(rmp)
self.openAndGoToNewEditorWidget(editor, "")
editor.modificationChanged.connect(self.onCurrentFileModificationChanged)
@Slot()
def onGameLaunched(self):
self.launchGame(self.loadedProject)
@Slot(bool)
def onCurrentFileModificationChanged(self, modified:bool):
index = self.currentTabIndex
tabText = self.ui.openFileTabs.tabBar().tabText(index)
showsModified = tabText.endswith(" *")
if modified and not showsModified:
self.ui.openFileTabs.tabBar().setTabText(index, tabText + " *")
elif not modified and showsModified:
self.ui.openFileTabs.tabBar().setTabText(index, tabText[:-2])
@Slot(int)
def onTabChanged(self, index:int):
if index < 0:
return
hasUndoRedo = self.currentTabHasUndoRedo()
hasClipboard = self.currentTabCanCutCopyPaste()
self.ui.actionUndo.setEnabled(hasUndoRedo)
self.ui.actionRedo.setEnabled(hasUndoRedo)
self.ui.actionCut.setEnabled(hasClipboard)
self.ui.actionCopy.setEnabled(hasClipboard)
self.ui.actionPaste.setEnabled(hasClipboard)
self.ui.actionSelect_All.setEnabled(self.currentTabHasSelectAll())
index = self.currentTabIndex
if index < len(self.openFilePaths):
currentPath = self.openFilePaths[index]
self.ui.actionSave.setEnabled(currentPath is not None)
self.ui.actionSave_As.setEnabled(currentPath is not None)
@Slot()
def onNextTabTriggered(self):
next = self.currentTabIndex + 1 if self.currentTabIndex+1 < len(self.openFilePaths) else 0
self.ui.openFileTabs.setCurrentIndex(next)
@Slot()
def onPrevTabTriggered(self):
prev = self.currentTabIndex - 1 if self.currentTabIndex > 0 else len(self.openFilePaths) - 1
self.ui.openFileTabs.setCurrentIndex(prev)
@Slot()
def onFileSaveTriggered(self):
currentPath = self.openFilePaths[self.currentTabIndex]
if currentPath is None:
ErrorDialog.showError(self, "Invalid tab path. This should not normally happen", "Error")
return
@Slot()
def onUndoTriggered(self):
if self.currentTabHasUndoRedo():
self.currentTabWidget.undo()
def onRedoTriggered(self):
if self.currentTabHasUndoRedo():
self.currentTabWidget.redo()
@Slot()
def onCutTriggered(self):
if self.currentTabCanCutCopyPaste():
self.ui.openFileTabs.currentWidget().cut()
@Slot()
def onCopyTriggered(self):
if self.currentTabCanCutCopyPaste():
self.ui.openFileTabs.currentWidget().copy()
@Slot()
def onPasteTriggered(self):
if self.currentTabCanCutCopyPaste():
self.ui.openFileTabs.currentWidget().paste()
@Slot()
def onSelectAllTriggered(self):
if self.currentTabHasSelectAll():
self.ui.openFileTabs.currentWidget().selectAll()
@Slot()
def onSettingsSaved(self):
settings = Settings()
self.startPage.refreshGameList()
self.engineSelector.setCurrentIndex(1 if settings.defaultEngine == "legacy" else 0)
QApplication.setStyle(settings.theme)
@Slot()
def onOpenFileTriggered(self):
filePath = self.showOpenFileDialog(FileType.AllSupported,
self.loadedProject.projectDir if self.loadedProject is not None else ".")
if filePath is not None:
self.openFile(filePath)
@Slot()
def onOpenProjectTriggered(self):
settings = Settings()
projectDirs = settings.projectDirs
startDir = None if len(projectDirs) == 0 else projectDirs[0]
projectDir = QFileDialog.getExistingDirectory(self, "Selct project directory", startDir)
if projectDir == "" or projectDir is None:
return
newProject = QSIProject()
if newProject.open(projectDir):
self.loadProject(newProject)
else:
ErrorDialog.showError(self, "Unable to open project directory")
@Slot()
def onOpenSettingsWindowTriggered(self):
self.settingsWindow.loadSettings()
self.settingsWindow.show()
@Slot(int,int)
def onTabMoved(self, fromIndex:int, toIndex:int):
fromPath = self.openFilePaths.pop(fromIndex)
if toIndex < len(self.openFilePaths):
self.openFilePaths.insert(toIndex, fromPath)
else:
self.openFilePaths.append(fromPath)
@Slot()
def onCurrentTabCloseRequested(self):
self.ui.openFileTabs.tabCloseRequested.emit(self.ui.openFileTabs.currentIndex())
@Slot(int)
def onTabCloseRequested(self, index:int):
if self.currentTabIsModified():
match self.showSaveConfirmationDialog():
case QMessageBox.StandardButton.Save:
self.saveCurrentTab(False)
case QMessageBox.StandardButton.Discard:
pass
case QMessageBox.StandardButton.Cancel:
return
widget = self.ui.openFileTabs.widget(index)
if hasattr(widget, "modificationChanged"):
widget.modificationChanged.disconnect()
self.ui.openFileTabs.removeTab(index)
self.openFilePaths.pop(index)
if self.ui.openFileTabs.count() == 0:
# reopen the start page if there are no more tabs
self.ui.openFileTabs.addTab(self.startPage, "Start Page")
self.openFilePaths.append(None)
# self.startPage.refreshGameList()
@Slot(int)
def onEngineChanged(self, index:int):
settings = Settings()
match index:
case 0:
settings.defaultEngine ="neosphere"
self.ui.actionLegacyConfig.setEnabled(False)
case 1:
settings.defaultEngine ="legacy"
self.ui.actionLegacyConfig.setEnabled(True)
@Slot()
def onStartGameTriggered(self):
game = self.startPage.selectedGame()
if game is None:
return
self.launchGame(game)
@Slot()
def onStartPageProjectSelected(self):
selected = self.startPage.selectedGame()
if selected is None:
self.closeProject()
else:
self.loadProject(selected)
@Slot()
def onOpenSelectedProjectDir(self):
selected = self.startPage.selectedGame()
project = self.startPage.currentProject if selected is None else selected
QDesktopServices.openUrl(QUrl.fromLocalFile(project.projectDir))
#endregion
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--verbose", "-v",
action="store_true",
default=False,
help="If set, QtSphere IDE will print a warning when it finds a directory with no parseable game file (game.sgm, Cellscript.js, etc)")
args = parser.parse_args()
QCoreApplication.setApplicationName(_APPLICATION_NAME)
QCoreApplication.setOrganizationName(_ORG_NAME)
QCoreApplication.setApplicationVersion(_VERSION)
app = QApplication(sys.argv)
window = MainWindow(verbose=args.verbose)
settings = Settings()
app.setStyle(settings.theme)
if settings.maximized:
window.showMaximized()
else:
window.show()
signal(SIGINT, SIG_DFL)
sys.exit(app.exec())