~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/widgets/mainwindows/mdi/mdichild.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
14
**   * Redistributions of source code must retain the above copyright
 
15
**     notice, this list of conditions and the following disclaimer.
 
16
**   * Redistributions in binary form must reproduce the above copyright
 
17
**     notice, this list of conditions and the following disclaimer in
 
18
**     the documentation and/or other materials provided with the
 
19
**     distribution.
 
20
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
 
21
**     of its contributors may be used to endorse or promote products derived
 
22
**     from this software without specific prior written permission.
 
23
**
 
24
**
 
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 
36
**
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
#include <QtWidgets>
 
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
}