Removes textFormat panel (bold, italic, etc. buttons)

This commit is contained in:
Olivier Keshavjee 2017-11-27 20:58:02 +01:00
parent 79ef970bfd
commit 7b29d332c5
6 changed files with 254 additions and 90 deletions

View file

@ -13,7 +13,6 @@ from manuskript import settings
from manuskript.enums import Outline
from manuskript.functions import allPaths, drawProgress
from manuskript.ui.editors.locker import locker
from manuskript.ui.editors.textFormat import textFormat
from manuskript.ui.editors.themes import findThemePath, generateTheme, setThemeEditorDatas
from manuskript.ui.editors.themes import loadThemeDatas
from manuskript.ui.views.MDEditView import MDEditView
@ -65,11 +64,7 @@ class fullScreenEditor(QWidget):
self.topPanel.layout().addStretch(1)
# Formatting
self.textFormat = textFormat(self)
self.topPanel.layout().addWidget(self.textFormat)
self.topPanel.layout().addStretch(1)
# Close
self.btnClose = QPushButton(self)
self.btnClose.setIcon(qApp.style().standardIcon(QStyle.SP_DialogCloseButton))
self.btnClose.clicked.connect(self.close)

View file

@ -270,9 +270,6 @@ class mainEditor(QWidget, Ui_mainEditor):
else:
visible = True
# Hides / show textFormat
self.textFormat.updateFromIndex(index)
self.btnRedacFolderText.setVisible(visible)
self.btnRedacFolderCork.setVisible(visible)
self.btnRedacFolderOutline.setVisible(visible)

View file

@ -2,7 +2,7 @@
# Form implementation generated from reading ui file 'manuskript/ui/editors/mainEditor_ui.ui'
#
# Created by: PyQt5 UI code generator 5.9
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!
@ -65,17 +65,6 @@ class Ui_mainEditor(object):
self.horizontalLayout_19.addWidget(self.sldCorkSizeFactor)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_19.addItem(spacerItem)
self.textFormat = textFormat(mainEditor)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.textFormat.sizePolicy().hasHeightForWidth())
self.textFormat.setSizePolicy(sizePolicy)
self.textFormat.setMinimumSize(QtCore.QSize(20, 20))
self.textFormat.setObjectName("textFormat")
self.horizontalLayout_19.addWidget(self.textFormat)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_19.addItem(spacerItem1)
self.lblRedacWC = QtWidgets.QLabel(mainEditor)
self.lblRedacWC.setMinimumSize(QtCore.QSize(10, 0))
self.lblRedacWC.setText("")
@ -110,4 +99,3 @@ class Ui_mainEditor(object):
self.btnRedacFullscreen.setShortcut(_translate("mainEditor", "F11"))
from manuskript.ui.editors.tabSplitter import tabSplitter
from manuskript.ui.editors.textFormat import textFormat

View file

@ -51,8 +51,7 @@
</property>
<property name="icon">
<iconset theme="go-up">
<normaloff/>
</iconset>
<normaloff>.</normaloff>.</iconset>
</property>
<property name="shortcut">
<string>Alt+Up</string>
@ -141,35 +140,6 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="textFormat" name="textFormat" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
@ -237,12 +207,6 @@
</layout>
</widget>
<customwidgets>
<customwidget>
<class>textFormat</class>
<extends>QWidget</extends>
<header>manuskript.ui.editors.textFormat.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>tabSplitter</class>
<extends>QWidget</extends>

View file

