Purging types from textEditView

This commit is contained in:
Olivier Keshavjee 2016-03-30 10:30:41 +02:00
parent 8a90f3213f
commit a5b0b6cd95

View file

@ -13,9 +13,6 @@ from manuskript.functions import toString
from manuskript.models.outlineModel import outlineModel
from manuskript.ui.editors.MMDHighlighter import MMDHighlighter
from manuskript.ui.editors.basicHighlighter import basicHighlighter
from manuskript.ui.editors.t2tFunctions import t2tClearFormat
from manuskript.ui.editors.t2tFunctions import t2tFormatSelection
from manuskript.ui.editors.t2tHighlighter import t2tHighlighter
from manuskript.ui.editors.textFormat import textFormat
try:
@ -151,40 +148,25 @@ class textEditView(QTextEdit):
self.updateText()
def setupEditorForIndex(self, index):
# what type of text are we editing?
# In which model are we editing?
if type(index.model()) != outlineModel:
self._textFormat = "text"
return
# what type of text are we editing?
if self._column not in [Outline.text.value, Outline.notes.value]:
self._textFormat = "text"
else:
item = index.internalPointer()
if item.isHTML():
self._textFormat = "html"
elif item.isT2T():
self._textFormat = "t2t"
elif item.isMD():
self._textFormat = "md"
else:
self._textFormat = "text"
# Accept richtext maybe
if self._textFormat == "html":
self.setAcceptRichText(True)
else:
self.setAcceptRichText(False)
self._textFormat = "md"
# Setting highlighter
if self._highlighting:
item = index.internalPointer()
if self._column == Outline.text.value and not (item.isT2T() or item.isMD()):
self.highlighter = basicHighlighter(self)
elif item.isT2T():
self.highlighter = t2tHighlighter(self)
elif item.isMD():
if self._column in [Outline.text.value, Outline.notes.value]:
self.highlighter = MMDHighlighter(self)
else:
self.highlighter = basicHighlighter(self)
self.highlighter.setDefaultBlockFormat(self._defaultBlockFormat)
@ -244,15 +226,7 @@ class textEditView(QTextEdit):
# self.objectName(), self.parent().objectName()))
if topLeft.row() <= self._index.row() <= bottomRight.row():
if self._column == Outline.text.value and \
topLeft.column() <= Outline.type.value <= bottomRight.column():
# If item type change, and we display the main text,
# we reset the index to set the proper
# highlighter and other defaults
self.setupEditorForIndex(self._index)
self.updateText()
elif topLeft.column() <= self._column <= bottomRight.column():
if topLeft.column() <= self._column <= bottomRight.column():
self.updateText()
elif self._indexes:
@ -288,15 +262,9 @@ class textEditView(QTextEdit):
self._updating = True
if self._index:
self.disconnectDocument()
if self._textFormat == "html":
if self.toHtml() != toString(self._model.data(self._index)):
# print(" Updating html")
html = self._model.data(self._index)
self.document().setHtml(toString(html))
else:
if self.toPlainText() != toString(self._model.data(self._index)):
# print(" Updating plaintext")
self.document().setPlainText(toString(self._model.data(self._index)))
if self.toPlainText() != toString(self._model.data(self._index)):
# print(" Updating plaintext")
self.document().setPlainText(toString(self._model.data(self._index)))
self.reconnectDocument()
elif self._indexes:
@ -313,7 +281,6 @@ class textEditView(QTextEdit):
break
if same:
# Assuming that we don't use HTML with multiple items
self.document().setPlainText(t[0])
else:
self.document().setPlainText("")
@ -332,26 +299,11 @@ class textEditView(QTextEdit):
# print("Submitting", self.objectName())
if self._index:
# item = self._index.internalPointer()
if self._textFormat == "html":
if self.toHtml() != self._model.data(self._index):
# print(" Submitting html")
self._updating = True
html = self.toHtml()
# We don't store paragraph and font settings
html = re.sub(r"font-family:.*?;\s*", "", html)
html = re.sub(r"font-size:.*?;\s*", "", html)
html = re.sub(r"margin-.*?;\s*", "", html)
html = re.sub(r"text-indent:.*?;\s*", "", html)
html = re.sub(r"line-height:.*?;\s*", "", html)
# print("Submitting:", html)
self._model.setData(self._index, html)
self._updating = False
else:
if self.toPlainText() != self._model.data(self._index):
# print(" Submitting plain text")
self._updating = True
self._model.setData(self._index, self.toPlainText())
self._updating = False
if self.toPlainText() != self._model.data(self._index):
# print(" Submitting plain text")
self._updating = True
self._model.setData(self._index, self.toPlainText())
self._updating = False
elif self._indexes:
self._updating = True
@ -503,70 +455,17 @@ class textEditView(QTextEdit):
def applyFormat(self, _format):
if self._textFormat == "html":
if _format == "Clear":
cursor = self.textCursor()
if _format == "Clear":
fmt = self._defaultCharFormat
cursor.setCharFormat(fmt)
bf = self._defaultBlockFormat
cursor.setBlockFormat(bf)
elif _format in ["Bold", "Italic", "Underline"]:
cursor = self.textCursor()
# If no selection, selects the word in which the cursor is now
if not cursor.hasSelection():
cursor.movePosition(QTextCursor.StartOfWord,
QTextCursor.MoveAnchor)
cursor.movePosition(QTextCursor.EndOfWord,
QTextCursor.KeepAnchor)
fmt = cursor.charFormat()
if _format == "Bold":
fmt.setFontWeight(QFont.Bold if fmt.fontWeight() != QFont.Bold else QFont.Normal)
elif _format == "Italic":
fmt.setFontItalic(not fmt.fontItalic())
elif _format == "Underline":
fmt.setFontUnderline(not fmt.fontUnderline())
fmt2 = self._defaultCharFormat
fmt2.setFontWeight(fmt.fontWeight())
fmt2.setFontItalic(fmt.fontItalic())
fmt2.setFontUnderline(fmt.fontUnderline())
cursor.mergeCharFormat(fmt2)
elif _format in ["Left", "Center", "Right", "Justify"]:
cursor = self.textCursor()
# bf = cursor.blockFormat()
bf = QTextBlockFormat()
bf.setAlignment(
Qt.AlignLeft if _format == "Left" else
Qt.AlignHCenter if _format == "Center" else
Qt.AlignRight if _format == "Right" else
Qt.AlignJustify)
cursor.setBlockFormat(bf)
self.setTextCursor(cursor)
elif self._textFormat == "t2t":
if _format == "Bold":
t2tFormatSelection(self, 0)
elif _format == "Italic":
t2tFormatSelection(self, 1)
elif _format == "Underline":
t2tFormatSelection(self, 2)
elif _format == "Clear":
t2tClearFormat(self)
elif self._textFormat == "md":
if self._textFormat == "md":
# FIXME
print("Not implemented yet.")
# Model: from t2tFunctions
# if self._textFormat == "t2t":
# if _format == "Bold":
# t2tFormatSelection(self, 0)
# elif _format == "Italic":
# t2tFormatSelection(self, 1)
# elif _format == "Underline":
# t2tFormatSelection(self, 2)
# elif _format == "Clear":
# t2tClearFormat(self)