buildstepspage.cpp 10.1 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
26
** contact the sales department at http://www.qtsoftware.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "buildstepspage.h"
hjk's avatar
hjk committed
31

con's avatar
con committed
32
33
34
#include "ui_buildstepspage.h"
#include "project.h"

35
#include <coreplugin/coreconstants.h>
con's avatar
con committed
36
#include <extensionsystem/pluginmanager.h>
hjk's avatar
hjk committed
37
#include <utils/qtcassert.h>
con's avatar
con committed
38
39
40
41
42
43
44
45
46
47
48
49

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

BuildStepsPage::BuildStepsPage(Project *project) :
    BuildStepConfigWidget(),
    m_ui(new Ui::BuildStepsPage),
    m_pro(project)
{
    m_ui->setupUi(this);

    m_ui->buildStepAddButton->setMenu(new QMenu(this));
50
51
    m_ui->buildStepAddButton->setIcon(QIcon(Core::Constants::ICON_PLUS));
    m_ui->buildStepRemoveToolButton->setIcon(QIcon(Core::Constants::ICON_MINUS));
con's avatar
con committed
52
53
54
55
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
88
89
90
91
92
93
94
    m_ui->buildStepUpToolButton->setArrowType(Qt::UpArrow);
    m_ui->buildStepDownToolButton->setArrowType(Qt::DownArrow);

    connect(m_ui->buildSettingsList, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
            this, SLOT(updateBuildStepWidget(QTreeWidgetItem *, QTreeWidgetItem *)));

    connect(m_ui->buildStepAddButton->menu(), SIGNAL(aboutToShow()),
            this, SLOT(updateAddBuildStepMenu()));

    connect(m_ui->buildStepAddButton, SIGNAL(clicked()),
            this, SLOT(addBuildStep()));
    connect(m_ui->buildStepRemoveToolButton, SIGNAL(clicked()),
            this, SLOT(removeBuildStep()));
    connect(m_ui->buildStepUpToolButton, SIGNAL(clicked()),
            this, SLOT(upBuildStep()));
    connect(m_ui->buildStepDownToolButton, SIGNAL(clicked()),
            this, SLOT(downBuildStep()));

    // Remove dummy pages
    while (QWidget *widget = m_ui->buildSettingsWidget->currentWidget()) {
        m_ui->buildSettingsWidget->removeWidget(widget);
        delete widget;
    }

    // Add buildsteps
    foreach (BuildStep *bs, m_pro->buildSteps()) {

        connect(bs, SIGNAL(displayNameChanged(BuildStep *, QString)),
                this, SLOT(displayNameChanged(BuildStep *,QString)));

        QTreeWidgetItem *buildStepItem = new QTreeWidgetItem();
        buildStepItem->setText(0, bs->displayName());
        m_ui->buildSettingsWidget->addWidget(bs->createConfigWidget());
        m_ui->buildSettingsList->invisibleRootItem()->addChild(buildStepItem);
    }
}

BuildStepsPage::~BuildStepsPage()
{
    // Also deletes all added widgets
    delete m_ui;
}

Friedemann Kleint's avatar
Friedemann Kleint committed
95
void BuildStepsPage::displayNameChanged(BuildStep *bs, const QString & /* displayName */)
con's avatar
con committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
{
    int index = m_pro->buildSteps().indexOf(bs);
    m_ui->buildSettingsList->invisibleRootItem()->child(index)->setText(0, bs->displayName());
}

QString BuildStepsPage::displayName() const
{
    return tr("Build Steps");
}

void BuildStepsPage::init(const QString &buildConfiguration)
{
    m_configuration = buildConfiguration;

    m_ui->buildSettingsList->setCurrentItem(m_ui->buildSettingsList->invisibleRootItem()->child(0));
    // make sure widget is updated
112
113
114
115
    if (m_ui->buildSettingsWidget->currentWidget()) {
        BuildStepConfigWidget *widget = qobject_cast<BuildStepConfigWidget *>(m_ui->buildSettingsWidget->currentWidget());
        widget->init(m_configuration);
    }
con's avatar
con committed
116
117
118
119
120
}