@ -1,12 +1,16 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
# from PyQt5.QtCore import
# from PyQt5.QtGui import
import re
from PyQt5.QtCore import QRegExp, Qt
from PyQt5.QtGui import QTextCursor
# from PyQt5.QtWidgets import
from manuskript.ui.views.textEditView import textEditView
from manuskript.ui.highlighters import MarkdownHighlighter
# from manuskript.ui.editors.textFormat import textFormat
# from manuskript.ui.editors.MDFunctions import MDFormatSelection
class MDEditView(textEditView):
@ -19,3 +23,248 @@ class MDEditView(textEditView):
# Highlighter
self._textFormat = "md"
self._highlighterClass = MarkdownHighlighter
# def focusInEvent(self, event):
# """Finds textFormatter and attach them to that view."""
# textEditView.focusInEvent(self, event)
#
# p = self.parent()
# while p.parent():
# p = p.parent()
#
# if self._index:
# for tF in p.findChildren(textFormat, QRegExp(".*"),
# Qt.FindChildrenRecursively):
# tF.updateFromIndex(self._index)
# tF.setTextEdit(self)
###########################################################################
# FORMATTING (#FIXME)
###########################################################################
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 selectWord(self, cursor):
if cursor.selectedText():
return
end = cursor.selectionEnd()
cursor.movePosition(QTextCursor.StartOfWord)
cursor.setPosition(end, QTextCursor.KeepAnchor)
cursor.movePosition(QTextCursor.EndOfWord, QTextCursor.KeepAnchor)
def selectBlock(self, cursor):
cursor.movePosition(QTextCursor.StartOfBlock)
cursor.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
def comment(self):
cursor = self.textCursor()
# Select begining and end of words
self.selectWord(cursor)
if cursor.hasSelection():
text = cursor.selectedText()
cursor.insertText("<!-- " + text + " -->")
else:
cursor.insertText("<!-- -->")
cursor.movePosition(QTextCursor.PreviousCharacter,
QTextCursor.MoveAnchor, 4)
self.setTextCursor(cursor)
def commentLine(self):
cursor = self.textCursor()
start = cursor.selectionStart()
end = cursor.selectionEnd()
block = self.document().findBlock(start)
block2 = self.document().findBlock(end)
if True:
# Method 1
cursor.beginEditBlock()
while block.isValid():
self.commentBlock(block)
if block == block2: break
block = block.next()
cursor.endEditBlock()
else:
# Method 2
cursor.beginEditBlock()
cursor.setPosition(block.position())
cursor.insertText("<!--\n")
cursor.setPosition(block2.position() + block2.length() - 1)
cursor.insertText("\n-->")
cursor.endEditBlock()
def commentBlock(self, block):
cursor = QTextCursor(block)
text = block.text()
if text[:5] == "<!-- " and \
text[-4:] == " -->":
text2 = text[5:-4]
else:
text2 = "<!-- " + text + " -->"
self.selectBlock(cursor)
cursor.insertText(text2)
def insertFormattingMarkup(self, markup):
cursor = self.textCursor()
# Select begining and end of words
self.selectWord(cursor)
if cursor.hasSelection():
start = cursor.selectionStart()
end = cursor.selectionEnd() + len(markup)
cursor.beginEditBlock()
cursor.setPosition(start)
cursor.insertText(markup)
cursor.setPosition(end)
cursor.insertText(markup)
cursor.endEditBlock()
cursor.movePosition(QTextCursor.PreviousCharacter,
QTextCursor.KeepAnchor, len(markup))
#self.setTextCursor(cursor)
else:
# Insert markup twice (for opening and closing around the cursor),
# and then move the cursor to be between the pair.
cursor.beginEditBlock()
cursor.insertText(markup)
cursor.insertText(markup)
cursor.movePosition(QTextCursor.PreviousCharacter,
QTextCursor.MoveAnchor, len(markup))
cursor.endEditBlock()
self.setTextCursor(cursor)
def clearFormat(self):
cursor = self.textCursor()
text = cursor.selectedText()
if not text:
self.selectBlock(cursor)
text = cursor.selectedText()
text = self.clearedFormat(text)
cursor.insertText(text)
def clearedFormat(self, text):
# FIXME: clear also block formats
for reg, rep, flags in [
("\*\*(.*?)\*\*", "\\1", None), # bold
("__(.*?)__", "\\1", None), # bold
("\*(.*?)\*", "\\1", None), # emphasis
("_(.*?)_", "\\1", None), # emphasis
("`(.*?)`", "\\1", None), # verbatim
("~~(.*?)~~", "\\1", None), # strike
("\^(.*?)\^", "\\1", None), # superscript
("~(.*?)~", "\\1", None), # subscript
("<!--(.*)-->", "\\1", re.S), # comments
# LINES OR BLOCKS
(r"^#*\s*(.+?)\s*", "\\1", re.M), # ATX
(r"^[=-]*$", "", re.M), # Setext
(r"^`*$", "", re.M), # Code block fenced
(r"^\s*[-+*]\s*(.*?)\s*$", "\\1", re.M), # Bullet List
(r"^\s*[0-9a-z](\.|\))\s*(.*?)\s*$", "\\2", re.M), # Bullet List
(r"\s*[>\s]*(.*?)\s*$", "\\1", re.M), # Code block and blockquote
]:
text = re.sub(reg, rep, text, flags if flags else 0)
return text
def clearedFormatForStats(self, text):
# Remove stuff that musn't be counted
# FIXME: clear also block formats
for reg, rep, flags in [
("<!--.*-->", "", re.S), # comments
]:
text = re.sub(reg, rep, text, flags if flags else 0)
return text
def titleSetext(self, level):
cursor = self.textCursor()
cursor.beginEditBlock()
# Is it already a Setext header?
if cursor.block().userState() in [
MS.MarkdownStateSetextHeading1Line2,
MS.MarkdownStateSetextHeading2Line2]:
cursor.movePosition(QTextCursor.PreviousBlock)
text = cursor.block().text()
if cursor.block().userState() in [
MS.MarkdownStateSetextHeading1Line1,
MS.MarkdownStateSetextHeading2Line1]:
# Need to remove line below
c = QTextCursor(cursor.block().next())
self.selectBlock(c)
c.insertText("")
char = "=" if level == 1 else "-"
text = re.sub("^#*\s*(.*)\s*#*", "\\1", text) # Removes #
sub = char * len(text)
text = text + "\n" + sub
self.selectBlock(cursor)
cursor.insertText(text)
cursor.endEditBlock()
def titleATX(self, level):
cursor = self.textCursor()
text = cursor.block().text()
# Are we in a Setext Header?
if cursor.block().userState() in [
MS.MarkdownStateSetextHeading1Line1,
MS.MarkdownStateSetextHeading2Line1]:
# Need to remove line below
cursor.beginEditBlock()
c = QTextCursor(cursor.block().next())
self.selectBlock(c)
c.insertText("")
self.selectBlock(cursor)
cursor.insertText(text)
cursor.endEditBlock()
return
elif cursor.block().userState() in [
MS.MarkdownStateSetextHeading1Line2,
MS.MarkdownStateSetextHeading2Line2]:
cursor.movePosition(QTextCursor.PreviousBlock)
self.setTextCursor(cursor)
self.titleATX(level)
return
m = re.match("^(#+)(\s*)(.+)", text)
if m:
pre = m.group(1)
space = m.group(2)
txt = m.group(3)
if len(pre) == level:
# Remove title
text = txt
else:
text = "#" * level + space + txt
else:
text = "#" * level + " " + text
self.selectBlock(cursor)
cursor.insertText(text)

