1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "qt4projectmanager.h"
35 #include "qt4projectmanagerconstants.h"
36 #include "qt4projectmanagerplugin.h"
38 #include "qt4project.h"
39 #include "qt4target.h"
40 #include "profilereader.h"
41 #include "qmakestep.h"
42 #include "qt4buildconfiguration.h"
43 #include "wizards/qtquickapp.h"
44 #include "wizards/html5app.h"
46 #include <coreplugin/icore.h>
47 #include <coreplugin/basefilewizard.h>
48 #include <coreplugin/messagemanager.h>
49 #include <coreplugin/uniqueidmanager.h>
50 #include <coreplugin/editormanager/editormanager.h>
51 #include <coreplugin/editormanager/ieditor.h>
52 #include <coreplugin/variablemanager.h>
53 #include <projectexplorer/projectexplorer.h>
54 #include <projectexplorer/buildmanager.h>
55 #include <projectexplorer/session.h>
56 #include <projectexplorer/project.h>
57 #include <projectexplorer/projectexplorerconstants.h>
58 #include <utils/qtcassert.h>
60 #include <QtCore/QCoreApplication>
61 #include <QtCore/QDir>
62 #include <QtCore/QFileInfo>
63 #include <QtCore/QVariant>
64 #include <QtGui/QFileDialog>
65 #include <QtGui/QMessageBox>
67 using namespace Qt4ProjectManager;
68 using namespace Qt4ProjectManager::Internal;
70 using ProjectExplorer::BuildStep;
71 using ProjectExplorer::FileType;
72 using ProjectExplorer::HeaderType;
73 using ProjectExplorer::SourceType;
74 using ProjectExplorer::FormType;
75 using ProjectExplorer::ResourceType;
76 using ProjectExplorer::UnknownFileType;
78 static const char * const kInstallBins = "CurrentProject:QT_INSTALL_BINS";
80 // Known file types of a Qt 4 project
81 static const char* qt4FileTypes[] = {
88 // Test for form editor (loosely coupled)
89 static inline bool isFormWindowEditor(const QObject *o)
91 return o && !qstrcmp(o->metaObject()->className(), "Designer::FormWindowEditor");
94 // Return contents of form editor (loosely coupled)
95 static inline QString formWindowEditorContents(const QObject *editor)
97 const QVariant contentV = editor->property("contents");
98 QTC_ASSERT(contentV.isValid(), return QString(); )
99 return contentV.toString();
102 Qt4Manager::Qt4Manager(Qt4ProjectManagerPlugin *plugin)
110 Qt4Manager::~Qt4Manager()
114 void Qt4Manager::registerProject(Qt4Project *project)
116 m_projects.append(project);
119 void Qt4Manager::unregisterProject(Qt4Project *project)
121 m_projects.removeOne(project);
124 void Qt4Manager::notifyChanged(const QString &name)
126 foreach (Qt4Project *pro, m_projects)
127 pro->notifyChanged(name);
130 void Qt4Manager::init()
132 connect(Core::EditorManager::instance(), SIGNAL(editorAboutToClose(Core::IEditor*)),
133 this, SLOT(editorAboutToClose(Core::IEditor*)));
135 connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
136 this, SLOT(editorChanged(Core::IEditor*)));
138 Core::VariableManager *vm = Core::VariableManager::instance();
139 vm->registerVariable(QLatin1String(kInstallBins),
140 tr("Full path to the bin/ install directory of the current project's Qt version."));
141 connect(vm, SIGNAL(variableUpdateRequested(QString)),
142 this, SLOT(updateVariable(QString)));
145 void Qt4Manager::editorChanged(Core::IEditor *editor)
148 if (isFormWindowEditor(m_lastEditor)) {
149 disconnect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
152 const QString contents = formWindowEditorContents(m_lastEditor);
153 foreach(Qt4Project *project, m_projects)
154 project->rootProjectNode()->updateCodeModelSupportFromEditor(m_lastEditor->file()->fileName(), contents);
159 m_lastEditor = editor;
162 if (isFormWindowEditor(m_lastEditor))
163 connect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
166 void Qt4Manager::editorAboutToClose(Core::IEditor *editor)
168 if (m_lastEditor == editor) {
169 // Oh no our editor is going to be closed
170 // get the content first
171 if (isFormWindowEditor(m_lastEditor)) {
172 disconnect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
174 const QString contents = formWindowEditorContents(m_lastEditor);
175 foreach(Qt4Project *project, m_projects)
176 project->rootProjectNode()->updateCodeModelSupportFromEditor(m_lastEditor->file()->fileName(), contents);
184 void Qt4Manager::updateVariable(const QString &variable)
186 if (variable == QLatin1String(kInstallBins)) {
187 Qt4Project *qt4pro = qobject_cast<Qt4Project *>(projectExplorer()->currentProject());
189 Core::VariableManager::instance()->remove(QLatin1String(kInstallBins));
192 QString value = qt4pro->activeTarget()->activeBuildConfiguration()
193 ->qtVersion()->versionInfo().value(QLatin1String("QT_INSTALL_BINS"));
194 Core::VariableManager::instance()->insert(QLatin1String(kInstallBins), value);
198 void Qt4Manager::uiEditorContentsChanged()
200 // cast sender, get filename
201 if (!m_dirty && isFormWindowEditor(sender()))
205 Core::Context Qt4Manager::projectContext() const
207 return m_plugin->projectContext();
210 Core::Context Qt4Manager::projectLanguage() const
212 return Core::Context(ProjectExplorer::Constants::LANG_CXX);
215 QString Qt4Manager::mimeType() const
217 return QLatin1String(Qt4ProjectManager::Constants::PROFILE_MIMETYPE);
220 static void updateBoilerPlateCodeFiles(const AbstractMobileApp *app, const QString &proFile)
222 const QList<AbstractGeneratedFileInfo> updates =
223 app->fileUpdates(proFile);
224 if (!updates.empty()) {
225 const QString title = Qt4Manager::tr("Update of Generated Files");
226 QStringList fileNames;
227 foreach (const AbstractGeneratedFileInfo &info, updates)
228 fileNames.append(QDir::toNativeSeparators(info.fileInfo.fileName()));
229 const QString message =
230 Qt4Manager::tr("The following files are either outdated or have been modified:<br><br>%1"
231 "<br><br>Do you want Qt Creator to update the files? Any changes will be lost.")
232 .arg(fileNames.join(QLatin1String(", ")));
233 if (QMessageBox::question(0, title, message, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
235 if (!app->updateFiles(updates, error))
236 QMessageBox::critical(0, title, error);
241 ProjectExplorer::Project *Qt4Manager::openProject(const QString &fileName)
243 Core::MessageManager *messageManager = Core::ICore::instance()->messageManager();
245 // TODO Make all file paths relative & remove this hack
246 // We convert the path to an absolute one here because qt4project.cpp
247 // && profileevaluator use absolute/canonical file paths all over the place
248 // Correct fix would be to remove these calls ...
249 QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
251 if (canonicalFilePath.isEmpty()) {
252 messageManager->printToOutputPane(tr("Failed opening project '%1': Project file does not exist").arg(QDir::toNativeSeparators(canonicalFilePath)));
256 foreach (ProjectExplorer::Project *pi, projectExplorer()->session()->projects()) {
257 if (canonicalFilePath == pi->file()->fileName()) {
258 messageManager->printToOutputPane(tr("Failed opening project '%1': Project already open").arg(QDir::toNativeSeparators(canonicalFilePath)));
263 const QtQuickApp qtQuickApp;
264 updateBoilerPlateCodeFiles(&qtQuickApp, canonicalFilePath);
265 const Html5App html5App;
266 updateBoilerPlateCodeFiles(&html5App, canonicalFilePath);
268 Qt4Project *pro = new Qt4Project(this, canonicalFilePath);
272 ProjectExplorer::ProjectExplorerPlugin *Qt4Manager::projectExplorer() const
274 return ProjectExplorer::ProjectExplorerPlugin::instance();
277 ProjectExplorer::Node *Qt4Manager::contextNode() const
279 return m_contextNode;
282 void Qt4Manager::setContextNode(ProjectExplorer::Node *node)
284 m_contextNode = node;
287 void Qt4Manager::setContextProject(ProjectExplorer::Project *project)
289 m_contextProject = project;
292 ProjectExplorer::Project *Qt4Manager::contextProject() const
294 return m_contextProject;
297 void Qt4Manager::runQMake()
299 runQMake(projectExplorer()->startupProject(), 0);
302 void Qt4Manager::runQMakeContextMenu()
304 runQMake(m_contextProject, m_contextNode);
307 void Qt4Manager::runQMake(ProjectExplorer::Project *p, ProjectExplorer::Node *node)
309 if (!ProjectExplorer::ProjectExplorerPlugin::instance()->saveModifiedFiles())
311 Qt4Project *qt4pro = qobject_cast<Qt4Project *>(p);
312 QTC_ASSERT(qt4pro, return);
314 if (!qt4pro->activeTarget() ||
315 !qt4pro->activeTarget()->activeBuildConfiguration())
318 Qt4BuildConfiguration *bc = qt4pro->activeTarget()->activeBuildConfiguration();
319 QMakeStep *qs = bc->qmakeStep();
323 //found qmakeStep, now use it
326 if (node != 0 && node != qt4pro->rootProjectNode())
327 if (Qt4ProFileNode *profile = qobject_cast<Qt4ProFileNode *>(node))
328 bc->setSubNodeBuild(profile);
330 projectExplorer()->buildManager()->appendStep(qs);
331 bc->setSubNodeBuild(0);
334 void Qt4Manager::buildSubDirContextMenu()
336 handleSubDirContexMenu(BUILD);
339 void Qt4Manager::cleanSubDirContextMenu()
341 handleSubDirContexMenu(CLEAN);
344 void Qt4Manager::rebuildSubDirContextMenu()
346 handleSubDirContexMenu(REBUILD);
349 void Qt4Manager::handleSubDirContexMenu(Qt4Manager::Action action)
351 Qt4Project *qt4pro = qobject_cast<Qt4Project *>(m_contextProject);
352 QTC_ASSERT(qt4pro, return);
354 if (!qt4pro->activeTarget() ||
355 !qt4pro->activeTarget()->activeBuildConfiguration())
358 Qt4BuildConfiguration *bc = qt4pro->activeTarget()->activeBuildConfiguration();
359 if (m_contextNode != 0 && m_contextNode != qt4pro->rootProjectNode())
360 if (Qt4ProFileNode *profile = qobject_cast<Qt4ProFileNode *>(m_contextNode))
361 bc->setSubNodeBuild(profile);
363 if (projectExplorer()->saveModifiedFiles()) {
364 if (action == BUILD) {
365 projectExplorer()->buildManager()->buildList(bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_BUILD));
366 } else if (action == CLEAN) {
367 projectExplorer()->buildManager()->buildList(bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_CLEAN));
368 } else if (action == REBUILD) {
369 QList<ProjectExplorer::BuildStepList *> stepLists;
370 stepLists << bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_CLEAN);
371 stepLists << bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_BUILD);
372 projectExplorer()->buildManager()->buildLists(stepLists);
376 bc->setSubNodeBuild(0);
379 QString Qt4Manager::fileTypeId(ProjectExplorer::FileType type)
383 return QLatin1String(qt4FileTypes[0]);
385 return QLatin1String(qt4FileTypes[1]);
387 return QLatin1String(qt4FileTypes[2]);
389 return QLatin1String(qt4FileTypes[3]);
390 case UnknownFileType: