runsettingspropertiespage.cpp 10.7 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
#include "runsettingspropertiespage.h"
#include "runconfiguration.h"
32
#include "project.h"
con's avatar
con committed
33 34 35

#include "ui_runsettingspropertiespage.h"

36
#include <coreplugin/coreconstants.h>
con's avatar
con committed
37
#include <extensionsystem/pluginmanager.h>
hjk's avatar
hjk committed
38
#include <utils/qtcassert.h>
con's avatar
con committed
39

hjk's avatar
hjk committed
40
#include <QtCore/QDebug>
con's avatar
con committed
41
#include <QtCore/QPair>
42
#include <QtGui/QMenu>
con's avatar
con committed
43 44 45

namespace ProjectExplorer {
namespace Internal {
hjk's avatar
hjk committed
46

con's avatar
con committed
47 48 49 50 51
struct FactoryAndType
{
    ProjectExplorer::IRunConfigurationFactory *factory;
    QString type;
};
hjk's avatar
hjk committed
52 53 54

} // namespace Internal
} // namespace ProjectExplorer
con's avatar
con committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

Q_DECLARE_METATYPE(ProjectExplorer::Internal::FactoryAndType);

namespace ProjectExplorer {
namespace Internal {

/*! A model to represent the run configurations of a project. */
class RunConfigurationsModel : public QAbstractListModel
{
public:
    RunConfigurationsModel(QObject *parent = 0)
        : QAbstractListModel(parent)
    {}

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

73 74
    void setRunConfigurations(const QList<RunConfiguration *> &runConfigurations);
    QList<RunConfiguration *> runConfigurations() const { return m_runConfigurations; }
con's avatar
con committed
75 76 77
    void nameChanged(RunConfiguration *rc);

private:
78
    QList<RunConfiguration *> m_runConfigurations;
con's avatar
con committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
};

} // namespace Internal
} // namespace ProjectExplorer

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;
using ExtensionSystem::PluginManager;

///
/// RunSettingsPanelFactory
///

bool RunSettingsPanelFactory::supports(Project * /* project */)
{
    return true;
}

97
IPropertiesPanel *RunSettingsPanelFactory::createPanel(Project *project)
con's avatar
con committed
98 99 100 101 102 103 104 105
{
    return new RunSettingsPanel(project);
}

///
/// RunSettingsPanel
///

106 107 108
RunSettingsPanel::RunSettingsPanel(Project *project) :
     m_widget(new RunSettingsWidget(project)),
     m_icon(":/projectexplorer/images/run.png")
con's avatar
con committed
109 110 111 112 113
{
}

RunSettingsPanel::~RunSettingsPanel()
{
114
    delete m_widget;
con's avatar
con committed
115 116 117 118
}

QString RunSettingsPanel::name() const
{
119
    return QApplication::tr("Run Settings");
con's avatar
con committed
120 121
}

122
QWidget *RunSettingsPanel::widget() const
con's avatar
con committed
123 124 125 126
{
    return m_widget;
}

127 128 129 130 131
QIcon RunSettingsPanel::icon() const
{
    return m_icon;
}

con's avatar
con committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
///
/// RunConfigurationsModel
///

int RunConfigurationsModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : m_runConfigurations.size();
}

int RunConfigurationsModel::columnCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : 1;
}

void RunConfigurationsModel::nameChanged(RunConfiguration *rc)
{
    for (int i = 0; i<m_runConfigurations.size(); ++i) {
149
        if (m_runConfigurations.at(i) == rc) {
con's avatar
con committed
150 151 152 153 154 155 156 157 158 159 160
            emit dataChanged(index(i, 0), index(i,0));
            break;
        }
    }
}

QVariant RunConfigurationsModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole) {
        const int row = index.row();
        if (row < m_runConfigurations.size()) {
161
            return m_runConfigurations.at(row)->name();
con's avatar
con committed
162 163 164 165 166 167
        }
    }

    return QVariant();
}

168
void RunConfigurationsModel::setRunConfigurations(const QList<RunConfiguration *> &runConfigurations)
con's avatar
con committed
169 170 171 172 173 174 175 176 177 178 179
{
    m_runConfigurations = runConfigurations;
    reset();
}


///
/// RunSettingsWidget
///

