mdichild.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include "mdichild.h"
  11. #include "mainwindow.h"
  12. #include <QtGlobal>
  13. #include <QtWidgets>
  14. #include <memory>
  15. MdiChild::MdiChild() {
  16. setAttribute(Qt::WA_DeleteOnClose);
  17. isUntitled = true;
  18. layoutIdx = 0;
  19. renderIdx = 0;
  20. preview = true;
  21. applyCairo = false;
  22. settingsSet = false;
  23. }
  24. void MdiChild::newFile() {
  25. static int sequenceNumber = 1;
  26. isUntitled = true;
  27. curFile = tr("graph%1.gv").arg(sequenceNumber++);
  28. setWindowTitle(curFile + QLatin1String("[*]"));
  29. connect(document(), &QTextDocument::contentsChange, this,
  30. &MdiChild::documentWasModified);
  31. }
  32. bool MdiChild::loadFile(const QString &fileName) {
  33. QFile file(fileName);
  34. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  35. QMessageBox::warning(
  36. this, tr("MDI"),
  37. tr("Cannot read file %1:\n%2.").arg(fileName).arg(file.errorString()));
  38. return false;
  39. }
  40. QTextStream in(&file);
  41. QApplication::setOverrideCursor(Qt::WaitCursor);
  42. setPlainText(in.readAll());
  43. QApplication::restoreOverrideCursor();
  44. setCurrentFile(fileName);
  45. connect(document(), &QTextDocument::contentsChange, this,
  46. &MdiChild::documentWasModified);
  47. return true;
  48. }
  49. bool MdiChild::save() {
  50. if (isUntitled) {
  51. return saveAs();
  52. }
  53. return saveFile(curFile);
  54. }
  55. bool MdiChild::saveAs() {
  56. QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), curFile);
  57. if (fileName.isEmpty())
  58. return false;
  59. return saveFile(fileName);
  60. }
  61. bool MdiChild::saveFile(const QString &fileName) {
  62. QFile file(fileName);
  63. if (!file.open(QFile::WriteOnly | QFile::Text)) {
  64. QMessageBox::warning(
  65. this, tr("MDI"),
  66. tr("Cannot write file %1:\n%2.").arg(fileName).arg(file.errorString()));
  67. return false;
  68. }
  69. QTextStream out(&file);
  70. QApplication::setOverrideCursor(Qt::WaitCursor);
  71. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  72. out.setCodec("UTF-8");
  73. #endif
  74. out << toPlainText();
  75. out.flush();
  76. QApplication::restoreOverrideCursor();
  77. setCurrentFile(fileName);
  78. return true;
  79. }
  80. QString MdiChild::userFriendlyCurrentFile() { return strippedName(curFile); }
  81. void MdiChild::closeEvent(QCloseEvent *event) {
  82. if (maybeSave()) {
  83. event->accept();
  84. } else {
  85. event->ignore();
  86. }
  87. }
  88. void MdiChild::documentWasModified() {
  89. setWindowModified(document()->isModified());
  90. }
  91. bool MdiChild::maybeSave() {
  92. if (document()->isModified()) {
  93. QMessageBox::StandardButton ret;
  94. ret = QMessageBox::warning(this, tr("MDI"),
  95. tr("'%1' has been modified.\n"
  96. "Do you want to save your changes?")
  97. .arg(userFriendlyCurrentFile()),
  98. QMessageBox::Save | QMessageBox::Discard |
  99. QMessageBox::Cancel);
  100. if (ret == QMessageBox::Save)
  101. return save();
  102. if (ret == QMessageBox::Cancel)
  103. return false;
  104. }
  105. return true;
  106. }
  107. void MdiChild::setCurrentFile(const QString &fileName) {
  108. curFile = QFileInfo(fileName).canonicalFilePath();
  109. isUntitled = false;
  110. document()->setModified(false);
  111. setWindowModified(false);
  112. setWindowTitle(userFriendlyCurrentFile() + QLatin1String("[*]"));
  113. }
  114. QString MdiChild::strippedName(const QString &fullFileName) {
  115. return QFileInfo(fullFileName).fileName();
  116. }
  117. bool MdiChild::loadPreview(const QString &fileName) {
  118. if (previewFrm == nullptr) {
  119. previewFrm = std::make_unique<ImageViewer>();
  120. previewFrm->graphWindow = this;
  121. QMdiSubWindow *s = parentFrm->mdiArea->addSubWindow(previewFrm.get());
  122. s->resize(600, 400);
  123. s->move(parentFrm->mdiArea->subWindowList().count() * 5,
  124. parentFrm->mdiArea->subWindowList().count() * 5);
  125. previewFrm->subWindowRef = s;
  126. }
  127. bool rv = previewFrm->open(fileName);
  128. if (rv)
  129. previewFrm->show();
  130. return rv;
  131. }
  132. bool MdiChild::firstTime() { return settingsSet; }