Exemplo n.º 1
0
    def __init__(self, parentWidget):
        QWidget.__init__(self, parentWidget)

        self.formatManager = FormatManager()
        self.formatManager.loadFormats()

        self.editView = StylableTextEdit(self, self.formatManager)
        self.editView.navigate.connect(self.navigate)
        self.editView.objectSelectionChanged.connect(
            self.objectSelectionChanged)

        toolbar = QWidget(self)
        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        toolbar.setLayout(layout)

        self.actionsSelector = ActionSelector(self)
        layout.addWidget(self.actionsSelector)

        self.textFormatSelector = TextStyleSelector(self)
        self.textFormatSelector.styleChanged.connect(
            self.editView.applyTextFormat)
        self.editView.updateCharFormat.connect(
            self.textFormatSelector.setCurrentStyle)
        layout.addWidget(self.textFormatSelector)

        self.styleSelector = BlockStyleSelector(self)
        self.styleSelector.styleChanged.connect(self.editView.applyBlockStyle)
        self.styleSelector.indentLess.connect(self.editView.indentLess)
        self.styleSelector.indentMore.connect(self.editView.indentMore)
        self.styleSelector.insertTable.connect(self.insertTable)
        self.editView.updateBlockFormat.connect(
            self.styleSelector.setCurrentStyle)
        layout.addWidget(self.styleSelector)

        searchMarker = self.formatManager.getFormat(
            ('searchMarker', None, None))
        self.findWidget = FindWidget(self, searchMarker.getCharFormat(),
                                     self.editView)
        self.findWidget.hide()

        self.editMathWidget = MathEditWidget(self)
        self.editMathWidget.apply.connect(self.applyMathFormula)
        self.editMathWidget.hide()

        #horizontalSpacer = QSpacerItem(0, 0) # 40, 20) # , QSizePolicy.Expanding, QSizePolicy.Minimum)
        #layout.addItem(horizontalSpacer)

        hLayout = QVBoxLayout(self)
        hLayout.setContentsMargins(0, 0, 0, 0)
        hLayout.addWidget(toolbar)
        hLayout.addWidget(self.findWidget)
        hLayout.addWidget(self.editView)
        hLayout.addWidget(self.editMathWidget)

        # The toolbar should take the minimum space and the edit view the remaining space
        hLayout.setStretch(0, 0)
        hLayout.setStretch(1, 0)
        hLayout.setStretch(2, 1)
        hLayout.setStretch(3, 0.1)
Exemplo n.º 2
0
    def __init__(self, parentWidget):
        QWidget.__init__(self, parentWidget)

        self.formatManager = FormatManager()
        self.formatManager.loadFormats()

        self.editView = StylableTextEdit(self, self.formatManager)
        self.editView.navigate.connect(self.navigate)
        self.editView.objectSelectionChanged.connect(self.objectSelectionChanged)

        toolbar = QWidget(self)
        layout = QHBoxLayout()
        layout.setContentsMargins(0,0,0,0)
        toolbar.setLayout(layout)

        self.actionsSelector = ActionSelector(self)
        layout.addWidget(self.actionsSelector)

        self.textFormatSelector = TextStyleSelector(self)
        self.textFormatSelector.styleChanged.connect(self.editView.applyTextFormat)
        self.editView.updateCharFormat.connect(self.textFormatSelector.setCurrentStyle)
        layout.addWidget(self.textFormatSelector)

        self.styleSelector = BlockStyleSelector(self)
        self.styleSelector.styleChanged.connect(self.editView.applyBlockStyle)
        self.styleSelector.indentLess.connect(self.editView.indentLess)
        self.styleSelector.indentMore.connect(self.editView.indentMore)
        self.styleSelector.insertTable.connect(self.insertTable)
        self.editView.updateBlockFormat.connect(self.styleSelector.setCurrentStyle)
        layout.addWidget(self.styleSelector)

        searchMarker = self.formatManager.getFormat( ('searchMarker', None, None) )
        self.findWidget = FindWidget(self, searchMarker.getCharFormat(), self.editView)
        self.findWidget.hide()

        self.editMathWidget = MathEditWidget(self)
        self.editMathWidget.apply.connect(self.applyMathFormula)
        self.editMathWidget.hide()

        #horizontalSpacer = QSpacerItem(0, 0) # 40, 20) # , QSizePolicy.Expanding, QSizePolicy.Minimum)
        #layout.addItem(horizontalSpacer)

        hLayout = QVBoxLayout(self)
        hLayout.setContentsMargins(0, 0, 0, 0)
        hLayout.addWidget(toolbar)
        hLayout.addWidget(self.findWidget)
        hLayout.addWidget(self.editView)
        hLayout.addWidget(self.editMathWidget)

        # The toolbar should take the minimum space and the edit view the remaining space
        hLayout.setStretch(0, 0)
        hLayout.setStretch(1, 0)
        hLayout.setStretch(2, 1)
        hLayout.setStretch(3, 0.1)