RunSettingsWidget::RunSettingsWidget(Project *project)
hjk's avatar
hjk committed
180 181 182
    : m_project(project),
      m_runConfigurationsModel(new RunConfigurationsModel(this)),
      m_runConfigurationWidget(0)
con's avatar
con committed
183 184 185 186 187
{
    m_ui = new Ui::RunSettingsPropertiesPage;
    m_ui->setupUi(this);
    m_addMenu = new QMenu(m_ui->addToolButton);
    m_ui->addToolButton->setMenu(m_addMenu);
dt's avatar
dt committed
188 189
    m_ui->addToolButton->setText(tr("Add"));
    m_ui->removeToolButton->setText(tr("Remove"));
con's avatar
con committed
190 191
    m_ui->runConfigurationCombo->setModel(m_runConfigurationsModel);

192 193 194 195
    m_makeActiveLabel = new QLabel(this);
    m_makeActiveLabel->setVisible(false);
    layout()->addWidget(m_makeActiveLabel);

con's avatar
con committed
196 197 198
    connect(m_addMenu, SIGNAL(aboutToShow()),
            this, SLOT(aboutToShowAddMenu()));
    connect(m_ui->runConfigurationCombo, SIGNAL(currentIndexChanged(int)),
199
            this, SLOT(showRunConfigurationWidget(int)));
con's avatar
con committed
200 201 202
    connect(m_ui->removeToolButton, SIGNAL(clicked(bool)),
            this, SLOT(removeRunConfiguration()));

dt's avatar
dt committed
203
    connect(m_project, SIGNAL(removedRunConfiguration(ProjectExplorer::Project *, QString)),
con's avatar
con committed
204
            this, SLOT(initRunConfigurationComboBox()));
dt's avatar
dt committed
205
    connect(m_project, SIGNAL(addedRunConfiguration(ProjectExplorer::Project *, QString)),
206
            this, SLOT(initRunConfigurationComboBox()));
207

208 209 210 211 212 213
    connect(m_project, SIGNAL(activeRunConfigurationChanged()),
            this, SLOT(updateMakeActiveLabel()));

    connect(m_makeActiveLabel, SIGNAL(linkActivated(QString)),
            this, SLOT(makeActive()));

con's avatar
con committed
214
    initRunConfigurationComboBox();
215
    const QList<RunConfiguration *> runConfigurations = m_project->runConfigurations();
con's avatar
con committed
216
    for (int i=0; i<runConfigurations.size(); ++i) {
217
        connect(runConfigurations.at(i), SIGNAL(nameChanged()),
con's avatar
con committed
218 219 220 221 222
                this, SLOT(nameChanged()));
    }

    // TODO: Add support for custom runner configuration widgets once we have some
    /*
223 224
    QList<IRunControlFactory *> runners = PluginManager::instance()->getObjects<IRunControlFactory>();
    foreach (IRunControlFactory * runner, runners) {
con's avatar
con committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238
        if (runner->canRun(activeRunConfiguration))
            m_ui->layout->addWidget(runner->configurationWidget(activeRunConfiguration));
    }
    */
}

RunSettingsWidget::~RunSettingsWidget()
{
    delete m_ui;
}

void RunSettingsWidget::aboutToShowAddMenu()
{
    m_addMenu->clear();
hjk's avatar
hjk committed
239 240 241
    QList<IRunConfigurationFactory *> factories =
        ExtensionSystem::PluginManager::instance()->getObjects<IRunConfigurationFactory>();
    foreach (IRunConfigurationFactory *factory, factories) {
con's avatar
con committed
242
        QStringList types = factory->availableCreationTypes(m_project);
con's avatar
con committed
243
        foreach (const QString &type, types) {
con's avatar
con committed
244
            QAction *action = m_addMenu->addAction(factory->displayNameForType(type));;
con's avatar
con committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
            FactoryAndType fat;
            fat.factory = factory;
            fat.type = type;
            QVariant v;
            v.setValue(fat);
            action->setData(v);
            connect(action, SIGNAL(triggered()),
                    this, SLOT(addRunConfiguration()));
        }
    }
}

