summaryrefslogtreecommitdiff
path: root/src/plugins/designer/formwindoweditor.cpp
blob: 76d606a9a2373af11dc30fdb7824cd6f3117f2a1 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "formwindoweditor.h"
#include "formwindowfile.h"
#include "designerconstants.h"
#include "resourcehandler.h"
#include "qt_private/formwindowbase_p.h"
#include "designerxmleditor.h"
#include <widgethost.h>

#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/mimedatabase.h>
#include <texteditor/basetextdocument.h>
#include <texteditor/plaintexteditor.h>

#include <utils/qtcassert.h>

#include <QtDesigner/QDesignerFormWindowInterface>

#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtCore/QFile>

namespace Designer {

struct FormWindowEditorPrivate {
    explicit FormWindowEditorPrivate(Internal::DesignerXmlEditor *editor,
                                     QDesignerFormWindowInterface *form);

    TextEditor::PlainTextEditorEditable m_textEditable;
    Internal::FormWindowFile m_file;
    QList<int> m_context;
};

FormWindowEditorPrivate::FormWindowEditorPrivate(Internal::DesignerXmlEditor *editor,
                                                 QDesignerFormWindowInterface *form) :
    m_textEditable(editor), m_file(form)
{
}

FormWindowEditor::FormWindowEditor(Internal::DesignerXmlEditor *editor,
                                                     QDesignerFormWindowInterface *form,
                                                     QObject *parent) :
    Core::IEditor(parent),
    d(new FormWindowEditorPrivate(editor, form))
{
    Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
    d->m_context << uidm->uniqueIdentifier(QLatin1String(Designer::Constants::K_DESIGNER_XML_EDITOR_ID))
                 << uidm->uniqueIdentifier(QLatin1String(Designer::Constants::C_DESIGNER_XML_EDITOR));
    connect(form, SIGNAL(changed()), this, SIGNAL(changed()));
    // Revert to saved/load externally modified files
    connect(&(d->m_file), SIGNAL(reload(QString)), this, SLOT(slotOpen(QString)));
    // Force update of open editors model.
    connect(&(d->m_file), SIGNAL(saved()), this, SIGNAL(changed()));
    connect(&(d->m_file), SIGNAL(changed()), this, SIGNAL(changed()));
    connect(this, SIGNAL(changed()), this, SLOT(configureXmlEditor()));
}

FormWindowEditor::~FormWindowEditor()
{
    delete d;
}

bool FormWindowEditor::createNew(const QString &contents)
{
    if (Designer::Constants::Internal::debug)
        qDebug() << "FormWindowEditor::createNew" << contents.size();

    syncXmlEditor(QString());

    QDesignerFormWindowInterface *form = d->m_file.formWindow();
    QTC_ASSERT(form, return false);

    if (contents.isEmpty())
        return false;

    form->setContents(contents);
    if (form->mainContainer() == 0)
        return false;

    syncXmlEditor(contents);
    d->m_file.setFileName(QString());
    return true;
}

void FormWindowEditor::slotOpen(const QString &fileName)
{
    open(fileName);
}

bool FormWindowEditor::open(const QString &fileName)
{
    if (Designer::Constants::Internal::debug)
        qDebug() << "FormWindowEditor::open" << fileName;

    QDesignerFormWindowInterface *form = d->m_file.formWindow();
    QTC_ASSERT(form, return false);

    if (fileName.isEmpty()) {
        setDisplayName(tr("untitled"));
        return true;
    }

    const QFileInfo fi(fileName);
    const QString absfileName = fi.absoluteFilePath();

    QFile file(absfileName);
    if (!file.open(QIODevice::ReadOnly|QIODevice::Text))
        return false;

    form->setFileName(absfileName);

    const QString contents = QString::fromUtf8(file.readAll());
    form->setContents(contents);
    file.close();
    if (!form->mainContainer())
        return false;
    form->setDirty(false);
    syncXmlEditor(contents);

    setDisplayName(fi.fileName());
    d->m_file.setFileName(absfileName);

    if (Internal::ResourceHandler *rh = qFindChild<Designer::Internal::ResourceHandler*>(form))
        rh->updateResources();

    emit changed();

    return true;
}

void FormWindowEditor::syncXmlEditor()
{
    if (Designer::Constants::Internal::debug)
        qDebug() << "FormWindowEditor::syncXmlEditor" << d->m_file.fileName();
    syncXmlEditor(contents());
}

void FormWindowEditor::configureXmlEditor() const
{
    TextEditor::PlainTextEditor *editor =
            qobject_cast<TextEditor::PlainTextEditor *>(d->m_textEditable.editor());
    if (editor)
        editor->configure(Core::ICore::instance()->mimeDatabase()->findByFile(
                d->m_file.fileName()));
}

void FormWindowEditor::syncXmlEditor(const QString &contents)
{
    d->m_textEditable.editor()->setPlainText(contents);
    d->m_textEditable.editor()->setReadOnly(true);
}

Core::IFile *FormWindowEditor::file()
{
    return &d->m_file;
}

QString FormWindowEditor::id() const
{
    return QLatin1String(Designer::Constants::K_DESIGNER_XML_EDITOR_ID);
}

QString FormWindowEditor::displayName() const
{
    return d->m_textEditable.displayName();
}

void FormWindowEditor::setDisplayName(const QString &title)
{
    d->m_textEditable.setDisplayName(title);
}

bool FormWindowEditor::duplicateSupported() const
{
    return false;
}

Core::IEditor *FormWindowEditor::duplicate(QWidget *)
{
    return 0;
}

QByteArray FormWindowEditor::saveState() const
{
    return d->m_textEditable.saveState();
}

bool FormWindowEditor::restoreState(const QByteArray &state)
{
    return d->m_textEditable.restoreState(state);
}

QList<int> FormWindowEditor::context() const
{
    return d->m_context;
}

QWidget *FormWindowEditor::widget()
{
    return d->m_textEditable.widget();
}

bool FormWindowEditor::isTemporary() const
{
    return false;
}

QWidget *FormWindowEditor::toolBar()
{
    return 0;
}

QString FormWindowEditor::contents() const
{
    const qdesigner_internal::FormWindowBase *fw = qobject_cast<const qdesigner_internal::FormWindowBase *>(d->m_file.formWindow());
    QTC_ASSERT(fw, return QString());
    return fw->fileContents(); // No warnings about spacers here
}

TextEditor::BaseTextDocument *FormWindowEditor::textDocument()
{
    return qobject_cast<TextEditor::BaseTextDocument*>(d->m_textEditable.file());
}

TextEditor::PlainTextEditorEditable *FormWindowEditor::textEditable()
{
    return &d->m_textEditable;
}

QString FormWindowEditor::preferredMode() const
{
    return QLatin1String(Core::Constants::MODE_DESIGN);
}

} // namespace Designer