OSDN Git Service

a4913f82d4ac346c8b9729ea01921d60cf5bd3b6
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qt4projectmanagerplugin.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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qt4projectmanagerplugin.h"
35
36 #include "qt4projectmanager.h"
37 #include "qmakestep.h"
38 #include "makestep.h"
39 #include "wizards/consoleappwizard.h"
40 #include "wizards/guiappwizard.h"
41 #include "wizards/mobileappwizard.h"
42 #include "wizards/librarywizard.h"
43 #include "wizards/testwizard.h"
44 #include "wizards/emptyprojectwizard.h"
45 #include "wizards/subdirsprojectwizard.h"
46 #include "wizards/qtquickappwizard.h"
47 #include "wizards/html5appwizard.h"
48 #include "customwidgetwizard/customwidgetwizard.h"
49 #include "profileeditorfactory.h"
50 #include "qt4projectmanagerconstants.h"
51 #include "qt4project.h"
52 #include "profileeditor.h"
53 #include "profilereader.h"
54 #include "qtversionmanager.h"
55 #include "qtoptionspage.h"
56 #include "externaleditors.h"
57 #include "gettingstartedwelcomepage.h"
58 #include "profilecompletion.h"
59
60 #include "qt-maemo/maemomanager.h"
61 #include "qt-s60/s60manager.h"
62 #include "qt-desktop/qt4desktoptargetfactory.h"
63 #include "qt-desktop/qt4simulatortargetfactory.h"
64 #include "qt-desktop/qt4runconfiguration.h"
65
66 #include <coreplugin/uniqueidmanager.h>
67 #include <coreplugin/icore.h>
68 #include <extensionsystem/pluginmanager.h>
69 #include <projectexplorer/buildmanager.h>
70 #include <projectexplorer/project.h>
71 #include <projectexplorer/session.h>
72 #include <projectexplorer/projectexplorer.h>
73 #include <projectexplorer/projectexplorerconstants.h>
74 #include <projectexplorer/projectnodes.h>
75 #include <coreplugin/mimedatabase.h>
76 #include <coreplugin/actionmanager/actionmanager.h>
77 #include <coreplugin/actionmanager/actioncontainer.h>
78 #include <coreplugin/actionmanager/command.h>
79 #include <coreplugin/editormanager/editormanager.h>
80 #include <texteditor/texteditoractionhandler.h>
81 #include <texteditor/texteditorconstants.h>
82 #include <texteditor/texteditorsettings.h>
83
84 #ifdef WITH_TESTS
85 #    include <QTest>
86 #endif
87
88 #include <QtCore/QtPlugin>
89 #include <QtGui/QMenu>
90
91 using namespace Qt4ProjectManager::Internal;
92 using namespace Qt4ProjectManager;
93 using ProjectExplorer::Project;
94
95 Qt4ProjectManagerPlugin::~Qt4ProjectManagerPlugin()
96 {
97     //removeObject(m_embeddedPropertiesPage);
98     //delete m_embeddedPropertiesPage;
99
100     removeObject(m_proFileEditorFactory);
101     delete m_proFileEditorFactory;
102     removeObject(m_qt4ProjectManager);
103     delete m_qt4ProjectManager;
104     removeObject(m_welcomePage);
105     delete m_welcomePage;
106 }
107
108 bool Qt4ProjectManagerPlugin::initialize(const QStringList &arguments, QString *errorMessage)
109 {
110     Q_UNUSED(arguments)
111     m_projectContext = Core::Context(Qt4ProjectManager::Constants::PROJECT_ID);
112
113     ProFileParser::initialize();
114     ProFileEvaluator::initialize();
115
116     Core::ICore *core = Core::ICore::instance();
117     if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":qt4projectmanager/Qt4ProjectManager.mimetypes.xml"), errorMessage))
118         return false;
119
120     m_projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance();
121     Core::ActionManager *am = core->actionManager();
122
123     QtVersionManager *mgr = new QtVersionManager;
124     addAutoReleasedObject(mgr);
125     addAutoReleasedObject(new QtOptionsPage);
126
127     m_welcomePage = new GettingStartedWelcomePage;
128     addObject(m_welcomePage);
129     connect(mgr, SIGNAL(updateExamples(QString,QString,QString)),
130             m_welcomePage, SLOT(updateExamples(QString,QString,QString)));
131
132     //create and register objects
133     m_qt4ProjectManager = new Qt4Manager(this);
134     addObject(m_qt4ProjectManager);
135
136     TextEditor::TextEditorActionHandler *editorHandler
137             = new TextEditor::TextEditorActionHandler(Constants::C_PROFILEEDITOR,
138                   TextEditor::TextEditorActionHandler::UnCommentSelection);
139
140     m_proFileEditorFactory = new ProFileEditorFactory(m_qt4ProjectManager, editorHandler);
141     addObject(m_proFileEditorFactory);
142
143     addAutoReleasedObject(new EmptyProjectWizard);
144     addAutoReleasedObject(new SubdirsProjectWizard);
145     addAutoReleasedObject(new GuiAppWizard);
146     addAutoReleasedObject(new ConsoleAppWizard);
147     addAutoReleasedObject(new MobileAppWizard);
148     addAutoReleasedObject(new QtQuickAppWizard);
149     addAutoReleasedObject(new Html5AppWizard);
150     addAutoReleasedObject(new LibraryWizard);
151     addAutoReleasedObject(new TestWizard);
152     addAutoReleasedObject(new CustomWidgetWizard);
153
154     CustomQt4ProjectWizard::registerSelf();
155
156     addAutoReleasedObject(new QMakeStepFactory);
157     addAutoReleasedObject(new MakeStepFactory);
158
159     addAutoReleasedObject(new Qt4RunConfigurationFactory);
160
161 #ifdef Q_OS_MAC
162     addAutoReleasedObject(new MacDesignerExternalEditor);
163 #else
164     addAutoReleasedObject(new DesignerExternalEditor);
165 #endif
166     addAutoReleasedObject(new LinguistExternalEditor);
167
168     addAutoReleasedObject(new S60Manager);
169     addAutoReleasedObject(new MaemoManager);
170
171     addAutoReleasedObject(new Qt4DesktopTargetFactory);
172     addAutoReleasedObject(new Qt4SimulatorTargetFactory);
173
174     ProFileCompletion *completion = new ProFileCompletion;
175     addAutoReleasedObject(completion);
176     // Set completion settings and keep them up to date
177     TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();
178     completion->setCompletionSettings(textEditorSettings->completionSettings());
179     connect(textEditorSettings, SIGNAL(completionSettingsChanged(TextEditor::CompletionSettings)),
180             completion, SLOT(setCompletionSettings(TextEditor::CompletionSettings)));
181
182     new ProFileCacheManager(this);
183
184     // TODO reenable
185     //m_embeddedPropertiesPage = new EmbeddedPropertiesPage;
186     //addObject(m_embeddedPropertiesPage);
187
188     //menus
189     Core::ActionContainer *mbuild =
190             am->actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
191     Core::ActionContainer *mproject =
192             am->actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
193     Core::ActionContainer *msubproject =
194             am->actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
195
196     //register actions
197     Core::Command *command;
198
199     QIcon qmakeIcon(QLatin1String(":/qt4projectmanager/images/run_qmake.png"));
200     qmakeIcon.addFile(QLatin1String(":/qt4projectmanager/images/run_qmake_small.png"));
201     m_runQMakeAction = new QAction(qmakeIcon, tr("Run qmake"), this);
202     command = am->registerAction(m_runQMakeAction, Constants::RUNQMAKE, m_projectContext);
203     command->setAttribute(Core::Command::CA_Hide);
204     mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_PROJECT);
205     connect(m_runQMakeAction, SIGNAL(triggered()), m_qt4ProjectManager, SLOT(runQMake()));
206
207     m_runQMakeActionContextMenu = new QAction(qmakeIcon, tr("Run qmake"), this);
208     command = am->registerAction(m_runQMakeActionContextMenu, Constants::RUNQMAKECONTEXTMENU, m_projectContext);
209     command->setAttribute(Core::Command::CA_Hide);
210     mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
211     msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
212     connect(m_runQMakeActionContextMenu, SIGNAL(triggered()), m_qt4ProjectManager, SLOT(runQMakeContextMenu()));
213
214     QIcon buildIcon(ProjectExplorer::Constants::ICON_BUILD);
215     buildIcon.addFile(ProjectExplorer::Constants::ICON_BUILD_SMALL);
216     m_buildSubProjectContextMenu = new QAction(buildIcon, tr("Build"), this);
217     command = am->registerAction(m_buildSubProjectContextMenu, Constants::BUILDSUBDIR, m_projectContext);
218     command->setAttribute(Core::Command::CA_Hide);
219     msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
220     connect(m_buildSubProjectContextMenu, SIGNAL(triggered()), m_qt4ProjectManager, SLOT(buildSubDirContextMenu()));
221
222     QIcon rebuildIcon(ProjectExplorer::Constants::ICON_REBUILD);
223     rebuildIcon.addFile(ProjectExplorer::Constants::ICON_REBUILD_SMALL);
224     m_rebuildSubProjectContextMenu = new QAction(rebuildIcon, tr("Rebuild"), this);
225     command = am->registerAction(m_rebuildSubProjectContextMenu, Constants::REBUILDSUBDIR, m_projectContext);
226     command->setAttribute(Core::Command::CA_Hide);
227     msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
228     connect(m_rebuildSubProjectContextMenu, SIGNAL(triggered()), m_qt4ProjectManager, SLOT(rebuildSubDirContextMenu()));
229
230     QIcon cleanIcon(ProjectExplorer::Constants::ICON_CLEAN);
231     cleanIcon.addFile(ProjectExplorer::Constants::ICON_CLEAN_SMALL);
232     m_cleanSubProjectContextMenu = new QAction(cleanIcon, tr("Clean"), this);
233     command = am->registerAction(m_cleanSubProjectContextMenu, Constants::CLEANSUBDIR, m_projectContext);
234     command->setAttribute(Core::Command::CA_Hide);
235     msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
236     connect(m_cleanSubProjectContextMenu, SIGNAL(triggered()), m_qt4ProjectManager, SLOT(cleanSubDirContextMenu()));
237
238     connect(m_projectExplorer,
239             SIGNAL(aboutToShowContextMenu(ProjectExplorer::Project*, ProjectExplorer::Node*)),
240             this, SLOT(updateContextMenu(ProjectExplorer::Project*, ProjectExplorer::Node*)));
241
242     connect(m_projectExplorer->buildManager(), SIGNAL(buildStateChanged(ProjectExplorer::Project *)),
243             this, SLOT(buildStateChanged(ProjectExplorer::Project *)));
244     connect(m_projectExplorer, SIGNAL(currentProjectChanged(ProjectExplorer::Project *)),
245             this, SLOT(currentProjectChanged()));
246
247     Core::ActionContainer *contextMenu = am->createMenu(Qt4ProjectManager::Constants::M_CONTEXT);
248
249     Core::Command *cmd;
250
251     Core::Context proFileEditorContext = Core::Context(Qt4ProjectManager::Constants::C_PROFILEEDITOR);
252
253     QAction *jumpToFile = new QAction(tr("Jump to File Under Cursor"), this);
254     cmd = am->registerAction(jumpToFile,
255         Constants::JUMP_TO_FILE, proFileEditorContext);
256     cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F2));
257     connect(jumpToFile, SIGNAL(triggered()),
258             this, SLOT(jumpToFile()));
259     contextMenu->addAction(cmd);
260
261     QAction *addLibrary = new QAction(tr("Add Library..."), this);
262     cmd = am->registerAction(addLibrary,
263         Constants::ADDLIBRARY, proFileEditorContext);
264     connect(addLibrary, SIGNAL(triggered()),
265             this, SLOT(addLibrary()));
266     contextMenu->addAction(cmd);
267
268     QAction *separator = new QAction(this);
269     separator->setSeparator(true);
270     contextMenu->addAction(am->registerAction(separator,
271                   Core::Id(Constants::SEPARATOR), proFileEditorContext));
272
273     cmd = am->command(TextEditor::Constants::UN_COMMENT_SELECTION);
274     contextMenu->addAction(cmd);
275
276     return true;
277 }
278
279 void Qt4ProjectManagerPlugin::extensionsInitialized()
280 {
281     m_qt4ProjectManager->init();
282 }
283
284 void Qt4ProjectManagerPlugin::updateContextMenu(Project *project,
285                                                 ProjectExplorer::Node *node)
286 {
287     m_qt4ProjectManager->setContextProject(project);
288     m_qt4ProjectManager->setContextNode(node);
289     m_runQMakeActionContextMenu->setEnabled(false);
290     m_buildSubProjectContextMenu->setEnabled(false);
291     m_rebuildSubProjectContextMenu->setEnabled(false);
292     m_cleanSubProjectContextMenu->setEnabled(false);
293
294     Qt4ProFileNode *proFileNode = qobject_cast<Qt4ProFileNode *>(node);
295     if (qobject_cast<Qt4Project *>(project) && proFileNode) {
296         m_runQMakeActionContextMenu->setVisible(true);
297         m_buildSubProjectContextMenu->setVisible(true);
298         m_rebuildSubProjectContextMenu->setVisible(true);
299         m_cleanSubProjectContextMenu->setVisible(true);
300
301         if (!m_projectExplorer->buildManager()->isBuilding(project)) {
302             m_runQMakeActionContextMenu->setEnabled(true);
303             m_buildSubProjectContextMenu->setEnabled(true);
304             m_rebuildSubProjectContextMenu->setEnabled(true);
305             m_cleanSubProjectContextMenu->setEnabled(true);
306         }
307     } else {
308         m_runQMakeActionContextMenu->setVisible(false);
309         m_buildSubProjectContextMenu->setVisible(false);
310         m_rebuildSubProjectContextMenu->setVisible(false);
311         m_cleanSubProjectContextMenu->setVisible(false);
312     }
313 }
314
315 void Qt4ProjectManagerPlugin::currentProjectChanged()
316 {
317     m_runQMakeAction->setEnabled(!m_projectExplorer->buildManager()->isBuilding(m_projectExplorer->currentProject()));
318 }
319
320 void Qt4ProjectManagerPlugin::buildStateChanged(ProjectExplorer::Project *pro)
321 {
322     ProjectExplorer::Project *currentProject = m_projectExplorer->currentProject();
323     if (pro == currentProject)
324         m_runQMakeAction->setEnabled(!m_projectExplorer->buildManager()->isBuilding(currentProject));
325     if (pro == m_qt4ProjectManager->contextProject())
326         m_runQMakeActionContextMenu->setEnabled(!m_projectExplorer->buildManager()->isBuilding(pro));
327 }
328
329 void Qt4ProjectManagerPlugin::addLibrary()
330 {
331     Core::EditorManager *em = Core::EditorManager::instance();
332     ProFileEditorWidget *editor = qobject_cast<ProFileEditorWidget*>(em->currentEditor()->widget());
333     if (editor)
334         editor->addLibrary();
335 }
336
337 void Qt4ProjectManagerPlugin::jumpToFile()
338 {
339     Core::EditorManager *em = Core::EditorManager::instance();
340     ProFileEditorWidget *editor = qobject_cast<ProFileEditorWidget*>(em->currentEditor()->widget());
341     if (editor)
342         editor->jumpToFile();
343 }
344
345 #ifdef WITH_TESTS
346 void Qt4ProjectManagerPlugin::testBasicProjectLoading()
347 {
348     QString testDirectory = ExtensionSystem::PluginManager::instance()->testDataDirectory() + "/qt4projectmanager/";
349     QString test1 = testDirectory + "test1/test1.pro";
350     m_projectExplorer->openProject(test1);
351     QVERIFY(!m_projectExplorer->session()->projects().isEmpty());
352     Qt4Project *qt4project = qobject_cast<Qt4Project *>(m_projectExplorer->session()->projects().first());
353     QVERIFY(qt4project);
354     QVERIFY(qt4project->rootProjectNode()->projectType() == ApplicationTemplate);
355     QVERIFY(m_projectExplorer->currentProject() != 0);
356 }
357 #endif
358
359 Q_EXPORT_PLUGIN(Qt4ProjectManagerPlugin)