formwindoweditor.cpp 10.5 KB
Newer Older
con's avatar
con committed
1
2
3
4
/***************************************************************************
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
10
11
**
** Non-Open Source Usage
**
con's avatar
con committed
12
13
14
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
15
16
17
18
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
con's avatar
con committed
19
20
21
22
23
24
25
26
27
28
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
29
** rights. These rights are described in the Nokia Qt GPL Exception
30
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
31
32
**
***************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "designerconstants.h"
#include "editorwidget.h"
#include "formeditorw.h"
#include "formwindoweditor.h"
#include "formwindowfile.h"
#include "formwindowhost.h"

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/nodesvisitor.h>

#include <QtDesigner/QDesignerFormWindowInterface>
#include <QtDesigner/QDesignerFormEditorInterface>
#include <QtDesigner/QDesignerFormWindowManagerInterface>
48
49
#include <QtDesigner/QDesignerPropertyEditorInterface>
#include <QtDesigner/QDesignerWidgetDataBaseInterface>
50
51
#include <qt_private/formwindowbase_p.h>
#include <qt_private/qtresourcemodel_p.h>
con's avatar
con committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

#include <QtCore/QFile>
#include <QtCore/QDir>
#include <QtCore/QByteArray>
#include <QtCore/QFileInfo>
#include <QtCore/QTemporaryFile>
#include <QtCore/QDebug>
#include <QtGui/QToolBar>

using namespace Designer::Internal;
using namespace Designer::Constants;
using namespace SharedTools;
using ProjectExplorer::NodesVisitor;
using ProjectExplorer::ProjectNode;
using ProjectExplorer::FolderNode;
using ProjectExplorer::FileNode;

hjk's avatar
hjk committed
69
70
class QrcFilesVisitor : public NodesVisitor
{
con's avatar
con committed
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
public:
    QStringList qrcFiles() const;

    void visitProjectNode(ProjectNode *node);
    void visitFolderNode(FolderNode *node);
private:
    QStringList m_qrcFiles;
};

QStringList QrcFilesVisitor::qrcFiles() const
{
    return m_qrcFiles;
}

void QrcFilesVisitor::visitProjectNode(ProjectNode *projectNode)
{
    visitFolderNode(projectNode);
}

void QrcFilesVisitor::visitFolderNode(FolderNode *folderNode)
{
    foreach (const FileNode *fileNode, folderNode->fileNodes()) {
        if (fileNode->fileType() == ProjectExplorer::ResourceType)
            m_qrcFiles.append(fileNode->path());
    }
}


hjk's avatar
hjk committed
99
FormWindowEditor::FormWindowEditor(const QList<int> &context,
con's avatar
con committed
100
                                   QDesignerFormWindowInterface *form,
hjk's avatar
hjk committed
101
102
                                   QObject *parent)
  : Core::IEditor(parent),
con's avatar
con committed
103
104
    m_context(context),
    m_formWindow(form),
hjk's avatar
hjk committed
105
    m_file(new FormWindowFile(form, this)),
con's avatar
con committed
106
107
108
109
110
111
    m_host(new FormWindowHost(form)),
    m_editorWidget(new EditorWidget(m_host)),
    m_toolBar(0),
    m_sessionNode(0),
    m_sessionWatcher(0)
{
112
113
    if (Designer::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << form << parent;
con's avatar
con committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

    connect(m_file, SIGNAL(reload(QString)), this, SLOT(slotOpen(QString)));
    connect(m_file, SIGNAL(setDisplayName(QString)), this, SLOT(slotSetDisplayName(QString)));
    connect(m_file, SIGNAL(changed()), this, SIGNAL(changed()));
    connect(m_file, SIGNAL(changed()), this, SLOT(updateResources()));
    connect(this, SIGNAL(opened(QString)), m_file, SLOT(setFileName(QString)));

    connect(m_host, SIGNAL(changed()), this, SIGNAL(changed()));

    connect(form, SIGNAL(toolChanged(int)), m_editorWidget, SLOT(toolChanged(int)));
    m_editorWidget->activate();
}

FormWindowEditor::~FormWindowEditor()
{
    // Close: Delete the Designer form window via embedding widget
    delete m_toolBar;
    delete m_host;
    delete m_editorWidget;
133
134
    if (Designer::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << m_displayName;
con's avatar
con committed
135
136
137
138
139
140
141
142
    if (m_sessionNode && m_sessionWatcher) {
        m_sessionNode->unregisterWatcher(m_sessionWatcher);
        delete m_sessionWatcher;
    }
}

bool FormWindowEditor::createNew(const QString &contents)
{
143
144
    if (Designer::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << contents.size() << "chars";
con's avatar
con committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

    if (!m_formWindow)
        return false;

    m_formWindow->setContents(contents);
    if (!m_formWindow->mainContainer())
        return false;

    if (qdesigner_internal::FormWindowBase *fw = qobject_cast<qdesigner_internal::FormWindowBase *>(m_formWindow))
        fw->setDesignerGrid(qdesigner_internal::FormWindowBase::defaultDesignerGrid());
    return true;
}

bool FormWindowEditor::open(const QString &fileName /*= QString()*/)
{
160
161
    if (Designer::Constants::Internal::debug)
        qDebug() << Q_FUNC_INFO << fileName;
con's avatar
con committed
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

    if (fileName.isEmpty()) {
        setDisplayName(tr("untitled"));
    } else {
        const QFileInfo fi(fileName);
        const QString fileName = fi.absoluteFilePath();

        QFile file(fileName);
        if (!file.exists())
            return false;

        if (!fi.isReadable())
            return false;

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

        m_formWindow->setFileName(fileName);
        m_formWindow->setContents(&file);
        file.close();
        if (!m_formWindow->mainContainer())
            return false;
        m_formWindow->setDirty(false);

        ProjectExplorer::ProjectExplorerPlugin *pe = ProjectExplorer::ProjectExplorerPlugin::instance();
        ProjectExplorer::SessionManager *session = pe->session();
        m_sessionNode = session->sessionNode();
        m_sessionWatcher = new ProjectExplorer::NodesWatcher();
        connect(m_sessionWatcher, SIGNAL(filesAdded()), this, SLOT(updateResources()));
        connect(m_sessionWatcher, SIGNAL(filesRemoved()), this, SLOT(updateResources()));
        connect(m_sessionWatcher, SIGNAL(foldersAdded()), this, SLOT(updateResources()));
        connect(m_sessionWatcher, SIGNAL(foldersRemoved()), this, SLOT(updateResources()));
        m_sessionNode->registerWatcher(m_sessionWatcher);

        if (qdesigner_internal::FormWindowBase *fw = qobject_cast<qdesigner_internal::FormWindowBase *>(m_formWindow)) {
            QtResourceSet *rs = fw->resourceSet();
            m_originalUiQrcPaths = rs->activeQrcPaths();
        }

        emit opened(fileName);
        updateResources();

        QDesignerFormWindowManagerInterface *fwm = FormEditorW::instance()->designerEditor()->formWindowManager();
        fwm->setActiveFormWindow(m_formWindow);

        setDisplayName(fi.fileName());
    }
    emit changed();
    return true;
}

