formwindowfile.cpp 5.97 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31 32
#include "formwindowfile.h"
#include "designerconstants.h"

33
#include <utils/qtcassert.h>
con's avatar
con committed
34

35 36 37
#include <QDesignerFormWindowInterface>
#include <QDesignerFormWindowManagerInterface>
#include <QDesignerFormEditorInterface>
38 39 40
#if QT_VERSION < 0x050000
#    include "qt_private/qsimpleresource_p.h"
#endif
con's avatar
con committed
41

42
#include <QUndoStack>
con's avatar
con committed
43

44 45 46
#include <QFileInfo>
#include <QDebug>
#include <QTextCodec>
con's avatar
con committed
47

48 49
namespace Designer {
namespace Internal {
con's avatar
con committed
50

hjk's avatar
hjk committed
51
FormWindowFile::FormWindowFile(QDesignerFormWindowInterface *form, QObject *parent)
52
  : Core::TextDocument(parent),
53
    m_mimeType(QLatin1String(Designer::Constants::FORM_MIMETYPE)),
54
    m_shouldAutoSave(false),
hjk's avatar
hjk committed
55
    m_formWindow(form)
con's avatar
con committed
56
{
57 58
    // Designer needs UTF-8 regardless of settings.
    setCodec(QTextCodec::codecForName("UTF-8"));
59 60
    connect(m_formWindow->core()->formWindowManager(), SIGNAL(formWindowRemoved(QDesignerFormWindowInterface*)),
            this, SLOT(slotFormWindowRemoved(QDesignerFormWindowInterface*)));
61 62
    connect(m_formWindow->commandHistory(), SIGNAL(indexChanged(int)),
            this, SLOT(setShouldAutoSave()));
con's avatar
con committed
63 64
}

65
bool FormWindowFile::save(QString *errorString, const QString &name, bool autoSave)
con's avatar
con committed
66 67 68
{
    const QString actualName = name.isEmpty() ? fileName() : name;

69 70
    if (Designer::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << name << "->" << actualName;
con's avatar
con committed
71

72 73
    QTC_ASSERT(m_formWindow, return false);

con's avatar
con committed
74 75 76 77 78
    if (actualName.isEmpty())
        return false;

    const QFileInfo fi(actualName);
    const QString oldFormName = m_formWindow->fileName();
79 80
    if (!autoSave)
        m_formWindow->setFileName(fi.absoluteFilePath());
81 82 83
#if QT_VERSION >= 0x050000
    const bool writeOK = writeFile(actualName, errorString);
#else
84 85 86
    const bool warningsEnabled = qdesigner_internal::QSimpleResource::setWarningsEnabled(false);
    const bool writeOK = writeFile(actualName, errorString);
    qdesigner_internal::QSimpleResource::setWarningsEnabled(warningsEnabled);
87
#endif
88 89 90 91
    m_shouldAutoSave = false;
    if (autoSave)
        return writeOK;

92
    if (!writeOK) {
con's avatar
con committed
93 94 95 96
        m_formWindow->setFileName(oldFormName);
        return false;
    }

97
    const QString oldFileName = m_fileName;
con's avatar
con committed
98 99 100
    m_fileName = fi.absoluteFilePath();
    emit setDisplayName(fi.fileName());
    m_formWindow->setDirty(false);
101
    emit fileNameChanged(oldFileName, m_fileName);
con's avatar
con committed
102
    emit changed();
103
    emit saved();
con's avatar
con committed
104 105 106 107

    return true;
}

dt's avatar
dt committed
108 109 110 111
void FormWindowFile::rename(const QString &newName)
{
    m_formWindow->setFileName(newName);
    QFileInfo fi(newName);
112
    const QString oldFileName = m_fileName;
dt's avatar
dt committed
113 114
    m_fileName = fi.absoluteFilePath();
    emit setDisplayName(fi.fileName());
115
    emit fileNameChanged(oldFileName, m_fileName);
dt's avatar
dt committed
116 117 118
    emit changed();
}

con's avatar
con committed
119 120 121 122 123
QString FormWindowFile::fileName() const
{
    return m_fileName;
}

124 125 126 127 128
bool FormWindowFile::shouldAutoSave() const
{
    return m_shouldAutoSave;
}

con's avatar
con committed
129 130
bool FormWindowFile::isModified() const
{
131
    return m_formWindow && m_formWindow->isDirty();
con's avatar
con committed
132 133 134 135 136 137 138
}

bool FormWindowFile::isSaveAsAllowed() const
{
    return true;
}

139
bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
140 141
{
    if (flag == FlagIgnore)
142
        return true;
143
    if (type == TypePermissions) {
con's avatar
con committed
144
        emit changed();
145
    } else {
146
        emit aboutToReload();
147
        emit reload(errorString, m_fileName);
148 149 150
        const bool success = errorString->isEmpty();
        emit reloadFinished(success);
        return success;
con's avatar
con committed
151
    }
152
    return true;
con's avatar
con committed
153 154 155 156 157 158 159 160 161
}

QString FormWindowFile::defaultPath() const
{
    return QString();
}

void FormWindowFile::setSuggestedFileName(const QString &fileName)
{
162 163
    if (Designer::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << m_fileName << fileName;
con's avatar
con committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177

    m_suggestedName = fileName;
}

QString FormWindowFile::suggestedFileName() const
{
    return m_suggestedName;
}

QString FormWindowFile::mimeType() const
{
    return m_mimeType;
}

178
bool FormWindowFile::writeFile(const QString &fileName, QString *errorString) const
con's avatar
con committed
179
{
180 181
    if (Designer::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << m_fileName << fileName;
182
    return write(fileName, format(), m_formWindow->contents(), errorString);
con's avatar
con committed
183 184 185 186 187 188
}

void FormWindowFile::setFileName(const QString &fname)
{
    m_fileName = fname;
}
189 190 191 192 193 194

QDesignerFormWindowInterface *FormWindowFile::formWindow() const
{
    return m_formWindow;
}

195 196 197 198 199 200 201 202 203
void FormWindowFile::slotFormWindowRemoved(QDesignerFormWindowInterface *w)
{
    // Release formwindow as soon as the FormWindowManager removes
    // as calls to isDirty() are triggered at arbitrary times
    // while building.
    if (w == m_formWindow)
        m_formWindow = 0;
}

204 205
} // namespace Internal
} // namespace Designer