Files
qt-creator/src/plugins/projectexplorer/runsettingspropertiespage.cpp

374 lines
12 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
2010-03-05 11:25:49 +01:00
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2008-12-02 12:01:29 +01:00
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
2009-08-14 09:30:56 +02:00
** contact the sales department at http://qt.nokia.com/contact.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 16:19:05 +01:00
2008-12-02 12:01:29 +01:00
#include "runsettingspropertiespage.h"
#include "runconfiguration.h"
#include "target.h"
#include "project.h"
2008-12-02 12:01:29 +01:00
#include "ui_runsettingspropertiespage.h"
#include <coreplugin/coreconstants.h>
2008-12-02 12:01:29 +01:00
#include <extensionsystem/pluginmanager.h>
2008-12-09 15:25:01 +01:00
#include <utils/qtcassert.h>
2008-12-02 12:01:29 +01:00
2008-12-09 15:25:01 +01:00
#include <QtCore/QDebug>
2008-12-02 12:01:29 +01:00
#include <QtCore/QPair>
#include <QtGui/QMenu>
2008-12-02 12:01:29 +01:00
namespace ProjectExplorer {
namespace Internal {
2008-12-09 15:25:01 +01:00
struct FactoryAndId
2008-12-02 12:01:29 +01:00
{
ProjectExplorer::IRunConfigurationFactory *factory;
QString id;
2008-12-02 12:01:29 +01:00
};
2008-12-09 15:25:01 +01:00
} // namespace Internal
} // namespace ProjectExplorer
2008-12-02 12:01:29 +01:00
Q_DECLARE_METATYPE(ProjectExplorer::Internal::FactoryAndId);
2008-12-02 12:01:29 +01:00
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),
m_activeRunConfiguration(0)
2008-12-02 12:01:29 +01:00
{}
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void setRunConfigurations(const QList<RunConfiguration *> &runConfigurations);
QList<RunConfiguration *> runConfigurations() const { return m_runConfigurations; }
void displayNameChanged(RunConfiguration *rc);
void activeRunConfigurationChanged(RunConfiguration *rc);
2008-12-02 12:01:29 +01:00
private:
QList<RunConfiguration *> m_runConfigurations;
RunConfiguration *m_activeRunConfiguration;
2008-12-02 12:01:29 +01:00
};
} // namespace Internal
} // namespace ProjectExplorer
using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;
using ExtensionSystem::PluginManager;
///
/// RunSettingsPanelFactory
///
QString RunSettingsPanelFactory::id() const
{
return QLatin1String(RUNSETTINGS_PANEL_ID);
}
2010-01-21 13:46:19 +01:00
QString RunSettingsPanelFactory::displayName() const
{
2010-02-25 10:34:25 +01:00
return QCoreApplication::translate("RunSettingsPanelFactory", "Run Settings");
2010-01-21 13:46:19 +01:00
}
bool RunSettingsPanelFactory::supports(Project *project)
{
return project->targets().count() == 1;
}
bool RunSettingsPanelFactory::supports(Target *target)
2008-12-02 12:01:29 +01:00
{
Q_UNUSED(target);
2008-12-02 12:01:29 +01:00
return true;
}
IPropertiesPanel *RunSettingsPanelFactory::createPanel(Project *project)
2008-12-02 12:01:29 +01:00
{
Q_ASSERT(supports(project));
return new RunSettingsPanel(project->activeTarget());
}
IPropertiesPanel *RunSettingsPanelFactory::createPanel(Target *target)
{
return new RunSettingsPanel(target);
2008-12-02 12:01:29 +01:00
}
///
/// RunSettingsPanel
///
RunSettingsPanel::RunSettingsPanel(Target *target) :
m_widget(new RunSettingsWidget(target)),
2010-02-19 14:16:37 +01:00
m_icon(":/projectexplorer/images/RunSettings.png")
2008-12-02 12:01:29 +01:00
{
}
RunSettingsPanel::~RunSettingsPanel()
{
delete m_widget;
2008-12-02 12:01:29 +01:00
}
QString RunSettingsPanel::displayName() const
2008-12-02 12:01:29 +01:00
{
2010-02-24 16:34:22 +01:00
return QCoreApplication::translate("RunSettingsPanel", "Run Settings");
2008-12-02 12:01:29 +01:00
}
QWidget *RunSettingsPanel::widget() const
2008-12-02 12:01:29 +01:00
{
return m_widget;
}
QIcon RunSettingsPanel::icon() const
{
return m_icon;
}
2008-12-02 12:01:29 +01:00
///
/// 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::displayNameChanged(RunConfiguration *rc)
2008-12-02 12:01:29 +01:00
{
for (int i = 0; i<m_runConfigurations.size(); ++i) {
if (m_runConfigurations.at(i) == rc) {
2008-12-02 12:01:29 +01:00
emit dataChanged(index(i, 0), index(i,0));
break;
}
}
}
void RunConfigurationsModel::activeRunConfigurationChanged(RunConfiguration *rc)
{
m_activeRunConfiguration = rc;
emit dataChanged(index(0, 0), index(m_runConfigurations.size()-1, 0));
}
2008-12-02 12:01:29 +01:00
QVariant RunConfigurationsModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
const int row = index.row();
if (row < m_runConfigurations.size()) {
RunConfiguration *rc = m_runConfigurations.at(row);
if (rc == m_activeRunConfiguration)
2010-02-24 16:34:22 +01:00
return QCoreApplication::translate("RunConfigurationsModel", "%1 (Active)").arg(rc->displayName());
return rc->displayName();
2008-12-02 12:01:29 +01:00
}
}
return QVariant();
}
void RunConfigurationsModel::setRunConfigurations(const QList<RunConfiguration *> &runConfigurations)
2008-12-02 12:01:29 +01:00
{
m_runConfigurations = runConfigurations;
reset();
}
///
/// RunSettingsWidget
///
RunSettingsWidget::RunSettingsWidget(Target *target)
: m_target(target),
2008-12-09 15:25:01 +01:00
m_runConfigurationsModel(new RunConfigurationsModel(this)),
m_runConfigurationWidget(0)
2008-12-02 12:01:29 +01:00
{
Q_ASSERT(m_target);
2008-12-02 12:01:29 +01:00
m_ui = new Ui::RunSettingsPropertiesPage;
m_ui->setupUi(this);
m_addMenu = new QMenu(m_ui->addToolButton);
m_ui->addToolButton->setMenu(m_addMenu);
m_ui->addToolButton->setText(tr("Add"));
m_ui->removeToolButton->setText(tr("Remove"));
2008-12-02 12:01:29 +01:00
m_ui->runConfigurationCombo->setModel(m_runConfigurationsModel);
connect(m_addMenu, SIGNAL(aboutToShow()),
this, SLOT(aboutToShowAddMenu()));
connect(m_ui->runConfigurationCombo, SIGNAL(currentIndexChanged(int)),
this, SLOT(currentRunConfigurationChanged(int)));
2008-12-02 12:01:29 +01:00
connect(m_ui->removeToolButton, SIGNAL(clicked(bool)),
this, SLOT(removeRunConfiguration()));
connect(m_ui->makeActiveButton, SIGNAL(clicked()),
this, SLOT(makeActive()));
2008-12-02 12:01:29 +01:00
connect(m_target, SIGNAL(removedRunConfiguration(ProjectExplorer::RunConfiguration *)),
this, SLOT(initRunConfigurationComboBox()));
connect(m_target, SIGNAL(addedRunConfiguration(ProjectExplorer::RunConfiguration *)),
this, SLOT(initRunConfigurationComboBox()));
connect(m_target, SIGNAL(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)),
this, SLOT(activeRunConfigurationChanged()));
2008-12-02 12:01:29 +01:00
initRunConfigurationComboBox();
const QList<RunConfiguration *> runConfigurations = m_target->runConfigurations();
2008-12-02 12:01:29 +01:00
for (int i=0; i<runConfigurations.size(); ++i) {
connect(runConfigurations.at(i), SIGNAL(displayNameChanged()),
this, SLOT(displayNameChanged()));
2008-12-02 12:01:29 +01:00
}
// TODO: Add support for custom runner configuration widgets once we have some
/*
QList<IRunControlFactory *> runners = PluginManager::instance()->getObjects<IRunControlFactory>();
foreach (IRunControlFactory * runner, runners) {
2008-12-02 12:01:29 +01:00
if (runner->canRun(activeRunConfiguration))
m_ui->layout->addWidget(runner->configurationWidget(activeRunConfiguration));
}
*/
}
RunSettingsWidget::~RunSettingsWidget()
{
delete m_ui;
}
void RunSettingsWidget::aboutToShowAddMenu()
{
m_addMenu->clear();
2008-12-09 15:25:01 +01:00
QList<IRunConfigurationFactory *> factories =
ExtensionSystem::PluginManager::instance()->getObjects<IRunConfigurationFactory>();
foreach (IRunConfigurationFactory *factory, factories) {
QStringList ids = factory->availableCreationIds(m_target);
foreach (const QString &id, ids) {
QAction *action = m_addMenu->addAction(factory->displayNameForId(id));;
FactoryAndId fai;
fai.factory = factory;
fai.id = id;
2008-12-02 12:01:29 +01:00
QVariant v;
v.setValue(fai);
2008-12-02 12:01:29 +01:00
action->setData(v);
connect(action, SIGNAL(triggered()),
this, SLOT(addRunConfiguration()));
}
}
}
RunConfiguration *RunSettingsWidget::currentRunConfiguration() const
{
RunConfiguration *currentSelection = 0;
const int index = m_ui->runConfigurationCombo->currentIndex();
if (index >= 0)
currentSelection = m_runConfigurationsModel->runConfigurations().at(index);
return currentSelection;
}
2008-12-02 12:01:29 +01:00
void RunSettingsWidget::addRunConfiguration()
{
QAction *act = qobject_cast<QAction *>(sender());
if (!act)
return;
FactoryAndId fai = act->data().value<FactoryAndId>();
RunConfiguration *newRC = fai.factory->create(m_target, fai.id);
2008-12-02 12:01:29 +01:00
if (!newRC)
return;
m_target->addRunConfiguration(newRC);
2008-12-02 12:01:29 +01:00
initRunConfigurationComboBox();
m_ui->runConfigurationCombo->setCurrentIndex(
m_runConfigurationsModel->runConfigurations().indexOf(newRC));
connect(newRC, SIGNAL(displayNameChanged()), this, SLOT(displayNameChanged()));
2008-12-02 12:01:29 +01:00
}
void RunSettingsWidget::removeRunConfiguration()
{
RunConfiguration *rc = currentRunConfiguration();
disconnect(rc, SIGNAL(displayNameChanged()), this, SLOT(displayNameChanged()));
m_target->removeRunConfiguration(rc);
2008-12-02 12:01:29 +01:00
initRunConfigurationComboBox();
}
void RunSettingsWidget::makeActive()
{
m_target->setActiveRunConfiguration(currentRunConfiguration());
}
2008-12-02 12:01:29 +01:00
void RunSettingsWidget::initRunConfigurationComboBox()
{
const QList<RunConfiguration *> &runConfigurations = m_target->runConfigurations();
RunConfiguration *activeRunConfiguration = m_target->activeRunConfiguration();
RunConfiguration *currentSelection = currentRunConfiguration();
2008-12-02 12:01:29 +01:00
m_runConfigurationsModel->setRunConfigurations(runConfigurations);
if (runConfigurations.contains(currentSelection))
m_ui->runConfigurationCombo->setCurrentIndex(runConfigurations.indexOf(currentSelection));
else
m_ui->runConfigurationCombo->setCurrentIndex(runConfigurations.indexOf(activeRunConfiguration));
2008-12-02 12:01:29 +01:00
m_ui->removeToolButton->setEnabled(runConfigurations.size() > 1);
activeRunConfigurationChanged();
}
void RunSettingsWidget::activeRunConfigurationChanged()
{
m_runConfigurationsModel->activeRunConfigurationChanged(m_target->activeRunConfiguration());
m_ui->makeActiveButton->setEnabled(currentRunConfiguration()
&& currentRunConfiguration() != m_target->activeRunConfiguration());
2008-12-02 12:01:29 +01:00
}
void RunSettingsWidget::currentRunConfigurationChanged(int index)
2008-12-02 12:01:29 +01:00
{
m_ui->makeActiveButton->setEnabled(currentRunConfiguration()
&& currentRunConfiguration() != m_target->activeRunConfiguration());
if (index == -1) {
delete m_runConfigurationWidget;
m_runConfigurationWidget = 0;
return;
}
RunConfiguration *selectedRunConfiguration =
m_runConfigurationsModel->runConfigurations().at(index);
2008-12-02 12:01:29 +01:00
// Update the run configuration configuration widget
delete m_runConfigurationWidget;
m_runConfigurationWidget = selectedRunConfiguration->configurationWidget();
layout()->addWidget(m_runConfigurationWidget);
}
void RunSettingsWidget::displayNameChanged()
2008-12-02 12:01:29 +01:00
{
RunConfiguration *rc = qobject_cast<RunConfiguration *>(sender());
m_runConfigurationsModel->displayNameChanged(rc);
2008-12-02 12:01:29 +01:00
}