View file

@ -10,10 +10,7 @@ from manuskript import settings
from manuskript.enums import Outline, World, Character, Plot
from manuskript import functions as F
from manuskript.models.outlineModel import outlineModel
from manuskript.ui.editors.MDFunctions import MDFormatSelection
from manuskript.ui.highlighters import BasicHighlighter
# from manuskript.ui.highlighters import MMDHighlighter
from manuskript.ui.editors.textFormat import textFormat
from manuskript.ui import style as S
try:
@ -544,32 +541,6 @@ class textEditView(QTextEdit):
QTextEdit.focusOutEvent(self, event)
self.submit()
def focusInEvent(self, event):
"""Finds textFormatter and attach them to that view."""
QTextEdit.focusInEvent(self, event)
p = self.parent()
while p.parent():
p = p.parent()
if self._index:
for tF in p.findChildren(textFormat, QRegExp(".*"), Qt.FindChildrenRecursively):
tF.updateFromIndex(self._index)
tF.setTextEdit(self)
def applyFormat(self, _format):
if self._textFormat == "md":
if _format == "Bold":
MDFormatSelection(self, 0)
elif _format == "Italic":
MDFormatSelection(self, 1)
elif _format == "Code":
MDFormatSelection(self, 2)
elif _format == "Clear":
MDFormatSelection(self)
###############################################################################
# KEYBOARD SHORTCUTS
###############################################################################