/* switch from one tree item / build step to another */
void BuildStepsPage::updateBuildStepWidget(QTreeWidgetItem *newItem, QTreeWidgetItem *oldItem)
{
hjk's avatar
hjk committed
121
    if (oldItem == newItem)
con's avatar
con committed
122
        return;
dt's avatar
dt committed
123
    Q_ASSERT(m_pro);
con's avatar
con committed
124

hjk's avatar
hjk committed
125
    if (newItem) {
con's avatar
con committed
126
127
128
        int row = m_ui->buildSettingsList->indexOfTopLevelItem(newItem);
        m_ui->buildSettingsWidget->setCurrentIndex(row);
        BuildStepConfigWidget *widget = qobject_cast<BuildStepConfigWidget *>(m_ui->buildSettingsWidget->currentWidget());
dt's avatar
dt committed
129
        Q_ASSERT(widget);
hjk's avatar
hjk committed
130
        widget->init(m_configuration);
con's avatar
con committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    }
    updateBuildStepButtonsState();
}


void BuildStepsPage::updateAddBuildStepMenu()
{
    QMap<QString, QPair<QString, IBuildStepFactory *> > map;
    //Build up a list of possible steps and save map the display names to the (internal) name and factories.
    QList<IBuildStepFactory *> factories = ExtensionSystem::PluginManager::instance()->getObjects<IBuildStepFactory>();
    foreach (IBuildStepFactory * factory, factories) {
        QStringList names = factory->canCreateForProject(m_pro);
        foreach (const QString &name, names) {
            map.insert(factory->displayNameForName(name), QPair<QString, IBuildStepFactory *>(name, factory));
        }
    }

    // Ask the user which one to add
    QMenu *menu = m_ui->buildStepAddButton->menu();
    m_addBuildStepHash.clear();
    menu->clear();
hjk's avatar
hjk committed
152
    if (!map.isEmpty()) {
con's avatar
con committed
153
154
155
        QStringList names;
        QMap<QString, QPair<QString, IBuildStepFactory *> >::const_iterator it, end;
        end = map.constEnd();
hjk's avatar
hjk committed
156
        for (it = map.constBegin(); it != end; ++it) {
con's avatar
con committed
157
158
159
160
161
162
163
164
165
166
167
            QAction *action = menu->addAction(it.key());
            connect(action, SIGNAL(triggered()),
                    this, SLOT(addBuildStep()));
            m_addBuildStepHash.insert(action, it.value());
        }
    }
}


void BuildStepsPage::addBuildStep()
{
hjk's avatar
hjk committed
168
    if (QAction *action = qobject_cast<QAction *>(sender())) {
con's avatar
con committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
        QPair<QString, IBuildStepFactory *> pair = m_addBuildStepHash.value(action);
        BuildStep *newStep = pair.second->create(m_pro, pair.first);
        m_pro->insertBuildStep(0, newStep);
        QTreeWidgetItem *buildStepItem = new QTreeWidgetItem();
        buildStepItem->setText(0, newStep->displayName());
        m_ui->buildSettingsList->invisibleRootItem()->insertChild(0, buildStepItem);
        m_ui->buildSettingsWidget->insertWidget(0, newStep->createConfigWidget());
        m_ui->buildSettingsList->setCurrentItem(buildStepItem);
    }
}

void BuildStepsPage::removeBuildStep()
{
    int pos = m_ui->buildSettingsList->currentIndex().row();
hjk's avatar
hjk committed
183
    if (m_pro->buildSteps().at(pos)->immutable())
con's avatar
con committed
184
185
186
187
188
189
190
        return;
    bool blockSignals = m_ui->buildSettingsList->blockSignals(true);
    delete m_ui->buildSettingsList->invisibleRootItem()->takeChild(pos);
    m_ui->buildSettingsList->blockSignals(blockSignals);
    QWidget *widget = m_ui->buildSettingsWidget->widget(pos);
    m_ui->buildSettingsWidget->removeWidget(widget);
    delete widget;
hjk's avatar
hjk committed
191
    if (pos < m_ui->buildSettingsList->invisibleRootItem()->childCount())
con's avatar
con committed
192
193
194
195
196
197
198
199
200
201
        m_ui->buildSettingsList->setCurrentItem(m_ui->buildSettingsList->invisibleRootItem()->child(pos));
    else
        m_ui->buildSettingsList->setCurrentItem(m_ui->buildSettingsList->invisibleRootItem()->child(pos - 1));
    m_pro->removeBuildStep(pos);
    updateBuildStepButtonsState();
}

void BuildStepsPage::upBuildStep()
{
    int pos = m_ui->buildSettingsList->currentIndex().row();
hjk's avatar
hjk committed
202
    if (pos < 1)
con's avatar
con committed
203
        return;
hjk's avatar
hjk committed
204
    if (pos > m_ui->buildSettingsList->invisibleRootItem()->childCount()-1)
con's avatar
con committed
205
        return;
hjk's avatar
hjk committed
206
    if (m_pro->buildSteps().at(pos)->immutable() && m_pro->buildSteps().at(pos-1)->immutable())
con's avatar
con committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
        return;

    bool blockSignals = m_ui->buildSettingsList->blockSignals(true);
    m_pro->moveBuildStepUp(pos);
    buildStepMoveUp(pos);
    QTreeWidgetItem *item = m_ui->buildSettingsList->invisibleRootItem()->child(pos - 1);
    m_ui->buildSettingsList->blockSignals(blockSignals);
    m_ui->buildSettingsList->setCurrentItem(item);
    updateBuildStepButtonsState();
}

void BuildStepsPage::downBuildStep()
{
    int pos = m_ui->buildSettingsList->currentIndex().row() + 1;
hjk's avatar
hjk committed
221
    if (pos < 1)
con's avatar
con committed
222
        return;
hjk's avatar
hjk committed
223
    if (pos > m_ui->buildSettingsList->invisibleRootItem()->childCount() - 1)
con's avatar
con committed
224
        return;
hjk's avatar
hjk committed
225
    if (m_pro->buildSteps().at(pos)->immutable() && m_pro->buildSteps().at(pos - 1)->immutable())
con's avatar
con committed
226
227
228
229
230
231
232
233
234
235
236
237
238
        return;

    bool blockSignals = m_ui->buildSettingsList->blockSignals(true);
    m_pro->moveBuildStepUp(pos);
    buildStepMoveUp(pos);
    QTreeWidgetItem *item = m_ui->buildSettingsList->invisibleRootItem()->child(pos);
    m_ui->buildSettingsList->blockSignals(blockSignals);
    m_ui->buildSettingsList->setCurrentItem(item);
    updateBuildStepButtonsState();
}

void BuildStepsPage::changeEvent(QEvent *e)
{
239
    BuildStepConfigWidget::changeEvent(e);
hjk's avatar
hjk committed
240
    switch (e->type()) {
con's avatar
con committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void BuildStepsPage::buildStepMoveUp(int pos)
{
    QWidget *widget = m_ui->buildSettingsWidget->widget(pos);
    m_ui->buildSettingsWidget->removeWidget(widget);
    m_ui->buildSettingsWidget->insertWidget(pos -1, widget);
    QTreeWidgetItem *item = m_ui->buildSettingsList->invisibleRootItem()->takeChild(pos);
    m_ui->buildSettingsList->invisibleRootItem()->insertChild(pos - 1, item);
}

void BuildStepsPage::updateBuildStepButtonsState()
{
    int pos = m_ui->buildSettingsList->currentIndex().row();

    m_ui->buildStepRemoveToolButton->setEnabled(!m_pro->buildSteps().at(pos)->immutable());
    bool enableUp = pos>0 && !(m_pro->buildSteps().at(pos)->immutable() && m_pro->buildSteps().at(pos-1)->immutable());
    m_ui->buildStepUpToolButton->setEnabled(enableUp);
    bool enableDown = pos < (m_ui->buildSettingsList->invisibleRootItem()->childCount() - 1) &&
                      !(m_pro->buildSteps().at(pos)->immutable() && m_pro->buildSteps().at(pos+1)->immutable());
    m_ui->buildStepDownToolButton->setEnabled(enableDown);
}