Fixed menu and shortcut action for bold/italic
This commit is contained in:
+18
-19
@@ -76,25 +76,24 @@ class nwDocAction(Enum):
|
|||||||
PASTE = 5
|
PASTE = 5
|
||||||
EMPH = 6
|
EMPH = 6
|
||||||
STRONG = 7
|
STRONG = 7
|
||||||
STRONGEMPH = 8
|
STRIKE = 8
|
||||||
STRIKE = 9
|
S_QUOTE = 9
|
||||||
S_QUOTE = 10
|
D_QUOTE = 10
|
||||||
D_QUOTE = 11
|
SEL_ALL = 11
|
||||||
SEL_ALL = 12
|
SEL_PARA = 12
|
||||||
SEL_PARA = 13
|
FIND = 13
|
||||||
FIND = 14
|
REPLACE = 14
|
||||||
REPLACE = 15
|
GO_NEXT = 15
|
||||||
GO_NEXT = 16
|
GO_PREV = 16
|
||||||
GO_PREV = 17
|
REPL_NEXT = 17
|
||||||
REPL_NEXT = 18
|
BLOCK_H1 = 18
|
||||||
BLOCK_H1 = 19
|
BLOCK_H2 = 19
|
||||||
BLOCK_H2 = 20
|
BLOCK_H3 = 20
|
||||||
BLOCK_H3 = 21
|
BLOCK_H4 = 21
|
||||||
BLOCK_H4 = 22
|
BLOCK_COM = 22
|
||||||
BLOCK_COM = 23
|
BLOCK_TXT = 23
|
||||||
BLOCK_TXT = 24
|
REPL_SNG = 24
|
||||||
REPL_SNG = 25
|
REPL_DBL = 25
|
||||||
REPL_DBL = 26
|
|
||||||
|
|
||||||
# END Enum nwDocAction
|
# END Enum nwDocAction
|
||||||
|
|
||||||
|
|||||||
+26
-70
@@ -515,13 +515,11 @@ class GuiDocEditor(QTextEdit):
|
|||||||
elif theAction == nwDocAction.PASTE:
|
elif theAction == nwDocAction.PASTE:
|
||||||
self.paste()
|
self.paste()
|
||||||
elif theAction == nwDocAction.EMPH:
|
elif theAction == nwDocAction.EMPH:
|
||||||
self._toggleEmph(1)
|
self._toggleFormat(1, "_")
|
||||||
elif theAction == nwDocAction.STRONG:
|
elif theAction == nwDocAction.STRONG:
|
||||||
self._toggleEmph(2)
|
self._toggleFormat(2, "*")
|
||||||
elif theAction == nwDocAction.STRONGEMPH:
|
|
||||||
self._toggleEmph(3)
|
|
||||||
elif theAction == nwDocAction.STRIKE:
|
elif theAction == nwDocAction.STRIKE:
|
||||||
self._toggleStrike()
|
self._toggleFormat(2, "~")
|
||||||
elif theAction == nwDocAction.S_QUOTE:
|
elif theAction == nwDocAction.S_QUOTE:
|
||||||
self._wrapSelection(self.typSQOpen, self.typSQClose)
|
self._wrapSelection(self.typSQOpen, self.typSQClose)
|
||||||
elif theAction == nwDocAction.D_QUOTE:
|
elif theAction == nwDocAction.D_QUOTE:
|
||||||
@@ -998,69 +996,27 @@ class GuiDocEditor(QTextEdit):
|
|||||||
theCursor = self.textCursor()
|
theCursor = self.textCursor()
|
||||||
if self.mainConf.autoSelect and not theCursor.hasSelection():
|
if self.mainConf.autoSelect and not theCursor.hasSelection():
|
||||||
theCursor.select(QTextCursor.WordUnderCursor)
|
theCursor.select(QTextCursor.WordUnderCursor)
|
||||||
self.setTextCursor(theCursor)
|
|
||||||
return theCursor
|
|
||||||
|
|
||||||
def _toggleEmph(self, eLevel):
|
|
||||||
"""Toggle emphasis of a given level between 1 and 3, where 1 is
|
|
||||||
italic, 2 is bold, and 3 is bold italic. The current level in
|
|
||||||
the text is cLevel. The rules are as follows:
|
|
||||||
|
|
||||||
cLevel | eLevel | Result
|
|
||||||
============+============+============
|
|
||||||
None | Italic | Italic
|
|
||||||
None | Bold | Bold
|
|
||||||
None | BoldItalic | BoldItalic
|
|
||||||
------------+------------+------------
|
|
||||||
Italic | Italic | None
|
|
||||||
Italic | Bold | BoldItalic
|
|
||||||
Italic | BoldItalic | BoldItalic
|
|
||||||
------------+------------+------------
|
|
||||||
Bold | Italic | BoldItalic
|
|
||||||
Bold | Bold | None
|
|
||||||
Bold | BoldItalic | BoldItalic
|
|
||||||
------------+------------+------------
|
|
||||||
BoldItalic | Italic | Bold
|
|
||||||
BoldItalic | Bold | Italic
|
|
||||||
BoldItalic | BoldItalic | None
|
|
||||||
|
|
||||||
"""
|
|
||||||
theCursor = self._autoSelect()
|
|
||||||
if theCursor.hasSelection():
|
|
||||||
posS = theCursor.selectionStart()
|
posS = theCursor.selectionStart()
|
||||||
posE = theCursor.selectionEnd()
|
posE = theCursor.selectionEnd()
|
||||||
|
|
||||||
numB = 0
|
# Underscore counts as a part of the word, so check that the
|
||||||
for n in range(3):
|
# selection isn't wrapped in italics markers.
|
||||||
if self.qDocument.characterAt(posS-n-1) == "*":
|
reSelect = False
|
||||||
numB += 1
|
if self.qDocument.characterAt(posS) == "_":
|
||||||
else:
|
posS += 1
|
||||||
break
|
reSelect = True
|
||||||
|
if self.qDocument.characterAt(posE) == "_":
|
||||||
|
posE -= 1
|
||||||
|
reSelect = True
|
||||||
|
if reSelect:
|
||||||
|
theCursor.clearSelection()
|
||||||
|
theCursor.setPosition(posE-1)
|
||||||
|
theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, posE-posS-1)
|
||||||
|
|
||||||
numA = 0
|
self.setTextCursor(theCursor)
|
||||||
for n in range(3):
|
return theCursor
|
||||||
if self.qDocument.characterAt(posE+n) == "*":
|
|
||||||
numA += 1
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
cLevel = min(numB, numA)
|
def _toggleFormat(self, fLen, fChar):
|
||||||
if cLevel == 0:
|
|
||||||
# Has no emphasis, set to desired level
|
|
||||||
self._wrapSelection("*"*eLevel)
|
|
||||||
elif cLevel == 3:
|
|
||||||
# Has max, so reduce by desired level
|
|
||||||
self._clearSurrounding(theCursor, eLevel)
|
|
||||||
elif cLevel == eLevel:
|
|
||||||
# Toggle mode, so clear what we had set
|
|
||||||
self._clearSurrounding(theCursor, cLevel)
|
|
||||||
else:
|
|
||||||
# Already at 1 or 2, increase to 3
|
|
||||||
self._wrapSelection("*"*(3 - cLevel))
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
def _toggleStrike(self):
|
|
||||||
"""Toggle strikethrough text.
|
"""Toggle strikethrough text.
|
||||||
"""
|
"""
|
||||||
theCursor = self._autoSelect()
|
theCursor = self._autoSelect()
|
||||||
@@ -1069,24 +1025,24 @@ class GuiDocEditor(QTextEdit):
|
|||||||
posE = theCursor.selectionEnd()
|
posE = theCursor.selectionEnd()
|
||||||
|
|
||||||
numB = 0
|
numB = 0
|
||||||
for n in range(2):
|
for n in range(fLen):
|
||||||
if self.qDocument.characterAt(posS-n-1) == "~":
|
if self.qDocument.characterAt(posS-n-1) == fChar:
|
||||||
numB += 1
|
numB += 1
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
numA = 0
|
numA = 0
|
||||||
for n in range(2):
|
for n in range(fLen):
|
||||||
if self.qDocument.characterAt(posE+n) == "~":
|
if self.qDocument.characterAt(posE+n) == fChar:
|
||||||
numA += 1
|
numA += 1
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
cLevel = min(numB, numA)
|
cLevel = min(numB, numA)
|
||||||
if cLevel == 2:
|
if cLevel == fLen:
|
||||||
self._clearSurrounding(theCursor, 2)
|
self._clearSurrounding(theCursor, fLen)
|
||||||
else:
|
else:
|
||||||
self._wrapSelection("~~")
|
self._wrapSelection(fChar*fLen)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -540,13 +540,6 @@ class GuiMainMenu(QMenuBar):
|
|||||||
self.aFmtStrong.triggered.connect(lambda: self._docAction(nwDocAction.STRONG))
|
self.aFmtStrong.triggered.connect(lambda: self._docAction(nwDocAction.STRONG))
|
||||||
self.fmtMenu.addAction(self.aFmtStrong)
|
self.fmtMenu.addAction(self.aFmtStrong)
|
||||||
|
|
||||||
# Format > Very Strong Emphasis
|
|
||||||
self.aFmtStrongEmph = QAction("Very Strong Emphasis", self)
|
|
||||||
self.aFmtStrongEmph.setStatusTip("Add very strong emphasis to selected text (bold and italic)")
|
|
||||||
self.aFmtStrongEmph.setShortcut("Ctrl+Shift+B")
|
|
||||||
self.aFmtStrongEmph.triggered.connect(lambda: self._docAction(nwDocAction.STRONGEMPH))
|
|
||||||
self.fmtMenu.addAction(self.aFmtStrongEmph)
|
|
||||||
|
|
||||||
# Format > Strikethrough
|
# Format > Strikethrough
|
||||||
self.aFmtStrike = QAction("Strikethrough", self)
|
self.aFmtStrike = QAction("Strikethrough", self)
|
||||||
self.aFmtStrike.setStatusTip("Add strikethrough to selected text")
|
self.aFmtStrike.setStatusTip("Add strikethrough to selected text")
|
||||||
|
|||||||
@@ -1008,7 +1008,6 @@ class GuiMain(QMainWindow):
|
|||||||
# Format
|
# Format
|
||||||
self.addAction(self.mainMenu.aFmtEmph)
|
self.addAction(self.mainMenu.aFmtEmph)
|
||||||
self.addAction(self.mainMenu.aFmtStrong)
|
self.addAction(self.mainMenu.aFmtStrong)
|
||||||
self.addAction(self.mainMenu.aFmtStrongEmph)
|
|
||||||
self.addAction(self.mainMenu.aFmtStrike)
|
self.addAction(self.mainMenu.aFmtStrike)
|
||||||
self.addAction(self.mainMenu.aFmtDQuote)
|
self.addAction(self.mainMenu.aFmtDQuote)
|
||||||
self.addAction(self.mainMenu.aFmtSQuote)
|
self.addAction(self.mainMenu.aFmtSQuote)
|
||||||
|
|||||||
Reference in New Issue
Block a user