23
#include "PreCompiled.h"
25
# include <QApplication>
27
# include <QMessageBox>
28
# include <QPushButton>
32
#include "TextDocumentEditorView.h"
33
#include "Application.h"
35
#include "MainWindow.h"
40
TYPESYSTEM_SOURCE_ABSTRACT(Gui::TextDocumentEditorView, Gui::MDIView)
42
TextDocumentEditorView::TextDocumentEditorView(
43
App::TextDocument* txtDoc, QPlainTextEdit* e,
46
Application::Instance->getDocument(txtDoc->getDocument()),
48
editor {e}, textDocument {txtDoc}
52
setCentralWidget(editor);
55
Gui::MainWindow* mw = Gui::getMainWindow();
56
connect(editor, &QPlainTextEdit::undoAvailable, mw, &MainWindow::updateEditorActions);
57
connect(editor, &QPlainTextEdit::redoAvailable, mw, &MainWindow::updateEditorActions);
58
connect(editor, &QPlainTextEdit::copyAvailable, mw, &MainWindow::updateEditorActions);
61
TextDocumentEditorView::~TextDocumentEditorView()
63
textConnection.disconnect();
64
labelConnection.disconnect();
67
void TextDocumentEditorView::showEvent(QShowEvent* event)
69
Gui::MainWindow* mw = Gui::getMainWindow();
70
mw->updateEditorActions();
71
MDIView::showEvent(event);
74
void TextDocumentEditorView::hideEvent(QHideEvent* event)
76
MDIView::hideEvent(event);
79
void TextDocumentEditorView::closeEvent(QCloseEvent* event)
81
MDIView::closeEvent(event);
82
if (event->isAccepted()) {
84
Gui::MainWindow* mw = Gui::getMainWindow();
85
mw->updateEditorActions();
89
bool TextDocumentEditorView::event(QEvent *event)
91
if (event->type() == QEvent::Show && sourceModified) {
93
sourceModified = false;
95
return MDIView::event(event);
98
void TextDocumentEditorView::setupEditor()
100
connect(getEditor()->document(), &QTextDocument::modificationChanged,
101
this, &TextDocumentEditorView::setWindowModified);
102
setWindowTitle(QString::fromUtf8(textDocument->Label.getValue())
103
+ QString::fromLatin1("[*]"));
104
getEditor()->setPlainText(
105
QString::fromUtf8(textDocument->Text.getValue()));
108
void TextDocumentEditorView::setupConnection()
111
textConnection = textDocument->connectText(
112
std::bind(&TextDocumentEditorView::sourceChanged, this));
113
labelConnection = textDocument->connectLabel(
114
std::bind(&TextDocumentEditorView::labelChanged, this));
118
void TextDocumentEditorView::sourceChanged()
120
if (getMainWindow()->activeWindow() == this) {
122
sourceModified = false;
124
sourceModified = true;
128
void TextDocumentEditorView::labelChanged()
130
setWindowTitle(QString::fromUtf8(textDocument->Label.getValue())
131
+ QString::fromLatin1("[*]"));
134
void TextDocumentEditorView::refresh()
136
QString text = QString::fromUtf8(
137
textDocument->Text.getValue());
138
if (isEditorModified()) {
139
QMessageBox msgBox {this};
140
msgBox.setWindowTitle(tr("Text updated"));
141
msgBox.setIcon(QMessageBox::Question);
143
"The text of the underlying object has changed. "
144
"Discard changes and reload the text from the object?"));
146
tr("Yes, reload."), QMessageBox::YesRole);
147
QPushButton* noBtt = msgBox.addButton(QMessageBox::No);
149
if (msgBox.clickedButton() == noBtt)
152
getEditor()->setPlainText(text);
155
bool TextDocumentEditorView::onMsg(const char* msg, const char**)
161
if (strcmp(msg,"Save") == 0) {
163
return getGuiDocument()->save();
165
if (strcmp(msg,"Cut") == 0) {
169
if (strcmp(msg,"Copy") == 0) {
173
if (strcmp(msg,"Paste") == 0) {
174
getEditor()->paste();
177
if (strcmp(msg,"Undo") == 0) {
181
if (strcmp(msg,"Redo") == 0) {
188
bool TextDocumentEditorView::isEditorModified() const
190
return getEditor()->document()->isModified();
193
bool TextDocumentEditorView::onHasMsg(const char* msg) const
199
if (strcmp(msg,"Save") == 0) {
202
if (strcmp(msg,"Cut") == 0) {
203
return (!getEditor()->isReadOnly() &&
204
getEditor()->textCursor().hasSelection());
206
if (strcmp(msg,"Copy") == 0) {
207
return (getEditor()->textCursor().hasSelection());
209
if (strcmp(msg,"Paste") == 0) {
210
if (getEditor()->isReadOnly())
212
QClipboard *cb = QApplication::clipboard();
213
QString text = cb->text();
214
return !text.isEmpty();
216
if (strcmp(msg,"Undo") == 0) {
217
return (getEditor()->document()->isUndoAvailable());
219
if (strcmp(msg,"Redo") == 0) {
220
return (getEditor()->document()->isRedoAvailable());
225
bool TextDocumentEditorView::canClose()
227
if (getEditor()->document()->isModified()) {
230
QMessageBox box(this);
231
box.setIcon(QMessageBox::Question);
232
box.setWindowTitle(tr("Unsaved document"));
233
box.setText(tr("Do you want to save your changes before closing?"));
234
box.setInformativeText(tr("If you don't save, your changes will be lost."));
235
box.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel | QMessageBox::Save);
236
box.setDefaultButton(QMessageBox::Save);
237
box.setEscapeButton(QMessageBox::Cancel);
240
QAbstractButton* saveBtn = box.button(QMessageBox::Save);
241
if (saveBtn->shortcut().isEmpty()) {
242
QString text = saveBtn->text();
243
text.prepend(QLatin1Char('&'));
244
saveBtn->setShortcut(QKeySequence::mnemonic(text));
247
QAbstractButton* discardBtn = box.button(QMessageBox::Discard);
248
if (discardBtn->shortcut().isEmpty()) {
249
QString text = discardBtn->text();
250
text.prepend(QLatin1Char('&'));
251
discardBtn->setShortcut(QKeySequence::mnemonic(text));
257
case QMessageBox::Save:
259
if (getGuiDocument()->isLastView())
260
return getGuiDocument()->save();
262
case QMessageBox::Discard:
264
case QMessageBox::Cancel:
272
return MDIView::canClose();
276
void TextDocumentEditorView::saveToObject()
278
boost::signals2::shared_connection_block textBlock {textConnection};
279
textDocument->Text.setValue(
280
getEditor()->document()->toPlainText().toUtf8());
281
textDocument->purgeTouched();
282
getEditor()->document()->setModified(false);
285
QStringList TextDocumentEditorView::undoActions() const
288
undo << tr("Edit text");
292
QStringList TextDocumentEditorView::redoActions() const
295
redo << tr("Edit text");
299
#include "moc_TextDocumentEditorView.cpp"