Adds formatting support to MDEditView

This commit is contained in:
Olivier Keshavjee 2017-11-30 13:12:55 +01:00
parent b1192101ce
commit f3156f5bbd
4 changed files with 94 additions and 25 deletions

View file

@ -28,6 +28,7 @@ from manuskript.ui.mainWindow import Ui_MainWindow
from manuskript.ui.tools.frequencyAnalyzer import frequencyAnalyzer
from manuskript.ui.views.outlineDelegates import outlineCharacterDelegate
from manuskript.ui.views.plotDelegate import plotDelegate
from manuskript.ui.views.MDEditView import MDEditView
# Spellcheck support
from manuskript.ui.views.textEditView import textEditView
@ -57,6 +58,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
# Var
self.currentProject = None
self._lastFocus = None
self._lastMDEditView = None
self._defaultCursorFlashTime = 1000 # Overriden at startup with system
# value. In manuskript.main.
@ -113,19 +115,43 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.actSaveAs.triggered.connect(self.welcome.saveAsFile)
self.actImport.triggered.connect(self.doImport)
self.actCompile.triggered.connect(self.doCompile)
self.actLabels.triggered.connect(self.settingsLabel)
self.actStatus.triggered.connect(self.settingsStatus)
self.actSettings.triggered.connect(self.settingsWindow)
self.actCloseProject.triggered.connect(self.closeProject)
self.actQuit.triggered.connect(self.close)
# Main menu:: Documents
# Main menu:: Edit
self.actCopy.triggered.connect(self.documentsCopy)
self.actCut.triggered.connect(self.documentsCut)
self.actPaste.triggered.connect(self.documentsPaste)
self.actRename.triggered.connect(self.documentsRename)
self.actDuplicate.triggered.connect(self.documentsDuplicate)
self.actDelete.triggered.connect(self.documentsDelete)
self.actLabels.triggered.connect(self.settingsLabel)
self.actStatus.triggered.connect(self.settingsStatus)
self.actSettings.triggered.connect(self.settingsWindow)
# Main menu:: Edit:: Format
self.actHeaderSetextL1.triggered.connect(self.formatSetext1)
self.actHeaderSetextL2.triggered.connect(self.formatSetext2)
self.actHeaderAtxL1.triggered.connect(self.formatAtx1)
self.actHeaderAtxL2.triggered.connect(self.formatAtx2)
self.actHeaderAtxL3.triggered.connect(self.formatAtx3)
self.actHeaderAtxL4.triggered.connect(self.formatAtx4)
self.actHeaderAtxL5.triggered.connect(self.formatAtx5)
self.actHeaderAtxL6.triggered.connect(self.formatAtx6)
self.actFormatBold.triggered.connect(self.formatBold)
self.actFormatItalic.triggered.connect(self.formatItalic)
self.actFormatStrike.triggered.connect(self.formatStrike)
self.actFormatVerbatim.triggered.connect(self.formatVerbatim)
self.actFormatSuperscript.triggered.connect(self.formatSuperscript)
self.actFormatSubscript.triggered.connect(self.formatSubscript)
self.actFormatCommentLines.triggered.connect(self.formatCommentLines)
self.actFormatList.triggered.connect(self.formatList)
self.actFormatOrderedList.triggered.connect(self.formatOrderedList)
self.actFormatBlockquote.triggered.connect(self.formatBlockquote)
self.actFormatCommentBlock.triggered.connect(self.formatCommentBlock)
self.actFormatClear.triggered.connect(self.formatClear)
# Main menu:: Organize
self.actMoveUp.triggered.connect(self.documentsMoveUp)
self.actMoveDown.triggered.connect(self.documentsMoveDown)
self.actSplitDialog.triggered.connect(self.documentsSplitDialog)
@ -225,6 +251,12 @@ class MainWindow(QMainWindow, Ui_MainWindow):
We get notified by qApp when focus changes, from old to new widget.
"""
# If new is a MDEditView, we keep it in memory
if issubclass(type(new), MDEditView):
self._lastMDEditView = new
else:
self._lastMDEditView = None
# Determine which view had focus last, to send the keyboard shortcuts
# to the right place
@ -443,12 +475,14 @@ class MainWindow(QMainWindow, Ui_MainWindow):
def openIndexes(self, indexes, newTab=True):
self.mainEditor.openIndexes(indexes, newTab=True)
# Menu Documents #############################################################
# Menu #############################################################
# Functions called by the menu Documents
# Functions called by the menus
# self._lastFocus is the last editor that had focus (either treeView or
# mainEditor). So we just pass along the signal.
# Edit
def documentsCopy(self):
"Copy selected item(s)."
if self._lastFocus: self._lastFocus.copy()
@ -467,6 +501,38 @@ class MainWindow(QMainWindow, Ui_MainWindow):
def documentsDelete(self):
"Delete selected item(s)."
if self._lastFocus: self._lastFocus.delete()
# Formats
def callLastMDEditView(self, functionName, param=[]):
"""
If last focused widget was MDEditView, call the given function.
"""
if self._lastMDEditView:
function = getattr(self._lastMDEditView, functionName)
function(*param)
def formatSetext1(self): self.callLastMDEditView("titleSetext", [1])
def formatSetext2(self): self.callLastMDEditView("titleSetext", [2])
def formatAtx1(self): self.callLastMDEditView("titleATX", [1])
def formatAtx2(self): self.callLastMDEditView("titleATX", [2])
def formatAtx3(self): self.callLastMDEditView("titleATX", [3])
def formatAtx4(self): self.callLastMDEditView("titleATX", [4])
def formatAtx5(self): self.callLastMDEditView("titleATX", [5])
def formatAtx6(self): self.callLastMDEditView("titleATX", [6])
def formatBold(self): self.callLastMDEditView("bold")
def formatItalic(self): self.callLastMDEditView("italic")
def formatStrike(self): self.callLastMDEditView("strike")
def formatVerbatim(self): self.callLastMDEditView("verbatim")
def formatSuperscript(self): self.callLastMDEditView("superscript")
def formatSubscript(self): self.callLastMDEditView("subscript")
def formatCommentLines(self): self.callLastMDEditView("commentLine")
def formatList(self): self.callLastMDEditView("unorderedList")
def formatOrderedList(self): self.callLastMDEditView("orderedList")
def formatBlockquote(self): self.callLastMDEditView("blockquote")
def formatCommentBlock(self): self.callLastMDEditView("comment")
def formatClear(self): self.callLastMDEditView("clearFormat")
# Organize
def documentsMoveUp(self):
"Move up selected item(s)."
if self._lastFocus: self._lastFocus.moveUp()

View file

@ -1262,8 +1262,8 @@ class Ui_MainWindow(object):
self.actFormatClear.setObjectName("actFormatClear")
self.actFormatCommentLines = QtWidgets.QAction(MainWindow)
self.actFormatCommentLines.setObjectName("actFormatCommentLines")
self.actFormatUnorderedList = QtWidgets.QAction(MainWindow)
self.actFormatUnorderedList.setObjectName("actFormatUnorderedList")
self.actFormatOrderedList = QtWidgets.QAction(MainWindow)
self.actFormatOrderedList.setObjectName("actFormatOrderedList")
self.actFormatList = QtWidgets.QAction(MainWindow)
icon = QtGui.QIcon.fromTheme("view-list")
self.actFormatList.setIcon(icon)
@ -1304,7 +1304,7 @@ class Ui_MainWindow(object):
self.mnuFormat.addAction(self.actFormatCommentLines)
self.mnuFormat.addSeparator()
self.mnuFormat.addAction(self.actFormatList)
self.mnuFormat.addAction(self.actFormatUnorderedList)
self.mnuFormat.addAction(self.actFormatOrderedList)
self.mnuFormat.addAction(self.actFormatBlockquote)
self.mnuFormat.addAction(self.actFormatCommentBlock)
self.mnuFormat.addSeparator()
@ -1539,8 +1539,8 @@ class Ui_MainWindow(object):
self.actFormatClear.setText(_translate("MainWindow", "Clear &formats"))
self.actFormatClear.setShortcut(_translate("MainWindow", "Ctrl+0"))
self.actFormatCommentLines.setText(_translate("MainWindow", "&Comment line(s)"))
self.actFormatCommentLines.setShortcut(_translate("MainWindow", "Ctrl+C"))
self.actFormatUnorderedList.setText(_translate("MainWindow", "&Ordered list"))
self.actFormatCommentLines.setShortcut(_translate("MainWindow", "Ctrl+D"))
self.actFormatOrderedList.setText(_translate("MainWindow", "&Ordered list"))
self.actFormatList.setText(_translate("MainWindow", "&Unordered list"))
self.actFormatBlockquote.setText(_translate("MainWindow", "B&lockquote"))

View file

@ -2170,7 +2170,7 @@
<addaction name="actFormatCommentLines"/>
<addaction name="separator"/>
<addaction name="actFormatList"/>
<addaction name="actFormatUnorderedList"/>
<addaction name="actFormatOrderedList"/>
<addaction name="actFormatBlockquote"/>
<addaction name="actFormatCommentBlock"/>
<addaction name="separator"/>
@ -2802,10 +2802,10 @@
<string>&amp;Comment line(s)</string>
</property>
<property name="shortcut">
<string>Ctrl+C</string>
<string>Ctrl+D</string>
</property>
</action>
<action name="actFormatUnorderedList">
<action name="actFormatOrderedList">
<property name="text">
<string>&amp;Ordered list</string>
</property>

View file

@ -10,6 +10,7 @@ from PyQt5.QtGui import QTextCursor
from manuskript.ui.views.textEditView import textEditView
from manuskript.ui.highlighters import MarkdownHighlighter
from manuskript import settings
from manuskript.ui.highlighters.markdownEnums import MarkdownState as MS
# from manuskript.ui.editors.textFormat import textFormat
# from manuskript.ui.editors.MDFunctions import MDFormatSelection
@ -79,23 +80,18 @@ class MDEditView(textEditView):
self.verticalScrollBar().blockSignals(False)
###########################################################################
# FORMATTING (#FIXME)
# FORMATTING
###########################################################################
def applyFormat(self, _format):
if self._textFormat == "md":
if _format == "Bold": self.bold()
elif _format == "Italic": self.italic()
elif _format == "Code": self.verbatim()
elif _format == "Clear": self.clearFormat()
def bold(self): self.insertFormattingMarkup("**")
def italic(self): self.insertFormattingMarkup("*")
def strike(self): self.insertFormattingMarkup("~~")
def verbatim(self): self.insertFormattingMarkup("`")
def superscript(self): self.insertFormattingMarkup("^")
def subscript(self): self.insertFormattingMarkup("~")
def blockquote(self): self.lineFormattingMarkup("> ")
def orderedList(self): self.lineFormattingMarkup(" 1. ")
def unorderedList(self): self.lineFormattingMarkup(" - ")
def selectWord(self, cursor):
if cursor.selectedText():
@ -161,6 +157,14 @@ class MDEditView(textEditView):
self.selectBlock(cursor)
cursor.insertText(text2)
def lineFormattingMarkup(self, markup):
"""
Adds (or remove if present) `markup` at the begining of block.
"""
cursor = self.textCursor()
cursor.movePosition(cursor.StartOfBlock)
cursor.insertText(markup)
def insertFormattingMarkup(self, markup):
cursor = self.textCursor()
@ -211,8 +215,7 @@ class MDEditView(textEditView):
("~~(.*?)~~", "\\1", None), # strike
("\^(.*?)\^", "\\1", None), # superscript
("~(.*?)~", "\\1", None), # subscript
("<!--(.*)-->", "\\1", re.S), # comments
("<!--\s*(.*?)\s*-->", "\\1", re.S), # comments
# LINES OR BLOCKS
(r"^#*\s*(.+?)\s*", "\\1", re.M), # ATX