예제 #1
0
파일: mainwindow.py 프로젝트: dVoCex/ts2
    def loadSimulation(self, fileName=None):
        """This is where stuff happens and the simulation is loaded

        """
        if fileName:

            # TODO check it exists and normalise path
            self.fileName = fileName

            QtWidgets.qApp.setOverrideCursor(Qt.WaitCursor)

            if self.simulation is not None:
                self.simulationDisconnect()
                self.simulation = None

            try:
                if zipfile.is_zipfile(fileName):
                    with zipfile.ZipFile(fileName) as zipArchive:
                        with zipArchive.open("simulation.json") as file:
                            self.simulation = simulation.load(self, file)
                else:
                    with open(fileName) as file:
                        self.simulation = simulation.load(self, file)
            except (utils.FormatException,
                    utils.MissingDependencyException) as err:
                QtWidgets.QMessageBox.critical(
                    self,
                    self.tr("Error while loading the simulation"),
                    str(err),
                    QtWidgets.QMessageBox.Ok
                )
                self.simulation = None
            except Exception as err:
                dialogs.ExceptionDialog.popupException(self, err)
                self.simulation = None
            else:
                self.setWindowTitle(self.tr(
                    "ts2 - Train Signalling Simulator - %s") % fileName)
                self.lblTitle.setText(self.simulation.option("title"))
                self.simulationConnect()
                self.simulationLoaded.emit(self.simulation)

                self.buttPause.toggled.connect(self.simulation.pause)
                self.buttPause.toggled.connect(self.setPauseButtonText)
                self.timeFactorSpinBox.valueChanged.connect(
                    self.simulation.setTimeFactor
                )
                self.timeFactorSpinBox.setValue(
                   float(self.simulation.option("timeFactor"))
                )
                settings.addRecent(fileName)
                self.refreshRecent()
                self.setControlsDisabled(False)
            finally:
                QtWidgets.QApplication.restoreOverrideCursor()
        else:
            self.onOpenSimulation()
예제 #2
0
    def loadSimulation(self, fileName=None):
        """This is where stuff happens and the simulation is loaded

        """
        if fileName:

            # TODO check it exists and normalise path
            self.fileName = fileName

            QtWidgets.qApp.setOverrideCursor(Qt.WaitCursor)

            if self.simulation is not None:
                self.simulationDisconnect()
                self.simulation = None

            try:
                if zipfile.is_zipfile(fileName):
                    with zipfile.ZipFile(fileName) as zipArchive:
                        with zipArchive.open("simulation.json") as file:
                            self.simulation = simulation.load(self, file)
                else:
                    with open(fileName) as file:
                        self.simulation = simulation.load(self, file)
            except (utils.FormatException,
                    utils.MissingDependencyException) as err:
                QtWidgets.QMessageBox.critical(
                    self, self.tr("Error while loading the simulation"),
                    str(err), QtWidgets.QMessageBox.Ok)
                self.simulation = None
            except Exception as err:
                dialogs.ExceptionDialog.popupException(self, err)
                self.simulation = None
            else:
                self.setWindowTitle(
                    self.tr("ts2 - Train Signalling Simulator - %s") %
                    fileName)
                self.lblTitle.setText(self.simulation.option("title"))
                self.simulationConnect()
                self.simulationLoaded.emit(self.simulation)

                self.buttPause.toggled.connect(self.simulation.pause)
                self.buttPause.toggled.connect(self.setPauseButtonText)
                self.timeFactorSpinBox.valueChanged.connect(
                    self.simulation.setTimeFactor)
                self.timeFactorSpinBox.setValue(
                    float(self.simulation.option("timeFactor")))
                settings.addRecent(fileName)
                self.refreshRecent()
                self.setControlsDisabled(False)
            finally:
                QtWidgets.QApplication.restoreOverrideCursor()
        else:
            self.onOpenSimulation()
예제 #3
0
 def saveGame(self):
     """Saves the current game to file."""
     if self.simulation is not None:
         fileName, _ = QtWidgets.QFileDialog.getSaveFileName(
             self, self.tr("Save the simulation as"),
             QtCore.QDir.homePath(), self.tr("TS2 game files (*.tsg)"))
         if fileName != "":
             QtWidgets.QApplication.setOverrideCursor(Qt.WaitCursor)
             # try:
             self.simulation.saveGame(fileName)
             # except:
             #     dialogs.ExceptionDialog.popupException(self)
             settings.addRecent(fileName)
             QtWidgets.QApplication.restoreOverrideCursor()
예제 #4
0
파일: mainwindow.py 프로젝트: dVoCex/ts2
 def saveGame(self):
     """Saves the current game to file."""
     if self.simulation is not None:
         self.panel.pauseButton.click()
         fileName, _ = QtWidgets.QFileDialog.getSaveFileName(
             self,
             self.tr("Save the simulation as"),
             QtCore.QDir.homePath(),
             self.tr("TS2 game files (*.tsg)")
         )
         if fileName != "":
             QtWidgets.QApplication.setOverrideCursor(Qt.WaitCursor)
             # try:
             self.simulation.saveGame(fileName)
             # except:
             #     dialogs.ExceptionDialog.popupException(self)
             settings.addRecent(fileName)
             QtWidgets.QApplication.restoreOverrideCursor()
예제 #5
0
    def loadSimulation(self, fileName=None):
        """This is where the simulation server is spawn"""
        if fileName:
            self.fileName = fileName
            if zipfile.is_zipfile(fileName):
                with zipfile.ZipFile(fileName) as zipArchive:
                    zipArchive.extract("simulation.json",
                                       path=tempfile.gettempdir())
                fileName = path.join(tempfile.gettempdir(), "simulation.json")

            QtWidgets.qApp.setOverrideCursor(Qt.WaitCursor)
            logLevel = "info"
            if settings.debug:
                logLevel = "dbug"

            if not self.simServer:
                cmd = settings.serverLoc
            else:
                cmd = self.simServer

            self.simulationClose()
            try:
                serverCmd = subprocess.Popen(
                    [cmd, "-loglevel", logLevel, fileName])
            except FileNotFoundError:
                QtWidgets.qApp.restoreOverrideCursor()
                QtWidgets.QMessageBox.critical(
                    self, "Configuration Error",
                    "ts2-sim-server executable not found in the server directory.\n"
                    "Go to File->Options to download it")
                raise
            except OSError as e:
                QtWidgets.qApp.restoreOverrideCursor()
                dialogs.ExceptionDialog.popupException(self, e)
                raise
            self.serverPID = serverCmd.pid
            settings.addRecent(self.fileName)
            time.sleep(1)
            QtWidgets.qApp.restoreOverrideCursor()
            self.connectToServer("localhost", "22222")
        else:
            self.onOpenSimulation()