Exemplo n.º 3
0
class EditorWidget(QWidget):
    
    l = logging.getLogger('EditorWidget')

    message = pyqtSignal(str)
    titleChanged = pyqtSignal(str)
    navigate = pyqtSignal(str)


    def __init__(self, parentWidget):
        QWidget.__init__(self, parentWidget)

        self.formatManager = FormatManager()
        self.formatManager.loadFormats()

        self.editView = StylableTextEdit(self, self.formatManager)
        self.editView.navigate.connect(self.navigate)
        self.editView.objectSelectionChanged.connect(self.objectSelectionChanged)

        toolbar = QWidget(self)
        layout = QHBoxLayout()
        layout.setContentsMargins(0,0,0,0)
        toolbar.setLayout(layout)

        self.actionsSelector = ActionSelector(self)
        layout.addWidget(self.actionsSelector)

        self.textFormatSelector = TextStyleSelector(self)
        self.textFormatSelector.styleChanged.connect(self.editView.applyTextFormat)
        self.editView.updateCharFormat.connect(self.textFormatSelector.setCurrentStyle)
        layout.addWidget(self.textFormatSelector)

        self.styleSelector = BlockStyleSelector(self)
        self.styleSelector.styleChanged.connect(self.editView.applyBlockStyle)
        self.styleSelector.indentLess.connect(self.editView.indentLess)
        self.styleSelector.indentMore.connect(self.editView.indentMore)
        self.styleSelector.insertTable.connect(self.insertTable)
        self.editView.updateBlockFormat.connect(self.styleSelector.setCurrentStyle)
        layout.addWidget(self.styleSelector)

        searchMarker = self.formatManager.getFormat( ('searchMarker', None, None) )
        self.findWidget = FindWidget(self, searchMarker.getCharFormat(), self.editView)
        self.findWidget.hide()

        self.editMathWidget = MathEditWidget(self)
        self.editMathWidget.apply.connect(self.applyMathFormula)
        self.editMathWidget.hide()

        #horizontalSpacer = QSpacerItem(0, 0) # 40, 20) # , QSizePolicy.Expanding, QSizePolicy.Minimum)
        #layout.addItem(horizontalSpacer)

        hLayout = QVBoxLayout(self)
        hLayout.setContentsMargins(0, 0, 0, 0)
        hLayout.addWidget(toolbar)
        hLayout.addWidget(self.findWidget)
        hLayout.addWidget(self.editView)
        hLayout.addWidget(self.editMathWidget)

        # The toolbar should take the minimum space and the edit view the remaining space
        hLayout.setStretch(0, 0)
        hLayout.setStretch(1, 0)
        hLayout.setStretch(2, 1)
        hLayout.setStretch(3, 0.1)


    def load(self, notepad, pageId):
        self.l.debug('Loading page "{}" from notepad "{}"'.format(pageId, notepad.getName()))

        self.page = notepad.getPage(pageId)
        doc = self.page.getDocument()

        self.editView.blockSignals(True)
        self.editView.setDocument(doc)
        self.editView.blockSignals(False)

        # TODO: review
        doc.undoAvailable.connect(self.actionsSelector.undoAction.setEnabled)
        doc.redoAvailable.connect(self.actionsSelector.redoAction.setEnabled)

        # Setup modification flag handling
        doc.modificationChanged.connect(self.updateWindowTitle)
        doc.setModified(False)

        doc.setUndoRedoEnabled(True)
        self.actionsSelector.undoAction.setEnabled(False)
        self.actionsSelector.redoAction.setEnabled(False)

        self.editView.setFocus()
        self.editView.handleCursorPositionChanged()    # update toolbars

        self.contentFile = os.path.join(notepad.getName(),str(pageId))
        self.message.emit('Loaded {}: {}'.format(notepad.getName(), self.page.getName()))


    def updateWindowTitle(self, flag):
        self.actionsSelector.saveAction.setEnabled(flag)
        self.titleChanged.emit('{}{}: {}'.format('*' if flag else '', self.page.notepad.getRootpath(), self.page.getName()))


## Actions

    def save(self):
        if self.editView.document().isModified():
            self.l.debug('Page "{}" from notepad "{}" has been modified, saving page'.format(self.page.getName(), self.page.notepad.getName()))
            self.page.save()
            self.message.emit('Saved {}: {}'.format(self.page.notepad.getName(), self.page.getName()))


    def toggleNonprintable(self):
        if self.actionsSelector.nonprintableAction.isChecked():
            option = QTextOption()
            option.setFlags(QTextOption.ShowTabsAndSpaces | QTextOption.ShowLineAndParagraphSeparators)
            self.editView.document().setDefaultTextOption(option)
        else:
            option = QTextOption()
            self.editView.document().setDefaultTextOption(option)

    
    def navigateBack(self):
        QMessageBox.information(self, "Error", "navigateBack not yet implemented")


    def navigateForward(self):
        QMessageBox.information(self, "Error", "navigateForward not yet implemented")


    def undo(self):
        self.editView.undo()


    def redo(self):
        self.editView.redo()


    def insertImage(self):
        filePath = QFileDialog.getOpenFileName(self, caption='Select Image file')[0]
        # print('{}: "{}" {}'.format(type(filePath), filePath, len(filePath)))
        if len(filePath) > 0:
            image = QImage(filePath)
            if image.isNull():
                QMessageBox.information(self, "Error", '"{}"\nis not a valid image file'.format(filePath))
            else:
                self.editView.insertImage(image)


    def insertFormula(self):
        self.editView.insertFormula()



    def insertTable(self):
        cursor = self.editView.textCursor()
        
        #fmt = QTextFrameFormat()
        #fmt.setBorder(2)
        #fmt.setBorderStyle(QTextFrameFormat.BorderStyle_Dashed)
        #cursor.insertFrame(fmt)

        cursor.insertTable(2, 2)
        cursor.insertText("Hello World")





#===============================================================================
#     def selectedBlocks(self):
#         cursor = self.editView.textCursor()
#         document = self.editView.document()
# 
#         startBlock = document.findBlock(cursor.selectionStart()) 
#         endBlock = document.findBlock(cursor.selectionEnd())
#         done = False
#         while not done:
#             yield startBlock
#             if startBlock == endBlock:
#                 done = True
#             else:
#                 startBlock = startBlock.next()
#===============================================================================


    def findInPage(self):
        self.findWidget.showWidget()


    def objectSelectionChanged(self):
        obj = self.editView.getSelectedObject()
        if type(obj) is MathFormulaObject:              # type(None) is also NOT MathFormulaObject
            self.editMathWidget.setFormula(obj.formula)
            self.editMathWidget.showWidget()
        else:
            self.editMathWidget.hideWidget()


    def applyMathFormula(self):
        obj = self.editView.getSelectedObject()
        if obj is not None:         # should always be true!
            obj.setFormula(self.editMathWidget.getFormula())
            obj.renderFormula()
            self.editView.viewport().update()
            self.editView.document().setModified(True)
Exemplo n.º 4
0
class EditorWidget(QWidget):

    l = logging.getLogger('EditorWidget')

    message = pyqtSignal(str)
    titleChanged = pyqtSignal(str)
    navigate = pyqtSignal(str)

    def __init__(self, parentWidget):
        QWidget.__init__(self, parentWidget)

        self.formatManager = FormatManager()
        self.formatManager.loadFormats()

        self.editView = StylableTextEdit(self, self.formatManager)
        self.editView.navigate.connect(self.navigate)
        self.editView.objectSelectionChanged.connect(
            self.objectSelectionChanged)

        toolbar = QWidget(self)
        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        toolbar.setLayout(layout)

        self.actionsSelector = ActionSelector(self)
        layout.addWidget(self.actionsSelector)

        self.textFormatSelector = TextStyleSelector(self)
        self.textFormatSelector.styleChanged.connect(
            self.editView.applyTextFormat)
        self.editView.updateCharFormat.connect(
            self.textFormatSelector.setCurrentStyle)
        layout.addWidget(self.textFormatSelector)

        self.styleSelector = BlockStyleSelector(self)
        self.styleSelector.styleChanged.connect(self.editView.applyBlockStyle)
        self.styleSelector.indentLess.connect(self.editView.indentLess)
        self.styleSelector.indentMore.connect(self.editView.indentMore)
        self.styleSelector.insertTable.connect(self.insertTable)
        self.editView.updateBlockFormat.connect(
            self.styleSelector.setCurrentStyle)
        layout.addWidget(self.styleSelector)

        searchMarker = self.formatManager.getFormat(
            ('searchMarker', None, None))
        self.findWidget = FindWidget(self, searchMarker.getCharFormat(),
                                     self.editView)
        self.findWidget.hide()

        self.editMathWidget = MathEditWidget(self)
        self.editMathWidget.apply.connect(self.applyMathFormula)
        self.editMathWidget.hide()

        #horizontalSpacer = QSpacerItem(0, 0) # 40, 20) # , QSizePolicy.Expanding, QSizePolicy.Minimum)
        #layout.addItem(horizontalSpacer)

        hLayout = QVBoxLayout(self)
        hLayout.setContentsMargins(0, 0, 0, 0)
        hLayout.addWidget(toolbar)
        hLayout.addWidget(self.findWidget)
        hLayout.addWidget(self.editView)
        hLayout.addWidget(self.editMathWidget)

        # The toolbar should take the minimum space and the edit view the remaining space
        hLayout.setStretch(0, 0)
        hLayout.setStretch(1, 0)
        hLayout.setStretch(2, 1)
        hLayout.setStretch(3, 0.1)

    def load(self, notepad, pageId):
        self.l.debug('Loading page "{}" from notepad "{}"'.format(
            pageId, notepad.getName()))

        self.page = notepad.getPage(pageId)
        doc = self.page.getDocument()

        self.editView.blockSignals(True)
        self.editView.setDocument(doc)
        self.editView.blockSignals(False)

        # TODO: review
        doc.undoAvailable.connect(self.actionsSelector.undoAction.setEnabled)
        doc.redoAvailable.connect(self.actionsSelector.redoAction.setEnabled)

        # Setup modification flag handling
        doc.modificationChanged.connect(self.updateWindowTitle)
        doc.setModified(False)

        doc.setUndoRedoEnabled(True)
        self.actionsSelector.undoAction.setEnabled(False)
        self.actionsSelector.redoAction.setEnabled(False)

        self.editView.setFocus()
        self.editView.handleCursorPositionChanged()  # update toolbars

        self.contentFile = os.path.join(notepad.getName(), str(pageId))
        self.message.emit('Loaded {}: {}'.format(notepad.getName(),
                                                 self.page.getName()))

    def updateWindowTitle(self, flag):
        self.actionsSelector.saveAction.setEnabled(flag)
        self.titleChanged.emit('{}{}: {}'.format(
            '*' if flag else '', self.page.notepad.getRootpath(),
            self.page.getName()))

## Actions

    def save(self):
        if self.editView.document().isModified():
            self.l.debug(
                'Page "{}" from notepad "{}" has been modified, saving page'.
                format(self.page.getName(), self.page.notepad.getName()))
            self.page.save()
            self.message.emit('Saved {}: {}'.format(
                self.page.notepad.getName(), self.page.getName()))

    def toggleNonprintable(self):
        if self.actionsSelector.nonprintableAction.isChecked():
            option = QTextOption()
            option.setFlags(QTextOption.ShowTabsAndSpaces
                            | QTextOption.ShowLineAndParagraphSeparators)
            self.editView.document().setDefaultTextOption(option)
        else:
            option = QTextOption()
            self.editView.document().setDefaultTextOption(option)

    def navigateBack(self):
        QMessageBox.information(self, "Error",
                                "navigateBack not yet implemented")

    def navigateForward(self):
        QMessageBox.information(self, "Error",
                                "navigateForward not yet implemented")

    def undo(self):
        self.editView.undo()

    def redo(self):
        self.editView.redo()

    def insertImage(self):
        filePath = QFileDialog.getOpenFileName(self,
                                               caption='Select Image file')[0]
        # print('{}: "{}" {}'.format(type(filePath), filePath, len(filePath)))
        if len(filePath) > 0:
            image = QImage(filePath)
            if image.isNull():
                QMessageBox.information(
                    self, "Error",
                    '"{}"\nis not a valid image file'.format(filePath))
            else:
                self.editView.insertImage(image)

    def insertFormula(self):
        self.editView.insertFormula()

    def insertTable(self):
        cursor = self.editView.textCursor()

        #fmt = QTextFrameFormat()
        #fmt.setBorder(2)
        #fmt.setBorderStyle(QTextFrameFormat.BorderStyle_Dashed)
        #cursor.insertFrame(fmt)

        cursor.insertTable(2, 2)
        cursor.insertText("Hello World")

#===============================================================================
#     def selectedBlocks(self):
#         cursor = self.editView.textCursor()
#         document = self.editView.document()
#
#         startBlock = document.findBlock(cursor.selectionStart())
#         endBlock = document.findBlock(cursor.selectionEnd())
#         done = False
#         while not done:
#             yield startBlock
#             if startBlock == endBlock:
#                 done = True
#             else:
#                 startBlock = startBlock.next()
#===============================================================================

    def findInPage(self):
        self.findWidget.showWidget()

    def objectSelectionChanged(self):
        obj = self.editView.getSelectedObject()
        if type(
                obj
        ) is MathFormulaObject:  # type(None) is also NOT MathFormulaObject
            self.editMathWidget.setFormula(obj.formula)
            self.editMathWidget.showWidget()
        else:
            self.editMathWidget.hideWidget()

    def applyMathFormula(self):
        obj = self.editView.getSelectedObject()
        if obj is not None:  # should always be true!
            obj.setFormula(self.editMathWidget.getFormula())
            obj.renderFormula()
            self.editView.viewport().update()
            self.editView.document().setModified(True)