Commit 6ea8550d authored by Volker Vogelhuber's avatar Volker Vogelhuber Committed by hjk
Browse files

Kit: Allow setting kit specific environment variables



Usual Utils::EnvironmentChanges syntax applies:
* "TEST=somevalue" in one line will set TEST to somevalue
* "PATH=/some/value:${PATH}" in one line will prepend /some/value to PATH
* "IGNORE_ME" will make sure IGNORE_ME is *not* set

Task-number: QTCREATORBUG-9898
Change-Id: Id7231c294fc8625f414f7ea05fe301fb80efce3e
Reviewed-by: default avatarVolker Vogelhuber <wiendl@web.de>
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent f9f5fdcf
......@@ -467,4 +467,81 @@ void DeviceKitInformation::devicesChanged()
setup(k); // Set default device if necessary
}
// --------------------------------------------------------------------------
// EnvironmentKitInformation:
// --------------------------------------------------------------------------
EnvironmentKitInformation::EnvironmentKitInformation()
{
setObjectName(QLatin1String("EnvironmentKitInformation"));
setId(EnvironmentKitInformation::id());
setPriority(29000);
}
QVariant EnvironmentKitInformation::defaultValue(Kit *k) const
{
Q_UNUSED(k)
return QStringList();
}
QList<Task> EnvironmentKitInformation::validate(const Kit *k) const
{
QList<Task> result;
const QVariant variant = k->value(EnvironmentKitInformation::id());
if (!variant.isNull() && !variant.canConvert(QVariant::List)) {
result.append(Task(Task::Error, tr("The environment setting value is invalid."),
Utils::FileName(), -1, Core::Id(Constants::TASK_CATEGORY_BUILDSYSTEM)));
}
return result;
}
void EnvironmentKitInformation::fix(Kit *k)
{
const QVariant variant = k->value(EnvironmentKitInformation::id());
if (!variant.isNull() && !variant.canConvert(QVariant::List)) {
qWarning("Kit \"%s\" has a wrong environment value set.", qPrintable(k->displayName()));
setEnvironmentChanges(k, QList<Utils::EnvironmentItem>());
}
}
void EnvironmentKitInformation::addToEnvironment(const Kit *k, Utils::Environment &env) const
{
const QVariant envValue = k->value(EnvironmentKitInformation::id());
if (envValue.isValid())
env.modify(Utils::EnvironmentItem::fromStringList(envValue.toStringList()));
}
KitConfigWidget *EnvironmentKitInformation::createConfigWidget(Kit *k) const
{
return new Internal::KitEnvironmentConfigWidget(k, this);
}
KitInformation::ItemList EnvironmentKitInformation::toUserOutput(const Kit *k) const
{
ItemList retVal;
QVariant envValue = k->value(EnvironmentKitInformation::id());
if (envValue.isValid())
retVal << qMakePair(QLatin1Literal("Environment"), envValue.toStringList().join(QLatin1Literal("\n")));
return retVal;
}
Core::Id EnvironmentKitInformation::id()
{
return "PE.Profile.Environment";
}
QList<Utils::EnvironmentItem> EnvironmentKitInformation::environmentChanges(const Kit *k)
{
if (k)
return Utils::EnvironmentItem::fromStringList(k->value(EnvironmentKitInformation::id()).toStringList());
return QList<Utils::EnvironmentItem>();
}
void EnvironmentKitInformation::setEnvironmentChanges(Kit *k, const QList<Utils::EnvironmentItem> &changes)
{
if (k)
k->setValue(EnvironmentKitInformation::id(), Utils::EnvironmentItem::toStringList(changes));
}
} // namespace ProjectExplorer
......@@ -34,6 +34,7 @@
#include "kit.h"
#include "devicesupport/idevice.h"
#include <utils/environment.h>
#include <QVariant>
......@@ -169,6 +170,32 @@ private slots:
void kitUpdated(ProjectExplorer::Kit *k);
};
// --------------------------------------------------------------------------
// EnvironmentKitInformation:
// --------------------------------------------------------------------------
class PROJECTEXPLORER_EXPORT EnvironmentKitInformation : public KitInformation
{
Q_OBJECT
public:
EnvironmentKitInformation();
QVariant defaultValue(Kit *k) const;
QList<Task> validate(const Kit *k) const;
void fix(Kit *k);
void addToEnvironment(const Kit *k, Utils::Environment &env) const;
KitConfigWidget *createConfigWidget(Kit *k) const;
ItemList toUserOutput(const Kit *k) const;
static Core::Id id();
static QList<Utils::EnvironmentItem> environmentChanges(const Kit *k);
static void setEnvironmentChanges(Kit *k, const QList<Utils::EnvironmentItem> &changes);
};
} // namespace ProjectExplorer
#endif // KITINFORMATION_H
......@@ -37,14 +37,24 @@
#include "kitinformation.h"
#include "toolchain.h"
#include "toolchainmanager.h"
#include "environmentwidget.h"
#include <coreplugin/icore.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/pathchooser.h>
#include <utils/algorithm.h>
#include <utils/fancylineedit.h>
#include <utils/environment.h>
#include <utils/qtcassert.h>
#include <utils/pathchooser.h>
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFontMetrics>
#include <QLabel>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QVBoxLayout>
using namespace Core;
......@@ -392,5 +402,110 @@ void DeviceInformationConfigWidget::currentDeviceChanged()
DeviceKitInformation::setDeviceId(m_kit, m_model->deviceId(m_comboBox->currentIndex()));
}
// --------------------------------------------------------------------
// KitEnvironmentConfigWidget:
// --------------------------------------------------------------------
KitEnvironmentConfigWidget::KitEnvironmentConfigWidget(Kit *workingCopy, const KitInformation *ki) :
KitConfigWidget(workingCopy, ki),
m_summaryLabel(new QLabel),
m_manageButton(new QPushButton),
m_dialog(0),
m_editor(0)
{
refresh();
m_manageButton->setText(tr("Change ..."));
connect(m_manageButton, SIGNAL(clicked()), this, SLOT(editEnvironmentChanges()));
}
QWidget *KitEnvironmentConfigWidget::mainWidget() const
{
return m_summaryLabel;
}
QString KitEnvironmentConfigWidget::displayName() const
{
return tr("Environment:");
}
QString KitEnvironmentConfigWidget::toolTip() const
{
return tr("Additional environment settings when using this kit.");
}
void KitEnvironmentConfigWidget::refresh()
{
QList<Utils::EnvironmentItem> changes = EnvironmentKitInformation::environmentChanges(m_kit);
Utils::sort(changes, [](const Utils::EnvironmentItem &lhs, const Utils::EnvironmentItem &rhs)
{ return QString::localeAwareCompare(lhs.name, rhs.name) < 0; });
QString shortSummary = Utils::EnvironmentItem::toStringList(changes).join(QLatin1String("; "));
QFontMetrics fm(m_summaryLabel->font());
shortSummary = fm.elidedText(shortSummary, Qt::ElideRight, m_summaryLabel->width());
m_summaryLabel->setText(shortSummary.isEmpty() ? tr("No Changes to apply") : shortSummary);
if (m_editor)
m_editor->setPlainText(Utils::EnvironmentItem::toStringList(changes).join(QLatin1Char('\n')));
}
void KitEnvironmentConfigWidget::makeReadOnly()
{
m_manageButton->setEnabled(false);
if (m_dialog)
m_dialog->reject();
}
void KitEnvironmentConfigWidget::editEnvironmentChanges()
{
if (m_dialog) {
m_dialog->activateWindow();
m_dialog->raise();
return;
}
QTC_ASSERT(!m_editor, return);
m_dialog = new QDialog(m_summaryLabel);
m_dialog->setWindowTitle(tr("Edit Environment Changes"));
QVBoxLayout *layout = new QVBoxLayout(m_dialog);
m_editor = new QPlainTextEdit;
QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Apply|QDialogButtonBox::Cancel);
layout->addWidget(m_editor);
layout->addWidget(buttons);
connect(buttons, SIGNAL(accepted()), m_dialog, SLOT(accept()));
connect(buttons, SIGNAL(rejected()), m_dialog, SLOT(reject()));
connect(m_dialog, SIGNAL(accepted()), this, SLOT(acceptChangesDialog()));
connect(m_dialog, SIGNAL(rejected()), this, SLOT(closeChangesDialog()));
connect(buttons->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(applyChanges()));
refresh();
m_dialog->show();
}
void KitEnvironmentConfigWidget::applyChanges()
{
QTC_ASSERT(m_editor, return);
auto changes = Utils::EnvironmentItem::fromStringList(m_editor->toPlainText().split(QLatin1Char('\n')));
EnvironmentKitInformation::setEnvironmentChanges(m_kit, changes);
}
void KitEnvironmentConfigWidget::closeChangesDialog()
{
m_dialog->deleteLater();
m_dialog = 0;
m_editor = 0;
}
void KitEnvironmentConfigWidget::acceptChangesDialog()
{
applyChanges();
closeChangesDialog();
}
QWidget *KitEnvironmentConfigWidget::buttonWidget() const
{
return m_manageButton;
}
} // namespace Internal
} // namespace ProjectExplorer
......@@ -34,8 +34,12 @@
#include <coreplugin/id.h>
#include <utils/environment.h>
QT_BEGIN_NAMESPACE
class QComboBox;
class QLabel;
class QPlainTextEdit;
class QPushButton;
QT_END_NAMESPACE
......@@ -45,6 +49,7 @@ namespace ProjectExplorer {
class DeviceManagerModel;
class ToolChain;
class EnvironmentWidget;
namespace Internal {
......@@ -168,6 +173,34 @@ private:
Core::Id m_selectedId;
};
class KitEnvironmentConfigWidget : public KitConfigWidget
{
Q_OBJECT
public:
KitEnvironmentConfigWidget(Kit *workingCopy, const KitInformation *ki);
QWidget *mainWidget() const;
QWidget *buttonWidget() const;
QString displayName() const;
QString toolTip() const;
void refresh();
void makeReadOnly();
private slots:
void editEnvironmentChanges();
void applyChanges();
void closeChangesDialog();
void acceptChangesDialog();
private:
QLabel *m_summaryLabel;
QPushButton *m_manageButton;
QDialog *m_dialog;
QPlainTextEdit *m_editor;
};
} // namespace Internal
} // namespace ProjectExplorer
......
......@@ -397,6 +397,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
KitManager::registerKitInformation(new DeviceKitInformation);
KitManager::registerKitInformation(new ToolChainKitInformation);
KitManager::registerKitInformation(new SysRootKitInformation);
KitManager::registerKitInformation(new EnvironmentKitInformation);
addAutoReleasedObject(new Internal::ToolChainOptionsPage);
addAutoReleasedObject(new KitOptionsPage);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment