qt4projectmanager.cpp 12.1 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
con's avatar
con committed
25
26
27
28
29
30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31
**
32
**************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34
#include "qt4projectmanager.h"
hjk's avatar
hjk committed
35

con's avatar
con committed
36
37
38
39
#include "qt4projectmanagerconstants.h"
#include "qt4projectmanagerplugin.h"
#include "qt4nodes.h"
#include "qt4project.h"
Tobias Hunger's avatar
Tobias Hunger committed
40
#include "qt4target.h"
con's avatar
con committed
41
42
#include "profilereader.h"
#include "qmakestep.h"
dt's avatar
dt committed
43
#include "qt4buildconfiguration.h"
44
#include "wizards/qmlstandaloneapp.h"
con's avatar
con committed
45

46
#include <coreplugin/icore.h>
con's avatar
con committed
47
48
49
50
#include <coreplugin/basefilewizard.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/editormanager/editormanager.h>
51
#include <coreplugin/editormanager/ieditor.h>
52
#include <projectexplorer/projectexplorer.h>
53
#include <projectexplorer/buildmanager.h>
54
#include <projectexplorer/session.h>
con's avatar
con committed
55
#include <projectexplorer/project.h>
56
#include <projectexplorer/projectexplorerconstants.h>
Tobias Hunger's avatar
Tobias Hunger committed
57
#include <utils/qtcassert.h>
con's avatar
con committed
58
59

#include <QtCore/QCoreApplication>
60
61
62
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QVariant>
con's avatar
con committed
63
#include <QtGui/QFileDialog>
64
#include <QtGui/QMessageBox>
con's avatar
con committed
65
66
67
68
69
70
71
72
73
74
75
76
77

using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;

using ProjectExplorer::BuildStep;
using ProjectExplorer::FileType;
using ProjectExplorer::HeaderType;
using ProjectExplorer::SourceType;
using ProjectExplorer::FormType;
using ProjectExplorer::ResourceType;
using ProjectExplorer::UnknownFileType;

// Known file types of a Qt 4 project
78
79
80
81
82
83
static const char* qt4FileTypes[] = {
    "CppHeaderFiles",
    "CppSourceFiles",
    "Qt4FormFiles",
    "Qt4ResourceFiles"
};
con's avatar
con committed
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Test for form editor (loosely coupled)
static inline bool isFormWindowEditor(const QObject *o)
{
    return o && !qstrcmp(o->metaObject()->className(), "Designer::FormWindowEditor");
}

// Return contents of form editor (loosely coupled)
static inline QString formWindowEditorContents(const QObject *editor)
{
    const QVariant contentV = editor->property("contents");
    QTC_ASSERT(contentV.isValid(), return QString(); )
    return contentV.toString();
}

99
Qt4Manager::Qt4Manager(Qt4ProjectManagerPlugin *plugin)
Tobias Hunger's avatar
Tobias Hunger committed
100
  : m_plugin(plugin),
con's avatar
con committed
101
    m_contextProject(0),
dt's avatar
dt committed
102
103
    m_lastEditor(0),
    m_dirty(false)
con's avatar
con committed
104
105
106
107
108
109
110
{
}

Qt4Manager::~Qt4Manager()
{
}

dt's avatar
dt committed
111
112
113
114
115
116
117
118
119
120
121
122
void Qt4Manager::registerProject(Qt4Project *project)
{
    m_projects.append(project);
}

void Qt4Manager::unregisterProject(Qt4Project *project)
{
    m_projects.removeOne(project);
}

void Qt4Manager::notifyChanged(const QString &name)
{
hjk's avatar
hjk committed
123
    foreach (Qt4Project *pro, m_projects)
dt's avatar
dt committed
124
125
126
        pro->notifyChanged(name);
}

con's avatar
con committed
127
128
void Qt4Manager::init()
{
dt's avatar
dt committed
129
130
131
132
133
134
135
136
137
138
    connect(Core::EditorManager::instance(), SIGNAL(editorAboutToClose(Core::IEditor*)),
            this, SLOT(editorAboutToClose(Core::IEditor*)));

    connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(editorChanged(Core::IEditor*)));
}

void Qt4Manager::editorChanged(Core::IEditor *editor)
{
    // Handle old editor
139
140
    if (isFormWindowEditor(m_lastEditor)) {
        disconnect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
dt's avatar
dt committed
141
142

        if (m_dirty) {
143
            const QString contents = formWindowEditorContents(m_lastEditor);
dt's avatar
dt committed
144
            foreach(Qt4Project *project, m_projects)
145
                project->rootProjectNode()->updateCodeModelSupportFromEditor(m_lastEditor->file()->fileName(), contents);
dt's avatar
dt committed
146
147
148
149
150
151
152
            m_dirty = false;
        }
    }

    m_lastEditor = editor;

    // Handle new editor
153
154
    if (isFormWindowEditor(m_lastEditor))
        connect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
dt's avatar
dt committed
155
156
157
158
159
160
161
}

void Qt4Manager::editorAboutToClose(Core::IEditor *editor)
{
    if (m_lastEditor == editor) {
        // Oh no our editor is going to be closed
        // get the content first
162
163
        if (isFormWindowEditor(m_lastEditor)) {
            disconnect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
dt's avatar
dt committed
164
            if (m_dirty) {
165
                const QString contents = formWindowEditorContents(m_lastEditor);
dt's avatar
dt committed
166
                foreach(Qt4Project *project, m_projects)
167
                    project->rootProjectNode()->updateCodeModelSupportFromEditor(m_lastEditor->file()->fileName(), contents);
dt's avatar
dt committed
168
169
170
171
172
173
174
175
176
177
                m_dirty = false;
            }
        }
        m_lastEditor = 0;
    }
}

void Qt4Manager::uiEditorContentsChanged()
{
    // cast sender, get filename
178
179
    if (!m_dirty && isFormWindowEditor(sender()))
        m_dirty = true;
con's avatar
con committed
180
181
}

dt's avatar
dt committed
182
Core::Context Qt4Manager::projectContext() const
con's avatar
con committed
183
184
185
186
{
     return m_plugin->projectContext();
}

187
Core::Context Qt4Manager::projectLanguage() const
con's avatar
con committed
188
{
189
    return Core::Context(ProjectExplorer::Constants::LANG_CXX);
con's avatar
con committed
190
191
192
193
}

QString Qt4Manager::mimeType() const
{
Tobias Hunger's avatar
Tobias Hunger committed
194
    return QLatin1String(Qt4ProjectManager::Constants::PROFILE_MIMETYPE);
con's avatar
con committed
195
196
}

