File: mdichild.cpp

package info (click to toggle)
graphviz 14.0.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,388 kB
  • sloc: ansic: 141,938; cpp: 11,957; python: 7,766; makefile: 4,043; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,388; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (157 lines) | stat: -rw-r--r-- 4,362 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Details at https://graphviz.org
 *************************************************************************/

#include "mdichild.h"
#include "mainwindow.h"
#include <QtGlobal>
#include <QtWidgets>
#include <memory>

MdiChild::MdiChild() {
  setAttribute(Qt::WA_DeleteOnClose);
  isUntitled = true;
  layoutIdx = 0;
  renderIdx = 0;
  preview = true;
  applyCairo = false;
  settingsSet = false;
}

void MdiChild::newFile() {
  static int sequenceNumber = 1;

  isUntitled = true;
  curFile = tr("graph%1.gv").arg(sequenceNumber++);
  setWindowTitle(curFile + QLatin1String("[*]"));

  connect(document(), &QTextDocument::contentsChange, this,
          &MdiChild::documentWasModified);
}

bool MdiChild::loadFile(const QString &fileName) {
  QFile file(fileName);
  if (!file.open(QFile::ReadOnly | QFile::Text)) {
    QMessageBox::warning(
        this, tr("MDI"),
        tr("Cannot read file %1:\n%2.").arg(fileName).arg(file.errorString()));
    return false;
  }

  QTextStream in(&file);
  QApplication::setOverrideCursor(Qt::WaitCursor);
  setPlainText(in.readAll());
  QApplication::restoreOverrideCursor();

  setCurrentFile(fileName);

  connect(document(), &QTextDocument::contentsChange, this,
          &MdiChild::documentWasModified);

  return true;
}

bool MdiChild::save() {
  if (isUntitled) {
    return saveAs();
  }
  return saveFile(curFile);
}

bool MdiChild::saveAs() {
  QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), curFile);
  if (fileName.isEmpty())
    return false;

  return saveFile(fileName);
}

bool MdiChild::saveFile(const QString &fileName) {
  QFile file(fileName);
  if (!file.open(QFile::WriteOnly | QFile::Text)) {
    QMessageBox::warning(
        this, tr("MDI"),
        tr("Cannot write file %1:\n%2.").arg(fileName).arg(file.errorString()));
    return false;
  }

  QTextStream out(&file);
  QApplication::setOverrideCursor(Qt::WaitCursor);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  out.setCodec("UTF-8");
#endif
  out << toPlainText();
  out.flush();
  QApplication::restoreOverrideCursor();

  setCurrentFile(fileName);
  return true;
}

QString MdiChild::userFriendlyCurrentFile() { return strippedName(curFile); }

void MdiChild::closeEvent(QCloseEvent *event) {
  if (maybeSave()) {
    event->accept();
  } else {
    event->ignore();
  }
}

void MdiChild::documentWasModified() {
  setWindowModified(document()->isModified());
}

bool MdiChild::maybeSave() {
  if (document()->isModified()) {
    QMessageBox::StandardButton ret;
    ret = QMessageBox::warning(this, tr("MDI"),
                               tr("'%1' has been modified.\n"
                                  "Do you want to save your changes?")
                                   .arg(userFriendlyCurrentFile()),
                               QMessageBox::Save | QMessageBox::Discard |
                                   QMessageBox::Cancel);
    if (ret == QMessageBox::Save)
      return save();
    if (ret == QMessageBox::Cancel)
      return false;
  }
  return true;
}

void MdiChild::setCurrentFile(const QString &fileName) {
  curFile = QFileInfo(fileName).canonicalFilePath();
  isUntitled = false;
  document()->setModified(false);
  setWindowModified(false);
  setWindowTitle(userFriendlyCurrentFile() + QLatin1String("[*]"));
}

QString MdiChild::strippedName(const QString &fullFileName) {
  return QFileInfo(fullFileName).fileName();
}

bool MdiChild::loadPreview(const QString &fileName) {
  if (previewFrm == nullptr) {
    previewFrm = std::make_unique<ImageViewer>();
    previewFrm->graphWindow = this;
    QMdiSubWindow *s = parentFrm->mdiArea->addSubWindow(previewFrm.get());

    s->resize(600, 400);
    s->move(parentFrm->mdiArea->subWindowList().count() * 5,
            parentFrm->mdiArea->subWindowList().count() * 5);
    previewFrm->subWindowRef = s;
  }
  bool rv = previewFrm->open(fileName);
  if (rv)
    previewFrm->show();
  return rv;
}

bool MdiChild::firstTime() { return settingsSet; }