Commit 8b82f94c authored by Nikita Sirgienko's avatar Nikita Sirgienko
Browse files

Add edit protection for rendered result in LatexEntry and MarkdownEntry

parent 9354bc29
......@@ -128,6 +128,7 @@ void LatexEntry::setContent(const QDomElement& content, const KZip& file)
format.setProperty(EpsRenderer::ImagePath, imagePath);
format.setProperty(EpsRenderer::Code, latexCode);
cursor.insertText(QString(QChar::ObjectReplacementCharacter), format);
m_textItem->denyEditing();
} else
{
cursor.insertText(latexCode);
......@@ -235,6 +236,7 @@ void LatexEntry::resolveImagesAtCursor()
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
cursor.insertText(m_textItem->resolveImages(cursor));
m_textItem->allowEditing();
}
QString LatexEntry::latexCode()
......@@ -359,6 +361,7 @@ bool LatexEntry::renderLatexCode()
cursor.movePosition(QTextCursor::Start);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
cursor.insertText(QString(QChar::ObjectReplacementCharacter), formulaFormat);
m_textItem->denyEditing();
}
delete renderer;
return success;
......
......@@ -66,9 +66,7 @@ void MarkdownEntry::setContent(const QString& content)
{
rendered = false;
plain = content;
QTextDocument* doc = m_textItem->document();
doc->setPlainText(plain);
m_textItem->setDocument(doc);
setPlainText(plain);
}
void MarkdownEntry::setContent(const QDomElement& content, const KZip& file)
......@@ -93,9 +91,9 @@ void MarkdownEntry::setContent(const QDomElement& content, const KZip& file)
html = QLatin1String(""); // No plain text provided. The entry shouldn't render anything, or the user can't re-edit it.
}
if(rendered)
m_textItem->setHtml(html);
setRenderedHtml(html);
else
m_textItem->setPlainText(plain);
setPlainText(plain);
}
QDomElement MarkdownEntry::toXml(QDomDocument& doc, KZip* archive)
......@@ -169,7 +167,7 @@ bool MarkdownEntry::renderMarkdown(QString& plain)
html = QString::fromUtf8(htmlDocument, htmlSize);
mkd_cleanup(mdHandle);
m_textItem->setHtml(html);
setRenderedHtml(html);
return true;
#else
Q_UNUSED(plain);
......@@ -216,9 +214,7 @@ bool MarkdownEntry::eventFilter(QObject* object, QEvent* event)
if(!mouseEvent) return false;
if(mouseEvent->button() == Qt::LeftButton && rendered)
{
QTextDocument* doc = m_textItem->document();
doc->setPlainText(plain);
m_textItem->setDocument(doc);
setPlainText(plain);
m_textItem->setCursorPosition(mouseEvent->pos());
m_textItem->textCursor().clearSelection();
rendered = false;
......@@ -233,3 +229,17 @@ bool MarkdownEntry::wantToEvaluate()
{
return !rendered;
}
void MarkdownEntry::setRenderedHtml(const QString& html)
{
m_textItem->setHtml(html);
m_textItem->denyEditing();
}
void MarkdownEntry::setPlainText(const QString& plain)
{
QTextDocument* doc = m_textItem->document();
doc->setPlainText(plain);
m_textItem->setDocument(doc);
m_textItem->allowEditing();
}
......@@ -62,6 +62,8 @@ class MarkdownEntry : public WorksheetEntry
bool renderMarkdown(QString& plain);
bool eventFilter(QObject* object, QEvent* event) override;
bool wantToEvaluate() override;
void setRenderedHtml(const QString& html);
void setPlainText(const QString& plain);
protected:
WorksheetTextItem* m_textItem;
......
......@@ -437,9 +437,6 @@ Cantor::Session* WorksheetTextItem::session()
void WorksheetTextItem::keyPressEvent(QKeyEvent *event)
{
if (!isEditable())
return;
switch (event->key()) {
case Qt::Key_Left:
if (event->modifiers() == Qt::NoModifier && textCursor().atStart()) {
......@@ -912,4 +909,12 @@ void WorksheetTextItem::setFontSize(int size)
mergeFormatOnWordOrSelection(fmt);
}
void WorksheetTextItem::allowEditing()
{
setTextInteractionFlags(Qt::TextEditorInteraction);
}
void WorksheetTextItem::denyEditing()
{
setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
}
......@@ -69,6 +69,8 @@ class WorksheetTextItem : public QGraphicsTextItem
QString resolveImages(const QTextCursor& cursor);
bool isEditable();
void allowEditing();
void denyEditing();
void setBackgroundColor(const QColor&);
const QColor& backgroundColor() const;
bool richTextEnabled();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment