OSDN Git Service

60c65be6301c5f725d0c43da2c6eb5e6c2c745f9
[qt-creator-jp/qt-creator-jp.git] / src / plugins / genericprojectmanager / genericmakestep.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 "genericmakestep.h"
35 #include "genericprojectconstants.h"
36 #include "genericproject.h"
37 #include "generictarget.h"
38 #include "ui_genericmakestep.h"
39 #include "genericbuildconfiguration.h"
40
41 #include <extensionsystem/pluginmanager.h>
42 #include <projectexplorer/buildsteplist.h>
43 #include <projectexplorer/toolchain.h>
44 #include <projectexplorer/projectexplorer.h>
45 #include <projectexplorer/gnumakeparser.h>
46 #include <coreplugin/variablemanager.h>
47 #include <utils/stringutils.h>
48 #include <utils/qtcassert.h>
49 #include <utils/qtcprocess.h>
50
51 #include <QtGui/QFormLayout>
52 #include <QtGui/QGroupBox>
53 #include <QtGui/QCheckBox>
54 #include <QtGui/QLineEdit>
55 #include <QtGui/QListWidget>
56
57 using namespace GenericProjectManager;
58 using namespace GenericProjectManager::Internal;
59
60 namespace {
61 const char * const GENERIC_MS_ID("GenericProjectManager.GenericMakeStep");
62 const char * const GENERIC_MS_DISPLAY_NAME(QT_TRANSLATE_NOOP("GenericProjectManager::Internal::GenericMakeStep",
63                                                              "Make"));
64
65 const char * const BUILD_TARGETS_KEY("GenericProjectManager.GenericMakeStep.BuildTargets");
66 const char * const MAKE_ARGUMENTS_KEY("GenericProjectManager.GenericMakeStep.MakeArguments");
67 const char * const MAKE_COMMAND_KEY("GenericProjectManager.GenericMakeStep.MakeCommand");
68 }
69
70 GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent) :
71     AbstractProcessStep(parent, QLatin1String(GENERIC_MS_ID))
72 {
73     ctor();
74 }
75
76 GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent, const QString &id) :
77     AbstractProcessStep(parent, id)
78 {
79     ctor();
80 }
81
82 GenericMakeStep::GenericMakeStep(ProjectExplorer::BuildStepList *parent, GenericMakeStep *bs) :
83     AbstractProcessStep(parent, bs),
84     m_buildTargets(bs->m_buildTargets),
85     m_makeArguments(bs->m_makeArguments),
86     m_makeCommand(bs->m_makeCommand)
87 {
88     ctor();
89 }
90
91 void GenericMakeStep::ctor()
92 {
93     setDefaultDisplayName(QCoreApplication::translate("GenericProjectManager::Internal::GenericMakeStep",
94                                                       GENERIC_MS_DISPLAY_NAME));
95 }
96
97 GenericMakeStep::~GenericMakeStep()
98 {
99 }
100
101 GenericBuildConfiguration *GenericMakeStep::genericBuildConfiguration() const
102 {
103     return static_cast<GenericBuildConfiguration *>(buildConfiguration());
104 }
105
106 bool GenericMakeStep::init()
107 {
108     GenericBuildConfiguration *bc = genericBuildConfiguration();
109
110     setEnabled(true);
111     ProjectExplorer::ProcessParameters *pp = processParameters();
112     pp->setMacroExpander(bc->macroExpander());
113     pp->setWorkingDirectory(bc->buildDirectory());
114     pp->setEnvironment(bc->environment());
115     pp->setCommand(makeCommand());
116     pp->setArguments(allArguments());
117
118     setOutputParser(new ProjectExplorer::GnuMakeParser());
119     if (bc->genericTarget()->genericProject()->toolChain())
120         appendOutputParser(bc->genericTarget()->genericProject()->toolChain()->outputParser());
121     outputParser()->setWorkingDirectory(pp->effectiveWorkingDirectory());
122
123     return AbstractProcessStep::init();
124 }
125
126 QVariantMap GenericMakeStep::toMap() const
127 {
128     QVariantMap map(AbstractProcessStep::toMap());
129
130     map.insert(QLatin1String(BUILD_TARGETS_KEY), m_buildTargets);
131     map.insert(QLatin1String(MAKE_ARGUMENTS_KEY), m_makeArguments);
132     map.insert(QLatin1String(MAKE_COMMAND_KEY), m_makeCommand);
133     return map;
134 }
135
136 bool GenericMakeStep::fromMap(const QVariantMap &map)
137 {
138     m_buildTargets = map.value(QLatin1String(BUILD_TARGETS_KEY)).toStringList();
139     m_makeArguments = map.value(QLatin1String(MAKE_ARGUMENTS_KEY)).toString();
140     m_makeCommand = map.value(QLatin1String(MAKE_COMMAND_KEY)).toString();
141
142     return BuildStep::fromMap(map);
143 }
144
145 QString GenericMakeStep::allArguments() const
146 {
147     QString args = m_makeArguments;
148     Utils::QtcProcess::addArgs(&args, m_buildTargets);
149     return args;
150 }
151
152 QString GenericMakeStep::makeCommand() const
153 {
154     QString command = m_makeCommand;
155     if (command.isEmpty()) {
156         GenericProject *pro = genericBuildConfiguration()->genericTarget()->genericProject();
157         if (ProjectExplorer::ToolChain *toolChain = pro->toolChain())
158             command = toolChain->makeCommand();
159         else
160             command = QLatin1String("make");
161     }
162     return command;
163 }
164
165 void GenericMakeStep::run(QFutureInterface<bool> &fi)
166 {
167     AbstractProcessStep::run(fi);
168 }
169
170 ProjectExplorer::BuildStepConfigWidget *GenericMakeStep::createConfigWidget()
171 {
172     return new GenericMakeStepConfigWidget(this);
173 }
174
175 bool GenericMakeStep::immutable() const
176 {
177     return false;
178 }
179
180 bool GenericMakeStep::buildsTarget(const QString &target) const
181 {
182     return m_buildTargets.contains(target);
183 }
184
185 void GenericMakeStep::setBuildTarget(const QString &target, bool on)
186 {
187     QStringList old = m_buildTargets;
188     if (on && !old.contains(target))
189          old << target;
190     else if(!on && old.contains(target))
191         old.removeOne(target);
192
193     m_buildTargets = old;
194 }
195
196 //
197 // GenericMakeStepConfigWidget
198 //
199
200 GenericMakeStepConfigWidget::GenericMakeStepConfigWidget(GenericMakeStep *makeStep)
201     : m_makeStep(makeStep)
202 {
203     m_ui = new Ui::GenericMakeStep;
204     m_ui->setupUi(this);
205
206     // TODO update this list also on rescans of the GenericLists.txt
207     GenericProject *pro = m_makeStep->genericBuildConfiguration()->genericTarget()->genericProject();
208     foreach (const QString &target, pro->buildTargets()) {
209         QListWidgetItem *item = new QListWidgetItem(target, m_ui->targetsList);
210         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
211         item->setCheckState(Qt::Unchecked);
212     }
213
214     connect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)),
215             this, SLOT(itemChanged(QListWidgetItem*)));
216     connect(m_ui->makeLineEdit, SIGNAL(textEdited(QString)),
217             this, SLOT(makeLineEditTextEdited()));
218     connect(m_ui->makeArgumentsLineEdit, SIGNAL(textEdited(QString)),
219             this, SLOT(makeArgumentsLineEditTextEdited()));
220
221     connect(ProjectExplorer::ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
222             this, SLOT(updateMakeOverrrideLabel()));
223     connect(ProjectExplorer::ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
224             this, SLOT(updateDetails()));
225 }
226
227 QString GenericMakeStepConfigWidget::displayName() const
228 {
229     return tr("Make", "GenericMakestep display name.");
230 }
231
232 // TODO: Label should update when tool chain is changed
233 void GenericMakeStepConfigWidget::updateMakeOverrrideLabel()
234 {
235     m_ui->makeLabel->setText(tr("Override %1:").arg(m_makeStep->makeCommand()));
236 }
237
238 void GenericMakeStepConfigWidget::init()
239 {
240     updateMakeOverrrideLabel();
241
242     m_ui->makeLineEdit->setText(m_makeStep->m_makeCommand);
243     m_ui->makeArgumentsLineEdit->setText(m_makeStep->m_makeArguments);
244
245     // Disconnect to make the changes to the items
246     disconnect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
247
248     int count = m_ui->targetsList->count();
249     for (int i = 0; i < count; ++i) {
250         QListWidgetItem *item = m_ui->targetsList->item(i);
251         item->setCheckState(m_makeStep->buildsTarget(item->text()) ? Qt::Checked : Qt::Unchecked);
252     }
253
254     updateDetails();
255     // and connect again
256     connect(m_ui->targetsList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
257 }
258
259 void GenericMakeStepConfigWidget::updateDetails()
260 {
261     GenericBuildConfiguration *bc = m_makeStep->genericBuildConfiguration();
262
263     ProjectExplorer::ProcessParameters param;
264     param.setMacroExpander(bc->macroExpander());
265     param.setWorkingDirectory(bc->buildDirectory());
266     param.setEnvironment(bc->environment());
267     param.setCommand(m_makeStep->makeCommand());
268     param.setArguments(m_makeStep->allArguments());
269     m_summaryText = param.summary(displayName());
270     emit updateSummary();
271 }
272
273 QString GenericMakeStepConfigWidget::summaryText() const
274 {
275     return m_summaryText;
276 }
277
278 void GenericMakeStepConfigWidget::itemChanged(QListWidgetItem *item)
279 {
280     m_makeStep->setBuildTarget(item->text(), item->checkState() & Qt::Checked);
281     updateDetails();
282 }
283
284 void GenericMakeStepConfigWidget::makeLineEditTextEdited()
285 {
286     m_makeStep->m_makeCommand = m_ui->makeLineEdit->text();
287     updateDetails();
288 }
289
290 void GenericMakeStepConfigWidget::makeArgumentsLineEditTextEdited()
291 {
292     m_makeStep->m_makeArguments = m_ui->makeArgumentsLineEdit->text();
293     updateDetails();
294 }
295
296 //
297 // GenericMakeStepFactory
298 //
299
300 GenericMakeStepFactory::GenericMakeStepFactory(QObject *parent) :
301     ProjectExplorer::IBuildStepFactory(parent)
302 {
303 }
304
305 GenericMakeStepFactory::~GenericMakeStepFactory()
306 {
307 }
308
309 bool GenericMakeStepFactory::canCreate(ProjectExplorer::BuildStepList *parent,
310                                        const QString &id) const
311 {
312     if (parent->target()->project()->id() != QLatin1String(Constants::GENERICPROJECT_ID))
313         return false;
314     return id == QLatin1String(GENERIC_MS_ID);
315 }
316
317 ProjectExplorer::BuildStep *GenericMakeStepFactory::create(ProjectExplorer::BuildStepList *parent,
318                                                            const QString &id)
319 {
320     if (!canCreate(parent, id))
321         return 0;
322     return new GenericMakeStep(parent);
323 }
324
325 bool GenericMakeStepFactory::canClone(ProjectExplorer::BuildStepList *parent,
326                                       ProjectExplorer::BuildStep *source) const
327 {
328     const QString id(source->id());
329     return canCreate(parent, id);
330 }
331
332 ProjectExplorer::BuildStep *GenericMakeStepFactory::clone(ProjectExplorer::BuildStepList *parent,
333                                                           ProjectExplorer::BuildStep *source)
334 {
335     if (!canClone(parent, source))
336         return 0;
337     GenericMakeStep *old(qobject_cast<GenericMakeStep *>(source));
338     Q_ASSERT(old);
339     return new GenericMakeStep(parent, old);
340 }
341
342 bool GenericMakeStepFactory::canRestore(ProjectExplorer::BuildStepList *parent,
343                                         const QVariantMap &map) const
344 {
345     QString id(ProjectExplorer::idFromMap(map));
346     return canCreate(parent, id);
347 }
348
349 ProjectExplorer::BuildStep *GenericMakeStepFactory::restore(ProjectExplorer::BuildStepList *parent,
350                                                             const QVariantMap &map)
351 {
352     if (!canRestore(parent, map))
353         return 0;
354     GenericMakeStep *bs(new GenericMakeStep(parent));
355     if (bs->fromMap(map))
356         return bs;
357     delete bs;
358     return 0;
359 }
360
361 QStringList GenericMakeStepFactory::availableCreationIds(ProjectExplorer::BuildStepList *parent) const
362 {
363     if (parent->target()->project()->id() != QLatin1String(Constants::GENERICPROJECT_ID))
364         return QStringList();
365     return QStringList() << QLatin1String(GENERIC_MS_ID);
366 }
367
368 QString GenericMakeStepFactory::displayNameForId(const QString &id) const
369 {
370     if (id == QLatin1String(GENERIC_MS_ID))
371         return QCoreApplication::translate("GenericProjectManager::Internal::GenericMakeStep",
372                                            GENERIC_MS_DISPLAY_NAME);
373     return QString();
374 }