Block formatting now complete

This commit is contained in:
Veronica K. B. Olsen
2019-11-21 14:31:12 +01:00
parent 20e1c2f6f2
commit 1711e5c615
3 changed files with 79 additions and 3 deletions
+1
View File
@@ -76,6 +76,7 @@ class nwDocAction(Enum):
BLOCK_H3 = 20
BLOCK_H4 = 21
BLOCK_COM = 22
BLOCK_TXT = 23
# END Enum nwDocAction
+71 -3
View File
@@ -450,6 +450,8 @@ class GuiDocEditor(QTextEdit):
self._formatBlock(nwDocAction.BLOCK_H4)
elif theAction == nwDocAction.BLOCK_COM:
self._formatBlock(nwDocAction.BLOCK_COM)
elif theAction == nwDocAction.BLOCK_TXT:
self._formatBlock(nwDocAction.BLOCK_TXT)
else:
logger.debug("Unknown or unsupported document action %s" % str(theAction))
return False
@@ -784,9 +786,75 @@ class GuiDocEditor(QTextEdit):
theCursor = self.textCursor()
theBlock = theCursor.block()
if theBlock.isValid():
theText = theBlock.text()
print(theText)
if not theBlock.isValid():
logger.debug("Invalid block selected for action %s" % str(docAction))
return
theText = theBlock.text()
if len(theText.strip()) == 0:
logger.debug("Empty block selected for action %s" % str(docAction))
return
# Remove existing format first, if any
if theText.startswith("@"):
logger.error("Cannot apply block format to keyword/value line")
return
elif theText.startswith("%"):
newText = theText[1:]
cOffset = 1
elif theText.startswith("# "):
newText = theText[2:]
cOffset = 2
elif theText.startswith("## "):
newText = theText[3:]
cOffset = 3
elif theText.startswith("### "):
newText = theText[4:]
cOffset = 4
elif theText.startswith("#### "):
newText = theText[5:]
cOffset = 5
else:
newText = theText
cOffset = 0
# Apply new format
if docAction == nwDocAction.BLOCK_COM:
theText = "%"+newText
cOffset -= 1
elif docAction == nwDocAction.BLOCK_H1:
theText = "# "+newText
cOffset -= 2
elif docAction == nwDocAction.BLOCK_H2:
theText = "## "+newText
cOffset -= 3
elif docAction == nwDocAction.BLOCK_H3:
theText = "### "+newText
cOffset -= 4
elif docAction == nwDocAction.BLOCK_H4:
theText = "#### "+newText
cOffset -= 5
elif docAction == nwDocAction.BLOCK_TXT:
theText = newText
cOffset -= 0
else:
logger.error("Unknown or unsupported block format requested: %s" % str(docAction))
return
# Replace the block text
theCursor.beginEditBlock()
posO = theCursor.position()
theCursor.select(QTextCursor.BlockUnderCursor)
posS = theCursor.selectionStart()
theCursor.removeSelectedText()
theCursor.setPosition(posS)
if posS > 0:
theCursor.insertBlock()
theCursor.insertText(theText)
if posO - cOffset >= 0:
theCursor.setPosition(posO - cOffset)
theCursor.endEditBlock()
self.setTextCursor(theCursor)
return
+7
View File
@@ -618,6 +618,13 @@ class GuiMainMenu(QMenuBar):
self.aFmtComment.triggered.connect(lambda: self._docAction(nwDocAction.BLOCK_COM))
self.fmtMenu.addAction(self.aFmtComment)
# Format > Remove Format
self.aFmtNoFormat = QAction("Remove Format", self)
self.aFmtNoFormat.setStatusTip("Strips block format")
self.aFmtNoFormat.setShortcuts(["Ctrl+0","Ctrl+Shift+/"])
self.aFmtNoFormat.triggered.connect(lambda: self._docAction(nwDocAction.BLOCK_TXT))
self.fmtMenu.addAction(self.aFmtNoFormat)
return
def _buildToolsMenu(self):