| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include <QtGui>
|
|---|
| 42 |
|
|---|
| 43 | #include "mdichild.h"
|
|---|
| 44 |
|
|---|
| 45 | MdiChild::MdiChild()
|
|---|
| 46 | {
|
|---|
| 47 | setAttribute(Qt::WA_DeleteOnClose);
|
|---|
| 48 | isUntitled = true;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | void MdiChild::newFile()
|
|---|
| 52 | {
|
|---|
| 53 | static int sequenceNumber = 1;
|
|---|
| 54 |
|
|---|
| 55 | isUntitled = true;
|
|---|
| 56 | curFile = tr("document%1.txt").arg(sequenceNumber++);
|
|---|
| 57 | setWindowTitle(curFile + "[*]");
|
|---|
| 58 |
|
|---|
| 59 | connect(document(), SIGNAL(contentsChanged()),
|
|---|
| 60 | this, SLOT(documentWasModified()));
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | bool MdiChild::loadFile(const QString &fileName)
|
|---|
| 64 | {
|
|---|
| 65 | QFile file(fileName);
|
|---|
| 66 | if (!file.open(QFile::ReadOnly | QFile::Text)) {
|
|---|
| 67 | QMessageBox::warning(this, tr("MDI"),
|
|---|
| 68 | tr("Cannot read file %1:\n%2.")
|
|---|
| 69 | .arg(fileName)
|
|---|
| 70 | .arg(file.errorString()));
|
|---|
| 71 | return false;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | QTextStream in(&file);
|
|---|
| 75 | QApplication::setOverrideCursor(Qt::WaitCursor);
|
|---|
| 76 | setPlainText(in.readAll());
|
|---|
| 77 | QApplication::restoreOverrideCursor();
|
|---|
| 78 |
|
|---|
| 79 | setCurrentFile(fileName);
|
|---|
| 80 |
|
|---|
| 81 | connect(document(), SIGNAL(contentsChanged()),
|
|---|
| 82 | this, SLOT(documentWasModified()));
|
|---|
| 83 |
|
|---|
| 84 | return true;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | bool MdiChild::save()
|
|---|
| 88 | {
|
|---|
| 89 | if (isUntitled) {
|
|---|
| 90 | return saveAs();
|
|---|
| 91 | } else {
|
|---|
| 92 | return saveFile(curFile);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | bool MdiChild::saveAs()
|
|---|
| 97 | {
|
|---|
| 98 | QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
|
|---|
| 99 | curFile);
|
|---|
| 100 | if (fileName.isEmpty())
|
|---|
| 101 | return false;
|
|---|
| 102 |
|
|---|
| 103 | return saveFile(fileName);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | bool MdiChild::saveFile(const QString &fileName)
|
|---|
| 107 | {
|
|---|
| 108 | QFile file(fileName);
|
|---|
| 109 | if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
|---|
| 110 | QMessageBox::warning(this, tr("MDI"),
|
|---|
| 111 | tr("Cannot write file %1:\n%2.")
|
|---|
| 112 | .arg(fileName)
|
|---|
| 113 | .arg(file.errorString()));
|
|---|
| 114 | return false;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | QTextStream out(&file);
|
|---|
| 118 | QApplication::setOverrideCursor(Qt::WaitCursor);
|
|---|
| 119 | out << toPlainText();
|
|---|
| 120 | QApplication::restoreOverrideCursor();
|
|---|
| 121 |
|
|---|
| 122 | setCurrentFile(fileName);
|
|---|
| 123 | return true;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | QString MdiChild::userFriendlyCurrentFile()
|
|---|
| 127 | {
|
|---|
| 128 | return strippedName(curFile);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | void MdiChild::closeEvent(QCloseEvent *event)
|
|---|
| 132 | {
|
|---|
| 133 | if (maybeSave()) {
|
|---|
| 134 | event->accept();
|
|---|
| 135 | } else {
|
|---|
| 136 | event->ignore();
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | void MdiChild::documentWasModified()
|
|---|
| 141 | {
|
|---|
| 142 | setWindowModified(document()->isModified());
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | bool MdiChild::maybeSave()
|
|---|
| 146 | {
|
|---|
| 147 | if (document()->isModified()) {
|
|---|
| 148 | QMessageBox::StandardButton ret;
|
|---|
| 149 | ret = QMessageBox::warning(this, tr("MDI"),
|
|---|
| 150 | tr("'%1' has been modified.\n"
|
|---|
| 151 | "Do you want to save your changes?")
|
|---|
| 152 | .arg(userFriendlyCurrentFile()),
|
|---|
| 153 | QMessageBox::Save | QMessageBox::Discard
|
|---|
| 154 | | QMessageBox::Cancel);
|
|---|
| 155 | if (ret == QMessageBox::Save)
|
|---|
| 156 | return save();
|
|---|
| 157 | else if (ret == QMessageBox::Cancel)
|
|---|
| 158 | return false;
|
|---|
| 159 | }
|
|---|
| 160 | return true;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | void MdiChild::setCurrentFile(const QString &fileName)
|
|---|
| 164 | {
|
|---|
| 165 | curFile = QFileInfo(fileName).canonicalFilePath();
|
|---|
| 166 | isUntitled = false;
|
|---|
| 167 | document()->setModified(false);
|
|---|
| 168 | setWindowModified(false);
|
|---|
| 169 | setWindowTitle(userFriendlyCurrentFile() + "[*]");
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | QString MdiChild::strippedName(const QString &fullFileName)
|
|---|
| 173 | {
|
|---|
| 174 | return QFileInfo(fullFileName).fileName();
|
|---|
| 175 | }
|
|---|