buildsettingspropertiespage.cpp 11.4 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
#include "buildsettingspropertiespage.h"
#include "buildstep.h"
#include "buildstepspage.h"
#include "project.h"
34
#include "buildconfiguration.h"
con's avatar
con committed
35

36
#include <coreplugin/coreconstants.h>
con's avatar
con committed
37
38
39
40
41
42
#include <extensionsystem/pluginmanager.h>

#include <QtCore/QDebug>
#include <QtCore/QPair>
#include <QtGui/QInputDialog>
#include <QtGui/QLabel>
dt's avatar
dt committed
43
#include <QtGui/QVBoxLayout>
44
#include <QtGui/QMenu>
con's avatar
con committed
45
46
47
48
49
50
51
52

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

///
/// BuildSettingsPanelFactory
///

53
bool BuildSettingsPanelFactory::supports(Project *project)
con's avatar
con committed
54
{
55
    return project->hasBuildSettings();
con's avatar
con committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
}

PropertiesPanel *BuildSettingsPanelFactory::createPanel(Project *project)
{
    return new BuildSettingsPanel(project);
}

///
/// BuildSettingsPanel
///

BuildSettingsPanel::BuildSettingsPanel(Project *project)
        : PropertiesPanel(),
          m_widget(new BuildSettingsWidget(project))
{
}

BuildSettingsPanel::~BuildSettingsPanel()
{
    delete m_widget;
}

QString BuildSettingsPanel::name() const
{
    return tr("Build Settings");
}

QWidget *BuildSettingsPanel::widget()
{
    return m_widget;
}

dt's avatar
dt committed
88
89
90
91
92
93
94
95
96
97
98
///
// BuildSettingsSubWidgets
///

BuildSettingsSubWidgets::~BuildSettingsSubWidgets()
{
    clear();
}

void BuildSettingsSubWidgets::addWidget(const QString &name, QWidget *widget)
{
dt's avatar
dt committed
99
100
    QSpacerItem *item = new QSpacerItem(1, 10, QSizePolicy::Fixed, QSizePolicy::Fixed);

dt's avatar
dt committed
101
102
103
104
105
106
107
    QLabel *label = new QLabel(this);
    label->setText(name);
    QFont f = label->font();
    f.setBold(true);
    f.setPointSizeF(f.pointSizeF() *1.2);
    label->setFont(f);

dt's avatar
dt committed
108
    layout()->addItem(item);
dt's avatar
dt committed
109
110
111
112
113
114
115
116
117
    layout()->addWidget(label);
    layout()->addWidget(widget);

    m_labels.append(label);
    m_widgets.append(widget);
}

void BuildSettingsSubWidgets::clear()
{
dt's avatar
dt committed
118
119
120
    foreach(QSpacerItem *item, m_spacerItems)
        layout()->removeItem(item);
    qDeleteAll(m_spacerItems);
dt's avatar
dt committed
121
122
    qDeleteAll(m_widgets);
    qDeleteAll(m_labels);
123
124
    m_widgets.clear();
    m_labels.clear();
dt's avatar
dt committed
125
126
127
128
129
130
131
132
}

QList<QWidget *> BuildSettingsSubWidgets::widgets() const
{
    return m_widgets;
}

BuildSettingsSubWidgets::BuildSettingsSubWidgets(QWidget *parent)
dt's avatar
dt committed
133
    : QWidget(parent)
dt's avatar
dt committed
134
135
{
    new QVBoxLayout(this);
136
    layout()->setMargin(0);
dt's avatar
dt committed
137
138
}

con's avatar
con committed
139
140
141
142
143
144
145
146
147
148
149
///
/// BuildSettingsWidget
///

BuildSettingsWidget::~BuildSettingsWidget()
{
}

BuildSettingsWidget::BuildSettingsWidget(Project *project)
    : m_project(project)
{
dt's avatar
dt committed
150
    QVBoxLayout *vbox = new QVBoxLayout(this);
con's avatar
con committed
151
    vbox->setContentsMargins(0, -1, 0, -1);
dt's avatar
dt committed
152
    QHBoxLayout *hbox = new QHBoxLayout();
dt's avatar
dt committed
153
    hbox->addWidget(new QLabel(tr("Edit Build Configuration:"), this));
dt's avatar
dt committed
154
    m_buildConfigurationComboBox = new QComboBox(this);
155
    m_buildConfigurationComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
dt's avatar
dt committed
156
157
158
    hbox->addWidget(m_buildConfigurationComboBox);

    m_addButton = new QPushButton(this);
159
    m_addButton->setText(tr("Add"));
dt's avatar
dt committed
160
161
162
163
    m_addButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    hbox->addWidget(m_addButton);

    m_removeButton = new QPushButton(this);
164
    m_removeButton->setText(tr("Remove"));
dt's avatar
dt committed
165
166
167
168
169
170
171
    m_removeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    hbox->addWidget(m_removeButton);
    hbox->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed));
    vbox->addLayout(hbox);

    m_subWidgets = new BuildSettingsSubWidgets(this);
    vbox->addWidget(m_subWidgets);
