Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
mdichild.cpp
Go to the documentation of this file.
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 "mdichild.h"
12#include "mainwindow.h"
13#include <QtGlobal>
14#include <QtWidgets>
15#include <memory>
16
18 setAttribute(Qt::WA_DeleteOnClose);
19 isUntitled = true;
20 layoutIdx = 0;
21 renderIdx = 0;
22 preview = true;
23 applyCairo = false;
24 settingsSet = false;
25}
26
28 static int sequenceNumber = 1;
29
30 isUntitled = true;
31 curFile = tr("graph%1.gv").arg(sequenceNumber++);
32 setWindowTitle(curFile + QLatin1String("[*]"));
33
34 connect(document(), &QTextDocument::contentsChange, this,
35 &MdiChild::documentWasModified);
36}
37
38bool MdiChild::loadFile(const QString &fileName) {
39 QFile file(fileName);
40 if (!file.open(QFile::ReadOnly | QFile::Text)) {
41 QMessageBox::warning(
42 this, tr("MDI"),
43 tr("Cannot read file %1:\n%2.").arg(fileName).arg(file.errorString()));
44 return false;
45 }
46
47 QTextStream in(&file);
48 QApplication::setOverrideCursor(Qt::WaitCursor);
49 setPlainText(in.readAll());
50 QApplication::restoreOverrideCursor();
51
52 setCurrentFile(fileName);
53
54 connect(document(), &QTextDocument::contentsChange, this,
55 &MdiChild::documentWasModified);
56
57 return true;
58}
59
61 if (isUntitled) {
62 return saveAs();
63 }
64 return saveFile(curFile);
65}
66
68 QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), curFile);
69 if (fileName.isEmpty())
70 return false;
71
72 return saveFile(fileName);
73}
74
75bool MdiChild::saveFile(const QString &fileName) {
76 QFile file(fileName);
77 if (!file.open(QFile::WriteOnly | QFile::Text)) {
78 QMessageBox::warning(
79 this, tr("MDI"),
80 tr("Cannot write file %1:\n%2.").arg(fileName).arg(file.errorString()));
81 return false;
82 }
83
84 QTextStream out(&file);
85 QApplication::setOverrideCursor(Qt::WaitCursor);
86#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
87 out.setCodec("UTF-8");
88#endif
89 out << toPlainText();
90 out.flush();
91 QApplication::restoreOverrideCursor();
92
93 setCurrentFile(fileName);
94 return true;
95}
96
97QString MdiChild::userFriendlyCurrentFile() { return strippedName(curFile); }
98
99void MdiChild::closeEvent(QCloseEvent *event) {
100 if (maybeSave()) {
101 event->accept();
102 } else {
103 event->ignore();
104 }
105}
106
107void MdiChild::documentWasModified() {
108 setWindowModified(document()->isModified());
109}
110
111bool MdiChild::maybeSave() {
112 if (document()->isModified()) {
113 QMessageBox::StandardButton ret;
114 ret = QMessageBox::warning(this, tr("MDI"),
115 tr("'%1' has been modified.\n"
116 "Do you want to save your changes?")
118 QMessageBox::Save | QMessageBox::Discard |
119 QMessageBox::Cancel);
120 if (ret == QMessageBox::Save)
121 return save();
122 if (ret == QMessageBox::Cancel)
123 return false;
124 }
125 return true;
126}
127
128void MdiChild::setCurrentFile(const QString &fileName) {
129 curFile = QFileInfo(fileName).canonicalFilePath();
130 isUntitled = false;
131 document()->setModified(false);
132 setWindowModified(false);
133 setWindowTitle(userFriendlyCurrentFile() + QLatin1String("[*]"));
134}
135
136QString MdiChild::strippedName(const QString &fullFileName) {
137 return QFileInfo(fullFileName).fileName();
138}
139
140bool MdiChild::loadPreview(const QString &fileName) {
141 if (previewFrm == nullptr) {
142 previewFrm = std::make_unique<ImageViewer>();
143 previewFrm->graphWindow = this;
144 QMdiSubWindow *s = parentFrm->mdiArea->addSubWindow(previewFrm.get());
145
146 s->resize(600, 400);
147 s->move(parentFrm->mdiArea->subWindowList().count() * 5,
148 parentFrm->mdiArea->subWindowList().count() * 5);
149 previewFrm->subWindowRef = s;
150 }
151 bool rv = previewFrm->open(fileName);
152 if (rv)
153 previewFrm->show();
154 return rv;
155}
156
static void out(agerrlevel_t level, const char *fmt, va_list args)
Report messages using a user-supplied or default write function.
Definition agerror.c:84
QMdiArea * mdiArea
Definition mainwindow.h:40
QString userFriendlyCurrentFile()
Definition mdichild.cpp:97
std::unique_ptr< ImageViewer > previewFrm
Definition mdichild.h:38
bool saveFile(const QString &fileName)
Definition mdichild.cpp:75
bool saveAs()
Definition mdichild.cpp:67
bool save()
Definition mdichild.cpp:60
bool preview
Definition mdichild.h:35
bool loadPreview(const QString &fileName)
Definition mdichild.cpp:140
bool applyCairo
Definition mdichild.h:36
bool firstTime()
Definition mdichild.cpp:157
bool loadFile(const QString &fileName)
Definition mdichild.cpp:38
int layoutIdx
Definition mdichild.h:32
void newFile()
Definition mdichild.cpp:27
bool settingsSet
Definition mdichild.h:42
void closeEvent(QCloseEvent *event)
Definition mdichild.cpp:99
int renderIdx
Definition mdichild.h:33
CMainWindow * parentFrm
Definition mdichild.h:39
char * fileName(ingraph_state *sp)
Return name of current file being processed.
Definition ingraphs.c:156
Definition grammar.c:93