Exemplo n.º 1
0
    def open(self, docid, name=None):
        idx = self.tabFind(docid)
        if idx is not None:
            self.tabs.setCurrentIndex(idx)
            return

        if name is None:
            item = self.treeFindDocument(docid)
            if not item:
                return
            name = item.text(TREE_COLUMN_NAME)

        meta = self.project.doc.getDocumentMeta(docid)
        _type = meta.get('type') or 'richtext'
        tabeditor = Editor.Editor(_type, self, self.project, docid, name)
        if not tabeditor:
            messageDialog(
                "Unknown document type",
                "Unknown document type. Could not find suitable editor for it.",
                details=_type)
            return

        self.editors[docid] = tabeditor
        self.tabs.addTab(tabeditor, name)
        tabeditor.setModified(tabeditor.isModified())

        c = self.tabs.count()
        if self.tabs.count() == 1:
            self.tabs.show()

        self.tabs.setCurrentIndex(c - 1)
Exemplo n.º 2
0
    def mouseReleaseEvent(self, event):
        if self.clickedAnchor and (event.button() & QtCore.Qt.LeftButton) and (
                    event.modifiers() & QtCore.Qt.ControlModifier):
            pos = event.pos()
            clickedAnchor = self.anchorAt(pos)

            messageDialog("Link clicked", "Link you clicked: {0}".format(clickedAnchor), details=clickedAnchor)
            self.linkClicked.emit(event)

            self.clickedAnchor = None
            return
        return super(QTextEdit, self).mouseReleaseEvent(event)
Exemplo n.º 3
0
    def removeDocument(self, docid):
        if not self.treeready:
            return

        item = self.treeFindDocument(docid)
        if not item:
            return

        name = item.text(TREE_COLUMN_NAME)
        uid = item.text(TREE_COLUMN_UID)

        # get subtree elements and close tabs if opened before removing documents
        tree, count = self.getDocumentTree(docid=uid)
        if count == 0:
            return

        if not messageDialog(
                "Subtree remove",
                "Are you sure you want to remove subtree here? This operation is unrecoverable",
                details="Root document: {0}\nSubtree elements: {1}".format(
                    name, count),
                OkCancel=True):
            return
        try:
            self.treeready = False
            self.closeTreeTabs(docid, tree)
            self.removeDocuments(docid, tree)
        finally:
            self.treeready = True
            self.saveDocumentsTree()
Exemplo n.º 4
0
    def closeRequest(self):
        modified = self.isModified()

        if modified:
            if not messageDialog(
                    "Close project: unsaved changes",
                    "You are about close project with unsaved changes. Are you sure?",
                    details="Unsaved documents: " + str(modified),
                    OkCancel=True):
                return False
        return True
Exemplo n.º 5
0
    def exportToPdf(self):
        if not self.can_print:
            return

        result = Qt.QFileDialog.getSaveFileName(self, "Export document to pdf",
                                                "", "PDF document (*.pdf)")
        if QTVERSION == 4:
            filename = result
        else:
            filename, selectedfilter = result

        if not filename:
            return

        try:
            if os.path.isfile(filename):
                os.unlink(filename)
        except Exception as e:
            self.log.error(
                "exportToPdf(): failed to unlink destination file: %s: %s",
                e.__class__.__name__, e)
            messageDialog("PDF Export", "Failed to export as pdf.")
            return

        self.log.info("Export to PDF: %s", filename)
        printer = Qt.QPrinter(Qt.QPrinter.PrinterResolution)
        printer.setOutputFormat(Qt.QPrinter.PdfFormat)
        printer.setPaperSize(Qt.QPrinter.A4)
        printer.setOutputFileName(filename)
        printer.setCreator("AppleTree")
        printer.setPrintProgram("AppleTree")
        printer.setFontEmbeddingEnabled(True)
        printer.setDocName(self.docname)

        self.print(printer)

        messageDialog("PDF Export",
                      "PDF saved as: " + Qt.QDir.toNativeSeparators(filename))
        del printer
Exemplo n.º 6
0
    def on_menu_project_export(self, *args):
        result = Qt.QFileDialog.getSaveFileName(self, "Export project", "", "AppleTree Project Archive (*.atarch)")

        if QTVERSION == 4:
            filename = result
        else:
            filename, selectedfilter = result

        if not filename:
            return

        projectid, projectv = self.getCurrentProject()

        if not projectid:
            return

        del projectv

        arch = AppleTreeArchive(filename)
        if not arch.create():
            messageDialog(T("Failed to create archive"))
            return

        ProgressDialog.create(0, self)
        try:
            project = self.projects.get(projectid)
            arch.putProject(projectid, project.name)
            documents = listProjectDocumentsTree(project)
            c = len(documents)
            i = 0
            for docid in documents:
                i += 1
                ProgressDialog.progress(i * 100 / c)
                body = project.doc.getDocumentBody(docid)
                meta = project.doc.getDocumentMeta(docid)
                images = project.doc.getImages(docid)

                arch.putDocument(projectid, docid, meta, body)
                for image in images:
                    ProgressDialog.yield_()
                    data = project.doc.getImageRaw(docid, image)
                    arch.putDocumentImage(projectid, docid, image, data)

            arch.close()
            ProgressDialog.done()
            messageDialog(T("Project export"), T("Project exported successfully"))
        except Exception as e:
            self.log.error("Failed to export: %s: %s", e.__class__.__name__, e)
            messageDialog(T("Project export failed"), T("Project export failed"),
                          details="{0}:{1}".format(e.__class__.__name__, e))
        finally:
            ProgressDialog.done()
Exemplo n.º 7
0
    def cloneDocuments(self, srcprojectid, srcdocumentid, dstdocumentid):
        """ clone documents from source project:document to this:document"""

        if not self.treeready:
            return

        self.log.info("Clone documents from %s:%s to %s", srcprojectid,
                      srcdocumentid, dstdocumentid)
        win = self.win()
        if not win:
            return

        srcprojectv = win.projectsViews.get(srcprojectid)
        if not srcdocumentid:
            self.log.warn(
                "Can't clone documents, src project is not opened: %s",
                srcprojectid)
            return

        srcitem = srcprojectv.treeFindDocument(srcdocumentid)
        if not srcitem:
            return
        srcname = srcitem.text(TREE_COLUMN_NAME)

        try:
            self.treeready = False
            # find document subtree (excluding root src document) in src project tree:
            doctree, count = srcprojectv.getDocumentTree(docid=srcdocumentid)

            if not messageDialog(
                    "Subtree paste",
                    "Are you sure you want to paste subtree here?",
                    details="Subtree elements: {0}".format(count),
                    OkCancel=True):
                return

            self._cloneDocuments(srcdocumentid, srcname, doctree,
                                 dstdocumentid, srcprojectv)
        finally:
            self.treeready = True
        self.saveDocumentsTree()
Exemplo n.º 8
0
    def on_toolbar_action(self, action, *args):
        if action == 'save':
            self.save()
            return True

        if action == 'insert-image':
            if not self.has_images:
                return
            return self.on_toolbar_insert_image()

        if action == 'export-to-pdf':
            return self.exportToPdf()

        if action == 'drop-draft':
            if not self.isModified():
                return
            if not messageDialog(
                    "Restore saved document",
                    "You are about to drop unsaved changes and restore saved document. Are you sure?",
                    OkCancel=True):
                return

            return self.load(draft=False)