| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include "mainwindow.h" |
| 14 |
#include "mdichild.h" |
| 15 |
#include <QtGlobal> |
| 16 |
#include <QtWidgets> |
| 17 |
#include <memory> |
| 18 |
|
| 19 |
MdiChild::MdiChild() { |
| 20 |
setAttribute(Qt::WA_DeleteOnClose); |
| 21 |
isUntitled = true; |
| 22 |
layoutIdx = 0; |
| 23 |
renderIdx = 0; |
| 24 |
preview = true; |
| 25 |
applyCairo = false; |
| 26 |
settingsSet = false; |
| 27 |
} |
| 28 |
|
| 29 |
void MdiChild::newFile() { |
| 30 |
static int sequenceNumber = 1; |
| 31 |
|
| 32 |
isUntitled = true; |
| 33 |
curFile = tr("graph%1.gv").arg(sequenceNumber++); |
| 34 |
setWindowTitle(curFile + QLatin1String("[*]")); |
| 35 |
|
| 36 |
connect(document(), &QTextDocument::contentsChange, this, |
| 37 |
&MdiChild::documentWasModified); |
| 38 |
} |
| 39 |
|
| 40 |
bool MdiChild::loadFile(const QString &fileName) { |
| 41 |
QFile file(fileName); |
| 42 |
if (!file.open(QFile::ReadOnly | QFile::Text)) { |
| 43 |
QMessageBox::warning( |
| 44 |
this, tr("MDI"), |
| 45 |
tr("Cannot read file %1:\n%2.").arg(fileName).arg(file.errorString())); |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
QTextStream in(&file); |
| 50 |
QApplication::setOverrideCursor(Qt::WaitCursor); |
| 51 |
setPlainText(in.readAll()); |
| 52 |
QApplication::restoreOverrideCursor(); |
| 53 |
|
| 54 |
setCurrentFile(fileName); |
| 55 |
|
| 56 |
connect(document(), &QTextDocument::contentsChange, this, |
| 57 |
&MdiChild::documentWasModified); |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
bool MdiChild::save() { |
| 63 |
if (isUntitled) { |
| 64 |
return saveAs(); |
| 65 |
} |
| 66 |
return saveFile(curFile); |
| 67 |
} |
| 68 |
|
| 69 |
bool MdiChild::saveAs() { |
| 70 |
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), curFile); |
| 71 |
if (fileName.isEmpty()) |
| 72 |
return false; |
| 73 |
|
| 74 |
return saveFile(fileName); |
| 75 |
} |
| 76 |
|
| 77 |
bool MdiChild::saveFile(const QString &fileName) { |
| 78 |
QFile file(fileName); |
| 79 |
if (!file.open(QFile::WriteOnly | QFile::Text)) { |
| 80 |
QMessageBox::warning( |
| 81 |
this, tr("MDI"), |
| 82 |
tr("Cannot write file %1:\n%2.").arg(fileName).arg(file.errorString())); |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
QTextStream out(&file); |
| 87 |
QApplication::setOverrideCursor(Qt::WaitCursor); |
| 88 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 89 |
out.setCodec("UTF-8"); |
| 90 |
#endif |
| 91 |
out << toPlainText(); |
| 92 |
out.flush(); |
| 93 |
QApplication::restoreOverrideCursor(); |
| 94 |
|
| 95 |
setCurrentFile(fileName); |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
QString MdiChild::userFriendlyCurrentFile() { return strippedName(curFile); } |
| 100 |
|
| 101 |
void MdiChild::closeEvent(QCloseEvent *event) { |
| 102 |
if (maybeSave()) { |
| 103 |
event->accept(); |
| 104 |
} else { |
| 105 |
event->ignore(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
void MdiChild::documentWasModified() { |
| 110 |
setWindowModified(document()->isModified()); |
| 111 |
} |
| 112 |
|
| 113 |
bool MdiChild::maybeSave() { |
| 114 |
if (document()->isModified()) { |
| 115 |
QMessageBox::StandardButton ret; |
| 116 |
ret = QMessageBox::warning(this, tr("MDI"), |
| 117 |
tr("'%1' has been modified.\n" |
| 118 |
"Do you want to save your changes?") |
| 119 |
.arg(userFriendlyCurrentFile()), |
| 120 |
QMessageBox::Save | QMessageBox::Discard | |
| 121 |
QMessageBox::Cancel); |
| 122 |
if (ret == QMessageBox::Save) |
| 123 |
return save(); |
| 124 |
if (ret == QMessageBox::Cancel) |
| 125 |
return false; |
| 126 |
} |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
void MdiChild::setCurrentFile(const QString &fileName) { |
| 131 |
curFile = QFileInfo(fileName).canonicalFilePath(); |
| 132 |
isUntitled = false; |
| 133 |
document()->setModified(false); |
| 134 |
setWindowModified(false); |
| 135 |
setWindowTitle(userFriendlyCurrentFile() + QLatin1String("[*]")); |
| 136 |
} |
| 137 |
|
| 138 |
QString MdiChild::strippedName(const QString &fullFileName) { |
| 139 |
return QFileInfo(fullFileName).fileName(); |
| 140 |
} |
| 141 |
|
| 142 |
bool MdiChild::loadPreview(const QString &fileName) { |
| 143 |
if (previewFrm == nullptr) { |
| 144 |
previewFrm = std::make_unique<ImageViewer>(); |
| 145 |
previewFrm->graphWindow = this; |
| 146 |
QMdiSubWindow *s = parentFrm->mdiArea->addSubWindow(previewFrm.get()); |
| 147 |
|
| 148 |
s->resize(600, 400); |
| 149 |
s->move(parentFrm->mdiArea->subWindowList().count() * 5, |
| 150 |
parentFrm->mdiArea->subWindowList().count() * 5); |
| 151 |
previewFrm->subWindowRef = s; |
| 152 |
} |
| 153 |
bool rv = previewFrm->open(fileName); |
| 154 |
if (rv) |
| 155 |
previewFrm->show(); |
| 156 |
return rv; |
| 157 |
} |
| 158 |
|
| 159 |
bool MdiChild::firstTime() { return settingsSet; } |