def onLanguageChange(self): languageName = self._langBox.currentText() if pyzo.config.settings.language == languageName: return # Save new language pyzo.config.settings.language = languageName setLanguage(pyzo.config.settings.language) # Notify user text = translate( 'wizard', """ The language has been changed for this wizard. Pyzo needs to restart for the change to take effect application-wide. """) m = QtGui.QMessageBox(self) m.setWindowTitle(translate("wizard", "Language changed")) m.setText(text) m.setIcon(m.Information) m.exec_() # Get props of current wizard geo = self.wizard().geometry() parent = self.wizard().parent() # Close ourself! self.wizard().close() # Start new one w = PyzoWizard(parent) w.setGeometry(geo) w.show()
def saveFile(self, editor=None, filename=None): """ Save the file. returns: True if succesfull, False if fails """ # get editor if editor is None: editor = self.getCurrentEditor() elif isinstance(editor, int): index = editor editor = None if index >= 0: item = self._tabs.items()[index] editor = item.editor if editor is None: return False # get filename if filename is None: filename = editor._filename if not filename: return self.saveFileAs(editor) # let the editor do the low level stuff... try: editor.save(filename) except Exception as err: # Notify in logger print("Error saving file:", err) # Make sure the user knows m = QtGui.QMessageBox(self) m.setWindowTitle("Error saving file") m.setText(str(err)) m.setIcon(m.Warning) m.exec_() # Return now return False # get actual normalized filename filename = editor._filename # notify # TODO: message concerining line endings print("saved file: {} ({})".format(filename, editor.lineEndingsHumanReadable)) self._tabs.updateItems() # todo: this is where we once detected whether the file being saved was a style file. # Notify done return True
def loadFile(self, filename, updateTabs=True): """ Load the specified file. On success returns the item of the file, also if it was already open.""" # Note that by giving the name of a tempfile, we can select that # temp file. # normalize path if filename[0] != '<': filename = normalizePath(filename) if not filename: return None # if the file is already open... for item in self._tabs.items(): if item.id == filename: # id gets _filename or _name for temp files break else: item = None if item: self._tabs.setCurrentItem(item) print("File already open: '{}'".format(filename)) return item # create editor try: editor = createEditor(self, filename) except Exception as err: # Notify in logger print("Error loading file: ", err) # Make sure the user knows m = QtGui.QMessageBox(self) m.setWindowTitle("Error loading file") m.setText(str(err)) m.setIcon(m.Warning) m.exec_() return None # create list item item = FileItem(editor) self._tabs.addItem(item, updateTabs) if updateTabs: self._tabs.setCurrentItem(item) # store the path self._lastpath = os.path.dirname(item.filename) return item
def testWhetherFileWasChanged(self): """ testWhetherFileWasChanged() Test to see whether the file was changed outside our backs, and let the user decide what to do. Returns True if it was changed. """ # get the path path = self._filename if not os.path.isfile(path): # file is deleted from the outside return # test the modification time... mtime = os.path.getmtime(path) if mtime != self._modifyTime: # ask user dlg = QtGui.QMessageBox(self) dlg.setWindowTitle('File was changed') dlg.setText("File has been modified outside of the editor:\n" + self._filename) dlg.setInformativeText("Do you want to reload?") t = dlg.addButton("Reload", QtGui.QMessageBox.AcceptRole) #0 dlg.addButton("Keep this version", QtGui.QMessageBox.RejectRole) #1 dlg.setDefaultButton(t) # whatever the result, we will reset the modified time self._modifyTime = os.path.getmtime(path) # get result and act result = dlg.exec_() if result == QtGui.QMessageBox.AcceptRole: self.reload() else: pass # when cancelled or explicitly said, do nothing # Return that indeed the file was changes return True
def simpleDialog(item, action, question, options, defaultOption): """ simpleDialog(editor, action, question, options, defaultOption) Options with special buttons ---------------------------- ok, open, save, cancel, close, discard, apply, reset, restoredefaults, help, saveall, yes, yestoall, no, notoall, abort, retry, ignore. Returns the selected option as a string, or None if canceled. """ # Get filename if isinstance(item, FileItem): filename = item.id else: filename = item.id() # create button map mb = QtGui.QMessageBox M = { 'ok': mb.Ok, 'open': mb.Open, 'save': mb.Save, 'cancel': mb.Cancel, 'close': mb.Close, 'discard': mb.Discard, 'apply': mb.Apply, 'reset': mb.Reset, 'restoredefaults': mb.RestoreDefaults, 'help': mb.Help, 'saveall': mb.SaveAll, 'yes': mb.Yes, 'yestoall': mb.YesToAll, 'no': mb.No, 'notoall': mb.NoToAll, 'abort': mb.Abort, 'retry': mb.Retry, 'ignore': mb.Ignore } # setup dialog dlg = QtGui.QMessageBox(pyzo.main) dlg.setWindowTitle('Pyzo') dlg.setText(action + " file:\n{}".format(filename)) dlg.setInformativeText(question) # process options buttons = {} for option in options: option_lower = option.lower() # Use standard button? if option_lower in M: button = dlg.addButton(M[option_lower]) else: button = dlg.addButton(option, dlg.AcceptRole) buttons[button] = option # Set as default? if option_lower == defaultOption.lower(): dlg.setDefaultButton(button) # get result result = dlg.exec_() button = dlg.clickedButton() if button in buttons: return buttons[button] else: return None