197
inline void updateBoilerPlateCodeFiles(const AbstractMobileApp &app, const QString proFile)
198
{
199
200
    const QList<AbstractGeneratedFileInfo> updates =
            app.fileUpdates(proFile);
201
202
203
204
    if (!updates.empty()) {
        // TODO Translate the folloing strings when we want to keep the code
        QString message = QLatin1String("The following files are either outdated or have been modified:");
        message.append(QLatin1String("<ul>"));
205
        foreach (const AbstractGeneratedFileInfo &info, updates) {
206
            QStringList reasons;
207
            if (info.statedChecksum != info.dataChecksum)
208
                reasons.append(QLatin1String("modified"));
209
            if (info.version != info.currentVersion)
210
211
212
213
214
215
216
217
218
219
                reasons.append(QLatin1String("outdated"));
            message.append(QString::fromLatin1("<li><nobr>%1 (%2)</nobr></li>")
                           .arg(QDir::toNativeSeparators(info.fileInfo.canonicalFilePath()))
                           .arg(reasons.join(QLatin1String(", "))));
        }
        message.append(QLatin1String("</ul>"));
        message.append(QLatin1String("Do you want Qt Creator to update the files? Any changes will be lost."));
        const QString title = QLatin1String("Update of the QmlApplicationView files");
        if (QMessageBox::question(0, title, message, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
            QString error;
220
            if (!app.updateFiles(updates, error))
221
222
223
224
225
                QMessageBox::critical(0, title, error);
        }
    }
}

226
ProjectExplorer::Project *Qt4Manager::openProject(const QString &fileName)
con's avatar
con committed
227
{
228
    Core::MessageManager *messageManager = Core::ICore::instance()->messageManager();
con's avatar
con committed
229
230
231
232
233
234
235
236

    // TODO Make all file paths relative & remove this hack
    // We convert the path to an absolute one here because qt4project.cpp
    // && profileevaluator use absolute/canonical file paths all over the place
    // Correct fix would be to remove these calls ...
    QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();

    if (canonicalFilePath.isEmpty()) {
237
        messageManager->printToOutputPane(tr("Failed opening project '%1': Project file does not exist").arg(QDir::toNativeSeparators(canonicalFilePath)));
con's avatar
con committed
238
239
240
241
242
        return 0;
    }

    foreach (ProjectExplorer::Project *pi, projectExplorer()->session()->projects()) {
        if (canonicalFilePath == pi->file()->fileName()) {
243
            messageManager->printToOutputPane(tr("Failed opening project '%1': Project already open").arg(QDir::toNativeSeparators(canonicalFilePath)));
con's avatar
con committed
244
245
246
247
            return 0;
        }
    }

248
249
    QmlStandaloneApp app;
    updateBoilerPlateCodeFiles(app, canonicalFilePath);
250

con's avatar
con committed
251
252
253
254
255
256
    Qt4Project *pro = new Qt4Project(this, canonicalFilePath);
    return pro;
}

ProjectExplorer::ProjectExplorerPlugin *Qt4Manager::projectExplorer() const
{
Tobias Hunger's avatar
Tobias Hunger committed
257
    return ProjectExplorer::ProjectExplorerPlugin::instance();
con's avatar
con committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
}

ProjectExplorer::Node *Qt4Manager::contextNode() const
{
    return m_contextNode;
}

void Qt4Manager::setContextNode(ProjectExplorer::Node *node)
{
    m_contextNode = node;
}

void Qt4Manager::setContextProject(ProjectExplorer::Project *project)
{
    m_contextProject = project;
}

ProjectExplorer::Project *Qt4Manager::contextProject() const
{
    return m_contextProject;
}

void Qt4Manager::runQMake()
{
282
    runQMake(projectExplorer()->currentProject(), 0);
con's avatar
con committed
283
284
285
286
}

void Qt4Manager::runQMakeContextMenu()
{
287
    runQMake(m_contextProject, m_contextNode);
con's avatar
con committed
288
289
}

290
void Qt4Manager::runQMake(ProjectExplorer::Project *p, ProjectExplorer::Node *node)
con's avatar
con committed
291
{
292
293
    if (!ProjectExplorer::ProjectExplorerPlugin::instance()->saveModifiedFiles())
        return;
dt's avatar
dt committed
294
295
    Qt4Project *qt4pro = qobject_cast<Qt4Project *>(p);
    QTC_ASSERT(qt4pro, return);
296

Tobias Hunger's avatar
Tobias Hunger committed
297
298
299
300
301
    if (!qt4pro->activeTarget() ||
        !qt4pro->activeTarget()->activeBuildConfiguration())
    return;

    Qt4BuildConfiguration *bc = qt4pro->activeTarget()->activeBuildConfiguration();
dt's avatar
dt committed
302
    QMakeStep *qs = bc->qmakeStep();
303

304
305
    if (!qs)
        return;
con's avatar
con committed
306
    //found qmakeStep, now use it
307
    qs->setForced(true);
308
309
310
311
312

    if (node != 0 && node != qt4pro->rootProjectNode())
        if (Qt4ProFileNode *profile = qobject_cast<Qt4ProFileNode *>(node))
            bc->setSubNodeBuild(profile);

Tobias Hunger's avatar
Tobias Hunger committed
313
    projectExplorer()->buildManager()->appendStep(qs);
314
315
316
317
    bc->setSubNodeBuild(0);
}

void Qt4Manager::buildSubDirContextMenu()
dt's avatar
dt committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
{
    handleSubDirContexMenu(BUILD);
}

void Qt4Manager::cleanSubDirContextMenu()
{
    handleSubDirContexMenu(CLEAN);
}

void Qt4Manager::rebuildSubDirContextMenu()
{
    handleSubDirContexMenu(REBUILD);
}

void Qt4Manager::handleSubDirContexMenu(Qt4Manager::Action action)
333
334
335
336
{
    Qt4Project *qt4pro = qobject_cast<Qt4Project *>(m_contextProject);
    QTC_ASSERT(qt4pro, return);

Tobias Hunger's avatar
Tobias Hunger committed
337
338
339
340
341
    if (!qt4pro->activeTarget() ||
        !qt4pro->activeTarget()->activeBuildConfiguration())
    return;

    Qt4BuildConfiguration *bc = qt4pro->activeTarget()->activeBuildConfiguration();
342
343
344
345
    if (m_contextNode != 0 && m_contextNode != qt4pro->rootProjectNode())
        if (Qt4ProFileNode *profile = qobject_cast<Qt4ProFileNode *>(m_contextNode))
            bc->setSubNodeBuild(profile);

dt's avatar
dt committed
346
    if (projectExplorer()->saveModifiedFiles()) {
Tobias Hunger's avatar
Tobias Hunger committed
347
348
349
350
351
352
353
354
355
        if (action == BUILD) {
            projectExplorer()->buildManager()->buildList(bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_BUILD));
        } else if (action == CLEAN) {
            projectExplorer()->buildManager()->buildList(bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_CLEAN));
        } else if (action == REBUILD) {
            QList<ProjectExplorer::BuildStepList *> stepLists;
            stepLists << bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_CLEAN);
            stepLists << bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_BUILD);
            projectExplorer()->buildManager()->buildLists(stepLists);
dt's avatar
dt committed
356
357
        }
    }
358
359

    bc->setSubNodeBuild(0);
con's avatar
con committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
}

QString Qt4Manager::fileTypeId(ProjectExplorer::FileType type)
{
    switch (type) {
    case HeaderType:
        return QLatin1String(qt4FileTypes[0]);
    case SourceType:
        return QLatin1String(qt4FileTypes[1]);
    case FormType:
        return QLatin1String(qt4FileTypes[2]);
    case ResourceType:
        return QLatin1String(qt4FileTypes[3]);
    case UnknownFileType:
    default:
        break;
    }
    return QString();
}