void RunSettingsWidget::addRunConfiguration()
{
    QAction *act = qobject_cast<QAction *>(sender());
    if (!act)
        return;
    FactoryAndType fat = act->data().value<FactoryAndType>();
263
    RunConfiguration *newRC = fat.factory->create(m_project, fat.type);
con's avatar
con committed
264 265 266 267
    if (!newRC)
        return;
    m_project->addRunConfiguration(newRC);
    initRunConfigurationComboBox();
268 269
    m_ui->runConfigurationCombo->setCurrentIndex(
            m_runConfigurationsModel->runConfigurations().indexOf(newRC));
270
    connect(newRC, SIGNAL(nameChanged()), this, SLOT(nameChanged()));
con's avatar
con committed
271 272 273 274 275
}

void RunSettingsWidget::removeRunConfiguration()
{
    int index = m_ui->runConfigurationCombo->currentIndex();
276 277
    RunConfiguration *rc = m_runConfigurationsModel->runConfigurations().at(index);
    disconnect(rc, SIGNAL(nameChanged()), this, SLOT(nameChanged()));
con's avatar
con committed
278 279 280 281 282 283
    m_project->removeRunConfiguration(rc);
    initRunConfigurationComboBox();
}

void RunSettingsWidget::initRunConfigurationComboBox()
{
284 285 286 287 288 289
    const QList<RunConfiguration *> &runConfigurations = m_project->runConfigurations();
    RunConfiguration *activeRunConfiguration = m_project->activeRunConfiguration();
    RunConfiguration *currentSelection = 0;
    if (m_ui->runConfigurationCombo->currentIndex() >= 0)
        currentSelection = m_runConfigurationsModel->runConfigurations().at(m_ui->runConfigurationCombo->currentIndex());

con's avatar
con committed
290
    m_runConfigurationsModel->setRunConfigurations(runConfigurations);
291 292 293 294
    if (runConfigurations.contains(currentSelection))
        m_ui->runConfigurationCombo->setCurrentIndex(runConfigurations.indexOf(currentSelection));
    else
        m_ui->runConfigurationCombo->setCurrentIndex(runConfigurations.indexOf(activeRunConfiguration));
con's avatar
con committed
295
    m_ui->removeToolButton->setEnabled(runConfigurations.size() > 1);
296
    updateMakeActiveLabel();
con's avatar
con committed
297 298
}

299
void RunSettingsWidget::showRunConfigurationWidget(int index)
con's avatar
con committed
300
{
dt's avatar
dt committed
301
    Q_ASSERT(m_project);
dt's avatar
dt committed
302 303 304 305 306
    if (index == -1) {
        delete m_runConfigurationWidget;
        m_runConfigurationWidget = 0;
        return;
    }
307
    RunConfiguration *selectedRunConfiguration =
308
            m_runConfigurationsModel->runConfigurations().at(index);
con's avatar
con committed
309 310 311 312

    // Update the run configuration configuration widget
    delete m_runConfigurationWidget;
    m_runConfigurationWidget = selectedRunConfiguration->configurationWidget();
dt's avatar
dt committed
313
    layout()->addWidget(m_runConfigurationWidget);
314 315 316 317 318 319
    updateMakeActiveLabel();
}

void RunSettingsWidget::updateMakeActiveLabel()
{
    m_makeActiveLabel->setVisible(false);
320
    RunConfiguration *rc = 0;
321 322 323 324 325 326 327 328 329 330 331 332 333 334
    int index = m_ui->runConfigurationCombo->currentIndex();
    if (index != -1) {
        rc = m_runConfigurationsModel->runConfigurations().at(index);
    }
    if (rc) {
        if (m_project->activeRunConfiguration() != rc) {
            m_makeActiveLabel->setText(tr("<a href=\"#\">Make %1 active.</a>").arg(rc->name()));
            m_makeActiveLabel->setVisible(true);
        }
    }
}

void RunSettingsWidget::makeActive()
{
335
    RunConfiguration *rc = 0;
336 337 338 339 340 341
    int index = m_ui->runConfigurationCombo->currentIndex();
    if (index != -1) {
        rc = m_runConfigurationsModel->runConfigurations().at(index);
    }
    if (rc)
        m_project->setActiveRunConfiguration(rc);
342 343
}

con's avatar
con committed
344 345 346 347
void RunSettingsWidget::nameChanged()
{
    RunConfiguration *rc = qobject_cast<RunConfiguration *>(sender());
    m_runConfigurationsModel->nameChanged(rc);
348
    updateMakeActiveLabel();
con's avatar
con committed
349
}