con's avatar
con committed
172

con's avatar
con committed
173
174
175
    m_addButtonMenu = new QMenu(this);
    m_addButton->setMenu(m_addButtonMenu);
    updateAddButtonMenu();
con's avatar
con committed
176

177
    m_buildConfiguration = m_project->activeBuildConfiguration()->name();
dt's avatar
dt committed
178

dt's avatar
dt committed
179
180
    connect(m_buildConfigurationComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(currentIndexChanged(int)));
con's avatar
con committed
181

dt's avatar
dt committed
182
    connect(m_removeButton, SIGNAL(clicked()),
con's avatar
con committed
183
            this, SLOT(deleteConfiguration()));
dt's avatar
dt committed
184

con's avatar
con committed
185
186
    connect(m_project, SIGNAL(buildConfigurationDisplayNameChanged(const QString &)),
            this, SLOT(buildConfigurationDisplayNameChanged(const QString &)));
con's avatar
con committed
187
188
    if (m_project->buildConfigurationFactory())
        connect(m_project->buildConfigurationFactory(), SIGNAL(availableCreationTypesChanged()), SLOT(updateAddButtonMenu()));
con's avatar
con committed
189
190
191
192

    updateBuildSettings();
}

con's avatar
con committed
193
void BuildSettingsWidget::updateAddButtonMenu()
194
{
con's avatar
con committed
195
196
    m_addButtonMenu->clear();
    m_addButtonMenu->addAction(tr("&Clone Selected"),
197
198
199
200
                             this, SLOT(cloneConfiguration()));
    IBuildConfigurationFactory *factory = m_project->buildConfigurationFactory();
    if (factory) {
        foreach (const QString &type, factory->availableCreationTypes()) {
con's avatar
con committed
201
            QAction *action = m_addButtonMenu->addAction(factory->displayNameForType(type), this, SLOT(createConfiguration()));
202
203
204
205
206
            action->setData(type);
        }
    }
}

con's avatar
con committed
207
208
void BuildSettingsWidget::buildConfigurationDisplayNameChanged(const QString &buildConfiguration)
{
dt's avatar
dt committed
209
210
    for (int i=0; i<m_buildConfigurationComboBox->count(); ++i) {
        if (m_buildConfigurationComboBox->itemData(i).toString() == buildConfiguration) {
211
            m_buildConfigurationComboBox->setItemText(i, m_project->buildConfiguration(buildConfiguration)->displayName());
dt's avatar
dt committed
212
            break;
con's avatar
con committed
213
214
215
216
217
218
219
        }
    }
}


void BuildSettingsWidget::updateBuildSettings()
{
dt's avatar
dt committed
220
    // TODO save position, entry from combbox
con's avatar
con committed
221
222

    // Delete old tree items
223
    bool blocked = m_buildConfigurationComboBox->blockSignals(true);
dt's avatar
dt committed
224
225
    m_buildConfigurationComboBox->clear();
    m_subWidgets->clear();
con's avatar
con committed
226

dt's avatar
dt committed
227
228
    // update buttons
    m_removeButton->setEnabled(m_project->buildConfigurations().size() > 1);
con's avatar
con committed
229
230

    // Add pages
dt's avatar
dt committed
231
    BuildConfigWidget *generalConfigWidget = m_project->createConfigWidget();
dt's avatar
dt committed
232
233
234
235
    m_subWidgets->addWidget(generalConfigWidget->displayName(), generalConfigWidget);

    m_subWidgets->addWidget(tr("Build Steps"), new BuildStepsPage(m_project));
    m_subWidgets->addWidget(tr("Clean Steps"), new BuildStepsPage(m_project, true));
con's avatar
con committed
236

dt's avatar
dt committed
237
238
    QList<BuildConfigWidget *> subConfigWidgets = m_project->subConfigWidgets();
    foreach (BuildConfigWidget *subConfigWidget, subConfigWidgets)
dt's avatar
dt committed
239
        m_subWidgets->addWidget(subConfigWidget->displayName(), subConfigWidget);
con's avatar
con committed
240
241

    // Add tree items
242
243
244
    foreach (const BuildConfiguration *bc, m_project->buildConfigurations()) {
        m_buildConfigurationComboBox->addItem(bc->displayName(), bc->name());
        if (bc->name() == m_buildConfiguration)
dt's avatar
dt committed
245
            m_buildConfigurationComboBox->setCurrentIndex(m_buildConfigurationComboBox->count() - 1);
con's avatar
con committed
246
247
    }

248
    m_buildConfigurationComboBox->blockSignals(blocked);
con's avatar
con committed
249

dt's avatar
dt committed
250
251
252
    // TODO Restore position, entry from combbox
    // TODO? select entry from combobox ?
    activeBuildConfigurationChanged();
con's avatar
con committed
253
254
}

dt's avatar
dt committed
255
void BuildSettingsWidget::currentIndexChanged(int index)
con's avatar
con committed
256
{
dt's avatar
dt committed
257
258
    m_buildConfiguration = m_buildConfigurationComboBox->itemData(index).toString();
    activeBuildConfigurationChanged();
con's avatar
con committed
259
260
}

dt's avatar
dt committed
261
void BuildSettingsWidget::activeBuildConfigurationChanged()
con's avatar
con committed
262
{
dt's avatar
dt committed
263
    for (int i = 0; i < m_buildConfigurationComboBox->count(); ++i) {
dt's avatar
dt committed
264
        if (m_buildConfigurationComboBox->itemData(i).toString() == m_buildConfiguration) {
dt's avatar
dt committed
265
266
267
268
269
            m_buildConfigurationComboBox->setCurrentIndex(i);
            break;
        }
    }
    foreach (QWidget *widget, m_subWidgets->widgets()) {
dt's avatar
dt committed
270
        if (BuildConfigWidget *buildStepWidget = qobject_cast<BuildConfigWidget*>(widget)) {
dt's avatar
dt committed
271
            buildStepWidget->init(m_buildConfiguration);
con's avatar
con committed
272
273
274
275
276
277
        }
    }
}

void BuildSettingsWidget::createConfiguration()
{
278
279
    QAction *action = qobject_cast<QAction *>(sender());
    const QString &type = action->data().toString();
280
    if (m_project->buildConfigurationFactory()->create(type)) {
con's avatar
con committed
281
        // TODO switching to last buildconfiguration in list might not be what we want
282
        m_buildConfiguration = m_project->buildConfigurations().last()->name();
283
284
        updateBuildSettings();
    }
con's avatar
con committed
285
286
287
288
}

void BuildSettingsWidget::cloneConfiguration()
{
dt's avatar
dt committed
289
    const QString configuration = m_buildConfigurationComboBox->itemData(m_buildConfigurationComboBox->currentIndex()).toString();
con's avatar
con committed
290
291
292
293
294
    cloneConfiguration(configuration);
}

void BuildSettingsWidget::deleteConfiguration()
{
dt's avatar
dt committed
295
    const QString configuration = m_buildConfigurationComboBox->itemData(m_buildConfigurationComboBox->currentIndex()).toString();
con's avatar
con committed
296
297
298
299
300
    deleteConfiguration(configuration);
}

void BuildSettingsWidget::cloneConfiguration(const QString &sourceConfiguration)
{
hjk's avatar
hjk committed
301
    if (sourceConfiguration.isEmpty())
con's avatar
con committed
302
303
304
        return;

    QString newBuildConfiguration = QInputDialog::getText(this, tr("Clone configuration"), tr("New Configuration Name:"));
hjk's avatar
hjk committed
305
    if (newBuildConfiguration.isEmpty())
con's avatar
con committed
306
307
308
        return;

    QString newDisplayName = newBuildConfiguration;
309
310
311
312
313
314
315
316
317
318
319
320
    QStringList buildConfigurationDisplayNames;
    foreach(BuildConfiguration *bc, m_project->buildConfigurations())
        buildConfigurationDisplayNames << bc->displayName();
    newDisplayName = Project::makeUnique(newDisplayName, buildConfigurationDisplayNames);

    QStringList buildConfigurationNames;
    foreach(BuildConfiguration *bc, m_project->buildConfigurations())
        buildConfigurationNames << bc->name();

    newBuildConfiguration = Project::makeUnique(newBuildConfiguration, buildConfigurationNames);

    qDebug()<<"BuildSettingsWidget::cloneConfiguration source:"<<sourceConfiguration<<"to: "<<newBuildConfiguration;
con's avatar
con committed
321
322

    m_project->copyBuildConfiguration(sourceConfiguration, newBuildConfiguration);
323
    m_project->setDisplayNameFor(m_project->buildConfiguration(newBuildConfiguration), newDisplayName);
con's avatar
con committed
324

dt's avatar
dt committed
325
    m_buildConfiguration = newBuildConfiguration;
con's avatar
con committed
326
327
328
329
330
331
332
333
    updateBuildSettings();
}

void BuildSettingsWidget::deleteConfiguration(const QString &deleteConfiguration)
{
    if (deleteConfiguration.isEmpty() || m_project->buildConfigurations().size() <= 1)
        return;

334
335
336
337
    if (m_project->activeBuildConfiguration()->name() == deleteConfiguration) {
        foreach (BuildConfiguration *bc, m_project->buildConfigurations()) {
            if (bc->name() != deleteConfiguration) {
                m_project->setActiveBuildConfiguration(bc);
con's avatar
con committed
338
339
340
341
342
                break;
            }
        }
    }

dt's avatar
dt committed
343
    if (m_buildConfiguration == deleteConfiguration) {
344
345
346
        foreach (const BuildConfiguration *bc, m_project->buildConfigurations()) {
            if (bc->name() != deleteConfiguration) {
                m_buildConfiguration = bc->name();
dt's avatar
dt committed
347
348
349
350
351
                break;
            }
        }
    }

352
    m_project->removeBuildConfiguration(m_project->buildConfiguration(deleteConfiguration));
con's avatar
con committed
353
354
355

    updateBuildSettings();
}