示例#1
0
 def fileImport(self):
     """Prompt for an import type, then a file to import.
     """
     importControl = imports.ImportControl()
     model = importControl.interactiveImport()
     if model:
         self.createLocalControl(importControl.filePath, model)
         self.activeControl.imported = True
示例#2
0
 def fileImport(self):
     """Prompt for an import type, then a file to import.
     """
     importControl = imports.ImportControl()
     structure = importControl.interactiveImport()
     if structure:
         self.createLocalControl(importControl.pathObj, structure)
         if importControl.treeLineRootAttrib:
             self.activeControl.printData.readData(
                 importControl.treeLineRootAttrib)
         self.activeControl.imported = True
示例#3
0
    def openFile(self,
                 pathObj,
                 forceNewWindow=False,
                 checkModified=False,
                 importOnFail=True):
        """Open the file given by path if not already open.

        If already open in a different window, focus and raise the window.
        Arguments:
            pathObj -- the path object to read
            forceNewWindow -- if True, use a new window regardless of option
            checkModified -- if True & not new win, prompt if file modified
            importOnFail -- if True, prompts for import on non-TreeLine files
        """
        match = [
            control for control in self.localControls
            if pathObj == control.filePathObj
        ]
        if match and self.activeControl not in match:
            control = match[0]
            control.activeWindow.activateAndRaise()
            self.updateLocalControlRef(control)
            return
        if checkModified and not (forceNewWindow
                                  or globalref.genOptions['OpenNewWindow']
                                  or self.activeControl.checkSaveChanges()):
            return
        if not self.checkAutoSave(pathObj):
            if not self.localControls:
                self.createLocalControl()
            return
        QApplication.setOverrideCursor(Qt.WaitCursor)
        try:
            self.createLocalControl(pathObj, None, forceNewWindow)
            self.recentFiles.addItem(pathObj)
            if not (globalref.genOptions['SaveTreeStates'] and
                    self.recentFiles.retrieveTreeState(self.activeControl)):
                self.activeControl.expandRootNodes()
                self.activeControl.selectRootSpot()
            QApplication.restoreOverrideCursor()
        except IOError:
            QApplication.restoreOverrideCursor()
            QMessageBox.warning(
                QApplication.activeWindow(), 'TreeLine',
                _('Error - could not read file {0}').format(pathObj))
            self.recentFiles.removeItem(pathObj)
        except (ValueError, KeyError, TypeError):
            fileObj = pathObj.open('rb')
            fileObj, encrypted = self.decryptFile(fileObj)
            if not fileObj:
                if not self.localControls:
                    self.createLocalControl()
                QApplication.restoreOverrideCursor()
                return
            fileObj, compressed = self.decompressFile(fileObj)
            if compressed or encrypted:
                try:
                    textFileObj = io.TextIOWrapper(fileObj, encoding='utf-8')
                    self.createLocalControl(textFileObj, None, forceNewWindow)
                    fileObj.close()
                    textFileObj.close()
                    self.recentFiles.addItem(pathObj)
                    if not (globalref.genOptions['SaveTreeStates']
                            and self.recentFiles.retrieveTreeState(
                                self.activeControl)):
                        self.activeControl.expandRootNodes()
                        self.activeControl.selectRootSpot()
                    self.activeControl.compressed = compressed
                    self.activeControl.encrypted = encrypted
                    QApplication.restoreOverrideCursor()
                    return
                except (ValueError, KeyError, TypeError):
                    pass
            fileObj.close()
            importControl = imports.ImportControl(pathObj)
            structure = importControl.importOldTreeLine()
            if structure:
                self.createLocalControl(pathObj, structure, forceNewWindow)
                self.activeControl.printData.readData(
                    importControl.treeLineRootAttrib)
                self.recentFiles.addItem(pathObj)
                self.activeControl.expandRootNodes()
                self.activeControl.imported = True
                QApplication.restoreOverrideCursor()
                return
            QApplication.restoreOverrideCursor()
            if importOnFail:
                importControl = imports.ImportControl(pathObj)
                structure = importControl.interactiveImport(True)
                if structure:
                    self.createLocalControl(pathObj, structure, forceNewWindow)
                    self.activeControl.imported = True
                    return
            else:
                QMessageBox.warning(
                    QApplication.activeWindow(), 'TreeLine',
                    _('Error - invalid TreeLine file {0}').format(pathObj))
                self.recentFiles.removeItem(pathObj)
        if not self.localControls:
            self.createLocalControl()
