1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "qtwizard.h"
27
28 #include <qmakeprojectmanager/qmakeproject.h>
29 #include <qmakeprojectmanager/qmakeprojectmanagerconstants.h>
30
31 #include <coreplugin/icore.h>
32
33 #include <cpptools/cpptoolsconstants.h>
34
35 #include <projectexplorer/kitinformation.h>
36 #include <projectexplorer/projectexplorer.h>
37 #include <projectexplorer/projectexplorerconstants.h>
38 #include <projectexplorer/targetsetuppage.h>
39 #include <projectexplorer/task.h>
40
41 #include <qtsupport/qtkitinformation.h>
42 #include <qtsupport/qtsupportconstants.h>
43
44 #include <utils/algorithm.h>
45
46 #include <QCoreApplication>
47 #include <QVariant>
48
49 using namespace ProjectExplorer;
50 using namespace QtSupport;
51
52 namespace QmakeProjectManager {
53 namespace Internal {
54
55 // -------------------- QtWizard
QtWizard()56 QtWizard::QtWizard()
57 {
58 setSupportedProjectTypes({Constants::QMAKEPROJECT_ID});
59 }
60
sourceSuffix()61 QString QtWizard::sourceSuffix()
62 {
63 return preferredSuffix(QLatin1String(ProjectExplorer::Constants::CPP_SOURCE_MIMETYPE));
64 }
65
headerSuffix()66 QString QtWizard::headerSuffix()
67 {
68 return preferredSuffix(QLatin1String(ProjectExplorer::Constants::CPP_HEADER_MIMETYPE));
69 }
70
formSuffix()71 QString QtWizard::formSuffix()
72 {
73 return preferredSuffix(QLatin1String(ProjectExplorer::Constants::FORM_MIMETYPE));
74 }
75
profileSuffix()76 QString QtWizard::profileSuffix()
77 {
78 return preferredSuffix(QLatin1String(Constants::PROFILE_MIMETYPE));
79 }
80
postGenerateFiles(const QWizard * w,const Core::GeneratedFiles & l,QString * errorMessage) const81 bool QtWizard::postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l, QString *errorMessage) const
82 {
83 return QtWizard::qt4ProjectPostGenerateFiles(w, l, errorMessage);
84 }
85
qt4ProjectPostGenerateFiles(const QWizard * w,const Core::GeneratedFiles & generatedFiles,QString * errorMessage)86 bool QtWizard::qt4ProjectPostGenerateFiles(const QWizard *w,
87 const Core::GeneratedFiles &generatedFiles,
88 QString *errorMessage)
89 {
90 const auto *dialog = qobject_cast<const BaseQmakeProjectWizardDialog *>(w);
91
92 // Generate user settings
93 for (const Core::GeneratedFile &file : generatedFiles)
94 if (file.attributes() & Core::GeneratedFile::OpenProjectAttribute) {
95 dialog->writeUserFile(file.path());
96 break;
97 }
98
99 // Post-Generate: Open the projects/editors
100 return ProjectExplorer::CustomProjectWizard::postGenerateOpen(generatedFiles ,errorMessage);
101 }
102
templateDir()103 QString QtWizard::templateDir()
104 {
105 return Core::ICore::resourcePath("templates/qt4project").toString();
106 }
107
lowerCaseFiles()108 bool QtWizard::lowerCaseFiles()
109 {
110 QString lowerCaseSettingsKey = QLatin1String(CppTools::Constants::CPPTOOLS_SETTINGSGROUP);
111 lowerCaseSettingsKey += QLatin1Char('/');
112 lowerCaseSettingsKey += QLatin1String(CppTools::Constants::LOWERCASE_CPPFILES_KEY);
113 const bool lowerCaseDefault = CppTools::Constants::LOWERCASE_CPPFILES_DEFAULT;
114 return Core::ICore::settings()->value(lowerCaseSettingsKey, QVariant(lowerCaseDefault)).toBool();
115 }
116
117 // ------------ CustomQmakeProjectWizard
118 CustomQmakeProjectWizard::CustomQmakeProjectWizard() = default;
119
create(QWidget * parent,const Core::WizardDialogParameters & parameters) const120 Core::BaseFileWizard *CustomQmakeProjectWizard::create(QWidget *parent,
121 const Core::WizardDialogParameters ¶meters) const
122 {
123 auto *wizard = new BaseQmakeProjectWizardDialog(this, parent, parameters);
124
125 if (!parameters.extraValues().contains(QLatin1String(ProjectExplorer::Constants::PROJECT_KIT_IDS)))
126 wizard->addTargetSetupPage(targetPageId);
127
128 initProjectWizardDialog(wizard, parameters.defaultPath(), wizard->extensionPages());
129 return wizard;
130 }
131
postGenerateFiles(const QWizard * w,const Core::GeneratedFiles & l,QString * errorMessage) const132 bool CustomQmakeProjectWizard::postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l,
133 QString *errorMessage) const
134 {
135 return QtWizard::qt4ProjectPostGenerateFiles(w, l, errorMessage);
136 }
137
138 // ----------------- BaseQmakeProjectWizardDialog
BaseQmakeProjectWizardDialog(const Core::BaseFileWizardFactory * factory,QWidget * parent,const Core::WizardDialogParameters & parameters)139 BaseQmakeProjectWizardDialog::BaseQmakeProjectWizardDialog(
140 const Core::BaseFileWizardFactory *factory,
141 QWidget *parent,
142 const Core::WizardDialogParameters ¶meters)
143 : ProjectExplorer::BaseProjectWizardDialog(factory, parent, parameters)
144 {
145 m_profileIds = Utils::transform(parameters.extraValues()
146 .value(ProjectExplorer::Constants::PROJECT_KIT_IDS)
147 .toStringList(),
148 &Utils::Id::fromString);
149
150 connect(this, &BaseProjectWizardDialog::projectParametersChanged,
151 this, &BaseQmakeProjectWizardDialog::generateProfileName);
152 }
153
BaseQmakeProjectWizardDialog(const Core::BaseFileWizardFactory * factory,Utils::ProjectIntroPage * introPage,int introId,QWidget * parent,const Core::WizardDialogParameters & parameters)154 BaseQmakeProjectWizardDialog::BaseQmakeProjectWizardDialog(
155 const Core::BaseFileWizardFactory *factory,
156 Utils::ProjectIntroPage *introPage,
157 int introId,
158 QWidget *parent,
159 const Core::WizardDialogParameters ¶meters)
160 : ProjectExplorer::BaseProjectWizardDialog(factory, introPage, introId, parent, parameters)
161 {
162 m_profileIds = Utils::transform(parameters.extraValues()
163 .value(ProjectExplorer::Constants::PROJECT_KIT_IDS)
164 .toStringList(),
165 &Utils::Id::fromString);
166 connect(this, &BaseProjectWizardDialog::projectParametersChanged,
167 this, &BaseQmakeProjectWizardDialog::generateProfileName);
168 }
169
~BaseQmakeProjectWizardDialog()170 BaseQmakeProjectWizardDialog::~BaseQmakeProjectWizardDialog()
171 {
172 if (m_targetSetupPage && !m_targetSetupPage->parent())
173 delete m_targetSetupPage;
174 }
175
addTargetSetupPage(int id)176 int BaseQmakeProjectWizardDialog::addTargetSetupPage(int id)
177 {
178 m_targetSetupPage = new ProjectExplorer::TargetSetupPage;
179
180 m_targetSetupPage->setTasksGenerator([this](const Kit *k) -> Tasks {
181 if (!QtKitAspect::qtVersionPredicate(requiredFeatures())(k))
182 return {
183 ProjectExplorer::CompileTask(Task::Error, tr("Required Qt features not present."))};
184
185 const Utils::Id platform = selectedPlatform();
186 if (platform.isValid() && !QtKitAspect::platformPredicate(platform)(k))
187 return {ProjectExplorer::CompileTask(
188 ProjectExplorer::Task::Warning,
189 tr("Qt version does not target the expected platform."))};
190 QSet<Utils::Id> features = {QtSupport::Constants::FEATURE_DESKTOP};
191 if (!QtKitAspect::qtVersionPredicate(features)(k))
192 return {ProjectExplorer::CompileTask(ProjectExplorer::Task::Unknown,
193 tr("Qt version does not provide all features."))};
194 return {};
195 });
196
197 resize(900, 450);
198 if (id >= 0)
199 setPage(id, m_targetSetupPage);
200 else
201 id = addPage(m_targetSetupPage);
202
203 return id;
204 }
205
writeUserFile(const QString & proFileName) const206 bool BaseQmakeProjectWizardDialog::writeUserFile(const QString &proFileName) const
207 {
208 if (!m_targetSetupPage)
209 return false;
210
211 QmakeProject *pro = new QmakeProject(Utils::FilePath::fromString(proFileName));
212 bool success = m_targetSetupPage->setupProject(pro);
213 if (success)
214 pro->saveSettings();
215 delete pro;
216 return success;
217 }
218
selectedKits() const219 QList<Utils::Id> BaseQmakeProjectWizardDialog::selectedKits() const
220 {
221 if (!m_targetSetupPage)
222 return m_profileIds;
223 return m_targetSetupPage->selectedKits();
224 }
225
generateProfileName(const QString & name,const QString & path)226 void BaseQmakeProjectWizardDialog::generateProfileName(const QString &name, const QString &path)
227 {
228 if (!m_targetSetupPage)
229 return;
230
231 const QString proFile = QDir::cleanPath(path + '/' + name + '/' + name + ".pro");
232
233 m_targetSetupPage->setProjectPath(Utils::FilePath::fromString(proFile));
234 }
235
236 } // Internal
237 } // QmakeProjectManager
238