示例#1
0
class GuiBuildNovel(QDialog):

    FMT_PDF = 1  # Print to PDF

    FMT_ODT = 2  # Open Document file
    FMT_FODT = 3  # Flat Open Document file
    FMT_HTM = 4  # HTML5
    FMT_NWD = 5  # nW Markdown
    FMT_MD = 6  # Standard Markdown
    FMT_GH = 7  # GitHub Markdown

    FMT_JSON_H = 8  # HTML5 wrapped in JSON
    FMT_JSON_M = 9  # nW Markdown wrapped in JSON

    def __init__(self, theParent, theProject):
        QDialog.__init__(self, theParent)

        logger.debug("Initialising GuiBuildNovel ...")
        self.setObjectName("GuiBuildNovel")

        self.mainConf = nw.CONFIG
        self.theProject = theProject
        self.theParent = theParent
        self.theTheme = theParent.theTheme
        self.optState = self.theProject.optState

        self.htmlText = []  # List of html documents
        self.htmlStyle = []  # List of html styles
        self.htmlSize = 0  # Size of the html document
        self.buildTime = 0  # The timestamp of the last build

        self.setWindowTitle(self.tr("Build Novel Project"))
        self.setMinimumWidth(self.mainConf.pxInt(700))
        self.setMinimumHeight(self.mainConf.pxInt(600))

        self.resize(
            self.mainConf.pxInt(
                self.optState.getInt("GuiBuildNovel", "winWidth", 900)),
            self.mainConf.pxInt(
                self.optState.getInt("GuiBuildNovel", "winHeight", 800)))

        self.docView = GuiBuildNovelDocView(self, self.theProject)

        hS = self.theTheme.fontPixelSize
        wS = 2 * hS

        # Title Formats
        # =============

        self.titleGroup = QGroupBox(self.tr("Title Formats for Novel Files"),
                                    self)
        self.titleForm = QGridLayout(self)
        self.titleGroup.setLayout(self.titleForm)

        fmtHelp = "<br>".join([
            "<b>%s</b>" % self.tr("Formatting Codes:"),
            self.tr("{0} for the title as set in the document").format(
                r"%title%"),
            self.tr("{0} for chapter number (1, 2, 3)").format(r"%ch%"),
            self.tr("{0} for chapter number as a word (one, two)").format(
                r"%chw%"),
            self.tr("{0} for chapter number in upper case Roman").format(
                r"%chI%"),
            self.tr("{0} for chapter number in lower case Roman").format(
                r"%chi%"),
            self.tr("{0} for scene number within chapter").format(r"%sc%"),
            self.tr("{0} for scene number within novel").format(r"%sca%"),
        ])
        fmtScHelp = "<br><br>%s" % self.tr(
            "Leave blank to skip this heading, or set to a static text, like "
            "for instance '{0}', to make a separator. The separator will "
            "be centred automatically and only appear between sections of "
            "the same type.").format("* * *")
        xFmt = self.mainConf.pxInt(100)

        self.fmtTitle = QLineEdit()
        self.fmtTitle.setMaxLength(200)
        self.fmtTitle.setMinimumWidth(xFmt)
        self.fmtTitle.setToolTip(fmtHelp)
        self.fmtTitle.setText(
            self._reFmtCodes(self.theProject.titleFormat["title"]))

        self.fmtChapter = QLineEdit()
        self.fmtChapter.setMaxLength(200)
        self.fmtChapter.setMinimumWidth(xFmt)
        self.fmtChapter.setToolTip(fmtHelp)
        self.fmtChapter.setText(
            self._reFmtCodes(self.theProject.titleFormat["chapter"]))

        self.fmtUnnumbered = QLineEdit()
        self.fmtUnnumbered.setMaxLength(200)
        self.fmtUnnumbered.setMinimumWidth(xFmt)
        self.fmtUnnumbered.setToolTip(fmtHelp)
        self.fmtUnnumbered.setText(
            self._reFmtCodes(self.theProject.titleFormat["unnumbered"]))

        self.fmtScene = QLineEdit()
        self.fmtScene.setMaxLength(200)
        self.fmtScene.setMinimumWidth(xFmt)
        self.fmtScene.setToolTip(fmtHelp + fmtScHelp)
        self.fmtScene.setText(
            self._reFmtCodes(self.theProject.titleFormat["scene"]))

        self.fmtSection = QLineEdit()
        self.fmtSection.setMaxLength(200)
        self.fmtSection.setMinimumWidth(xFmt)
        self.fmtSection.setToolTip(fmtHelp + fmtScHelp)
        self.fmtSection.setText(
            self._reFmtCodes(self.theProject.titleFormat["section"]))

        self.buildLang = QComboBox()
        self.buildLang.setMinimumWidth(xFmt)
        theLangs = self.mainConf.listLanguages(self.mainConf.LANG_PROJ)
        self.buildLang.addItem("[%s]" % self.tr("Not Set"), "None")
        for langID, langName in theLangs:
            self.buildLang.addItem(langName, langID)

        langIdx = self.buildLang.findData(self.theProject.projLang)
        if langIdx != -1:
            self.buildLang.setCurrentIndex(langIdx)

        # Dummy boxes due to QGridView and QLineEdit expand bug
        self.boxTitle = QHBoxLayout()
        self.boxTitle.addWidget(self.fmtTitle)
        self.boxChapter = QHBoxLayout()
        self.boxChapter.addWidget(self.fmtChapter)
        self.boxUnnumb = QHBoxLayout()
        self.boxUnnumb.addWidget(self.fmtUnnumbered)
        self.boxScene = QHBoxLayout()
        self.boxScene.addWidget(self.fmtScene)
        self.boxSection = QHBoxLayout()
        self.boxSection.addWidget(self.fmtSection)

        titleLabel = QLabel(self.tr("Title"))
        chapterLabel = QLabel(self.tr("Chapter"))
        unnumbLabel = QLabel(self.tr("Unnumbered"))
        sceneLabel = QLabel(self.tr("Scene"))
        sectionLabel = QLabel(self.tr("Section"))
        langLabel = QLabel(self.tr("Language"))

        self.titleForm.addWidget(titleLabel, 0, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addLayout(self.boxTitle, 0, 1, 1, 1, Qt.AlignRight)
        self.titleForm.addWidget(chapterLabel, 1, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addLayout(self.boxChapter, 1, 1, 1, 1, Qt.AlignRight)
        self.titleForm.addWidget(unnumbLabel, 2, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addLayout(self.boxUnnumb, 2, 1, 1, 1, Qt.AlignRight)
        self.titleForm.addWidget(sceneLabel, 3, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addLayout(self.boxScene, 3, 1, 1, 1, Qt.AlignRight)
        self.titleForm.addWidget(sectionLabel, 4, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addLayout(self.boxSection, 4, 1, 1, 1, Qt.AlignRight)
        self.titleForm.addWidget(langLabel, 5, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addWidget(self.buildLang, 5, 1, 1, 1, Qt.AlignRight)

        self.titleForm.setColumnStretch(0, 0)
        self.titleForm.setColumnStretch(1, 1)

        # Font Options
        # ============

        self.fontGroup = QGroupBox(self.tr("Font Options"), self)
        self.fontForm = QGridLayout(self)
        self.fontGroup.setLayout(self.fontForm)

        ## Font Family
        self.textFont = QLineEdit()
        self.textFont.setReadOnly(True)
        self.textFont.setMinimumWidth(xFmt)
        self.textFont.setText(
            self.optState.getString("GuiBuildNovel", "textFont",
                                    self.mainConf.textFont))
        self.fontButton = QPushButton("...")
        self.fontButton.setMaximumWidth(
            int(2.5 * self.theTheme.getTextWidth("...")))
        self.fontButton.clicked.connect(self._selectFont)

        self.textSize = QSpinBox(self)
        self.textSize.setFixedWidth(6 * self.theTheme.textNWidth)
        self.textSize.setMinimum(6)
        self.textSize.setMaximum(72)
        self.textSize.setSingleStep(1)
        self.textSize.setValue(
            self.optState.getInt("GuiBuildNovel", "textSize",
                                 self.mainConf.textSize))

        self.lineHeight = QDoubleSpinBox(self)
        self.lineHeight.setFixedWidth(6 * self.theTheme.textNWidth)
        self.lineHeight.setMinimum(0.8)
        self.lineHeight.setMaximum(3.0)
        self.lineHeight.setSingleStep(0.05)
        self.lineHeight.setDecimals(2)
        self.lineHeight.setValue(
            self.optState.getFloat("GuiBuildNovel", "lineHeight", 1.15))

        # Dummy box due to QGridView and QLineEdit expand bug
        self.boxFont = QHBoxLayout()
        self.boxFont.addWidget(self.textFont)

        fontFamilyLabel = QLabel(self.tr("Font family"))
        fontSizeLabel = QLabel(self.tr("Font size"))
        lineHeightLabel = QLabel(self.tr("Line height"))
        justifyLabel = QLabel(self.tr("Justify text"))
        stylingLabel = QLabel(self.tr("Disable styling"))

        self.fontForm.addWidget(fontFamilyLabel, 0, 0, 1, 1, Qt.AlignLeft)
        self.fontForm.addLayout(self.boxFont, 0, 1, 1, 1, Qt.AlignRight)
        self.fontForm.addWidget(self.fontButton, 0, 2, 1, 1, Qt.AlignRight)
        self.fontForm.addWidget(fontSizeLabel, 1, 0, 1, 1, Qt.AlignLeft)
        self.fontForm.addWidget(self.textSize, 1, 1, 1, 2, Qt.AlignRight)
        self.fontForm.addWidget(lineHeightLabel, 2, 0, 1, 1, Qt.AlignLeft)
        self.fontForm.addWidget(self.lineHeight, 2, 1, 1, 2, Qt.AlignRight)

        self.fontForm.setColumnStretch(0, 0)
        self.fontForm.setColumnStretch(1, 1)
        self.fontForm.setColumnStretch(2, 0)

        # Styling Options
        # ===============

        self.styleGroup = QGroupBox(self.tr("Styling Options"), self)
        self.styleForm = QGridLayout(self)
        self.styleGroup.setLayout(self.styleForm)

        self.justifyText = QSwitch(width=wS, height=hS)
        self.justifyText.setChecked(
            self.optState.getBool("GuiBuildNovel", "justifyText", False))

        self.noStyling = QSwitch(width=wS, height=hS)
        self.noStyling.setChecked(
            self.optState.getBool("GuiBuildNovel", "noStyling", False))

        self.styleForm.addWidget(justifyLabel, 1, 0, 1, 1, Qt.AlignLeft)
        self.styleForm.addWidget(self.justifyText, 1, 1, 1, 2, Qt.AlignRight)
        self.styleForm.addWidget(stylingLabel, 2, 0, 1, 1, Qt.AlignLeft)
        self.styleForm.addWidget(self.noStyling, 2, 1, 1, 2, Qt.AlignRight)

        self.styleForm.setColumnStretch(0, 0)
        self.styleForm.setColumnStretch(1, 1)

        # Include Options
        # ===============

        self.textGroup = QGroupBox(self.tr("Include Options"), self)
        self.textForm = QGridLayout(self)
        self.textGroup.setLayout(self.textForm)

        self.includeSynopsis = QSwitch(width=wS, height=hS)
        self.includeSynopsis.setChecked(
            self.optState.getBool("GuiBuildNovel", "incSynopsis", False))

        self.includeComments = QSwitch(width=wS, height=hS)
        self.includeComments.setChecked(
            self.optState.getBool("GuiBuildNovel", "incComments", False))

        self.includeKeywords = QSwitch(width=wS, height=hS)
        self.includeKeywords.setChecked(
            self.optState.getBool("GuiBuildNovel", "incKeywords", False))

        self.includeBody = QSwitch(width=wS, height=hS)
        self.includeBody.setChecked(
            self.optState.getBool("GuiBuildNovel", "incBodyText", True))

        synopsisLabel = QLabel(self.tr("Include synopsis"))
        commentsLabel = QLabel(self.tr("Include comments"))
        keywordsLabel = QLabel(self.tr("Include keywords"))
        bodyLabel = QLabel(self.tr("Include body text"))

        self.textForm.addWidget(synopsisLabel, 0, 0, 1, 1, Qt.AlignLeft)
        self.textForm.addWidget(self.includeSynopsis, 0, 1, 1, 1,
                                Qt.AlignRight)
        self.textForm.addWidget(commentsLabel, 1, 0, 1, 1, Qt.AlignLeft)
        self.textForm.addWidget(self.includeComments, 1, 1, 1, 1,
                                Qt.AlignRight)
        self.textForm.addWidget(keywordsLabel, 2, 0, 1, 1, Qt.AlignLeft)
        self.textForm.addWidget(self.includeKeywords, 2, 1, 1, 1,
                                Qt.AlignRight)
        self.textForm.addWidget(bodyLabel, 3, 0, 1, 1, Qt.AlignLeft)
        self.textForm.addWidget(self.includeBody, 3, 1, 1, 1, Qt.AlignRight)

        self.textForm.setColumnStretch(0, 1)
        self.textForm.setColumnStretch(1, 0)

        # File Filter Options
        # ===================

        self.fileGroup = QGroupBox(self.tr("File Filter Options"), self)
        self.fileForm = QGridLayout(self)
        self.fileGroup.setLayout(self.fileForm)

        self.novelFiles = QSwitch(width=wS, height=hS)
        self.novelFiles.setToolTip(
            self.tr("Include files with layouts other than 'Note'."))
        self.novelFiles.setChecked(
            self.optState.getBool("GuiBuildNovel", "addNovel", True))

        self.noteFiles = QSwitch(width=wS, height=hS)
        self.noteFiles.setToolTip(self.tr("Include files with layout 'Note'."))
        self.noteFiles.setChecked(
            self.optState.getBool("GuiBuildNovel", "addNotes", False))

        self.ignoreFlag = QSwitch(width=wS, height=hS)
        self.ignoreFlag.setToolTip(
            self.
            tr("Ignore the 'Include when building project' setting and include "
               "all files in the output."))
        self.ignoreFlag.setChecked(
            self.optState.getBool("GuiBuildNovel", "ignoreFlag", False))

        novelLabel = QLabel(self.tr("Include novel files"))
        notesLabel = QLabel(self.tr("Include note files"))
        exportLabel = QLabel(self.tr("Ignore export flag"))

        self.fileForm.addWidget(novelLabel, 0, 0, 1, 1, Qt.AlignLeft)
        self.fileForm.addWidget(self.novelFiles, 0, 1, 1, 1, Qt.AlignRight)
        self.fileForm.addWidget(notesLabel, 1, 0, 1, 1, Qt.AlignLeft)
        self.fileForm.addWidget(self.noteFiles, 1, 1, 1, 1, Qt.AlignRight)
        self.fileForm.addWidget(exportLabel, 2, 0, 1, 1, Qt.AlignLeft)
        self.fileForm.addWidget(self.ignoreFlag, 2, 1, 1, 1, Qt.AlignRight)

        self.fileForm.setColumnStretch(0, 1)
        self.fileForm.setColumnStretch(1, 0)

        # Export Options
        # ==============

        self.exportGroup = QGroupBox(self.tr("Export Options"), self)
        self.exportForm = QGridLayout(self)
        self.exportGroup.setLayout(self.exportForm)

        self.replaceTabs = QSwitch(width=wS, height=hS)
        self.replaceTabs.setChecked(
            self.optState.getBool("GuiBuildNovel", "replaceTabs", False))

        self.replaceUCode = QSwitch(width=wS, height=hS)
        self.replaceUCode.setChecked(
            self.optState.getBool("GuiBuildNovel", "replaceUCode", False))

        tabsLabel = QLabel(self.tr("Replace tabs with spaces"))
        uCodeLabel = QLabel(self.tr("Replace Unicode in HTML"))

        self.exportForm.addWidget(tabsLabel, 0, 0, 1, 1, Qt.AlignLeft)
        self.exportForm.addWidget(self.replaceTabs, 0, 1, 1, 1, Qt.AlignRight)
        self.exportForm.addWidget(uCodeLabel, 1, 0, 1, 1, Qt.AlignLeft)
        self.exportForm.addWidget(self.replaceUCode, 1, 1, 1, 1, Qt.AlignRight)

        self.exportForm.setColumnStretch(0, 1)
        self.exportForm.setColumnStretch(1, 0)

        # Build Button
        # ============

        self.buildProgress = QProgressBar()

        self.buildNovel = QPushButton(self.tr("Build Preview"))
        self.buildNovel.clicked.connect(self._buildPreview)

        # Action Buttons
        # ==============

        self.buttonBox = QHBoxLayout()

        # Printing

        self.printMenu = QMenu(self)
        self.btnPrint = QPushButton(self.tr("Print"))
        self.btnPrint.setMenu(self.printMenu)

        self.printSend = QAction(self.tr("Print Preview"), self)
        self.printSend.triggered.connect(self._printDocument)
        self.printMenu.addAction(self.printSend)

        self.printFile = QAction(self.tr("Print to PDF"), self)
        self.printFile.triggered.connect(
            lambda: self._saveDocument(self.FMT_PDF))
        self.printMenu.addAction(self.printFile)

        # Saving to File

        self.saveMenu = QMenu(self)
        self.btnSave = QPushButton(self.tr("Save As"))
        self.btnSave.setMenu(self.saveMenu)

        self.saveODT = QAction(self.tr("Open Document (.odt)"), self)
        self.saveODT.triggered.connect(
            lambda: self._saveDocument(self.FMT_ODT))
        self.saveMenu.addAction(self.saveODT)

        self.saveFODT = QAction(self.tr("Flat Open Document (.fodt)"), self)
        self.saveFODT.triggered.connect(
            lambda: self._saveDocument(self.FMT_FODT))
        self.saveMenu.addAction(self.saveFODT)

        self.saveHTM = QAction(self.tr("novelWriter HTML (.htm)"), self)
        self.saveHTM.triggered.connect(
            lambda: self._saveDocument(self.FMT_HTM))
        self.saveMenu.addAction(self.saveHTM)

        self.saveNWD = QAction(self.tr("novelWriter Markdown (.nwd)"), self)
        self.saveNWD.triggered.connect(
            lambda: self._saveDocument(self.FMT_NWD))
        self.saveMenu.addAction(self.saveNWD)

        self.saveMD = QAction(self.tr("Standard Markdown (.md)"), self)
        self.saveMD.triggered.connect(lambda: self._saveDocument(self.FMT_MD))
        self.saveMenu.addAction(self.saveMD)

        self.saveGH = QAction(self.tr("GitHub Markdown (.md)"), self)
        self.saveGH.triggered.connect(lambda: self._saveDocument(self.FMT_GH))
        self.saveMenu.addAction(self.saveGH)

        self.saveJsonH = QAction(self.tr("JSON + novelWriter HTML (.json)"),
                                 self)
        self.saveJsonH.triggered.connect(
            lambda: self._saveDocument(self.FMT_JSON_H))
        self.saveMenu.addAction(self.saveJsonH)

        self.saveJsonM = QAction(
            self.tr("JSON + novelWriter Markdown (.json)"), self)
        self.saveJsonM.triggered.connect(
            lambda: self._saveDocument(self.FMT_JSON_M))
        self.saveMenu.addAction(self.saveJsonM)

        self.btnClose = QPushButton(self.tr("Close"))
        self.btnClose.clicked.connect(self._doClose)

        self.buttonBox.addWidget(self.btnSave)
        self.buttonBox.addWidget(self.btnPrint)
        self.buttonBox.addWidget(self.btnClose)
        self.buttonBox.setSpacing(self.mainConf.pxInt(4))

        # Assemble GUI
        # ============

        # Splitter Position
        boxWidth = self.mainConf.pxInt(350)
        boxWidth = self.optState.getInt("GuiBuildNovel", "boxWidth", boxWidth)
        docWidth = max(self.width() - boxWidth, 100)
        docWidth = self.optState.getInt("GuiBuildNovel", "docWidth", docWidth)

        # The Tool Box
        self.toolsBox = QVBoxLayout()
        self.toolsBox.addWidget(self.titleGroup)
        self.toolsBox.addWidget(self.fontGroup)
        self.toolsBox.addWidget(self.styleGroup)
        self.toolsBox.addWidget(self.textGroup)
        self.toolsBox.addWidget(self.fileGroup)
        self.toolsBox.addWidget(self.exportGroup)
        self.toolsBox.addStretch(1)

        # Tool Box Wrapper Widget
        self.toolsWidget = QWidget()
        self.toolsWidget.setSizePolicy(QSizePolicy.MinimumExpanding,
                                       QSizePolicy.Minimum)
        self.toolsWidget.setLayout(self.toolsBox)

        # Tool Box Scroll Area
        self.toolsArea = QScrollArea()
        self.toolsArea.setMinimumWidth(self.mainConf.pxInt(250))
        self.toolsArea.setWidgetResizable(True)
        self.toolsArea.setWidget(self.toolsWidget)

        if self.mainConf.hideVScroll:
            self.toolsArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        else:
            self.toolsArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)

        if self.mainConf.hideHScroll:
            self.toolsArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        else:
            self.toolsArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)

        # Tools and Buttons Layout
        tSp = self.mainConf.pxInt(8)
        self.innerBox = QVBoxLayout()
        self.innerBox.addWidget(self.toolsArea)
        self.innerBox.addSpacing(tSp)
        self.innerBox.addWidget(self.buildProgress)
        self.innerBox.addWidget(self.buildNovel)
        self.innerBox.addSpacing(tSp)
        self.innerBox.addLayout(self.buttonBox)

        # Tools and Buttons Wrapper Widget
        self.innerWidget = QWidget()
        self.innerWidget.setLayout(self.innerBox)

        # Main Dialog Splitter
        self.mainSplit = QSplitter(Qt.Horizontal)
        self.mainSplit.addWidget(self.innerWidget)
        self.mainSplit.addWidget(self.docView)
        self.mainSplit.setSizes([boxWidth, docWidth])

        self.idxSettings = self.mainSplit.indexOf(self.innerWidget)
        self.idxDocument = self.mainSplit.indexOf(self.docView)

        self.mainSplit.setCollapsible(self.idxSettings, False)
        self.mainSplit.setCollapsible(self.idxDocument, False)

        # Outer Layout
        self.outerBox = QHBoxLayout()
        self.outerBox.addWidget(self.mainSplit)

        self.setLayout(self.outerBox)
        self.buildNovel.setFocus()

        logger.debug("GuiBuildNovel initialisation complete")

        return

    def viewCachedDoc(self):
        """Load the previously generated document from cache.
        """
        if self._loadCache():
            textFont = self.textFont.text()
            textSize = self.textSize.value()
            justifyText = self.justifyText.isChecked()
            self.docView.setTextFont(textFont, textSize)
            self.docView.setJustify(justifyText)
            if self.noStyling.isChecked():
                self.docView.clearStyleSheet()
            else:
                self.docView.setStyleSheet(self.htmlStyle)

            htmlSize = sum([len(x) for x in self.htmlText])
            if htmlSize < nwConst.MAX_BUILDSIZE:
                qApp.processEvents()
                self.docView.setContent(self.htmlText, self.buildTime)
            else:
                self.docView.setText(
                    self.tr(
                        "Failed to generate preview. The result is too big."))

        else:
            self.htmlText = []
            self.htmlStyle = []
            self.buildTime = 0
            return False

        return True

    ##
    #  Slots and Related
    ##

    def _buildPreview(self):
        """Build a preview of the project in the document viewer.
        """
        # Get Settings
        justifyText = self.justifyText.isChecked()
        noStyling = self.noStyling.isChecked()
        textFont = self.textFont.text()
        textSize = self.textSize.value()
        replaceTabs = self.replaceTabs.isChecked()

        self.htmlText = []
        self.htmlStyle = []
        self.htmlSize = 0

        # Build Preview
        # =============

        makeHtml = ToHtml(self.theProject, self.theParent)
        self._doBuild(makeHtml, isPreview=True)
        if replaceTabs:
            makeHtml.replaceTabs()

        self.htmlText = makeHtml.fullHTML
        self.htmlStyle = makeHtml.getStyleSheet()
        self.htmlSize = makeHtml.getFullResultSize()
        self.buildTime = int(time())

        # Load Preview
        # ============

        self.docView.setTextFont(textFont, textSize)
        self.docView.setJustify(justifyText)
        if noStyling:
            self.docView.clearStyleSheet()
        else:
            self.docView.setStyleSheet(self.htmlStyle)

        if self.htmlSize < nwConst.MAX_BUILDSIZE:
            self.docView.setContent(self.htmlText, self.buildTime)
        else:
            self.docView.setText(
                "Failed to generate preview. The result is too big.")

        self._saveCache()

        return

    def _doBuild(self, bldObj, isPreview=False, doConvert=True):
        """Rund the build with a specific build object.
        """
        tStart = int(time())

        # Get Settings
        fmtTitle = self.fmtTitle.text().strip()
        fmtChapter = self.fmtChapter.text().strip()
        fmtUnnumbered = self.fmtUnnumbered.text().strip()
        fmtScene = self.fmtScene.text().strip()
        fmtSection = self.fmtSection.text().strip()
        textFont = self.textFont.text()
        textSize = self.textSize.value()
        lineHeight = self.lineHeight.value()
        justifyText = self.justifyText.isChecked()
        noStyling = self.noStyling.isChecked()
        incSynopsis = self.includeSynopsis.isChecked()
        incComments = self.includeComments.isChecked()
        incKeywords = self.includeKeywords.isChecked()
        novelFiles = self.novelFiles.isChecked()
        noteFiles = self.noteFiles.isChecked()
        ignoreFlag = self.ignoreFlag.isChecked()
        includeBody = self.includeBody.isChecked()
        replaceUCode = self.replaceUCode.isChecked()

        # The language lookup dict is reloaded if needed
        self.theProject.setProjectLang(self.buildLang.currentData())

        # Get font information
        fontInfo = QFontInfo(QFont(textFont, textSize))
        textFixed = fontInfo.fixedPitch()

        isHtml = isinstance(bldObj, ToHtml)
        isOdt = isinstance(bldObj, ToOdt)

        bldObj.setTitleFormat(fmtTitle)
        bldObj.setChapterFormat(fmtChapter)
        bldObj.setUnNumberedFormat(fmtUnnumbered)
        bldObj.setSceneFormat(fmtScene, fmtScene == "")
        bldObj.setSectionFormat(fmtSection, fmtSection == "")

        bldObj.setFont(textFont, textSize, textFixed)
        bldObj.setJustify(justifyText)
        bldObj.setLineHeight(lineHeight)

        bldObj.setSynopsis(incSynopsis)
        bldObj.setComments(incComments)
        bldObj.setKeywords(incKeywords)
        bldObj.setBodyText(includeBody)

        if isHtml:
            bldObj.setStyles(not noStyling)
            bldObj.setReplaceUnicode(replaceUCode)

        if isOdt:
            bldObj.setColourHeaders(not noStyling)
            bldObj.initDocument()

        # Make sure the project and document is up to date
        self.theParent.treeView.flushTreeOrder()
        self.theParent.saveDocument()

        self.buildProgress.setMaximum(len(self.theProject.projTree))
        self.buildProgress.setValue(0)

        for nItt, tItem in enumerate(self.theProject.projTree):

            noteRoot = noteFiles
            noteRoot &= tItem.itemType == nwItemType.ROOT
            noteRoot &= tItem.itemClass != nwItemClass.NOVEL
            noteRoot &= tItem.itemClass != nwItemClass.ARCHIVE

            try:
                if noteRoot:
                    # Add headers for root folders of notes
                    bldObj.addRootHeading(tItem.itemHandle)
                    if doConvert:
                        bldObj.doConvert()

                elif self._checkInclude(tItem, noteFiles, novelFiles,
                                        ignoreFlag):
                    bldObj.setText(tItem.itemHandle)
                    bldObj.doPreProcessing()
                    bldObj.tokenizeText()
                    bldObj.doHeaders()
                    if doConvert:
                        bldObj.doConvert()
                    bldObj.doPostProcessing()

            except Exception:
                logger.error("Failed to build document '%s'" %
                             tItem.itemHandle)
                nw.logException()
                if isPreview:
                    self.docView.setText(
                        ("Failed to generate preview. "
                         "Document with title '%s' could not be parsed.") %
                        tItem.itemName)

                return False

            # Update progress bar, also for skipped items
            self.buildProgress.setValue(nItt + 1)

        if isOdt:
            bldObj.closeDocument()

        tEnd = int(time())
        logger.debug("Built project in %.3f ms" % (1000 * (tEnd - tStart)))

        if bldObj.errData:
            self.theParent.makeAlert(
                "%s:<br>-&nbsp;%s" %
                (self.tr("There were problems when building the project"),
                 "<br>-&nbsp;".join(bldObj.errData)), nwAlert.ERROR)

        return

    def _checkInclude(self, theItem, noteFiles, novelFiles, ignoreFlag):
        """This function checks whether a file should be included in the
        export or not. For standard note and novel files, this is
        controlled by the options selected by the user. For other files
        classified as non-exportable, a few checks must be made, and the
        following are not:
        * Items that are not actual files.
        * Items that have been orphaned which are tagged as NO_LAYOUT
          and NO_CLASS.
        * Items that appear in the TRASH folder or have parent set to
          None (orphaned files).
        """
        if theItem is None:
            return False

        if not theItem.isExported and not ignoreFlag:
            return False

        isNone = theItem.itemType != nwItemType.FILE
        isNone |= theItem.itemLayout == nwItemLayout.NO_LAYOUT
        isNone |= theItem.itemClass == nwItemClass.NO_CLASS
        isNone |= theItem.itemClass == nwItemClass.TRASH
        isNone |= theItem.itemParent == self.theProject.projTree.trashRoot()
        isNone |= theItem.itemParent is None
        isNote = theItem.itemLayout == nwItemLayout.NOTE
        isNovel = not isNone and not isNote

        if isNone:
            return False
        if isNote and not noteFiles:
            return False
        if isNovel and not novelFiles:
            return False

        rootItem = self.theProject.projTree.getRootItem(theItem.itemHandle)
        if rootItem.itemClass == nwItemClass.ARCHIVE:
            return False

        return True

    def _saveDocument(self, theFmt):
        """Save the document to various formats.
        """
        replaceTabs = self.replaceTabs.isChecked()

        fileExt = ""
        textFmt = ""

        # Settings
        # ========

        if theFmt == self.FMT_ODT:
            fileExt = "odt"
            textFmt = self.tr("Open Document")

        elif theFmt == self.FMT_FODT:
            fileExt = "fodt"
            textFmt = self.tr("Flat Open Document")

        elif theFmt == self.FMT_HTM:
            fileExt = "htm"
            textFmt = self.tr("Plain HTML")

        elif theFmt == self.FMT_NWD:
            fileExt = "nwd"
            textFmt = self.tr("novelWriter Markdown")

        elif theFmt == self.FMT_MD:
            fileExt = "md"
            textFmt = self.tr("Standard Markdown")

        elif theFmt == self.FMT_GH:
            fileExt = "md"
            textFmt = self.tr("GitHub Markdown")

        elif theFmt == self.FMT_JSON_H:
            fileExt = "json"
            textFmt = self.tr("JSON + novelWriter HTML")

        elif theFmt == self.FMT_JSON_M:
            fileExt = "json"
            textFmt = self.tr("JSON + novelWriter Markdown")

        elif theFmt == self.FMT_PDF:
            fileExt = "pdf"
            textFmt = self.tr("PDF")

        else:
            return False

        # Generate File Name
        # ==================

        if fileExt:

            cleanName = makeFileNameSafe(self.theProject.projName)
            fileName = "%s.%s" % (cleanName, fileExt)
            saveDir = self.mainConf.lastPath
            savePath = os.path.join(saveDir, fileName)
            if not os.path.isdir(saveDir):
                saveDir = self.mainConf.homePath

            savePath, _ = QFileDialog.getSaveFileName(
                self, self.tr("Save Document As"), savePath)
            if not savePath:
                return False

            self.mainConf.setLastPath(savePath)

        else:
            return False

        # Build and Write
        # ===============

        errMsg = ""
        wSuccess = False

        if theFmt == self.FMT_ODT:
            makeOdt = ToOdt(self.theProject, self.theParent, isFlat=False)
            self._doBuild(makeOdt)
            try:
                makeOdt.saveOpenDocText(savePath)
                wSuccess = True
            except Exception as e:
                errMsg = str(e)

        elif theFmt == self.FMT_FODT:
            makeOdt = ToOdt(self.theProject, self.theParent, isFlat=True)
            self._doBuild(makeOdt)
            try:
                makeOdt.saveFlatXML(savePath)
                wSuccess = True
            except Exception as e:
                errMsg = str(e)

        elif theFmt == self.FMT_HTM:
            makeHtml = ToHtml(self.theProject, self.theParent)
            self._doBuild(makeHtml)
            if replaceTabs:
                makeHtml.replaceTabs()

            try:
                makeHtml.saveHTML5(savePath)
                wSuccess = True
            except Exception as e:
                errMsg = str(e)

        elif theFmt == self.FMT_NWD:
            makeNwd = ToMarkdown(self.theProject, self.theParent)
            makeNwd.setKeepMarkdown(True)
            self._doBuild(makeNwd, doConvert=False)
            if replaceTabs:
                makeNwd.replaceTabs(spaceChar=" ")

            try:
                makeNwd.saveRawMarkdown(savePath)
                wSuccess = True
            except Exception as e:
                errMsg = str(e)

        elif theFmt in (self.FMT_MD, self.FMT_GH):
            makeMd = ToMarkdown(self.theProject, self.theParent)
            if theFmt == self.FMT_GH:
                makeMd.setGitHubMarkdown()
            else:
                makeMd.setStandardMarkdown()

            self._doBuild(makeMd)
            if replaceTabs:
                makeMd.replaceTabs(nSpaces=4, spaceChar=" ")

            try:
                makeMd.saveMarkdown(savePath)
                wSuccess = True
            except Exception as e:
                errMsg = str(e)

        elif theFmt == self.FMT_JSON_H or theFmt == self.FMT_JSON_M:
            jsonData = {
                "meta": {
                    "workingTitle": self.theProject.projName,
                    "novelTitle": self.theProject.bookTitle,
                    "authors": self.theProject.bookAuthors,
                    "buildTime": self.buildTime,
                }
            }

            if theFmt == self.FMT_JSON_H:
                makeHtml = ToHtml(self.theProject, self.theParent)
                self._doBuild(makeHtml)
                if replaceTabs:
                    makeHtml.replaceTabs()

                theBody = []
                for htmlPage in makeHtml.fullHTML:
                    theBody.append(htmlPage.rstrip("\n").split("\n"))
                jsonData["text"] = {
                    "css": self.htmlStyle,
                    "html": theBody,
                }

            elif theFmt == self.FMT_JSON_M:
                makeMd = ToHtml(self.theProject, self.theParent)
                makeMd.setKeepMarkdown(True)
                self._doBuild(makeMd, doConvert=False)
                if replaceTabs:
                    makeMd.replaceTabs(spaceChar=" ")

                theBody = []
                for nwdPage in makeMd.theMarkdown:
                    theBody.append(nwdPage.split("\n"))
                jsonData["text"] = {
                    "nwd": theBody,
                }

            try:
                with open(savePath, mode="w", encoding="utf8") as outFile:
                    outFile.write(json.dumps(jsonData, indent=2))
                    wSuccess = True
            except Exception as e:
                errMsg = str(e)

        elif theFmt == self.FMT_PDF:
            try:
                thePrinter = QPrinter()
                thePrinter.setOutputFormat(QPrinter.PdfFormat)
                thePrinter.setOrientation(QPrinter.Portrait)
                thePrinter.setDuplex(QPrinter.DuplexLongSide)
                thePrinter.setFontEmbeddingEnabled(True)
                thePrinter.setColorMode(QPrinter.Color)
                thePrinter.setOutputFileName(savePath)
                self.docView.qDocument.print(thePrinter)
                wSuccess = True

            except Exception as e:
                errMsg - str(e)

        else:
            errMsg = self.tr("Unknown format")

        # Report to user
        if wSuccess:
            self.theParent.makeAlert(
                "%s<br>%s" %
                (self.tr("{0} file successfully written to:").format(textFmt),
                 savePath), nwAlert.INFO)
        else:
            self.theParent.makeAlert(
                self.tr("Failed to write {0} file. {1}").format(
                    textFmt, errMsg), nwAlert.ERROR)

        return wSuccess

    def _printDocument(self):
        """Open the print preview dialog.
        """
        thePreview = QPrintPreviewDialog(self)
        thePreview.paintRequested.connect(self._doPrintPreview)
        thePreview.exec_()
        return

    def _doPrintPreview(self, thePrinter):
        """Connect the print preview painter to the document viewer.
        """
        qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
        thePrinter.setOrientation(QPrinter.Portrait)
        self.docView.qDocument.print(thePrinter)
        qApp.restoreOverrideCursor()
        return

    def _selectFont(self):
        """Open the QFontDialog and set a font for the font style.
        """
        currFont = QFont()
        currFont.setFamily(self.textFont.text())
        currFont.setPointSize(self.textSize.value())
        theFont, theStatus = QFontDialog.getFont(currFont, self)
        if theStatus:
            self.textFont.setText(theFont.family())
            self.textSize.setValue(theFont.pointSize())

        self.raise_()  # Move the dialog to front (fixes a bug on macOS)

        return

    def _loadCache(self):
        """Save the current data to cache.
        """
        buildCache = os.path.join(self.theProject.projCache,
                                  nwFiles.BUILD_CACHE)
        dataCount = 0
        if os.path.isfile(buildCache):

            logger.debug("Loading build cache")
            try:
                with open(buildCache, mode="r", encoding="utf8") as inFile:
                    theJson = inFile.read()
                theData = json.loads(theJson)
            except Exception:
                logger.error("Failed to load build cache")
                nw.logException()
                return False

            if "buildTime" in theData.keys():
                self.buildTime = theData["buildTime"]
            if "htmlStyle" in theData.keys():
                self.htmlStyle = theData["htmlStyle"]
                dataCount += 1
            if "htmlText" in theData.keys():
                self.htmlText = theData["htmlText"]
                dataCount += 1

        return dataCount == 2

    def _saveCache(self):
        """Save the current data to cache.
        """
        buildCache = os.path.join(self.theProject.projCache,
                                  nwFiles.BUILD_CACHE)

        logger.debug("Saving build cache")
        try:
            with open(buildCache, mode="w+", encoding="utf8") as outFile:
                outFile.write(
                    json.dumps(
                        {
                            "buildTime": self.buildTime,
                            "htmlStyle": self.htmlStyle,
                            "htmlText": self.htmlText,
                        },
                        indent=2))
        except Exception:
            logger.error("Failed to save build cache")
            nw.logException()
            return False

        return True

    def _doClose(self):
        """Close button was clicked.
        """
        self.close()
        return

    ##
    #  Events
    ##

    def closeEvent(self, theEvent):
        """Capture the user closing the window so we can save settings.
        """
        self._saveSettings()
        self.docView.clear()
        theEvent.accept()
        return

    ##
    #  Internal Functions
    ##

    def _saveSettings(self):
        """Save the various user settings.
        """
        logger.debug("Saving GuiBuildNovel settings")

        # Formatting
        self.theProject.setTitleFormat({
            "title":
            self.fmtTitle.text().strip(),
            "chapter":
            self.fmtChapter.text().strip(),
            "unnumbered":
            self.fmtUnnumbered.text().strip(),
            "scene":
            self.fmtScene.text().strip(),
            "section":
            self.fmtSection.text().strip(),
        })

        buildLang = self.buildLang.currentData()
        winWidth = self.mainConf.rpxInt(self.width())
        winHeight = self.mainConf.rpxInt(self.height())
        justifyText = self.justifyText.isChecked()
        noStyling = self.noStyling.isChecked()
        textFont = self.textFont.text()
        textSize = self.textSize.value()
        lineHeight = self.lineHeight.value()
        novelFiles = self.novelFiles.isChecked()
        noteFiles = self.noteFiles.isChecked()
        ignoreFlag = self.ignoreFlag.isChecked()
        incSynopsis = self.includeSynopsis.isChecked()
        incComments = self.includeComments.isChecked()
        incKeywords = self.includeKeywords.isChecked()
        incBodyText = self.includeBody.isChecked()
        replaceTabs = self.replaceTabs.isChecked()
        replaceUCode = self.replaceUCode.isChecked()

        mainSplit = self.mainSplit.sizes()
        boxWidth = self.mainConf.rpxInt(mainSplit[0])
        docWidth = self.mainConf.rpxInt(mainSplit[1])

        self.theProject.setProjectLang(buildLang)

        # GUI Settings
        self.optState.setValue("GuiBuildNovel", "winWidth", winWidth)
        self.optState.setValue("GuiBuildNovel", "winHeight", winHeight)
        self.optState.setValue("GuiBuildNovel", "boxWidth", boxWidth)
        self.optState.setValue("GuiBuildNovel", "docWidth", docWidth)
        self.optState.setValue("GuiBuildNovel", "justifyText", justifyText)
        self.optState.setValue("GuiBuildNovel", "noStyling", noStyling)
        self.optState.setValue("GuiBuildNovel", "textFont", textFont)
        self.optState.setValue("GuiBuildNovel", "textSize", textSize)
        self.optState.setValue("GuiBuildNovel", "lineHeight", lineHeight)
        self.optState.setValue("GuiBuildNovel", "addNovel", novelFiles)
        self.optState.setValue("GuiBuildNovel", "addNotes", noteFiles)
        self.optState.setValue("GuiBuildNovel", "ignoreFlag", ignoreFlag)
        self.optState.setValue("GuiBuildNovel", "incSynopsis", incSynopsis)
        self.optState.setValue("GuiBuildNovel", "incComments", incComments)
        self.optState.setValue("GuiBuildNovel", "incKeywords", incKeywords)
        self.optState.setValue("GuiBuildNovel", "incBodyText", incBodyText)
        self.optState.setValue("GuiBuildNovel", "replaceTabs", replaceTabs)
        self.optState.setValue("GuiBuildNovel", "replaceUCode", replaceUCode)

        self.optState.saveSettings()

        return

    def _reFmtCodes(self, theFormat):
        """Translates old formatting codes to new ones.
        """
        theFormat = theFormat.replace(r"%chnum%", r"%ch%")
        theFormat = theFormat.replace(r"%scnum%", r"%sc%")
        theFormat = theFormat.replace(r"%scabsnum%", r"%sca%")
        theFormat = theFormat.replace(r"%chnumword%", r"%chw%")
        return theFormat
示例#2
0
class GuiBuildNovel(QDialog):

    FMT_ODT = 1
    FMT_PDF = 2
    FMT_HTM = 3
    FMT_MD = 4
    FMT_NWD = 5
    FMT_TXT = 6
    FMT_JSON_H = 7
    FMT_JSON_M = 8

    def __init__(self, theParent, theProject):
        QDialog.__init__(self, theParent)

        logger.debug("Initialising GuiBuildNovel ...")
        self.setObjectName("GuiBuildNovel")

        self.mainConf = nw.CONFIG
        self.theProject = theProject
        self.theParent = theParent
        self.theTheme = theParent.theTheme
        self.optState = self.theProject.optState

        self.htmlText = []  # List of html documents
        self.htmlStyle = []  # List of html styles
        self.nwdText = []  # List of markdown documents
        self.buildTime = 0  # The timestamp of the last build

        self.setWindowTitle("Build Novel Project")
        self.setMinimumWidth(self.mainConf.pxInt(700))
        self.setMinimumHeight(self.mainConf.pxInt(600))

        self.resize(
            self.mainConf.pxInt(
                self.optState.getInt("GuiBuildNovel", "winWidth", 900)),
            self.mainConf.pxInt(
                self.optState.getInt("GuiBuildNovel", "winHeight", 800)))

        self.docView = GuiBuildNovelDocView(self, self.theProject)

        # Title Formats
        # =============

        self.titleGroup = QGroupBox("Title Formats for Novel Files", self)
        self.titleForm = QGridLayout(self)
        self.titleGroup.setLayout(self.titleForm)

        fmtHelp = (r"<b>Formatting Codes:</b><br>"
                   r"%title% for the title as set in the document<br>"
                   r"%ch% for chapter number (1, 2, 3)<br>"
                   r"%chw% for chapter number as a word (one, two)<br>"
                   r"%chI% for chapter number in upper case Roman<br>"
                   r"%chi% for chapter number in lower case Roman<br>"
                   r"%sc% for scene number within chapter<br>"
                   r"%sca% for scene number within novel")
        fmtScHelp = (
            r"<br><br>"
            r"Leave blank to skip this heading, or set to a static text, like "
            r"for instance '* * *', to make a separator. The separator will "
            r"be centred automatically and only appear between sections of "
            r"the same type.")
        xFmt = self.mainConf.pxInt(100)

        self.fmtTitle = QLineEdit()
        self.fmtTitle.setMaxLength(200)
        self.fmtTitle.setMinimumWidth(xFmt)
        self.fmtTitle.setToolTip(fmtHelp)
        self.fmtTitle.setText(
            self._reFmtCodes(self.theProject.titleFormat["title"]))

        self.fmtChapter = QLineEdit()
        self.fmtChapter.setMaxLength(200)
        self.fmtChapter.setMinimumWidth(xFmt)
        self.fmtChapter.setToolTip(fmtHelp)
        self.fmtChapter.setText(
            self._reFmtCodes(self.theProject.titleFormat["chapter"]))

        self.fmtUnnumbered = QLineEdit()
        self.fmtUnnumbered.setMaxLength(200)
        self.fmtUnnumbered.setMinimumWidth(xFmt)
        self.fmtUnnumbered.setToolTip(fmtHelp)
        self.fmtUnnumbered.setText(
            self._reFmtCodes(self.theProject.titleFormat["unnumbered"]))

        self.fmtScene = QLineEdit()
        self.fmtScene.setMaxLength(200)
        self.fmtScene.setMinimumWidth(xFmt)
        self.fmtScene.setToolTip(fmtHelp + fmtScHelp)
        self.fmtScene.setText(
            self._reFmtCodes(self.theProject.titleFormat["scene"]))

        self.fmtSection = QLineEdit()
        self.fmtSection.setMaxLength(200)
        self.fmtSection.setMinimumWidth(xFmt)
        self.fmtSection.setToolTip(fmtHelp + fmtScHelp)
        self.fmtSection.setText(
            self._reFmtCodes(self.theProject.titleFormat["section"]))

        # Dummy boxes due to QGridView and QLineEdit expand bug
        self.boxTitle = QHBoxLayout()
        self.boxTitle.addWidget(self.fmtTitle)
        self.boxChapter = QHBoxLayout()
        self.boxChapter.addWidget(self.fmtChapter)
        self.boxUnnumbered = QHBoxLayout()
        self.boxUnnumbered.addWidget(self.fmtUnnumbered)
        self.boxScene = QHBoxLayout()
        self.boxScene.addWidget(self.fmtScene)
        self.boxSection = QHBoxLayout()
        self.boxSection.addWidget(self.fmtSection)

        self.titleForm.addWidget(QLabel("Title"), 0, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addLayout(self.boxTitle, 0, 1, 1, 1, Qt.AlignRight)
        self.titleForm.addWidget(QLabel("Chapter"), 1, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addLayout(self.boxChapter, 1, 1, 1, 1, Qt.AlignRight)
        self.titleForm.addWidget(QLabel("Unnumbered"), 2, 0, 1, 1,
                                 Qt.AlignLeft)
        self.titleForm.addLayout(self.boxUnnumbered, 2, 1, 1, 1, Qt.AlignRight)
        self.titleForm.addWidget(QLabel("Scene"), 3, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addLayout(self.boxScene, 3, 1, 1, 1, Qt.AlignRight)
        self.titleForm.addWidget(QLabel("Section"), 4, 0, 1, 1, Qt.AlignLeft)
        self.titleForm.addLayout(self.boxSection, 4, 1, 1, 1, Qt.AlignRight)

        self.titleForm.setColumnStretch(0, 0)
        self.titleForm.setColumnStretch(1, 1)

        # Text Options
        # =============

        self.formatGroup = QGroupBox("Formatting Options", self)
        self.formatForm = QGridLayout(self)
        self.formatGroup.setLayout(self.formatForm)

        ## Font Family
        self.textFont = QLineEdit()
        self.textFont.setReadOnly(True)
        self.textFont.setMinimumWidth(xFmt)
        self.textFont.setText(
            self.optState.getString("GuiBuildNovel", "textFont",
                                    self.mainConf.textFont))
        self.fontButton = QPushButton("...")
        self.fontButton.setMaximumWidth(
            int(2.5 * self.theTheme.getTextWidth("...")))
        self.fontButton.clicked.connect(self._selectFont)

        self.textSize = QSpinBox(self)
        self.textSize.setFixedWidth(5 * self.theTheme.textNWidth)
        self.textSize.setMinimum(6)
        self.textSize.setMaximum(72)
        self.textSize.setSingleStep(1)
        self.textSize.setToolTip(
            "The size is used for PDF and printing. Other formats have no size set."
        )
        self.textSize.setValue(
            self.optState.getInt("GuiBuildNovel", "textSize",
                                 self.mainConf.textSize))

        self.justifyText = QSwitch()
        self.justifyText.setToolTip(
            "Applies to PDF, printing, HTML, and Open Document exports.")
        self.justifyText.setChecked(
            self.optState.getBool("GuiBuildNovel", "justifyText", False))

        self.noStyling = QSwitch()
        self.noStyling.setToolTip("Disable all styling of the text.")
        self.noStyling.setChecked(
            self.optState.getBool("GuiBuildNovel", "noStyling", False))

        # Dummy box due to QGridView and QLineEdit expand bug
        self.boxFont = QHBoxLayout()
        self.boxFont.addWidget(self.textFont)

        self.formatForm.addWidget(QLabel("Font family"), 0, 0, 1, 1,
                                  Qt.AlignLeft)
        self.formatForm.addLayout(self.boxFont, 0, 1, 1, 1, Qt.AlignRight)
        self.formatForm.addWidget(self.fontButton, 0, 2, 1, 1, Qt.AlignRight)
        self.formatForm.addWidget(QLabel("Font size"), 1, 0, 1, 1,
                                  Qt.AlignLeft)
        self.formatForm.addWidget(self.textSize, 1, 1, 1, 2, Qt.AlignRight)
        self.formatForm.addWidget(QLabel("Justify text"), 2, 0, 1, 1,
                                  Qt.AlignLeft)
        self.formatForm.addWidget(self.justifyText, 2, 1, 1, 2, Qt.AlignRight)
        self.formatForm.addWidget(QLabel("Disable styling"), 3, 0, 1, 1,
                                  Qt.AlignLeft)
        self.formatForm.addWidget(self.noStyling, 3, 1, 1, 2, Qt.AlignRight)

        self.formatForm.setColumnStretch(0, 0)
        self.formatForm.setColumnStretch(1, 1)
        self.formatForm.setColumnStretch(2, 0)

        # Include Switches
        # ================

        self.textGroup = QGroupBox("Text Options", self)
        self.textForm = QGridLayout(self)
        self.textGroup.setLayout(self.textForm)

        self.includeSynopsis = QSwitch()
        self.includeSynopsis.setToolTip(
            "Include synopsis comments in the output.")
        self.includeSynopsis.setChecked(
            self.optState.getBool("GuiBuildNovel", "incSynopsis", False))

        self.includeComments = QSwitch()
        self.includeComments.setToolTip(
            "Include plain comments in the output.")
        self.includeComments.setChecked(
            self.optState.getBool("GuiBuildNovel", "incComments", False))

        self.includeKeywords = QSwitch()
        self.includeKeywords.setToolTip(
            "Include meta keywords (tags, references) in the output.")
        self.includeKeywords.setChecked(
            self.optState.getBool("GuiBuildNovel", "incKeywords", False))

        self.includeBody = QSwitch()
        self.includeBody.setToolTip("Include body text in the output.")
        self.includeBody.setChecked(
            self.optState.getBool("GuiBuildNovel", "incBodyText", True))

        self.textForm.addWidget(QLabel("Include synopsis"), 0, 0, 1, 1,
                                Qt.AlignLeft)
        self.textForm.addWidget(self.includeSynopsis, 0, 1, 1, 1,
                                Qt.AlignRight)
        self.textForm.addWidget(QLabel("Include comments"), 1, 0, 1, 1,
                                Qt.AlignLeft)
        self.textForm.addWidget(self.includeComments, 1, 1, 1, 1,
                                Qt.AlignRight)
        self.textForm.addWidget(QLabel("Include keywords"), 2, 0, 1, 1,
                                Qt.AlignLeft)
        self.textForm.addWidget(self.includeKeywords, 2, 1, 1, 1,
                                Qt.AlignRight)
        self.textForm.addWidget(QLabel("Include body text"), 3, 0, 1, 1,
                                Qt.AlignLeft)
        self.textForm.addWidget(self.includeBody, 3, 1, 1, 1, Qt.AlignRight)

        self.textForm.setColumnStretch(0, 1)
        self.textForm.setColumnStretch(1, 0)

        # File Filter Options
        # ===================

        self.fileGroup = QGroupBox("File Filter Options", self)
        self.fileForm = QGridLayout(self)
        self.fileGroup.setLayout(self.fileForm)

        self.novelFiles = QSwitch()
        self.novelFiles.setToolTip(
            "Include files with layouts 'Book', 'Page', 'Partition', "
            "'Chapter', 'Unnumbered', and 'Scene'.")
        self.novelFiles.setChecked(
            self.optState.getBool("GuiBuildNovel", "addNovel", True))

        self.noteFiles = QSwitch()
        self.noteFiles.setToolTip("Include files with layout 'Note'.")
        self.noteFiles.setChecked(
            self.optState.getBool("GuiBuildNovel", "addNotes", False))

        self.ignoreFlag = QSwitch()
        self.ignoreFlag.setToolTip(
            "Ignore the 'Include when building project' setting and include "
            "all files in the output.")
        self.ignoreFlag.setChecked(
            self.optState.getBool("GuiBuildNovel", "ignoreFlag", False))

        self.fileForm.addWidget(QLabel("Include novel files"), 0, 0, 1, 1,
                                Qt.AlignLeft)
        self.fileForm.addWidget(self.novelFiles, 0, 1, 1, 1, Qt.AlignRight)
        self.fileForm.addWidget(QLabel("Include note files"), 1, 0, 1, 1,
                                Qt.AlignLeft)
        self.fileForm.addWidget(self.noteFiles, 1, 1, 1, 1, Qt.AlignRight)
        self.fileForm.addWidget(QLabel("Ignore export flag"), 2, 0, 1, 1,
                                Qt.AlignLeft)
        self.fileForm.addWidget(self.ignoreFlag, 2, 1, 1, 1, Qt.AlignRight)

        self.fileForm.setColumnStretch(0, 1)
        self.fileForm.setColumnStretch(1, 0)

        # Export Options
        # ==============

        self.exportGroup = QGroupBox("Export Options", self)
        self.exportForm = QGridLayout(self)
        self.exportGroup.setLayout(self.exportForm)

        self.replaceTabs = QSwitch()
        self.replaceTabs.setToolTip("Replace all tabs with eight spaces.")
        self.replaceTabs.setChecked(
            self.optState.getBool("GuiBuildNovel", "replaceTabs", False))

        self.exportForm.addWidget(QLabel("Replace tabs with spaces"), 0, 0, 1,
                                  1, Qt.AlignLeft)
        self.exportForm.addWidget(self.replaceTabs, 0, 1, 1, 1, Qt.AlignRight)

        self.exportForm.setColumnStretch(0, 1)
        self.exportForm.setColumnStretch(1, 0)

        # Build Button
        # ============

        self.buildProgress = QProgressBar()
        self.buildProgress = QProgressBar()

        self.buildNovel = QPushButton("Build Project")
        self.buildNovel.clicked.connect(self._buildPreview)

        # Action Buttons
        # ==============

        self.buttonBox = QHBoxLayout()

        self.btnPrint = QPushButton("Print")
        self.btnPrint.clicked.connect(self._printDocument)

        self.btnSave = QPushButton("Save As")
        self.saveMenu = QMenu(self)
        self.btnSave.setMenu(self.saveMenu)

        self.saveODT = QAction("Open Document (.odt)", self)
        self.saveODT.triggered.connect(
            lambda: self._saveDocument(self.FMT_ODT))
        self.saveMenu.addAction(self.saveODT)

        self.savePDF = QAction("Portable Document Format (.pdf)", self)
        self.savePDF.triggered.connect(
            lambda: self._saveDocument(self.FMT_PDF))
        self.saveMenu.addAction(self.savePDF)

        self.saveHTM = QAction("novelWriter HTML (.htm)", self)
        self.saveHTM.triggered.connect(
            lambda: self._saveDocument(self.FMT_HTM))
        self.saveMenu.addAction(self.saveHTM)

        self.saveNWD = QAction("novelWriter Markdown (.nwd)", self)
        self.saveNWD.triggered.connect(
            lambda: self._saveDocument(self.FMT_NWD))
        self.saveMenu.addAction(self.saveNWD)

        if self.mainConf.verQtValue >= 51400:
            self.saveMD = QAction("Markdown (.md)", self)
            self.saveMD.triggered.connect(
                lambda: self._saveDocument(self.FMT_MD))
            self.saveMenu.addAction(self.saveMD)

        self.saveTXT = QAction("Plain Text (.txt)", self)
        self.saveTXT.triggered.connect(
            lambda: self._saveDocument(self.FMT_TXT))
        self.saveMenu.addAction(self.saveTXT)

        self.saveJsonH = QAction("JSON + novelWriter HTML (.json)", self)
        self.saveJsonH.triggered.connect(
            lambda: self._saveDocument(self.FMT_JSON_H))
        self.saveMenu.addAction(self.saveJsonH)

        self.saveJsonM = QAction("JSON + novelWriters Markdown (.json)", self)
        self.saveJsonM.triggered.connect(
            lambda: self._saveDocument(self.FMT_JSON_M))
        self.saveMenu.addAction(self.saveJsonM)

        self.btnClose = QPushButton("Close")
        self.btnClose.clicked.connect(self._doClose)

        self.buttonBox.addWidget(self.btnSave)
        self.buttonBox.addWidget(self.btnPrint)
        self.buttonBox.addWidget(self.btnClose)
        self.buttonBox.setSpacing(4)

        # Assemble GUI
        # ============

        # Splitter Position
        boxWidth = self.mainConf.pxInt(350)
        boxWidth = self.optState.getInt("GuiBuildNovel", "boxWidth", boxWidth)
        docWidth = max(self.width() - boxWidth, 100)
        docWidth = self.optState.getInt("GuiBuildNovel", "docWidth", docWidth)

        # The Tool Box
        self.toolsBox = QVBoxLayout()
        self.toolsBox.addWidget(self.titleGroup)
        self.toolsBox.addWidget(self.formatGroup)
        self.toolsBox.addWidget(self.textGroup)
        self.toolsBox.addWidget(self.fileGroup)
        self.toolsBox.addWidget(self.exportGroup)
        self.toolsBox.addStretch(1)

        # Tool Box Wrapper Widget
        self.toolsWidget = QWidget()
        self.toolsWidget.setSizePolicy(QSizePolicy.MinimumExpanding,
                                       QSizePolicy.Minimum)
        self.toolsWidget.setLayout(self.toolsBox)

        # Tool Box Scroll Area
        self.toolsArea = QScrollArea()
        self.toolsArea.setMinimumWidth(self.mainConf.pxInt(250))
        self.toolsArea.setWidgetResizable(True)
        self.toolsArea.setWidget(self.toolsWidget)

        if self.mainConf.hideVScroll:
            self.toolsArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        else:
            self.toolsArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)

        if self.mainConf.hideHScroll:
            self.toolsArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        else:
            self.toolsArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)

        # Tools and Buttons Layout
        self.innerBox = QVBoxLayout()
        self.innerBox.addWidget(self.toolsArea)
        self.innerBox.addSpacing(8)
        self.innerBox.addWidget(self.buildProgress)
        self.innerBox.addWidget(self.buildNovel)
        self.innerBox.addSpacing(8)
        self.innerBox.addLayout(self.buttonBox)

        # Tools and Buttons Wrapper Widget
        self.innerWidget = QWidget()
        self.innerWidget.setLayout(self.innerBox)

        # Main Dialog Splitter
        self.mainSplit = QSplitter(Qt.Horizontal)
        self.mainSplit.addWidget(self.innerWidget)
        self.mainSplit.addWidget(self.docView)
        self.mainSplit.setSizes([boxWidth, docWidth])

        # Outer Layout
        self.outerBox = QHBoxLayout()
        self.outerBox.addWidget(self.mainSplit)

        self.setLayout(self.outerBox)
        self.buildNovel.setFocus()

        logger.debug("GuiBuildNovel initialisation complete")

        return

    def viewCachedDoc(self):
        """Load the previously generated document from cache.
        """
        if self._loadCache():
            textFont = self.textFont.text()
            textSize = self.textSize.value()
            justifyText = self.justifyText.isChecked()
            self.docView.setTextFont(textFont, textSize)
            self.docView.setJustify(justifyText)
            if self.noStyling.isChecked():
                self.docView.clearStyleSheet()
            else:
                self.docView.setStyleSheet(self.htmlStyle)

            htmlSize = sum([len(x) for x in self.htmlText])
            if htmlSize < nwConst.maxBuildSize:
                qApp.processEvents()
                self.docView.setContent(self.htmlText, self.buildTime)
            else:
                self.docView.setText(
                    "Failed to generate preview. The result is too big.")
                self._enableQtSave(False)

        else:
            self.htmlText = []
            self.htmlStyle = []
            self.nwdText = []
            self.buildTime = 0
            return False

        return True

    ##
    #  Slots
    ##

    def _buildPreview(self):
        """Build a preview of the project in the document viewer.
        """
        # Get Settings
        fmtTitle = self.fmtTitle.text().strip()
        fmtChapter = self.fmtChapter.text().strip()
        fmtUnnumbered = self.fmtUnnumbered.text().strip()
        fmtScene = self.fmtScene.text().strip()
        fmtSection = self.fmtSection.text().strip()
        justifyText = self.justifyText.isChecked()
        noStyling = self.noStyling.isChecked()
        textFont = self.textFont.text()
        textSize = self.textSize.value()
        incSynopsis = self.includeSynopsis.isChecked()
        incComments = self.includeComments.isChecked()
        incKeywords = self.includeKeywords.isChecked()
        novelFiles = self.novelFiles.isChecked()
        noteFiles = self.noteFiles.isChecked()
        ignoreFlag = self.ignoreFlag.isChecked()
        includeBody = self.includeBody.isChecked()
        replaceTabs = self.replaceTabs.isChecked()

        makeHtml = ToHtml(self.theProject, self.theParent)
        makeHtml.setTitleFormat(fmtTitle)
        makeHtml.setChapterFormat(fmtChapter)
        makeHtml.setUnNumberedFormat(fmtUnnumbered)
        makeHtml.setSceneFormat(fmtScene, fmtScene == "")
        makeHtml.setSectionFormat(fmtSection, fmtSection == "")
        makeHtml.setBodyText(includeBody)
        makeHtml.setSynopsis(incSynopsis)
        makeHtml.setComments(incComments)
        makeHtml.setKeywords(incKeywords)
        makeHtml.setJustify(justifyText)
        makeHtml.setStyles(not noStyling)

        # Make sure the tree order is correct
        self.theParent.treeView.flushTreeOrder()

        self.buildProgress.setMaximum(len(self.theProject.projTree))
        self.buildProgress.setValue(0)

        tStart = int(time())

        self.htmlText = []
        self.htmlStyle = []
        self.nwdText = []

        htmlSize = 0

        for nItt, tItem in enumerate(self.theProject.projTree):

            noteRoot = noteFiles
            noteRoot &= tItem.itemType == nwItemType.ROOT
            noteRoot &= tItem.itemClass != nwItemClass.NOVEL
            noteRoot &= tItem.itemClass != nwItemClass.ARCHIVE

            try:
                if noteRoot:
                    # Add headers for root folders of notes
                    makeHtml.addRootHeading(tItem.itemHandle)
                    makeHtml.doConvert()
                    self.htmlText.append(makeHtml.getResult())
                    self.nwdText.append(makeHtml.getFilteredMarkdown())
                    htmlSize += makeHtml.getResultSize()

                elif self._checkInclude(tItem, noteFiles, novelFiles,
                                        ignoreFlag):
                    makeHtml.setText(tItem.itemHandle)
                    makeHtml.doAutoReplace()
                    makeHtml.tokenizeText()
                    makeHtml.doHeaders()
                    makeHtml.doConvert()
                    makeHtml.doPostProcessing()
                    self.htmlText.append(makeHtml.getResult())
                    self.nwdText.append(makeHtml.getFilteredMarkdown())
                    htmlSize += makeHtml.getResultSize()

            except Exception as e:
                logger.error("Failed to generate html of document '%s'" %
                             tItem.itemHandle)
                logger.error(str(e))
                self.docView.setText(
                    ("Failed to generate preview. "
                     "Document with title '%s' could not be parsed.") %
                    tItem.itemName)
                return False

            # Update progress bar, also for skipped items
            self.buildProgress.setValue(nItt + 1)

        if makeHtml.errData:
            self.theParent.makeAlert(
                ("There were problems when building the project:"
                 "<br>-&nbsp;%s") % "<br>-&nbsp;".join(makeHtml.errData),
                nwAlert.ERROR)

        if replaceTabs:
            htmlText = []
            eightSpace = "&nbsp;" * 8
            for aLine in self.htmlText:
                htmlText.append(aLine.replace("\t", eightSpace))
            self.htmlText = htmlText

            nwdText = []
            for aLine in self.nwdText:
                nwdText.append(aLine.replace("\t", "        "))
            self.nwdText = nwdText

        tEnd = int(time())
        logger.debug("Built project in %.3f ms" % (1000 * (tEnd - tStart)))
        self.htmlStyle = makeHtml.getStyleSheet()
        self.buildTime = tEnd

        # Load the preview document with the html data
        self.docView.setTextFont(textFont, textSize)
        self.docView.setJustify(justifyText)
        if noStyling:
            self.docView.clearStyleSheet()
        else:
            self.docView.setStyleSheet(self.htmlStyle)

        if htmlSize < nwConst.maxBuildSize:
            self.docView.setContent(self.htmlText, self.buildTime)
            self._enableQtSave(True)
        else:
            self.docView.setText(
                "Failed to generate preview. The result is too big.")
            self._enableQtSave(False)

        self._saveCache()

        return

    def _checkInclude(self, theItem, noteFiles, novelFiles, ignoreFlag):
        """This function checks whether a file should be included in the
        export or not. For standard note and novel files, this is
        controlled by the options selected by the user. For other files
        classified as non-exportable, a few checks must be made, and the
        following are not:
        * Items that are not actual files.
        * Items that have been orphaned which are tagged as NO_LAYOUT
          and NO_CLASS.
        * Items that appear in the TRASH folder or have parent set to
          None (orphaned files).
        """
        if theItem is None:
            return False

        if not theItem.isExported and not ignoreFlag:
            return False

        isNone = theItem.itemType != nwItemType.FILE
        isNone |= theItem.itemLayout == nwItemLayout.NO_LAYOUT
        isNone |= theItem.itemClass == nwItemClass.NO_CLASS
        isNone |= theItem.itemClass == nwItemClass.TRASH
        isNone |= theItem.itemParent == self.theProject.projTree.trashRoot()
        isNone |= theItem.itemParent is None
        isNote = theItem.itemLayout == nwItemLayout.NOTE
        isNovel = not isNone and not isNote

        if isNone:
            return False
        if isNote and not noteFiles:
            return False
        if isNovel and not novelFiles:
            return False

        rootItem = self.theProject.projTree.getRootItem(theItem.itemHandle)
        if rootItem.itemClass == nwItemClass.ARCHIVE:
            return False

        return True

    def _saveDocument(self, theFormat):
        """Save the document to various formats.
        """
        byteFmt = QByteArray()
        fileExt = ""
        textFmt = ""
        outTool = ""

        # Create the settings
        if theFormat == self.FMT_ODT:
            byteFmt.append("odf")
            fileExt = "odt"
            textFmt = "Open Document"
            outTool = "Qt"

        elif theFormat == self.FMT_PDF:
            fileExt = "pdf"
            textFmt = "PDF"
            outTool = "QtPrint"

        elif theFormat == self.FMT_HTM:
            fileExt = "htm"
            textFmt = "Plain HTML"
            outTool = "NW"

        elif theFormat == self.FMT_MD:
            byteFmt.append("markdown")
            fileExt = "md"
            textFmt = "Markdown"
            outTool = "Qt"

        elif theFormat == self.FMT_NWD:
            fileExt = "nwd"
            textFmt = "%s Markdown" % nw.__package__
            outTool = "NW"

        elif theFormat == self.FMT_TXT:
            byteFmt.append("plaintext")
            fileExt = "txt"
            textFmt = "Plain Text"
            outTool = "Qt"

        elif theFormat == self.FMT_JSON_H:
            fileExt = "json"
            textFmt = "JSON + %s HTML" % nw.__package__
            outTool = "NW"

        elif theFormat == self.FMT_JSON_M:
            fileExt = "json"
            textFmt = "JSON + %s Markdown" % nw.__package__
            outTool = "NW"

        else:
            return False

        # Generate the file name
        if fileExt:

            cleanName = makeFileNameSafe(self.theProject.projName)
            fileName = "%s.%s" % (cleanName, fileExt)
            saveDir = self.mainConf.lastPath
            savePath = os.path.join(saveDir, fileName)
            if not os.path.isdir(saveDir):
                saveDir = self.mainConf.homePath

            if self.mainConf.showGUI:
                dlgOpt = QFileDialog.Options()
                dlgOpt |= QFileDialog.DontUseNativeDialog
                savePath, _ = QFileDialog.getSaveFileName(self,
                                                          "Save Document As",
                                                          savePath,
                                                          options=dlgOpt)
                if not savePath:
                    return False

            self.mainConf.setLastPath(savePath)

        else:
            return False

        # Do the actual writing
        wSuccess = False
        errMsg = ""
        if outTool == "Qt":
            docWriter = QTextDocumentWriter()
            docWriter.setFileName(savePath)
            docWriter.setFormat(byteFmt)
            wSuccess = docWriter.write(self.docView.qDocument)

        elif outTool == "NW":
            try:
                with open(savePath, mode="w", encoding="utf8") as outFile:
                    if theFormat == self.FMT_HTM:
                        # Write novelWriter HTML data
                        theStyle = self.htmlStyle.copy()
                        theStyle.append(
                            r"article {width: 800px; margin: 40px auto;}")
                        bodyText = "".join(self.htmlText)
                        bodyText = bodyText.replace("\t", "&#09;")

                        theHtml = ("<!DOCTYPE html>\n"
                                   "<html>\n"
                                   "<head>\n"
                                   "<meta charset='utf-8'>\n"
                                   "<title>{projTitle:s}</title>\n"
                                   "</head>\n"
                                   "<style>\n"
                                   "{htmlStyle:s}\n"
                                   "</style>\n"
                                   "<body>\n"
                                   "<article>\n"
                                   "{bodyText:s}\n"
                                   "</article>\n"
                                   "</body>\n"
                                   "</html>\n").format(
                                       projTitle=self.theProject.projName,
                                       htmlStyle="\n".join(theStyle),
                                       bodyText=bodyText,
                                   )
                        outFile.write(theHtml)

                    elif theFormat == self.FMT_NWD:
                        # Write novelWriter markdown data
                        for aLine in self.nwdText:
                            outFile.write(aLine)

                    elif theFormat == self.FMT_JSON_H or theFormat == self.FMT_JSON_M:
                        jsonData = {
                            "meta": {
                                "workingTitle": self.theProject.projName,
                                "novelTitle": self.theProject.bookTitle,
                                "authors": self.theProject.bookAuthors,
                                "buildTime": self.buildTime,
                            }
                        }

                        if theFormat == self.FMT_JSON_H:
                            theBody = []
                            for htmlPage in self.htmlText:
                                theBody.append(
                                    htmlPage.rstrip("\n").split("\n"))
                            jsonData["text"] = {
                                "css": self.htmlStyle,
                                "html": theBody,
                            }
                        elif theFormat == self.FMT_JSON_M:
                            theBody = []
                            for nwdPage in self.nwdText:
                                theBody.append(nwdPage.split("\n"))
                            jsonData["text"] = {
                                "nwd": theBody,
                            }

                        outFile.write(json.dumps(jsonData, indent=2))

                wSuccess = True

            except Exception as e:
                errMsg = str(e)

        elif outTool == "QtPrint" and theFormat == self.FMT_PDF:
            try:
                thePrinter = QPrinter()
                thePrinter.setOutputFormat(QPrinter.PdfFormat)
                thePrinter.setOrientation(QPrinter.Portrait)
                thePrinter.setDuplex(QPrinter.DuplexLongSide)
                thePrinter.setFontEmbeddingEnabled(True)
                thePrinter.setColorMode(QPrinter.Color)
                thePrinter.setOutputFileName(savePath)
                self.docView.qDocument.print(thePrinter)
                wSuccess = True

            except Exception as e:
                errMsg - str(e)

        else:
            errMsg = "Unknown format"

        # Report to user
        if self.mainConf.showGUI:
            if wSuccess:
                self.theParent.makeAlert(
                    "%s file successfully written to:<br> %s" %
                    (textFmt, savePath), nwAlert.INFO)
            else:
                self.theParent.makeAlert(
                    "Failed to write %s file. %s" % (textFmt, errMsg),
                    nwAlert.ERROR)

        return wSuccess

    def _printDocument(self):
        """Open the print preview dialog.
        """
        thePreview = QPrintPreviewDialog(self)
        thePreview.paintRequested.connect(self._doPrintPreview)
        thePreview.exec_()
        return

    def _doPrintPreview(self, thePrinter):
        """Connect the print preview painter to the document viewer.
        """
        qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
        thePrinter.setOrientation(QPrinter.Portrait)
        self.docView.qDocument.print(thePrinter)
        qApp.restoreOverrideCursor()
        return

    def _selectFont(self):
        """Open the QFontDialog and set a font for the font style.
        """
        currFont = QFont()
        currFont.setFamily(self.textFont.text())
        currFont.setPointSize(self.textSize.value())
        theFont, theStatus = QFontDialog.getFont(currFont, self)
        if theStatus:
            self.textFont.setText(theFont.family())
            self.textSize.setValue(theFont.pointSize())

        self.raise_()  # Move the dialog to front (fixes a bug on macOS)

        return

    def _loadCache(self):
        """Save the current data to cache.
        """
        buildCache = os.path.join(self.theProject.projCache,
                                  nwFiles.BUILD_CACHE)
        dataCount = 0
        if os.path.isfile(buildCache):

            logger.debug("Loading build cache")
            try:
                with open(buildCache, mode="r", encoding="utf8") as inFile:
                    theJson = inFile.read()
                theData = json.loads(theJson)
            except Exception as e:
                logger.error("Failed to load build cache")
                logger.error(str(e))
                return False

            if "htmlText" in theData.keys():
                self.htmlText = theData["htmlText"]
                dataCount += 1
            if "htmlStyle" in theData.keys():
                self.htmlStyle = theData["htmlStyle"]
                dataCount += 1
            if "nwdText" in theData.keys():
                self.nwdText = theData["nwdText"]
                dataCount += 1
            if "buildTime" in theData.keys():
                self.buildTime = theData["buildTime"]

        return dataCount == 3

    def _saveCache(self):
        """Save the current data to cache.
        """
        buildCache = os.path.join(self.theProject.projCache,
                                  nwFiles.BUILD_CACHE)

        if self.mainConf.debugInfo:
            nIndent = 2
        else:
            nIndent = None

        logger.debug("Saving build cache")
        try:
            with open(buildCache, mode="w+", encoding="utf8") as outFile:
                outFile.write(
                    json.dumps(
                        {
                            "htmlText": self.htmlText,
                            "htmlStyle": self.htmlStyle,
                            "nwdText": self.nwdText,
                            "buildTime": self.buildTime,
                        },
                        indent=nIndent))
        except Exception as e:
            logger.error("Failed to save build cache")
            logger.error(str(e))
            return False

        return True

    def _doClose(self):
        """Close button was clicked.
        """
        self.close()
        return

    ##
    #  Events
    ##

    def closeEvent(self, theEvent):
        """Capture the user closing the window so we can save settings.
        """
        self._saveSettings()
        self.docView.clear()
        theEvent.accept()
        return

    ##
    #  Internal Functions
    ##

    def _enableQtSave(self, theState):
        """Set the enabled status of Save menu entries that depend on
        the QTextDocument.
        """
        self.saveODT.setEnabled(theState)
        self.savePDF.setEnabled(theState)
        self.saveTXT.setEnabled(theState)
        if self.mainConf.verQtValue >= 51400:
            self.saveMD.setEnabled(theState)
        return

    def _saveSettings(self):
        """Save the various user settings.
        """
        logger.debug("Saving GuiBuildNovel settings")

        # Formatting
        self.theProject.setTitleFormat({
            "title":
            self.fmtTitle.text().strip(),
            "chapter":
            self.fmtChapter.text().strip(),
            "unnumbered":
            self.fmtUnnumbered.text().strip(),
            "scene":
            self.fmtScene.text().strip(),
            "section":
            self.fmtSection.text().strip(),
        })

        winWidth = self.mainConf.rpxInt(self.width())
        winHeight = self.mainConf.rpxInt(self.height())
        justifyText = self.justifyText.isChecked()
        noStyling = self.noStyling.isChecked()
        textFont = self.textFont.text()
        textSize = self.textSize.value()
        novelFiles = self.novelFiles.isChecked()
        noteFiles = self.noteFiles.isChecked()
        ignoreFlag = self.ignoreFlag.isChecked()
        incSynopsis = self.includeSynopsis.isChecked()
        incComments = self.includeComments.isChecked()
        incKeywords = self.includeKeywords.isChecked()
        incBodyText = self.includeBody.isChecked()
        replaceTabs = self.replaceTabs.isChecked()

        mainSplit = self.mainSplit.sizes()
        if len(mainSplit) == 2:
            boxWidth = self.mainConf.rpxInt(mainSplit[0])
            docWidth = self.mainConf.rpxInt(mainSplit[1])
        else:
            boxWidth = 100
            docWidth = 100

        # GUI Settings
        self.optState.setValue("GuiBuildNovel", "winWidth", winWidth)
        self.optState.setValue("GuiBuildNovel", "winHeight", winHeight)
        self.optState.setValue("GuiBuildNovel", "boxWidth", boxWidth)
        self.optState.setValue("GuiBuildNovel", "docWidth", docWidth)
        self.optState.setValue("GuiBuildNovel", "justifyText", justifyText)
        self.optState.setValue("GuiBuildNovel", "noStyling", noStyling)
        self.optState.setValue("GuiBuildNovel", "textFont", textFont)
        self.optState.setValue("GuiBuildNovel", "textSize", textSize)
        self.optState.setValue("GuiBuildNovel", "addNovel", novelFiles)
        self.optState.setValue("GuiBuildNovel", "addNotes", noteFiles)
        self.optState.setValue("GuiBuildNovel", "ignoreFlag", ignoreFlag)
        self.optState.setValue("GuiBuildNovel", "incSynopsis", incSynopsis)
        self.optState.setValue("GuiBuildNovel", "incComments", incComments)
        self.optState.setValue("GuiBuildNovel", "incKeywords", incKeywords)
        self.optState.setValue("GuiBuildNovel", "incBodyText", incBodyText)
        self.optState.setValue("GuiBuildNovel", "replaceTabs", replaceTabs)
        self.optState.saveSettings()

        return

    def _reFmtCodes(self, theFormat):
        """Translates old formatting codes to new ones.
        """
        theFormat = theFormat.replace(r"%chnum%", r"%ch%")
        theFormat = theFormat.replace(r"%scnum%", r"%sc%")
        theFormat = theFormat.replace(r"%scabsnum%", r"%sca%")
        theFormat = theFormat.replace(r"%chnumword%", r"%chw%")
        return theFormat
示例#3
0
class GuiProjectDetailsContents(QWidget):

    C_TITLE = 0
    C_WORDS = 1
    C_PAGES = 2
    C_PAGE = 3
    C_PROG = 4

    def __init__(self, theParent, theProject):
        QWidget.__init__(self, theParent)

        self.mainConf = nw.CONFIG
        self.theParent = theParent
        self.theProject = theProject
        self.theTheme = theParent.theTheme
        self.theIndex = theParent.theIndex
        self.optState = theProject.optState

        # Internal
        self._theToC = []

        iPx = self.theTheme.baseIconSize
        hPx = self.mainConf.pxInt(12)
        vPx = self.mainConf.pxInt(4)

        # Contents Tree
        # =============

        self.tocTree = QTreeWidget()
        self.tocTree.setIconSize(QSize(iPx, iPx))
        self.tocTree.setIndentation(0)
        self.tocTree.setColumnCount(6)
        self.tocTree.setSelectionMode(QAbstractItemView.NoSelection)
        self.tocTree.setHeaderLabels([
            self.tr("Title"),
            self.tr("Words"),
            self.tr("Pages"),
            self.tr("Page"),
            self.tr("Progress"), ""
        ])

        treeHeadItem = self.tocTree.headerItem()
        treeHeadItem.setTextAlignment(self.C_WORDS, Qt.AlignRight)
        treeHeadItem.setTextAlignment(self.C_PAGES, Qt.AlignRight)
        treeHeadItem.setTextAlignment(self.C_PAGE, Qt.AlignRight)
        treeHeadItem.setTextAlignment(self.C_PROG, Qt.AlignRight)

        treeHeader = self.tocTree.header()
        treeHeader.setStretchLastSection(True)
        treeHeader.setMinimumSectionSize(hPx)

        wCol0 = self.mainConf.pxInt(
            self.optState.getInt("GuiProjectDetails", "widthCol0", 200))
        wCol1 = self.mainConf.pxInt(
            self.optState.getInt("GuiProjectDetails", "widthCol1", 60))
        wCol2 = self.mainConf.pxInt(
            self.optState.getInt("GuiProjectDetails", "widthCol2", 60))
        wCol3 = self.mainConf.pxInt(
            self.optState.getInt("GuiProjectDetails", "widthCol3", 60))
        wCol4 = self.mainConf.pxInt(
            self.optState.getInt("GuiProjectDetails", "widthCol4", 90))

        self.tocTree.setColumnWidth(0, wCol0)
        self.tocTree.setColumnWidth(1, wCol1)
        self.tocTree.setColumnWidth(2, wCol2)
        self.tocTree.setColumnWidth(3, wCol3)
        self.tocTree.setColumnWidth(4, wCol4)
        self.tocTree.setColumnWidth(5, hPx)

        # Options
        # =======

        wordsPerPage = self.optState.getInt("GuiProjectDetails",
                                            "wordsPerPage", 350)
        countFrom = self.optState.getInt("GuiProjectDetails", "countFrom", 1)
        clearDouble = self.optState.getInt("GuiProjectDetails", "clearDouble",
                                           True)

        wordsHelp = (self.tr(
            "Typical word count for a 5 by 8 inch book page with 11 pt font is 350."
        ))
        offsetHelp = (self.tr("Start counting page numbers from this page."))
        dblHelp = (self.tr(
            "Assume a new chapter or partition always start on an odd numbered page."
        ))

        self.wpLabel = QLabel(self.tr("Words per page"))
        self.wpLabel.setToolTip(wordsHelp)

        self.wpValue = QSpinBox()
        self.wpValue.setMinimum(10)
        self.wpValue.setMaximum(1000)
        self.wpValue.setSingleStep(10)
        self.wpValue.setValue(wordsPerPage)
        self.wpValue.setToolTip(wordsHelp)
        self.wpValue.valueChanged.connect(self._populateTree)

        self.poLabel = QLabel(self.tr("Count pages from"))
        self.poLabel.setToolTip(offsetHelp)

        self.poValue = QSpinBox()
        self.poValue.setMinimum(1)
        self.poValue.setMaximum(9999)
        self.poValue.setSingleStep(1)
        self.poValue.setValue(countFrom)
        self.poValue.setToolTip(offsetHelp)
        self.poValue.valueChanged.connect(self._populateTree)

        self.dblLabel = QLabel(self.tr("Clear double pages"))
        self.dblLabel.setToolTip(dblHelp)

        self.dblValue = QSwitch(self, 2 * iPx, iPx)
        self.dblValue.setChecked(clearDouble)
        self.dblValue.setToolTip(dblHelp)
        self.dblValue.clicked.connect(self._populateTree)

        self.optionsBox = QGridLayout()
        self.optionsBox.addWidget(self.wpLabel, 0, 0)
        self.optionsBox.addWidget(self.wpValue, 0, 1)
        self.optionsBox.addWidget(self.dblLabel, 0, 3)
        self.optionsBox.addWidget(self.dblValue, 0, 4)
        self.optionsBox.addWidget(self.poLabel, 1, 0)
        self.optionsBox.addWidget(self.poValue, 1, 1)
        self.optionsBox.setHorizontalSpacing(hPx)
        self.optionsBox.setVerticalSpacing(vPx)
        self.optionsBox.setColumnStretch(2, 1)

        # Assemble
        # ========

        self.outerBox = QVBoxLayout()
        self.outerBox.addWidget(
            QLabel("<b>%s</b>" % self.tr("Table of Contents")))
        self.outerBox.addWidget(self.tocTree)
        self.outerBox.addLayout(self.optionsBox)

        self.setLayout(self.outerBox)

        self._prepareData()
        self._populateTree()

        return

    def getColumnSizes(self):
        """Return the column widths for the tree columns.
        """
        retVals = [
            self.tocTree.columnWidth(0),
            self.tocTree.columnWidth(1),
            self.tocTree.columnWidth(2),
            self.tocTree.columnWidth(3),
            self.tocTree.columnWidth(4),
        ]
        return retVals

    ##
    #  Internal Functions
    ##

    def _prepareData(self):
        """Extract the data for the tree.
        """
        self._theToC = []
        self._theToC = self.theIndex.getTableOfContents(2)
        self._theToC.append(("", 0, self.tr("END"), 0))
        return

    ##
    #  Slots
    ##

    def _populateTree(self):
        """Set the content of the chapter/page tree.
        """
        dblPages = self.dblValue.isChecked()
        wpPage = self.wpValue.value()
        fstPage = self.poValue.value() - 1

        pTotal = 0
        tPages = 1

        theList = []
        for _, tLevel, tTitle, wCount in self._theToC:
            pCount = math.ceil(wCount / wpPage)
            if dblPages:
                pCount += pCount % 2

            pTotal += pCount
            theList.append((tLevel, tTitle, wCount, pCount))

        pMax = pTotal - fstPage

        self.tocTree.clear()
        for tLevel, tTitle, wCount, pCount in theList:
            newItem = QTreeWidgetItem()

            if tPages <= fstPage:
                progPage = numberToRoman(tPages, True)
                progText = ""
            else:
                cPage = tPages - fstPage
                pgProg = 100.0 * (cPage - 1) / pMax if pMax > 0 else 0.0
                progPage = f"{cPage:n}"
                progText = f"{pgProg:.1f}{nwUnicode.U_THSP}%"

            newItem.setIcon(self.C_TITLE,
                            self.theTheme.getIcon("doc_h%d" % tLevel))
            newItem.setText(self.C_TITLE, tTitle)
            newItem.setText(self.C_WORDS, f"{wCount:n}")
            newItem.setText(self.C_PAGES, f"{pCount:n}")
            newItem.setText(self.C_PAGE, progPage)
            newItem.setText(self.C_PROG, progText)

            newItem.setTextAlignment(self.C_WORDS, Qt.AlignRight)
            newItem.setTextAlignment(self.C_PAGES, Qt.AlignRight)
            newItem.setTextAlignment(self.C_PAGE, Qt.AlignRight)
            newItem.setTextAlignment(self.C_PROG, Qt.AlignRight)

            # Make pages and titles/partitions stand out
            if tLevel < 2:
                bFont = newItem.font(self.C_TITLE)
                if tLevel == 0:
                    bFont.setItalic(True)
                else:
                    bFont.setBold(True)
                    bFont.setUnderline(True)
                newItem.setFont(self.C_TITLE, bFont)

            tPages += pCount

            self.tocTree.addTopLevelItem(newItem)

        return