OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / wizards / librarywizarddialog.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 "librarywizarddialog.h"
34 #include "filespage.h"
35 #include "libraryparameters.h"
36 #include "modulespage.h"
37 #include "mobilelibrarywizardoptionpage.h"
38 #include "mobilelibraryparameters.h"
39 #include "abstractmobileapp.h"
40 #include "qt4projectmanagerconstants.h"
41
42 #include <utils/projectintropage.h>
43
44 #include <QtCore/QDebug>
45 #include <QtCore/QDir>
46
47 #include <QtGui/QComboBox>
48 #include <QtGui/QLabel>
49
50 enum { debugLibWizard = 0 };
51
52 namespace Qt4ProjectManager {
53 namespace Internal {
54
55 struct PluginBaseClasses {
56     const char *name;
57     const char *module;
58     // blank separated list or 0
59     const char *dependentModules;
60     const char *targetDirectory;
61 };
62
63 static const PluginBaseClasses pluginBaseClasses[] =
64 {
65     { "QAccessiblePlugin", "QtGui", "QtCore", "accessible" },
66     { "QDecorationPlugin", "QtGui", "QtCore", 0},
67     { "QIconEnginePluginV2", "QtGui", "QtCore", "imageformats" },
68     { "QImageIOPlugin", "QtGui", "QtCore", "imageformats" },
69     { "QScriptExtensionPlugin", "QtScript", "QtCore", 0 },
70     { "QSqlDriverPlugin", "QtSql", "QtCore", "sqldrivers" },
71     { "QStylePlugin", "QtGui", "QtCore", "styles" },
72     { "QTextCodecPlugin", "QtCore", 0, "codecs" }
73 };
74
75 enum { defaultPluginBaseClass = 6 };
76
77 static const PluginBaseClasses *findPluginBaseClass(const QString &name)
78 {
79     const int pluginBaseClassCount = sizeof(pluginBaseClasses)/sizeof(PluginBaseClasses);
80     for (int i = 0; i < pluginBaseClassCount; i++)
81         if (name == QLatin1String(pluginBaseClasses[i].name))
82             return pluginBaseClasses + i;
83     return 0;
84 }
85
86 // return dependencies of a plugin as a line ready for the 'QT=' line in a pro
87 // file
88 static QString pluginDependencies(const PluginBaseClasses *plb)
89 {
90     QString dependencies;
91     const QChar blank = QLatin1Char(' ');
92     // Find the module names and convert to ids
93     QStringList pluginModules= plb->dependentModules ?
94                                QString(QLatin1String(plb->dependentModules)).split(blank) :
95                                QStringList();
96     pluginModules.push_back(QLatin1String(plb->module));
97     foreach (const QString &module, pluginModules) {
98         if (!dependencies.isEmpty())
99             dependencies += blank;
100         dependencies += ModulesPage::idOfModule(module);
101     }
102     return dependencies;
103 }
104
105 // A Project intro page with an additional type chooser.
106 class LibraryIntroPage : public Utils::ProjectIntroPage
107 {
108     Q_DISABLE_COPY(LibraryIntroPage)
109 public:
110     explicit LibraryIntroPage(QWidget *parent = 0);
111
112     QtProjectParameters::Type type() const;
113
114 private:
115     QComboBox *m_typeCombo;
116 };
117
118 LibraryIntroPage::LibraryIntroPage(QWidget *parent) :
119     Utils::ProjectIntroPage(parent),
120     m_typeCombo(new QComboBox)
121 {
122     m_typeCombo->setEditable(false);
123     m_typeCombo->addItem(LibraryWizardDialog::tr("Shared Library"),
124                          QVariant(QtProjectParameters::SharedLibrary));
125     m_typeCombo->addItem(LibraryWizardDialog::tr("Statically Linked Library"),
126                          QVariant(QtProjectParameters::StaticLibrary));
127     m_typeCombo->addItem(LibraryWizardDialog::tr("Qt 4 Plugin"),
128                          QVariant(QtProjectParameters::Qt4Plugin));
129     insertControl(0, new QLabel(LibraryWizardDialog::tr("Type")), m_typeCombo);
130 }
131
132 QtProjectParameters::Type LibraryIntroPage::type() const
133 {
134     return static_cast<QtProjectParameters::Type>(m_typeCombo->itemData(m_typeCombo->currentIndex()).toInt());
135 }
136
137 // ------------------- LibraryWizardDialog
138 LibraryWizardDialog::LibraryWizardDialog(const QString &templateName,
139                                          const QIcon &icon,
140                                          const QList<QWizardPage*> &extensionPages,
141                                          bool showModulesPage,
142                                          QWidget *parent) :
143     BaseQt4ProjectWizardDialog(showModulesPage, new LibraryIntroPage, -1, parent),
144     m_filesPage(new FilesPage),
145     m_mobilePage(new MobileLibraryWizardOptionPage),
146     m_pluginBaseClassesInitialized(false),
147     m_filesPageId(-1), m_modulesPageId(-1), m_targetPageId(-1),
148     m_mobilePageId(-1)
149 {
150     setWindowIcon(icon);
151     setWindowTitle(templateName);
152     setSelectedModules(QLatin1String("core"));
153
154     // Note that QWizard::currentIdChanged() is emitted at strange times.
155     // Use the intro page instead, set up initially
156     setIntroDescription(tr("This wizard generates a C++ library project."));
157
158     m_targetPageId = addTargetSetupPage();
159
160     if (m_targetPageId != -1)
161         m_mobilePageId = addPage(m_mobilePage);
162
163     m_modulesPageId = addModulesPage();
164
165     m_filesPage->setNamespacesEnabled(true);
166     m_filesPage->setFormFileInputVisible(false);
167     m_filesPage->setClassTypeComboVisible(false);
168
169     m_filesPageId = addPage(m_filesPage);
170
171     Utils::WizardProgressItem *introItem = wizardProgress()->item(startId());
172     Utils::WizardProgressItem *targetItem;
173     if (m_targetPageId != -1)
174         targetItem = wizardProgress()->item(m_targetPageId);
175     Utils::WizardProgressItem *mobileItem = wizardProgress()->item(m_mobilePageId);
176     mobileItem->setTitle(QLatin1String("    ") + tr("Symbian Specific"));
177     Utils::WizardProgressItem *modulesItem = wizardProgress()->item(m_modulesPageId);
178     Utils::WizardProgressItem *filesItem = wizardProgress()->item(m_filesPageId);
179     filesItem->setTitle(tr("Details"));
180
181     if (m_targetPageId != -1) {
182         targetItem->setNextItems(QList<Utils::WizardProgressItem *>()
183                                  << mobileItem << modulesItem << filesItem);
184         targetItem->setNextShownItem(0);
185         mobileItem->setNextItems(QList<Utils::WizardProgressItem *>()
186                                  << modulesItem << filesItem);
187         mobileItem->setNextShownItem(0);
188     } else {
189         introItem->setNextItems(QList<Utils::WizardProgressItem *>()
190                                 << modulesItem << filesItem);
191         introItem->setNextShownItem(0);
192     }
193
194
195     connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(slotCurrentIdChanged(int)));
196
197     foreach (QWizardPage *p, extensionPages)
198         Core::BaseFileWizard::applyExtensionPageShortTitle(this, addPage(p));
199 }
200
201 void LibraryWizardDialog::setSuffixes(const QString &header, const QString &source,  const QString &form)
202 {
203     m_filesPage->setSuffixes(header, source, form);
204 }
205
206 void LibraryWizardDialog::setLowerCaseFiles(bool l)
207 {
208     m_filesPage->setLowerCaseFiles(l);
209 }
210
211 void LibraryWizardDialog::setSymbianUid(const QString &uid)
212 {
213     m_mobilePage->setSymbianUid(uid);
214 }
215
216 QtProjectParameters::Type  LibraryWizardDialog::type() const
217 {
218     return static_cast<const LibraryIntroPage*>(introPage())->type();
219 }
220
221 bool LibraryWizardDialog::isModulesPageSkipped() const
222 {
223     // When leaving the intro, target or mobile page, the modules page is skipped
224     // in the case of a plugin since it knows its dependencies by itself.
225     return type() == QtProjectParameters::Qt4Plugin;
226 }
227
228 int LibraryWizardDialog::skipModulesPageIfNeeded() const
229 {
230     if (isModulesPageSkipped())
231         return m_filesPageId;
232     return m_modulesPageId;
233 }
234
235 int LibraryWizardDialog::nextId() const
236 {
237     if (m_targetPageId != -1) {
238         if (currentId() == m_targetPageId) {
239
240             int next = m_modulesPageId;
241
242             const bool symbianTargetEnabled = isTargetSelected(QLatin1String(Constants::S60_DEVICE_TARGET_ID))
243                     || isTargetSelected(QLatin1String(Constants::S60_EMULATOR_TARGET_ID));
244
245             // If there was no Symbian target defined we omit "Symbian specific" step
246             // We also omit this step if the library type is not dll
247             if (symbianTargetEnabled
248                     && (type() == QtProjectParameters::SharedLibrary
249                         || type() == QtProjectParameters::Qt4Plugin))
250                 next = m_mobilePageId;
251
252             if (next == m_modulesPageId)
253                 return skipModulesPageIfNeeded();
254
255             return next;
256         } else if (currentId() == m_mobilePageId) {
257             return skipModulesPageIfNeeded();
258         }
259     } else if (currentId() == startId()) {
260         return skipModulesPageIfNeeded();
261     }
262
263     return BaseQt4ProjectWizardDialog::nextId();
264 }
265
266 void LibraryWizardDialog::initializePage(int id)
267 {
268     if (m_targetPageId != -1 && id == m_targetPageId) {
269         Utils::WizardProgressItem *mobileItem = wizardProgress()->item(m_mobilePageId);
270         Utils::WizardProgressItem *modulesItem = wizardProgress()->item(m_modulesPageId);
271         Utils::WizardProgressItem *filesItem = wizardProgress()->item(m_filesPageId);
272         if (isModulesPageSkipped())
273             mobileItem->setNextShownItem(filesItem);
274         else
275             mobileItem->setNextShownItem(modulesItem);
276
277     }
278     BaseQt4ProjectWizardDialog::initializePage(id);
279 }
280
281 void LibraryWizardDialog::cleanupPage(int id)
282 {
283     if (m_targetPageId != -1 && id == m_targetPageId) {
284         Utils::WizardProgressItem *mobileItem = wizardProgress()->item(m_mobilePageId);
285         mobileItem->setNextShownItem(0);
286     }
287     BaseQt4ProjectWizardDialog::cleanupPage(id);
288 }
289
290 QtProjectParameters LibraryWizardDialog::parameters() const
291 {
292     QtProjectParameters rc;
293     rc.type = type();
294     rc.fileName = projectName();
295     rc.path = path();
296     if (rc.type == QtProjectParameters::Qt4Plugin) {
297         // Plugin: Dependencies & Target directory
298         if (const PluginBaseClasses *plb = findPluginBaseClass(m_filesPage->baseClassName())) {
299             rc.selectedModules = pluginDependencies(plb);
300             if (plb->targetDirectory) {
301                 rc.targetDirectory = QLatin1String("$$[QT_INSTALL_PLUGINS]/");
302                 rc.targetDirectory += QLatin1String(plb->targetDirectory);
303             }
304         }
305     } else {
306         // Modules from modules page
307         rc.selectedModules = selectedModules();
308         rc.deselectedModules = deselectedModules();
309     }
310     return rc;
311 }
312
313 void LibraryWizardDialog::slotCurrentIdChanged(int id)
314 {
315     if (debugLibWizard)
316         qDebug() << Q_FUNC_INFO << id;
317     if (id == m_filesPageId)
318         setupFilesPage();// Switching to files page: Set up base class accordingly (plugin)
319     else if (id == m_mobilePageId
320              || (m_mobilePage->symbianUid().isEmpty()
321              && currentPage() && currentPage()->isFinalPage()))
322         setupMobilePage();
323 }
324
325 void LibraryWizardDialog::setupFilesPage()
326 {
327     switch (type()) {
328     case QtProjectParameters::Qt4Plugin:
329         if (!m_pluginBaseClassesInitialized) {
330             if (debugLibWizard)
331                 qDebug("initializing for plugins");
332             QStringList baseClasses;
333             const int pluginBaseClassCount = sizeof(pluginBaseClasses)/sizeof(PluginBaseClasses);
334             Q_ASSERT(defaultPluginBaseClass < pluginBaseClassCount);
335             for (int i = 0; i < pluginBaseClassCount; i++)
336                 baseClasses.push_back(QLatin1String(pluginBaseClasses[i].name));
337             m_filesPage->setBaseClassChoices(baseClasses);
338             m_filesPage->setBaseClassName(baseClasses.at(defaultPluginBaseClass));
339             m_pluginBaseClassesInitialized = true;
340         }
341         m_filesPage->setBaseClassInputVisible(true);
342         break;
343     default: {
344         // Urrm, figure out a good class name. Use project name this time
345         QString className = projectName();
346         if (!className.isEmpty())
347             className[0] = className.at(0).toUpper();
348         m_filesPage->setClassName(className);
349         m_filesPage->setBaseClassInputVisible(false);
350     }
351         break;
352     }
353 }
354
355 void LibraryWizardDialog::setupMobilePage()
356 {
357     m_mobilePage->setSymbianUid(AbstractMobileApp::symbianUidForPath(path()+projectName()));
358
359     if (type() == QtProjectParameters::Qt4Plugin)
360         m_mobilePage->setQtPluginDirectory(projectName());
361     m_mobilePage->setLibraryType(type());
362 }
363
364 LibraryParameters LibraryWizardDialog::libraryParameters() const
365 {
366     LibraryParameters rc;
367     rc.className = m_filesPage->className();
368     rc.baseClassName = m_filesPage->baseClassName();
369     rc.sourceFileName = m_filesPage->sourceFileName();
370     rc.headerFileName = m_filesPage->headerFileName();
371     if (!rc.baseClassName.isEmpty())
372         if (const PluginBaseClasses *plb = findPluginBaseClass(rc.baseClassName)) {
373             rc.baseClassModule = QLatin1String(plb->module);
374         }
375     return rc;
376 }
377
378 MobileLibraryParameters LibraryWizardDialog::mobileLibraryParameters() const
379 {
380     MobileLibraryParameters mlp;
381     mlp.libraryType = type();
382     mlp.fileName = projectName();
383
384     //Symbian and Maemo stuff should always be added to pro file. Even if no mobile target is specified
385     mlp.type |= MobileLibraryParameters::Symbian|MobileLibraryParameters::Maemo;
386
387     if (mlp.type & MobileLibraryParameters::Symbian) {
388         mlp.symbianUid = m_mobilePage->symbianUid();
389         mlp.qtPluginDirectory = m_mobilePage->qtPluginDirectory();
390         mlp.symbianCapabilities |= m_mobilePage->networkEnabled()?MobileLibraryParameters::NetworkServices:0;
391     }
392
393     return mlp;
394 }
395
396 } // namespace Internal
397 } // namespace Qt4ProjectManager