Debian Codegraph @

src/Gui/TextDocumentEditorView.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2017 Markus Hovorka <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QMessageBox>
27 #include <QPushButton>
28 #include <QString>
29
30
31 #include "TextDocumentEditorView.h"
32 #include "Application.h"
33 #include "BitmapFactory.h"
34 #include "Document.h"
35 #include "MainWindow.h"
36
37
38 using namespace Gui;
39
40 TYPESYSTEM_SOURCE_ABSTRACT(Gui::TextDocumentEditorView, Gui::MDIView) // NOLINT
41
42 TextDocumentEditorView::TextDocumentEditorView(App::TextDocument* txtDoc, QPlainTextEdit* e, QWidget* parent)
43 : MDIView(Application::Instance->getDocument(txtDoc->getDocument()), parent)
44 , editor {e}
45 , textDocument {txtDoc}
46 {
47 setupEditor();
48 setupConnection();
49 setCentralWidget(editor);
50 setWindowIcon(Gui::BitmapFactory().iconFromTheme("TextDocument"));
51
52 // clang-format off
53 // update editor actions on request
54 Gui::MainWindow* mw = Gui::getMainWindow();
55 connect(editor, &QPlainTextEdit::undoAvailable, mw, &MainWindow::updateEditorActions);
56 connect(editor, &QPlainTextEdit::redoAvailable, mw, &MainWindow::updateEditorActions);
57 connect(editor, &QPlainTextEdit::copyAvailable, mw, &MainWindow::updateEditorActions);
58 connect(editor, &QPlainTextEdit::textChanged, this, &TextDocumentEditorView::textChanged);
59 // clang-format on
60 }
61
62 TextDocumentEditorView::~TextDocumentEditorView()
63 {
64 textConnection.disconnect();
65 labelConnection.disconnect();
66 }
67
68 void TextDocumentEditorView::showEvent(QShowEvent* event)
69 {
70 Gui::MainWindow* mw = Gui::getMainWindow();
71 mw->updateEditorActions();
72 MDIView::showEvent(event);
73 }
74
75 void TextDocumentEditorView::closeEvent(QCloseEvent* event)
76 {
77 MDIView::closeEvent(event);
78 if (event->isAccepted()) {
79 aboutToClose = true;
80 Gui::MainWindow* mw = Gui::getMainWindow();
81 mw->updateEditorActions();
82 }
83 }
84
85 void TextDocumentEditorView::setupEditor()
86 {
87 // clang-format off
88 connect(getEditor()->document(), &QTextDocument::modificationChanged,
89 this, &TextDocumentEditorView::setWindowModified);
90 // clang-format on
91 labelChanged();
92 refresh();
93 }
94
95 void TextDocumentEditorView::setupConnection()
96 {
97 // NOLINTBEGIN
98 textConnection = textDocument->connectText(std::bind(&TextDocumentEditorView::sourceChanged, this));
99 labelConnection = textDocument->connectLabel(
100 std::bind(&TextDocumentEditorView::labelChanged, this)
101 );
102 // NOLINTEND
103 }
104
105 void TextDocumentEditorView::sourceChanged()
106 {
107 refresh();
108 }
109
110 void TextDocumentEditorView::textChanged()
111 {
112 saveToObject();
113 }
114
115 void TextDocumentEditorView::labelChanged()
116 {
117 setWindowTitle(QString::fromUtf8(textDocument->Label.getValue()) + QStringLiteral("[*]"));
118 }
119
120 void TextDocumentEditorView::refresh()
121 {
122 QString text = QString::fromUtf8(textDocument->Text.getValue());
123 getEditor()->setPlainText(text);
124 }
125
126 void TextDocumentEditorView::saveToObject()
127 {
128 boost::signals2::shared_connection_block textBlock {textConnection};
129 textDocument->Text.setValue(getEditor()->document()->toPlainText().toUtf8());
130 textDocument->purgeTouched();
131 }
132
133 QStringList TextDocumentEditorView::undoActions() const
134 {
135 QStringList undo;
136 undo << tr("Edit text");
137 return undo;
138 }
139
140 QStringList TextDocumentEditorView::redoActions() const
141 {
142 QStringList redo;
143 redo << tr("Edit text");
144 return redo;
145 }
146
147 bool TextDocumentEditorView::onHasMsg(const char* msg) const
148 {
149 // don't allow any actions if the editor is being closed
150 if (aboutToClose) {
151 return false;
152 }
153
154 if (strcmp(msg, "Save") == 0) {
155 return true;
156 }
157 if (strcmp(msg, "Cut") == 0) {
158 return (!getEditor()->isReadOnly() && getEditor()->textCursor().hasSelection());
159 }
160 if (strcmp(msg, "Copy") == 0) {
161 return (getEditor()->textCursor().hasSelection());
162 }
163 if (strcmp(msg, "Paste") == 0) {
164 if (getEditor()->isReadOnly()) {
165 return false;
166 }
167 QClipboard* cb = QApplication::clipboard();
168 QString text = cb->text();
169 return !text.isEmpty();
170 }
171 if (strcmp(msg, "Undo") == 0) {
172 return (getEditor()->document()->isUndoAvailable());
173 }
174 if (strcmp(msg, "Redo") == 0) {
175 return (getEditor()->document()->isRedoAvailable());
176 }
177 return false;
178 }
179
180 bool TextDocumentEditorView::onMsg(const char* msg, const char** output)
181 {
182 Q_UNUSED(output)
183
184 // don't allow any actions if the editor is being closed
185 if (aboutToClose) {
186 return false;
187 }
188
189 if (strcmp(msg, "Save") == 0) {
190 saveToObject();
191 getGuiDocument()->save();
192 return true;
193 }
194 if (strcmp(msg, "Cut") == 0) {
195 getEditor()->cut();
196 return true;
197 }
198 if (strcmp(msg, "Copy") == 0) {
199 getEditor()->copy();
200 return true;
201 }
202 if (strcmp(msg, "Paste") == 0) {
203 getEditor()->paste();
204 return true;
205 }
206 if (strcmp(msg, "Undo") == 0) {
207 getEditor()->undo();
208 return true;
209 }
210 if (strcmp(msg, "Redo") == 0) {
211 getEditor()->redo();
212 return true;
213 }
214 return false;
215 }
216
217 #include "moc_TextDocumentEditorView.cpp"