OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / wizards / librarywizard.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 "librarywizard.h"
35 #include "librarywizarddialog.h"
36 #include "qt4projectmanager.h"
37 #include "qt4projectmanagerconstants.h"
38 #include "mobilelibraryparameters.h"
39
40 #include <utils/codegeneration.h>
41 #include <cpptools/abstracteditorsupport.h>
42 #include <projectexplorer/projectexplorerconstants.h>
43
44 #include <QtCore/QDir>
45 #include <QtCore/QFileInfo>
46 #include <QtCore/QTextStream>
47 #include <QtGui/QIcon>
48
49 static const char *sharedHeaderPostfixC = "_global";
50
51 namespace Qt4ProjectManager {
52
53 namespace Internal {
54
55 LibraryWizard::LibraryWizard()
56   : QtWizard(QLatin1String("H.Qt4Library"),
57              QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_CATEGORY),
58              QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_TR_SCOPE),
59              QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_TR_CATEGORY),
60              tr("C++ Library"),
61              tr("Creates a C++ library based on qmake. This can be used to create:<ul>"
62                 "<li>a shared C++ library for use with <tt>QPluginLoader</tt> and runtime (Plugins)</li>"
63                 "<li>a shared or static C++ library for use with another project at linktime</li></ul>."),
64              QIcon(QLatin1String(":/wizards/images/lib.png")))
65 {
66 }
67
68 QWizard *LibraryWizard::createWizardDialog(QWidget *parent,
69                                           const QString &defaultPath,
70                                           const WizardPageList &extensionPages) const
71 {
72     LibraryWizardDialog *dialog = new  LibraryWizardDialog(displayName(), icon(), extensionPages,
73                                                            showModulesPageForLibraries(), parent);
74     dialog->setLowerCaseFiles(QtWizard::lowerCaseFiles());
75     dialog->setPath(defaultPath);
76     dialog->setProjectName(LibraryWizardDialog::uniqueProjectName(defaultPath));
77     dialog->setSuffixes(headerSuffix(), sourceSuffix(), formSuffix());
78     return dialog;
79 }
80
81
82 Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w,
83                                                  QString *errorMessage) const
84 {
85     Q_UNUSED(errorMessage)
86     const LibraryWizardDialog *dialog = qobject_cast<const LibraryWizardDialog *>(w);
87     const QtProjectParameters projectParams = dialog->parameters();
88     const QString projectPath = projectParams.projectPath();
89     const LibraryParameters params = dialog->libraryParameters();
90     const MobileLibraryParameters mobileParams = dialog->mobileLibraryParameters();
91
92     const QString sharedLibExportMacro = QtProjectParameters::exportMacro(projectParams.fileName);
93
94     Core::GeneratedFiles rc;
95     // Class header + source
96     const QString sourceFileName = buildFileName(projectPath, params.sourceFileName, sourceSuffix());
97     Core::GeneratedFile source(sourceFileName);
98     source.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
99
100     const QString headerFileFullName = buildFileName(projectPath, params.headerFileName, headerSuffix());
101     const QString headerFileName = QFileInfo(headerFileFullName).fileName();
102     Core::GeneratedFile header(headerFileFullName);
103
104     // Create files: global header for shared libs
105     QString globalHeaderFileName;
106     if (projectParams.type == QtProjectParameters::SharedLibrary) {
107         const QString globalHeaderName = buildFileName(projectPath, projectParams.fileName + QLatin1String(sharedHeaderPostfixC), headerSuffix());
108         Core::GeneratedFile globalHeader(globalHeaderName);
109         globalHeaderFileName = QFileInfo(globalHeader.path()).fileName();
110         globalHeader.setContents(CppTools::AbstractEditorSupport::licenseTemplate(globalHeaderFileName)
111                                  + LibraryParameters::generateSharedHeader(globalHeaderFileName, projectParams.fileName, sharedLibExportMacro));
112         rc.push_back(globalHeader);
113     }
114
115     // Generate code
116     QString headerContents, sourceContents;
117     params.generateCode(projectParams.type, projectParams.fileName,  headerFileName,
118                         globalHeaderFileName, sharedLibExportMacro,
119                         /* indentation*/ 4, &headerContents, &sourceContents);
120
121     source.setContents(CppTools::AbstractEditorSupport::licenseTemplate(sourceFileName, params.className)
122                        + sourceContents);
123     header.setContents(CppTools::AbstractEditorSupport::licenseTemplate(headerFileFullName, params.className)
124                        + headerContents);
125     rc.push_back(source);
126     rc.push_back(header);
127     // Create files: profile
128     const QString profileName = buildFileName(projectPath, projectParams.fileName, profileSuffix());
129     Core::GeneratedFile profile(profileName);
130     profile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
131     QString profileContents;
132     {
133         QTextStream proStr(&profileContents);
134         QtProjectParameters::writeProFileHeader(proStr);
135         projectParams.writeProFile(proStr);
136         proStr << "\nSOURCES += " << QFileInfo(source.path()).fileName()
137                << "\n\nHEADERS += " << headerFileName;
138         if (!globalHeaderFileName.isEmpty())
139             proStr << "\\\n        " << globalHeaderFileName << '\n';
140         if (mobileParams.type)
141             mobileParams.writeProFile(proStr);
142     }
143     profile.setContents(profileContents);
144     rc.push_back(profile);
145     return rc;
146 }
147
148 } // namespace Internal
149 } // namespace Qt4ProjectManager