Gui: improve usability of text document object

This commit is contained in:
wmayer
2020-03-17 11:36:03 +01:00
parent fa27ae2def
commit 17e0697c5e
6 changed files with 144 additions and 11 deletions

View File

@@ -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();
}

View File

@@ -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<EditorView*>(mdi);
if (view) {
QStringList vecUndos = view->undoActions();
EditorView* editview = qobject_cast<EditorView*>(mdi);
TextDocumentEditorView* textedit = qobject_cast<TextDocumentEditorView*>(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<EditorView*>(mdi);
if (view) {
QStringList vecRedos = view->redoActions();
EditorView* editview = qobject_cast<EditorView*>(mdi);
TextDocumentEditorView* textedit = qobject_cast<TextDocumentEditorView*>(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()));
}

View File

@@ -22,13 +22,19 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QAbstractTextDocumentLayout>
# include <QApplication>
# include <QClipboard>
# include <QString>
# include <QMessageBox>
# include <QPushButton>
# include <QTextBlock>
#endif
#include <iostream>
#include <boost/bind.hpp>
#include <boost/signals2.hpp>
#include <QString>
#include <QMessageBox>
#include <QPushButton>
#include <App/TextDocument.h>
#include <Gui/Document.h>
@@ -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"

View File

@@ -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;
};
}

View File

@@ -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<TextDocumentEditorView *>(v);
if (textView->getTextObject() == getObject()) {
return textView;
}
}
return nullptr;
}
bool ViewProviderTextDocument::activateView() const
{
auto views = getDocument()->getMDIViewsOfType(

View File

@@ -49,6 +49,8 @@ public:
void onChanged(const App::Property* prop);
virtual MDIView *getMDIView() const;
private:
bool activateView() const;