示例#4
0
    def openFile(self, path, checkModified=False, importOnFail=True):
        """Open the file given by path if not already open.

        If already open in a different window, focus and raise the window.
        Arguments:
            path -- the name of the file path to read
            checkModified -- if True, prompt user about current modified file
            importOnFail -- if True, prompts for import on non-TreeLine files
        """
        path = os.path.abspath(path)
        match = [
            control for control in self.localControls
            if os.path.normcase(control.filePath) == os.path.normcase(path)
        ]
        if match and self.activeControl not in match:
            control = match[0]
            control.activeWindow.activateAndRaise()
            self.updateLocalControlRef(control)
        elif (not checkModified
              or globalref.genOptions.getValue('OpenNewWindow')
              or self.activeControl.promptModifiedOk()):
            if not self.checkAutoSave(path):
                if not self.localControls:
                    self.createLocalControl()
                    return
            try:
                QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
                self.createLocalControl(path)
                self.recentFiles.addItem(path)
                if globalref.genOptions.getValue('SaveTreeStates'):
                    self.recentFiles.retrieveTreeState(self.activeControl)
                QtGui.QApplication.restoreOverrideCursor()
                if self.pluginInterface:
                    self.pluginInterface.execCallback(
                        self.pluginInterface.fileOpenCallbacks)
            except IOError:
                QtGui.QApplication.restoreOverrideCursor()
                QtGui.QMessageBox.warning(
                    QtGui.QApplication.activeWindow(), 'TreeLine',
                    _('Error - could not read file {0}').format(path))
                self.recentFiles.removeItem(path)
            except treeopener.ParseError:
                QtGui.QApplication.restoreOverrideCursor()
                compressed = False
                encrypted = False
                fileObj = open(path, 'rb')
                # decompress before decrypt to support TreeLine 1.4 and earlier
                fileObj, compressed = self.decompressFile(path, fileObj)
                fileObj, encrypted = self.decryptFile(path, fileObj)
                if not fileObj:
                    if not self.localControls:
                        self.createLocalControl()
                        QtGui.QApplication.restoreOverrideCursor()
                        return
                if encrypted and not compressed:
                    fileObj, compressed = self.decompressFile(path, fileObj)
                if compressed or encrypted:
                    try:
                        QtGui.QApplication.setOverrideCursor(
                            QtCore.Qt.WaitCursor)
                        self.createLocalControl(fileObj)
                        self.recentFiles.addItem(path)
                        if globalref.genOptions.getValue('SaveTreeStates'):
                            self.recentFiles.retrieveTreeState(
                                self.activeControl)
                        self.activeControl.compressed = compressed
                        self.activeControl.encrypted = encrypted
                        QtGui.QApplication.restoreOverrideCursor()
                        if self.pluginInterface:
                            self.pluginInterface.execCallback(
                                self.pluginInterface.fileOpenCallbacks)
                    except (treeopener.ParseError, zlib.error):
                        QtGui.QApplication.restoreOverrideCursor()
                        QtGui.QMessageBox.warning(
                            QtGui.QApplication.activeWindow(), 'TreeLine',
                            _('Error - {0} is not a '
                              'valid TreeLine file').format(path))
                    fileObj.close()
                else:
                    fileObj.close()
                    if importOnFail:
                        importControl = imports.ImportControl(path)
                        model = importControl.interactiveImport(True)
                        if model:
                            self.createLocalControl(importControl.filePath,
                                                    model)
                            self.activeControl.imported = True
            if not self.localControls:
                self.createLocalControl()