OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / wizards / qtwizard.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 "qtwizard.h"
34
35 #include "qt4project.h"
36 #include "qt4projectmanager.h"
37 #include "qt4projectmanagerconstants.h"
38 #include "qt4target.h"
39 #include "modulespage.h"
40 #include "targetsetuppage.h"
41
42 #include <coreplugin/icore.h>
43 #include <coreplugin/editormanager/editormanager.h>
44
45 #include <cpptools/cpptoolsconstants.h>
46
47 #include <projectexplorer/projectexplorer.h>
48 #include <projectexplorer/task.h>
49
50 #include <extensionsystem/pluginmanager.h>
51
52 #include <QtCore/QCoreApplication>
53 #include <QtCore/QVariant>
54
55 using namespace Qt4ProjectManager;
56 using namespace Qt4ProjectManager::Internal;
57
58 static inline Core::BaseFileWizardParameters
59     wizardParameters(const QString &id,
60                      const QString &category,
61                      const QString &categoryTranslationScope,
62                      const QString &displayCategory,
63                      const QString &name,
64                      const QString &description,
65                      const QIcon &icon)
66 {
67     Core::BaseFileWizardParameters rc(Core::IWizard::ProjectWizard);
68     rc.setCategory(category);
69     rc.setDisplayCategory(QCoreApplication::translate(categoryTranslationScope.toLatin1(),
70                                                       displayCategory.toLatin1()));
71     rc.setIcon(icon);
72     rc.setDisplayName(name);
73     rc.setId(id);
74     rc.setDescription(description);
75     return rc;
76 }
77
78 // -------------------- QtWizard
79 QtWizard::QtWizard(const QString &id,
80                    const QString &category,
81                    const QString &categoryTranslationScope,
82                    const QString &displayCategory,
83                    const QString &name,
84                    const QString &description, const QIcon &icon) :
85     Core::BaseFileWizard(wizardParameters(id,
86                                           category,
87                                           categoryTranslationScope,
88                                           displayCategory,
89                                           name,
90                                           description,
91                                           icon))
92 {
93 }
94
95 QString QtWizard::sourceSuffix()
96 {
97     return preferredSuffix(QLatin1String(Constants::CPP_SOURCE_MIMETYPE));
98 }
99
100 QString QtWizard::headerSuffix()
101 {
102     return preferredSuffix(QLatin1String(Constants::CPP_HEADER_MIMETYPE));
103 }
104
105 QString QtWizard::formSuffix()
106 {
107     return preferredSuffix(QLatin1String(Constants::FORM_MIMETYPE));
108 }
109
110 QString QtWizard::profileSuffix()
111 {
112     return preferredSuffix(QLatin1String(Constants::PROFILE_MIMETYPE));
113 }
114
115 bool QtWizard::postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l, QString *errorMessage)
116 {
117     return QtWizard::qt4ProjectPostGenerateFiles(w, l, errorMessage);
118 }
119
120 bool QtWizard::qt4ProjectPostGenerateFiles(const QWizard *w,
121                                            const Core::GeneratedFiles &generatedFiles,
122                                            QString *errorMessage)
123 {
124     const BaseQt4ProjectWizardDialog *dialog = qobject_cast<const BaseQt4ProjectWizardDialog *>(w);
125
126     // Generate user settings
127     foreach (const Core::GeneratedFile &file, generatedFiles)
128         if (file.attributes() & Core::GeneratedFile::OpenProjectAttribute) {
129             dialog->writeUserFile(file.path());
130             break;
131         }
132
133     // Post-Generate: Open the projects/editors
134     return ProjectExplorer::CustomProjectWizard::postGenerateOpen(generatedFiles ,errorMessage);
135 }
136
137 QString QtWizard::templateDir()
138 {
139     QString rc = Core::ICore::instance()->resourcePath();
140     rc += QLatin1String("/templates/qt4project");
141     return rc;
142 }
143
144 bool QtWizard::lowerCaseFiles()
145 {
146     QString lowerCaseSettingsKey = QLatin1String(CppTools::Constants::CPPTOOLS_SETTINGSGROUP);
147     lowerCaseSettingsKey += QLatin1Char('/');
148     lowerCaseSettingsKey += QLatin1String(CppTools::Constants::LOWERCASE_CPPFILES_KEY);
149     const bool lowerCaseDefault = CppTools::Constants::lowerCaseFilesDefault;
150     return Core::ICore::instance()->settings()->value(lowerCaseSettingsKey, QVariant(lowerCaseDefault)).toBool();
151 }
152
153 bool QtWizard::showModulesPageForApplications()
154 {
155     return false;
156 }
157
158 bool QtWizard::showModulesPageForLibraries()
159 {
160     return true;
161 }
162
163 // ------------ CustomQt4ProjectWizard
164 CustomQt4ProjectWizard::CustomQt4ProjectWizard(const Core::BaseFileWizardParameters& baseFileParameters,
165                                                QObject *parent) :
166     ProjectExplorer::CustomProjectWizard(baseFileParameters, parent)
167 {
168 }
169
170 QWizard *CustomQt4ProjectWizard::createWizardDialog(QWidget *parent,
171                                                     const QString &defaultPath,
172                                                     const WizardPageList &extensionPages) const
173 {
174     BaseQt4ProjectWizardDialog *wizard =  new BaseQt4ProjectWizardDialog(false, parent);
175     initProjectWizardDialog(wizard, defaultPath, extensionPages);
176     if (wizard->pageIds().contains(targetPageId))
177         qWarning("CustomQt4ProjectWizard: Unable to insert target page at %d", int(targetPageId));
178     wizard->addTargetSetupPage(QSet<QString>(), false, targetPageId);
179     return wizard;
180 }
181
182 bool CustomQt4ProjectWizard::postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l, QString *errorMessage)
183 {
184     return QtWizard::qt4ProjectPostGenerateFiles(w, l, errorMessage);
185 }
186
187 void CustomQt4ProjectWizard::registerSelf()
188 {
189     ProjectExplorer::CustomWizard::registerFactory<CustomQt4ProjectWizard>(QLatin1String("qt4project"));
190 }
191
192 // ----------------- BaseQt4ProjectWizardDialog
193 BaseQt4ProjectWizardDialog::BaseQt4ProjectWizardDialog(bool showModulesPage, QWidget *parent) :
194     ProjectExplorer::BaseProjectWizardDialog(parent),
195     m_modulesPage(0),
196     m_targetSetupPage(0)
197 {
198     init(showModulesPage);
199 }
200
201 BaseQt4ProjectWizardDialog::BaseQt4ProjectWizardDialog(bool showModulesPage,
202                                                        Utils::ProjectIntroPage *introPage,
203                                                        int introId, QWidget *parent) :
204     ProjectExplorer::BaseProjectWizardDialog(introPage, introId, parent),
205     m_modulesPage(0),
206     m_targetSetupPage(0)
207 {
208     init(showModulesPage);
209 }
210
211 BaseQt4ProjectWizardDialog::~BaseQt4ProjectWizardDialog()
212 {
213     if (m_targetSetupPage && !m_targetSetupPage->parent())
214         delete m_targetSetupPage;
215     if (m_modulesPage && !m_modulesPage->parent())
216         delete m_modulesPage;
217 }
218
219 void BaseQt4ProjectWizardDialog::init(bool showModulesPage)
220 {
221     if (showModulesPage)
222         m_modulesPage = new ModulesPage;
223     connect(this, SIGNAL(projectParametersChanged(QString,QString)),
224             this, SLOT(generateProfileName(QString,QString)));
225 }
226
227 int BaseQt4ProjectWizardDialog::addModulesPage(int id)
228 {
229     if (!m_modulesPage)
230         return -1;
231     if (id >= 0) {
232         setPage(id, m_modulesPage);
233         wizardProgress()->item(id)->setTitle(tr("Modules"));
234         return id;
235     }
236     const int newId = addPage(m_modulesPage);
237     wizardProgress()->item(newId)->setTitle(tr("Modules"));
238     return newId;
239 }
240
241 int BaseQt4ProjectWizardDialog::addTargetSetupPage(QSet<QString> targets, bool mobile, int id)
242 {
243     m_targetSetupPage = new TargetSetupPage;
244     m_targets = targets;
245     resize(900, 450);
246
247     m_targetSetupPage->setPreferMobile(mobile);
248
249     if (id >= 0)
250         setPage(id, m_targetSetupPage);
251     else
252         id = addPage(m_targetSetupPage);
253     wizardProgress()->item(id)->setTitle(tr("Targets"));
254
255     return id;
256 }
257
258 QString BaseQt4ProjectWizardDialog::selectedModules() const
259 {
260     return m_modulesPage ? m_modulesPage->selectedModules() : m_selectedModules;
261 }
262
263 void BaseQt4ProjectWizardDialog::setSelectedModules(const QString &modules, bool lock)
264 {
265     if (m_modulesPage) {
266         foreach(const QString &module, modules.split(QLatin1Char(' '))) {
267             m_modulesPage->setModuleSelected(module, true);
268             m_modulesPage->setModuleEnabled(module, !lock);
269         }
270     } else {
271         m_selectedModules = modules;
272     }
273 }
274
275 QString BaseQt4ProjectWizardDialog::deselectedModules() const
276 {
277     return m_modulesPage ? m_modulesPage->deselectedModules() : m_deselectedModules;
278 }
279
280 void BaseQt4ProjectWizardDialog::setDeselectedModules(const QString &modules)
281 {
282     if (m_modulesPage) {
283         foreach(const QString &module, modules.split(QLatin1Char(' ')))
284             m_modulesPage->setModuleSelected(module, false);
285     } else {
286         m_deselectedModules = modules;
287     }
288 }
289
290 bool BaseQt4ProjectWizardDialog::writeUserFile(const QString &proFileName) const
291 {
292     if (!m_targetSetupPage)
293         return false;
294
295     Qt4Manager *manager = ExtensionSystem::PluginManager::instance()->getObject<Qt4Manager>();
296     Q_ASSERT(manager);
297
298     Qt4Project *pro = new Qt4Project(manager, proFileName);
299     bool success = m_targetSetupPage->setupProject(pro);
300     if (success)
301         pro->saveSettings();
302     delete pro;
303     return success;
304 }
305
306 bool BaseQt4ProjectWizardDialog::setupProject(Qt4Project *project) const
307 {
308     return m_targetSetupPage->setupProject(project);
309 }
310
311 bool BaseQt4ProjectWizardDialog::isTargetSelected(const QString &targetid) const
312 {
313     return m_targetSetupPage->isTargetSelected(targetid);
314 }
315
316 void BaseQt4ProjectWizardDialog::generateProfileName(const QString &name, const QString &path)
317 {
318     const QString proFile = QDir::fromNativeSeparators(path) + QChar('/') + name + QChar('/') + name + QLatin1String(".pro");
319     m_targetSetupPage->setProFilePath(proFile);
320 }
321
322 QSet<QString> BaseQt4ProjectWizardDialog::desktopTarget()
323 {
324     QSet<QString> rc;
325     rc.insert(QLatin1String(Constants::DESKTOP_TARGET_ID));
326     return rc;
327 }