Commit 5f88aca3 authored by Nikita Sirgienko's avatar Nikita Sirgienko

[T9024] Add context menu feature for converting entry to another type and minor refactoring

parent deb88f41
......@@ -43,6 +43,7 @@
LatexEntry::LatexEntry(Worksheet* worksheet) : WorksheetEntry(worksheet), m_textItem(new WorksheetTextItem(this, Qt::TextEditorInteraction))
{
m_textItem->installEventFilter(this);
connect(m_textItem, &WorksheetTextItem::moveToPrevious, this, &LatexEntry::moveToPreviousEntry);
connect(m_textItem, &WorksheetTextItem::moveToNext, this, &LatexEntry::moveToNextEntry);
connect(m_textItem, SIGNAL(execute()), this, SLOT(evaluate()));
......@@ -561,3 +562,8 @@ void LatexEntry::resolveImagesAtCursor()
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
cursor.insertText(m_textItem->resolveImages(cursor));
}
QString LatexEntry::plain() const
{
return m_textItem->toPlainText();
}
......@@ -38,6 +38,8 @@ class LatexEntry : public WorksheetEntry
bool isEmpty() override;
QString plain() const;
bool acceptRichText() override;
bool focusEntry(int pos = WorksheetTextItem::TopLeft, qreal xCoord = 0) override;
......
......@@ -716,3 +716,8 @@ void MarkdownEntry::clearAttachments()
attachedImages.clear();
animateSizeChange();
}
QString MarkdownEntry::plainText() const
{
return m_textItem->toPlainText();
}
......@@ -57,6 +57,8 @@ class MarkdownEntry : public WorksheetEntry
QJsonValue toJupyterJson() override;
QString toPlain(const QString& commandSep, const QString& commentStartingSeq, const QString& commentEndingSeq) override;
QString plainText() const;
void interruptEvaluation() override;
void layOutForWidth(qreal w, bool force = false) override;
......
......@@ -52,6 +52,7 @@ TextEntry::TextEntry(Worksheet* worksheet) : WorksheetEntry(worksheet)
, m_textItem(new WorksheetTextItem(this, Qt::TextEditorInteraction))
{
m_textItem->enableRichText(true);
connect(m_textItem, &WorksheetTextItem::moveToPrevious, this, &TextEntry::moveToPreviousEntry);
connect(m_textItem, &WorksheetTextItem::moveToNext, this, &TextEntry::moveToNextEntry);
// Modern syntax of signal/stots don't work on this connection (arguments don't match)
......@@ -588,3 +589,8 @@ void TextEntry::addNewTarget(const QString& target)
action->setChecked(true);
m_targetMenu->insertAction(m_targetMenu->actions().last(), action);
}
QString TextEntry::text() const
{
return m_textItem->toPlainText();
}
......@@ -44,6 +44,8 @@ class TextEntry : public WorksheetEntry
enum {Type = UserType + 1};
int type() const override;
QString text() const;
bool isEmpty() override;
bool acceptRichText() override;
......
......@@ -96,10 +96,6 @@ Worksheet::Worksheet(Cantor::Backend* backend, QWidget* parent, bool useDeafultW
connect(m_cursorItemTimer, &QTimer::timeout, this, &Worksheet::animateEntryCursor);
m_cursorItemTimer->start(500);
m_isPrinting = false;
m_readOnly = false;
m_isLoadingFromFile = false;
m_jupyterMetadata = nullptr;
if (backend)
......@@ -351,11 +347,11 @@ void Worksheet::setWorksheetCursor(const WorksheetCursor& cursor)
WorksheetEntry* Worksheet::currentEntry()
{
QGraphicsItem* item = focusItem();
// Entry cursor activate
if (m_choosenCursorEntry || m_isCursorEntryAfterLastEntry)
return nullptr;
QGraphicsItem* item = focusItem();
if (!item /*&& !hasFocus()*/)
item = m_lastFocusedTextItem;
/*else
......@@ -1553,9 +1549,21 @@ void Worksheet::populateMenu(QMenu *menu, QPointF pos)
menu->addSeparator();
if (entry) {
QMenu* convertTo = new QMenu(menu);
QMenu* insert = new QMenu(menu);
QMenu* insertBefore = new QMenu(menu);
convertTo->addAction(QIcon::fromTheme(QLatin1String("run-build")), i18n("Command Entry"), entry, &WorksheetEntry::convertToCommandEntry);
convertTo->addAction(QIcon::fromTheme(QLatin1String("draw-text")), i18n("Text Entry"), entry, &WorksheetEntry::convertToTextEntry);
#ifdef Discount_FOUND
convertTo->addAction(QIcon::fromTheme(QLatin1String("text-x-markdown")), i18n("Markdown Entry"), entry, &WorksheetEntry::convertToMarkdownEntry);
#endif
#ifdef WITH_EPS
convertTo->addAction(QIcon::fromTheme(QLatin1String("text-x-tex")), i18n("LaTeX Entry"), entry, &WorksheetEntry::convertToLatexEntry);
#endif
convertTo->addAction(QIcon::fromTheme(QLatin1String("image-x-generic")), i18n("Image"), entry, &WorksheetEntry::convertToImageEntry);
convertTo->addAction(QIcon::fromTheme(QLatin1String("go-next-view-page")), i18n("Page Break"), entry, &WorksheetEntry::converToPageBreakEntry);
insert->addAction(QIcon::fromTheme(QLatin1String("run-build")), i18n("Command Entry"), entry, SLOT(insertCommandEntry()));
insert->addAction(QIcon::fromTheme(QLatin1String("draw-text")), i18n("Text Entry"), entry, SLOT(insertTextEntry()));
#ifdef Discount_FOUND
......@@ -1578,10 +1586,14 @@ void Worksheet::populateMenu(QMenu *menu, QPointF pos)
insertBefore->addAction(QIcon::fromTheme(QLatin1String("image-x-generic")), i18n("Image"), entry, SLOT(insertImageEntryBefore()));
insertBefore->addAction(QIcon::fromTheme(QLatin1String("go-next-view-page")), i18n("Page Break"), entry, SLOT(insertPageBreakEntryBefore()));
convertTo->setTitle(i18n("Convert Entry To"));
convertTo->setIcon(QIcon::fromTheme(QLatin1String("gtk-convert")));
insert->setTitle(i18n("Insert Entry After"));
insert->setIcon(QIcon::fromTheme(QLatin1String("edit-table-insert-row-below")));
insertBefore->setTitle(i18n("Insert Entry Before"));
insertBefore->setIcon(QIcon::fromTheme(QLatin1String("edit-table-insert-row-above")));
menu->addMenu(convertTo);
menu->addMenu(insert);
menu->addMenu(insertBefore);
} else {
......@@ -2280,3 +2292,66 @@ Worksheet::Type Worksheet::type() const
{
return m_type;
}
void Worksheet::changeEntryType(WorksheetEntry* target, int newType)
{
if (target && target->type() != newType)
{
bool animation_state = m_animationsEnabled;
m_animationsEnabled = false;
QString content;
switch(target->type())
{
case CommandEntry::Type:
content = static_cast<CommandEntry*>(target)->command();
break;
case MarkdownEntry::Type:
content = static_cast<MarkdownEntry*>(target)->plainText();
break;
case TextEntry::Type:
content = static_cast<TextEntry*>(target)->text();
break;
case LatexEntry::Type:
content = static_cast<LatexEntry*>(target)->plain();
}
WorksheetEntry* newEntry = WorksheetEntry::create(newType, this);
newEntry->setContent(content);
if (newEntry)
{
WorksheetEntry* tmp = target;
newEntry->setPrevious(tmp->previous());
newEntry->setNext(tmp->next());
tmp->setPrevious(nullptr);
tmp->setNext(nullptr);
tmp->clearFocus();
tmp->forceRemove();
if (newEntry->previous())
newEntry->previous()->setNext(newEntry);
else
setFirstEntry(newEntry);
if (newEntry->next())
newEntry->next()->setPrevious(newEntry);
else
setLastEntry(newEntry);
updateLayout();
makeVisible(newEntry);
focusEntry(newEntry);
setModified();
newEntry->focusEntry();
}
m_animationsEnabled = animation_state;
}
}
......@@ -228,6 +228,8 @@ class Worksheet : public QGraphicsScene
void setFontFamily(const QString&);
void setFontSize(int size);
void changeEntryType(WorksheetEntry* target, int newType);
Q_SIGNALS:
void modified();
void loaded();
......@@ -325,14 +327,14 @@ class Worksheet : public QGraphicsScene
bool m_showExpressionIds{false};
bool m_animationsEnabled{false};
bool m_isPrinting;
bool m_isLoadingFromFile;
bool m_readOnly;
bool m_isPrinting{false};
bool m_isLoadingFromFile{false};
bool m_readOnly{false};
Type m_type = CantorWorksheet;
QString m_backendName;
QJsonObject* m_jupyterMetadata;
QJsonObject* m_jupyterMetadata{nullptr};
};
#endif // WORKSHEET_H
......@@ -169,6 +169,35 @@ void WorksheetEntry::insertPageBreakEntryBefore()
worksheet()->insertPageBreakEntryBefore(this);
}
void WorksheetEntry::convertToCommandEntry()
{
worksheet()->changeEntryType(this, CommandEntry::Type);
}
void WorksheetEntry::convertToTextEntry()
{
worksheet()->changeEntryType(this, TextEntry::Type);
}
void WorksheetEntry::convertToMarkdownEntry()
{
worksheet()->changeEntryType(this, MarkdownEntry::Type);
}
void WorksheetEntry::convertToLatexEntry()
{
worksheet()->changeEntryType(this, LatexEntry::Type);
}
void WorksheetEntry::convertToImageEntry()
{
worksheet()->changeEntryType(this, ImageEntry::Type);
}
void WorksheetEntry::converToPageBreakEntry()
{
worksheet()->changeEntryType(this, PageBreakEntry::Type);
}
void WorksheetEntry::showCompletion()
{
......@@ -675,9 +704,7 @@ void WorksheetEntry::remove()
worksheet()->setLastEntry(previous());
// make the entry invisible to QGraphicsScene's itemAt() function
hide();
worksheet()->updateLayout();
deleteLater();
forceRemove();
}
void WorksheetEntry::setSize(QSizeF size)
......@@ -828,3 +855,10 @@ void WorksheetEntry::setJupyterMetadata(QJsonObject metadata)
m_jupyterMetadata = new QJsonObject();
*m_jupyterMetadata = metadata;
}
void WorksheetEntry::forceRemove()
{
hide();
worksheet()->updateLayout();
deleteLater();
}
......@@ -63,6 +63,8 @@ class WorksheetEntry : public QGraphicsObject
WorksheetEntry* next() const;
WorksheetEntry* previous() const;
void forceRemove();
void setNext(WorksheetEntry*);
void setPrevious(WorksheetEntry*);
......@@ -120,6 +122,7 @@ class WorksheetEntry : public QGraphicsObject
void insertLatexEntry();
void insertImageEntry();
void insertPageBreakEntry();
void insertCommandEntryBefore();
void insertTextEntryBefore();
void insertMarkdownEntryBefore();
......@@ -127,6 +130,13 @@ class WorksheetEntry : public QGraphicsObject
void insertImageEntryBefore();
void insertPageBreakEntryBefore();
void convertToCommandEntry();
void convertToTextEntry();
void convertToMarkdownEntry();
void convertToLatexEntry();
void convertToImageEntry();
void converToPageBreakEntry();
virtual void sizeAnimated();
virtual void startRemoving();
bool stopRemoving();
......
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