void FormWindowEditor::updateResources()
{
    if (qdesigner_internal::FormWindowBase *fw = qobject_cast<qdesigner_internal::FormWindowBase *>(m_formWindow)) {
        ProjectExplorer::ProjectExplorerPlugin *pe = ProjectExplorer::ProjectExplorerPlugin::instance();
        // filename could change in the meantime.
        ProjectExplorer::Project *project = pe->session()->projectForFile(m_file->fileName());

        qdesigner_internal::FormWindowBase::SaveResourcesBehaviour behaviour = qdesigner_internal::FormWindowBase::SaveAll;
        QtResourceSet *rs = fw->resourceSet();
        if (project) {
            ProjectNode *root = project->rootProjectNode();
            QrcFilesVisitor qrcVisitor;
            root->accept(&qrcVisitor);

            rs->activateQrcPaths(qrcVisitor.qrcFiles());
            behaviour = qdesigner_internal::FormWindowBase::SaveOnlyUsedQrcFiles;
        } else {
            rs->activateQrcPaths(m_originalUiQrcPaths);
        }
        fw->setSaveResourcesBehaviour(behaviour);
    }
}

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

void FormWindowEditor::slotSetDisplayName(const QString &title)
{
243
244
    if (Designer::Constants::Internal::debug)
        qDebug() <<  Q_FUNC_INFO << title;
con's avatar
con committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    setDisplayName(title);
}

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

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

Core::IFile *FormWindowEditor::file()
{
    return m_file;
}

const char *FormWindowEditor::kind() const
{
265
    return C_FORMEDITOR;
con's avatar
con committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
}

QString FormWindowEditor::displayName() const
{
    return m_displayName;
}

void FormWindowEditor::setDisplayName(const QString &title)
{
    m_displayName = title;
}

QToolBar *FormWindowEditor::toolBar()
{
    if (!m_toolBar)
        m_toolBar = FormEditorW::instance()->createEditorToolBar();
    return m_toolBar;
}

QByteArray FormWindowEditor::saveState() const
{
    return QByteArray();
}

bool FormWindowEditor::restoreState(const QByteArray &/*state*/)
{
    return true;
}

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

QWidget *FormWindowEditor::widget()
{
    return m_editorWidget;
}


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

QWidget *FormWindowEditor::integrationContainer()
{
    return m_host->integrationContainer();
}

void FormWindowEditor::updateFormWindowSelectionHandles(bool state)
{
    m_host->updateFormWindowSelectionHandles(state);
}

void FormWindowEditor::setSuggestedFileName(const QString &fileName)
{
    m_file->setSuggestedFileName(fileName);
}

void FormWindowEditor::activate()
{
    m_editorWidget->activate();
}
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351

QString FormWindowEditor::contextHelpId() const
{
    // TODO [13.2.09]: Replace this by QDesignerIntegrations context help Id
    // in the upcoming version of Qt
    QDesignerFormEditorInterface *core = FormEditorW::instance()->designerEditor();
    QObject *o = core->propertyEditor()->object();
    if (!o)
        return QString();
    const QDesignerWidgetDataBaseInterface *db = core->widgetDataBase();
    const int dbIndex = db->indexOfObject(o, true);
    if (dbIndex == -1)
        return QString();
    QString className = db->item(dbIndex)->name();
    if (className == QLatin1String("Line"))
        className = QLatin1String("QFrame");
    else if (className == QLatin1String("Spacer"))
        className = QLatin1String("QSpacerItem");
    else if (className == QLatin1String("QLayoutWidget"))
        className = QLatin1String("QLayout");
    return className;
}