OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qt4projectmanager.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "qt4projectmanager.h"
34
35 #include "qt4projectmanagerconstants.h"
36 #include "qt4projectmanagerplugin.h"
37 #include "qt4nodes.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"
45
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>
59
60 #include <QtCore/QCoreApplication>
61 #include <QtCore/QDir>
62 #include <QtCore/QFileInfo>
63 #include <QtCore/QVariant>
64 #include <QtGui/QFileDialog>
65 #include <QtGui/QMessageBox>
66
67 using namespace Qt4ProjectManager;
68 using namespace Qt4ProjectManager::Internal;
69
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;
77
78 static const char * const kInstallBins = "CurrentProject:QT_INSTALL_BINS";
79
80 // Known file types of a Qt 4 project
81 static const char* qt4FileTypes[] = {
82     "CppHeaderFiles",
83     "CppSourceFiles",
84     "Qt4FormFiles",
85     "Qt4ResourceFiles"
86 };
87
88 // Test for form editor (loosely coupled)
89 static inline bool isFormWindowEditor(const QObject *o)
90 {
91     return o && !qstrcmp(o->metaObject()->className(), "Designer::FormWindowEditor");
92 }
93
94 // Return contents of form editor (loosely coupled)
95 static inline QString formWindowEditorContents(const QObject *editor)
96 {
97     const QVariant contentV = editor->property("contents");
98     QTC_ASSERT(contentV.isValid(), return QString(); )
99     return contentV.toString();
100 }
101
102 Qt4Manager::Qt4Manager(Qt4ProjectManagerPlugin *plugin)
103   : m_plugin(plugin),
104     m_contextProject(0),
105     m_lastEditor(0),
106     m_dirty(false)
107 {
108 }
109
110 Qt4Manager::~Qt4Manager()
111 {
112 }
113
114 void Qt4Manager::registerProject(Qt4Project *project)
115 {
116     m_projects.append(project);
117 }
118
119 void Qt4Manager::unregisterProject(Qt4Project *project)
120 {
121     m_projects.removeOne(project);
122 }
123
124 void Qt4Manager::notifyChanged(const QString &name)
125 {
126     foreach (Qt4Project *pro, m_projects)
127         pro->notifyChanged(name);
128 }
129
130 void Qt4Manager::init()
131 {
132     connect(Core::EditorManager::instance(), SIGNAL(editorAboutToClose(Core::IEditor*)),
133             this, SLOT(editorAboutToClose(Core::IEditor*)));
134
135     connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
136             this, SLOT(editorChanged(Core::IEditor*)));
137
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)));
143 }
144
145 void Qt4Manager::editorChanged(Core::IEditor *editor)
146 {
147     // Handle old editor
148     if (isFormWindowEditor(m_lastEditor)) {
149         disconnect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
150
151         if (m_dirty) {
152             const QString contents = formWindowEditorContents(m_lastEditor);
153             foreach(Qt4Project *project, m_projects)
154                 project->rootProjectNode()->updateCodeModelSupportFromEditor(m_lastEditor->file()->fileName(), contents);
155             m_dirty = false;
156         }
157     }
158
159     m_lastEditor = editor;
160
161     // Handle new editor
162     if (isFormWindowEditor(m_lastEditor))
163         connect(m_lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
164 }
165
166 void Qt4Manager::editorAboutToClose(Core::IEditor *editor)
167 {
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()));
173             if (m_dirty) {
174                 const QString contents = formWindowEditorContents(m_lastEditor);
175                 foreach(Qt4Project *project, m_projects)
176                     project->rootProjectNode()->updateCodeModelSupportFromEditor(m_lastEditor->file()->fileName(), contents);
177                 m_dirty = false;
178             }
179         }
180         m_lastEditor = 0;
181     }
182 }
183
184 void Qt4Manager::updateVariable(const QString &variable)
185 {
186     if (variable == QLatin1String(kInstallBins)) {
187         Qt4Project *qt4pro = qobject_cast<Qt4Project *>(projectExplorer()->currentProject());
188         if (!qt4pro) {
189             Core::VariableManager::instance()->remove(QLatin1String(kInstallBins));
190             return;
191         }
192         QString value = qt4pro->activeTarget()->activeBuildConfiguration()
193                 ->qtVersion()->versionInfo().value(QLatin1String("QT_INSTALL_BINS"));
194         Core::VariableManager::instance()->insert(QLatin1String(kInstallBins), value);
195     }
196 }
197
198 void Qt4Manager::uiEditorContentsChanged()
199 {
200     // cast sender, get filename
201     if (!m_dirty && isFormWindowEditor(sender()))
202         m_dirty = true;
203 }
204
205 Core::Context Qt4Manager::projectContext() const
206 {
207      return m_plugin->projectContext();
208 }
209
210 Core::Context Qt4Manager::projectLanguage() const
211 {
212     return Core::Context(ProjectExplorer::Constants::LANG_CXX);
213 }
214
215 QString Qt4Manager::mimeType() const
216 {
217     return QLatin1String(Qt4ProjectManager::Constants::PROFILE_MIMETYPE);
218 }
219
220 static void updateBoilerPlateCodeFiles(const AbstractMobileApp *app, const QString &proFile)
221 {
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) {
234             QString error;
235             if (!app->updateFiles(updates, error))
236                 QMessageBox::critical(0, title, error);
237         }
238     }
239 }
240
241 ProjectExplorer::Project *Qt4Manager::openProject(const QString &fileName)
242 {
243     Core::MessageManager *messageManager = Core::ICore::instance()->messageManager();
244
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();
250
251     if (canonicalFilePath.isEmpty()) {
252         messageManager->printToOutputPane(tr("Failed opening project '%1': Project file does not exist").arg(QDir::toNativeSeparators(canonicalFilePath)));
253         return 0;
254     }
255
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)));
259             return 0;
260         }
261     }
262
263     const QtQuickApp qtQuickApp;
264     updateBoilerPlateCodeFiles(&qtQuickApp, canonicalFilePath);
265     const Html5App html5App;
266     updateBoilerPlateCodeFiles(&html5App, canonicalFilePath);
267
268     Qt4Project *pro = new Qt4Project(this, canonicalFilePath);
269     return pro;
270 }
271
272 ProjectExplorer::ProjectExplorerPlugin *Qt4Manager::projectExplorer() const
273 {
274     return ProjectExplorer::ProjectExplorerPlugin::instance();
275 }
276
277 ProjectExplorer::Node *Qt4Manager::contextNode() const
278 {
279     return m_contextNode;
280 }
281
282 void Qt4Manager::setContextNode(ProjectExplorer::Node *node)
283 {
284     m_contextNode = node;
285 }
286
287 void Qt4Manager::setContextProject(ProjectExplorer::Project *project)
288 {
289     m_contextProject = project;
290 }
291
292 ProjectExplorer::Project *Qt4Manager::contextProject() const
293 {
294     return m_contextProject;
295 }
296
297 void Qt4Manager::runQMake()
298 {
299     runQMake(projectExplorer()->startupProject(), 0);
300 }
301
302 void Qt4Manager::runQMakeContextMenu()
303 {
304     runQMake(m_contextProject, m_contextNode);
305 }
306
307 void Qt4Manager::runQMake(ProjectExplorer::Project *p, ProjectExplorer::Node *node)
308 {
309     if (!ProjectExplorer::ProjectExplorerPlugin::instance()->saveModifiedFiles())
310         return;
311     Qt4Project *qt4pro = qobject_cast<Qt4Project *>(p);
312     QTC_ASSERT(qt4pro, return);
313
314     if (!qt4pro->activeTarget() ||
315         !qt4pro->activeTarget()->activeBuildConfiguration())
316     return;
317
318     Qt4BuildConfiguration *bc = qt4pro->activeTarget()->activeBuildConfiguration();
319     QMakeStep *qs = bc->qmakeStep();
320
321     if (!qs)
322         return;
323     //found qmakeStep, now use it
324     qs->setForced(true);
325
326     if (node != 0 && node != qt4pro->rootProjectNode())
327         if (Qt4ProFileNode *profile = qobject_cast<Qt4ProFileNode *>(node))
328             bc->setSubNodeBuild(profile);
329
330     projectExplorer()->buildManager()->appendStep(qs);
331     bc->setSubNodeBuild(0);
332 }
333
334 void Qt4Manager::buildSubDirContextMenu()
335 {
336     handleSubDirContexMenu(BUILD);
337 }
338
339 void Qt4Manager::cleanSubDirContextMenu()
340 {
341     handleSubDirContexMenu(CLEAN);
342 }
343
344 void Qt4Manager::rebuildSubDirContextMenu()
345 {
346     handleSubDirContexMenu(REBUILD);
347 }
348
349 void Qt4Manager::handleSubDirContexMenu(Qt4Manager::Action action)
350 {
351     Qt4Project *qt4pro = qobject_cast<Qt4Project *>(m_contextProject);
352     QTC_ASSERT(qt4pro, return);
353
354     if (!qt4pro->activeTarget() ||
355         !qt4pro->activeTarget()->activeBuildConfiguration())
356     return;
357
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);
362
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);
373         }
374     }
375
376     bc->setSubNodeBuild(0);
377 }
378
379 QString Qt4Manager::fileTypeId(ProjectExplorer::FileType type)
380 {
381     switch (type) {
382     case HeaderType:
383         return QLatin1String(qt4FileTypes[0]);
384     case SourceType:
385         return QLatin1String(qt4FileTypes[1]);
386     case FormType:
387         return QLatin1String(qt4FileTypes[2]);
388     case ResourceType:
389         return QLatin1String(qt4FileTypes[3]);
390     case UnknownFileType:
391     default:
392         break;
393     }
394     return QString();
395 }
396