From 53b01f08f971429437969c9ff22034759008f04c Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 17 Mar 2020 11:36:03 +0100 Subject: [PATCH] Gui: improve usability of text document object --- src/Gui/CommandStd.cpp | 3 +- src/Gui/DlgUndoRedo.cpp | 25 +++++-- src/Gui/TextDocumentEditorView.cpp | 104 +++++++++++++++++++++++++-- src/Gui/TextDocumentEditorView.h | 8 +++ src/Gui/ViewProviderTextDocument.cpp | 13 ++++ src/Gui/ViewProviderTextDocument.h | 2 + 6 files changed, 144 insertions(+), 11 deletions(-) diff --git a/src/Gui/CommandStd.cpp b/src/Gui/CommandStd.cpp index 4ab910cee2..3e77ddfe6d 100644 --- a/src/Gui/CommandStd.cpp +++ b/src/Gui/CommandStd.cpp @@ -716,7 +716,8 @@ void StdCmdTextDocument::activated(int iMsg) Q_UNUSED(iMsg); openCommand("Insert text document"); - doCommand(Doc,"App.ActiveDocument.addObject(\"App::TextDocument\",\"%s\").Label=\"%s\"","Text document","Text document"); + doCommand(Doc, "App.ActiveDocument.addObject(\"App::TextDocument\",\"%s\").Label=\"%s\"","Text document","Text document"); + doCommand(Gui, "Gui.ActiveDocument.ActiveObject.doubleClicked()"); updateActive(); commitCommand(); } diff --git a/src/Gui/DlgUndoRedo.cpp b/src/Gui/DlgUndoRedo.cpp index b7a6b0f125..7ab8ac5f51 100644 --- a/src/Gui/DlgUndoRedo.cpp +++ b/src/Gui/DlgUndoRedo.cpp @@ -28,6 +28,7 @@ #include "MainWindow.h" #include "Document.h" #include "EditorView.h" +#include "TextDocumentEditorView.h" using namespace Gui::Dialog; @@ -60,9 +61,15 @@ void UndoDialog::onFetchInfo() clear(); // Remove first all items MDIView* mdi = getMainWindow()->activeWindow(); - EditorView* view = qobject_cast(mdi); - if (view) { - QStringList vecUndos = view->undoActions(); + EditorView* editview = qobject_cast(mdi); + TextDocumentEditorView* textedit = qobject_cast(mdi); + if (editview) { + QStringList vecUndos = editview->undoActions(); + for (QStringList::Iterator i = vecUndos.begin(); i != vecUndos.end(); ++i) + addAction(*i, this, SLOT(onSelected())); + } + else if (textedit) { + QStringList vecUndos = textedit->undoActions(); for (QStringList::Iterator i = vecUndos.begin(); i != vecUndos.end(); ++i) addAction(*i, this, SLOT(onSelected())); } @@ -117,9 +124,15 @@ void RedoDialog::onFetchInfo() clear(); // Remove first all items MDIView* mdi = getMainWindow()->activeWindow(); - EditorView* view = qobject_cast(mdi); - if (view) { - QStringList vecRedos = view->redoActions(); + EditorView* editview = qobject_cast(mdi); + TextDocumentEditorView* textedit = qobject_cast(mdi); + if (editview) { + QStringList vecRedos = editview->redoActions(); + for (QStringList::Iterator i = vecRedos.begin(); i != vecRedos.end(); ++i) + addAction(*i, this, SLOT(onSelected())); + } + else if (textedit) { + QStringList vecRedos = textedit->redoActions(); for (QStringList::Iterator i = vecRedos.begin(); i != vecRedos.end(); ++i) addAction(*i, this, SLOT(onSelected())); } diff --git a/src/Gui/TextDocumentEditorView.cpp b/src/Gui/TextDocumentEditorView.cpp index 83eb39223f..8d66d11d51 100644 --- a/src/Gui/TextDocumentEditorView.cpp +++ b/src/Gui/TextDocumentEditorView.cpp @@ -22,13 +22,19 @@ #include "PreCompiled.h" +#ifndef _PreComp_ +# include +# include +# include +# include +# include +# include +# include +#endif #include #include #include -#include -#include -#include #include #include @@ -53,6 +59,12 @@ TextDocumentEditorView::TextDocumentEditorView( setupEditor(); setupConnection(); setCentralWidget(editor); + + // update editor actions on request + Gui::MainWindow* mw = Gui::getMainWindow(); + connect(editor, SIGNAL(undoAvailable(bool)), mw, SLOT(updateEditorActions())); + connect(editor, SIGNAL(redoAvailable(bool)), mw, SLOT(updateEditorActions())); + connect(editor, SIGNAL(copyAvailable(bool)), mw, SLOT(updateEditorActions())); } TextDocumentEditorView::~TextDocumentEditorView() @@ -60,6 +72,28 @@ TextDocumentEditorView::~TextDocumentEditorView() textConnection.disconnect(); } +void TextDocumentEditorView::showEvent(QShowEvent* event) +{ + Gui::MainWindow* mw = Gui::getMainWindow(); + mw->updateEditorActions(); + MDIView::showEvent(event); +} + +void TextDocumentEditorView::hideEvent(QHideEvent* event) +{ + MDIView::hideEvent(event); +} + +void TextDocumentEditorView::closeEvent(QCloseEvent* event) +{ + MDIView::closeEvent(event); + if (event->isAccepted()) { + aboutToClose = true; + Gui::MainWindow* mw = Gui::getMainWindow(); + mw->updateEditorActions(); + } +} + bool TextDocumentEditorView::event(QEvent *event) { if (event->type() == QEvent::Show && sourceModified) { @@ -126,10 +160,34 @@ void TextDocumentEditorView::refresh() bool TextDocumentEditorView::onMsg(const char* msg, const char**) { + // don't allow any actions if the editor is being closed + if (aboutToClose) + return false; + if (strcmp(msg,"Save") == 0) { saveToObject(); return true; } + if (strcmp(msg,"Cut") == 0) { + getEditor()->cut(); + return true; + } + if (strcmp(msg,"Copy") == 0) { + getEditor()->copy(); + return true; + } + if (strcmp(msg,"Paste") == 0) { + getEditor()->paste(); + return true; + } + if (strcmp(msg,"Undo") == 0) { + getEditor()->undo(); + return true; + } + if (strcmp(msg,"Redo") == 0) { + getEditor()->redo(); + return true; + } return false; } @@ -140,8 +198,33 @@ bool TextDocumentEditorView::isEditorModified() const bool TextDocumentEditorView::onHasMsg(const char* msg) const { - if (strcmp(msg,"Save") == 0) + // don't allow any actions if the editor is being closed + if (aboutToClose) + return false; + + if (strcmp(msg,"Save") == 0) { return isEditorModified(); + } + if (strcmp(msg,"Cut") == 0) { + return (!getEditor()->isReadOnly() && + getEditor()->textCursor().hasSelection()); + } + if (strcmp(msg,"Copy") == 0) { + return (getEditor()->textCursor().hasSelection()); + } + if (strcmp(msg,"Paste") == 0) { + if (getEditor()->isReadOnly()) + return false; + QClipboard *cb = QApplication::clipboard(); + QString text = cb->text(); + return !text.isEmpty(); + } + if (strcmp(msg,"Undo") == 0) { + return (getEditor()->document()->isUndoAvailable()); + } + if (strcmp(msg,"Redo") == 0) { + return (getEditor()->document()->isRedoAvailable()); + } return false; } @@ -203,5 +286,18 @@ void TextDocumentEditorView::saveToObject() getEditor()->document()->setModified(false); } +QStringList TextDocumentEditorView::undoActions() const +{ + QStringList undo; + undo << tr("Edit text"); + return undo; +} + +QStringList TextDocumentEditorView::redoActions() const +{ + QStringList redo; + redo << tr("Edit text"); + return redo; +} #include "moc_TextDocumentEditorView.cpp" diff --git a/src/Gui/TextDocumentEditorView.h b/src/Gui/TextDocumentEditorView.h index eb64a8e723..9c5def6e2a 100644 --- a/src/Gui/TextDocumentEditorView.h +++ b/src/Gui/TextDocumentEditorView.h @@ -53,6 +53,13 @@ public: QPlainTextEdit* getEditor() const { return editor; } App::TextDocument* getTextObject() const { return textDocument; } + QStringList undoActions() const; + QStringList redoActions() const; + +protected: + void showEvent(QShowEvent*) override; + void hideEvent(QHideEvent*) override; + void closeEvent(QCloseEvent*) override; private: void setupEditor(); @@ -69,6 +76,7 @@ private: boost::signals2::connection textConnection; boost::signals2::connection labelConnection; bool sourceModified = false; + bool aboutToClose = false; }; } diff --git a/src/Gui/ViewProviderTextDocument.cpp b/src/Gui/ViewProviderTextDocument.cpp index 5f28b16071..989f82882c 100644 --- a/src/Gui/ViewProviderTextDocument.cpp +++ b/src/Gui/ViewProviderTextDocument.cpp @@ -122,6 +122,19 @@ void ViewProviderTextDocument::onChanged(const App::Property* prop) ViewProviderDocumentObject::onChanged(prop); } +MDIView* ViewProviderTextDocument::getMDIView() const +{ + auto views = getDocument()->getMDIViewsOfType( + TextDocumentEditorView::getClassTypeId()); + for (auto v : views) { + auto textView = static_cast(v); + if (textView->getTextObject() == getObject()) { + return textView; + } + } + return nullptr; +} + bool ViewProviderTextDocument::activateView() const { auto views = getDocument()->getMDIViewsOfType( diff --git a/src/Gui/ViewProviderTextDocument.h b/src/Gui/ViewProviderTextDocument.h index c618933b79..cbbd1a5453 100644 --- a/src/Gui/ViewProviderTextDocument.h +++ b/src/Gui/ViewProviderTextDocument.h @@ -49,6 +49,8 @@ public: void onChanged(const App::Property* prop); + virtual MDIView *getMDIView() const; + private: bool activateView() const;