Make expanders work with subexpanders

Change-Id: I30bad85ce2fbaf1f02043b3d97f657461f5a1995
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
hjk
2014-10-20 23:13:13 +02:00
parent 58be8e6476
commit c6ef3addcc
19 changed files with 247 additions and 130 deletions

View File

@@ -30,6 +30,8 @@
#include "macroexpander.h" #include "macroexpander.h"
#include "algorithm.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QFileInfo> #include <QFileInfo>
@@ -46,10 +48,15 @@ const char kFileBaseNamePostfix[] = ":FileBaseName";
class MacroExpanderPrivate class MacroExpanderPrivate
{ {
public: public:
MacroExpanderPrivate() : m_accumulating(false) {}
QHash<QByteArray, MacroExpander::StringFunction> m_map; QHash<QByteArray, MacroExpander::StringFunction> m_map;
QHash<QByteArray, MacroExpander::PrefixFunction> m_prefixMap; QHash<QByteArray, MacroExpander::PrefixFunction> m_prefixMap;
QMap<QByteArray, QString> m_descriptions; QMap<QByteArray, QString> m_descriptions;
QString m_displayName; QString m_displayName;
QVector<MacroExpanderProvider> m_subProviders;
QVector<MacroExpander *> m_subExpanders; // Not owned
bool m_accumulating;
}; };
} // Internal } // Internal
@@ -184,7 +191,18 @@ bool MacroExpander::resolveMacro(const QString &name, QString *ret)
{ {
bool found; bool found;
*ret = value(name.toUtf8(), &found); *ret = value(name.toUtf8(), &found);
return found; if (found)
return true;
found = Utils::anyOf(d->m_subProviders, [name, ret] (const MacroExpanderProvider &p) -> bool {
MacroExpander *expander = p ? p() : 0;
return expander && expander->resolveMacro(name, ret);
});
if (found)
return true;
return this == globalMacroExpander() ? false : globalMacroExpander()->resolveMacro(name, ret);
} }
/*! /*!
@@ -223,9 +241,8 @@ QString MacroExpander::value(const QByteArray &variable, bool *found)
*/ */
QString MacroExpander::expand(const QString &stringWithVariables) QString MacroExpander::expand(const QString &stringWithVariables)
{ {
QString res = Utils::expandMacros(stringWithVariables, this); QString res = stringWithVariables;
if (this != globalMacroExpander()) Utils::expandMacros(&res, this);
res = Utils::expandMacros(res, this);
return res; return res;
} }
@@ -329,6 +346,17 @@ QString MacroExpander::variableDescription(const QByteArray &variable)
return d->m_descriptions.value(variable); return d->m_descriptions.value(variable);
} }
MacroExpanders MacroExpander::subExpanders() const
{
MacroExpanders expanders;
foreach (const MacroExpanderProvider &provider, d->m_subProviders)
if (provider)
if (MacroExpander *expander = provider())
expanders.append(expander);
return expanders;
}
QString MacroExpander::displayName() const QString MacroExpander::displayName() const
{ {
return d->m_displayName; return d->m_displayName;
@@ -339,6 +367,20 @@ void MacroExpander::setDisplayName(const QString &displayName)
d->m_displayName = displayName; d->m_displayName = displayName;
} }
void MacroExpander::registerSubProvider(const MacroExpanderProvider &provider)
{
d->m_subProviders.append(provider);
}
bool MacroExpander::isAccumulating() const
{
return d->m_accumulating;
}
void MacroExpander::setAccumulating(bool on)
{
d->m_accumulating = on;
}
class GlobalMacroExpander : public MacroExpander class GlobalMacroExpander : public MacroExpander
{ {

View File

@@ -35,12 +35,22 @@
#include <functional> #include <functional>
#include <QList>
#include <QVector>
#include <QCoreApplication>
namespace Utils { namespace Utils {
namespace Internal { class MacroExpanderPrivate; } namespace Internal { class MacroExpanderPrivate; }
class MacroExpander;
typedef std::function<MacroExpander *()> MacroExpanderProvider;
typedef QVector<MacroExpander *> MacroExpanders;
class QTCREATOR_UTILS_EXPORT MacroExpander : public AbstractMacroExpander class QTCREATOR_UTILS_EXPORT MacroExpander : public AbstractMacroExpander
{ {
Q_DECLARE_TR_FUNCTIONS("MacroExpander")
public: public:
explicit MacroExpander(); explicit MacroExpander();
~MacroExpander(); ~MacroExpander();
@@ -71,9 +81,16 @@ public:
QList<QByteArray> variables(); QList<QByteArray> variables();
QString variableDescription(const QByteArray &variable); QString variableDescription(const QByteArray &variable);
MacroExpanders subExpanders() const;
QString displayName() const; QString displayName() const;
void setDisplayName(const QString &displayName); void setDisplayName(const QString &displayName);
void registerSubProvider(const MacroExpanderProvider &provider);
bool isAccumulating() const;
void setAccumulating(bool on);
private: private:
MacroExpander(const MacroExpander &) Q_DECL_EQ_DELETE; MacroExpander(const MacroExpander &) Q_DECL_EQ_DELETE;
void operator=(const MacroExpander &) Q_DECL_EQ_DELETE; void operator=(const MacroExpander &) Q_DECL_EQ_DELETE;

View File

@@ -32,6 +32,7 @@
#include "coreconstants.h" #include "coreconstants.h"
#include <utils/fancylineedit.h> // IconButton #include <utils/fancylineedit.h> // IconButton
#include <utils/headerviewstretcher.h> // IconButton
#include <utils/macroexpander.h> #include <utils/macroexpander.h>
#include <utils/treemodel.h> #include <utils/treemodel.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -42,6 +43,7 @@
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QListWidgetItem> #include <QListWidgetItem>
#include <QMenu>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QPointer> #include <QPointer>
#include <QTextEdit> #include <QTextEdit>
@@ -55,6 +57,31 @@ using namespace Utils;
namespace Core { namespace Core {
namespace Internal { namespace Internal {
enum {
UnexpandedTextRole = Qt::UserRole,
ExpandedTextRole
};
class VariableTreeView : public QTreeView
{
public:
VariableTreeView(QWidget *parent, VariableChooserPrivate *target)
: QTreeView(parent), m_target(target)
{
setAttribute(Qt::WA_MacSmallSize);
setAttribute(Qt::WA_MacShowFocusRect, false);
setIndentation(indentation() * 7/10);
header()->hide();
new Utils::HeaderViewStretcher(header(), 0);
}
void contextMenuEvent(QContextMenuEvent *ev);
private:
VariableChooserPrivate *m_target;
};
class VariableChooserPrivate : public QObject class VariableChooserPrivate : public QObject
{ {
public: public:
@@ -73,7 +100,7 @@ public:
void updateDescription(const QModelIndex &index); void updateDescription(const QModelIndex &index);
void updateCurrentEditor(QWidget *old, QWidget *widget); void updateCurrentEditor(QWidget *old, QWidget *widget);
void handleItemActivated(const QModelIndex &index); void handleItemActivated(const QModelIndex &index);
void insertVariable(const QString &variable); void insertText(const QString &variable);
void updatePositionAndShow(bool); void updatePositionAndShow(bool);
QWidget *currentWidget(); QWidget *currentWidget();
@@ -87,40 +114,17 @@ public:
QPointer<QPlainTextEdit> m_plainTextEdit; QPointer<QPlainTextEdit> m_plainTextEdit;
QPointer<Utils::IconButton> m_iconButton; QPointer<Utils::IconButton> m_iconButton;
QTreeView *m_variableTree; VariableTreeView *m_variableTree;
QLabel *m_variableDescription; QLabel *m_variableDescription;
QString m_defaultDescription; QString m_defaultDescription;
QByteArray m_currentVariableName; // Prevent recursive insertion of currently expanded item QByteArray m_currentVariableName; // Prevent recursive insertion of currently expanded item
}; };
class VariableItem : public TreeItem
{
public:
VariableItem()
{}
QVariant data(int column, int role) const
{
if (role == Qt::DisplayRole || role == Qt::EditRole) {
if (column == 0)
return m_display;
}
if (role == Qt::ToolTipRole)
return m_description;
return QVariant();
}
public:
QString m_display;
QString m_description;
};
class VariableGroupItem : public TreeItem class VariableGroupItem : public TreeItem
{ {
public: public:
VariableGroupItem(VariableChooserPrivate *chooser) VariableGroupItem()
: m_chooser(chooser), m_expander(0) : m_chooser(0), m_expander(0)
{ {
setLazy(true); setLazy(true);
} }
@@ -138,30 +142,87 @@ public:
if (column == 0 && ensureExpander()) if (column == 0 && ensureExpander())
return m_expander->displayName(); return m_expander->displayName();
} }
return QVariant(); return QVariant();
} }
void populate() void populate()
{ {
if (ensureExpander()) { if (ensureExpander())
foreach (const QByteArray &variable, m_expander->variables()) { populateGroup(m_expander);
auto item = new VariableItem;
item->m_display = QString::fromLatin1(variable);
item->m_description = m_expander->variableDescription(variable);
if (variable == m_chooser->m_currentVariableName)
item->setFlags(Qt::ItemIsSelectable); // not ItemIsEnabled
appendChild(item);
}
}
} }
void populateGroup(MacroExpander *expander);
public: public:
VariableChooserPrivate *m_chooser; // Not owned. VariableChooserPrivate *m_chooser; // Not owned.
MacroExpanderProvider m_provider; MacroExpanderProvider m_provider;
mutable MacroExpander *m_expander; // Not owned. mutable MacroExpander *m_expander; // Not owned.
QString m_displayName;
}; };
class VariableItem : public TreeItem
{
public:
VariableItem() {}
QVariant data(int column, int role) const
{
if (role == Qt::DisplayRole || role == Qt::EditRole) {
if (column == 0)
return m_variable;
}
if (role == Qt::ToolTipRole)
return m_expander->variableDescription(m_variable.toUtf8());
if (role == UnexpandedTextRole)
return QString(QLatin1String("%{") + m_variable + QLatin1Char('}'));
if (role == ExpandedTextRole)
return m_expander->expand(QLatin1String("%{") + m_variable + QLatin1Char('}'));
return QVariant();
}
public:
MacroExpander *m_expander;
QString m_variable;
};
void VariableTreeView::contextMenuEvent(QContextMenuEvent *ev)
{
const QModelIndex index = indexAt(ev->pos());
QString unexpandedText = index.data(UnexpandedTextRole).toString();
QString expandedText = index.data(ExpandedTextRole).toString();
QMenu menu;
QAction *insertUnexpandedAction = 0;
QAction *insertExpandedAction = 0;
if (unexpandedText.isEmpty()) {
insertUnexpandedAction = menu.addAction(tr("Insert unexpanded value"));
insertUnexpandedAction->setEnabled(false);
} else {
insertUnexpandedAction = menu.addAction(tr("Insert \"%1\"").arg(unexpandedText));
}
if (expandedText.isEmpty()) {
insertExpandedAction = menu.addAction(tr("Insert expanded value"));
insertExpandedAction->setEnabled(false);
} else {
insertExpandedAction = menu.addAction(tr("Insert \"%1\"").arg(expandedText));
}
QAction *act = menu.exec(ev->globalPos());
if (act == insertUnexpandedAction)
m_target->insertText(unexpandedText);
else if (act == insertExpandedAction)
m_target->insertText(expandedText);
}
VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent) VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
: q(parent), : q(parent),
m_lineEdit(0), m_lineEdit(0),
@@ -170,12 +231,8 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
{ {
m_defaultDescription = VariableChooser::tr("Select a variable to insert."); m_defaultDescription = VariableChooser::tr("Select a variable to insert.");
m_variableTree = new QTreeView(q); m_variableTree = new VariableTreeView(q, this);
m_variableTree->setAttribute(Qt::WA_MacSmallSize);
m_variableTree->setAttribute(Qt::WA_MacShowFocusRect, false);
m_variableTree->setModel(&m_model); m_variableTree->setModel(&m_model);
m_variableTree->header()->hide();
m_variableTree->header()->setStretchLastSection(true);
m_variableDescription = new QLabel(q); m_variableDescription = new QLabel(q);
m_variableDescription->setText(m_defaultDescription); m_variableDescription->setText(m_defaultDescription);
@@ -189,8 +246,6 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
verticalLayout->addWidget(m_variableTree); verticalLayout->addWidget(m_variableTree);
verticalLayout->addWidget(m_variableDescription); verticalLayout->addWidget(m_variableDescription);
// connect(m_variableList, &QTreeView::currentChanged,
// this, &VariableChooserPrivate::updateDescription);
connect(m_variableTree, &QTreeView::clicked, connect(m_variableTree, &QTreeView::clicked,
this, &VariableChooserPrivate::updateDescription); this, &VariableChooserPrivate::updateDescription);
connect(m_variableTree, &QTreeView::activated, connect(m_variableTree, &QTreeView::activated,
@@ -200,6 +255,29 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
updateCurrentEditor(0, qApp->focusWidget()); updateCurrentEditor(0, qApp->focusWidget());
} }
void VariableGroupItem::populateGroup(MacroExpander *expander)
{
foreach (const QByteArray &variable, expander->variables()) {
auto item = new VariableItem;
item->m_variable = QString::fromUtf8(variable);
item->m_expander = expander;
if (variable == m_chooser->m_currentVariableName)
item->setFlags(Qt::ItemIsSelectable); // not ItemIsEnabled
appendChild(item);
}
foreach (MacroExpander *subExpander, expander->subExpanders()) {
if (expander->isAccumulating()) {
populateGroup(subExpander);
} else {
auto item = new VariableGroupItem;
item->m_chooser = m_chooser;
item->m_expander = subExpander;
appendChild(item);
}
}
}
} // namespace Internal } // namespace Internal
using namespace Internal; using namespace Internal;
@@ -274,7 +352,8 @@ VariableChooser::~VariableChooser()
void VariableChooser::addMacroExpanderProvider(const MacroExpanderProvider &provider) void VariableChooser::addMacroExpanderProvider(const MacroExpanderProvider &provider)
{ {
auto *item = new VariableGroupItem(d); auto item = new VariableGroupItem;
item->m_chooser = d;
item->m_provider = provider; item->m_provider = provider;
d->m_model.rootItem()->prependChild(item); d->m_model.rootItem()->prependChild(item);
} }
@@ -378,6 +457,7 @@ void VariableChooserPrivate::updatePositionAndShow(bool)
q->show(); q->show();
q->raise(); q->raise();
q->activateWindow(); q->activateWindow();
m_variableTree->expandAll();
} }
/*! /*!
@@ -397,15 +477,16 @@ QWidget *VariableChooserPrivate::currentWidget()
*/ */
void VariableChooserPrivate::handleItemActivated(const QModelIndex &index) void VariableChooserPrivate::handleItemActivated(const QModelIndex &index)
{ {
insertVariable(m_model.data(index, Qt::DisplayRole).toString()); QString text = m_model.data(index, UnexpandedTextRole).toString();
if (!text.isEmpty())
insertText(text);
} }
/*! /*!
* \internal * \internal
*/ */
void VariableChooserPrivate::insertVariable(const QString &variable) void VariableChooserPrivate::insertText(const QString &text)
{ {
const QString text = QLatin1String("%{") + variable + QLatin1Char('}');
if (m_lineEdit) { if (m_lineEdit) {
m_lineEdit->insert(text); m_lineEdit->insert(text);
m_lineEdit->activateWindow(); m_lineEdit->activateWindow();
@@ -434,9 +515,9 @@ static bool handleEscapePressed(QKeyEvent *ke, QWidget *widget)
/*! /*!
* \internal * \internal
*/ */
void VariableChooser::keyPressEvent(QKeyEvent *ke) void VariableChooser::keyPressEvent(QKeyEvent *ev)
{ {
handleEscapePressed(ke, this); handleEscapePressed(ev, this);
} }
/*! /*!

View File

@@ -35,6 +35,7 @@
#include <QWidget> #include <QWidget>
#include <utils/macroexpander.h>
#include <functional> #include <functional>
namespace Utils { class MacroExpander; } namespace Utils { class MacroExpander; }
@@ -43,8 +44,6 @@ namespace Core {
namespace Internal { class VariableChooserPrivate; } namespace Internal { class VariableChooserPrivate; }
typedef std::function<Utils::MacroExpander *()> MacroExpanderProvider;
class CORE_EXPORT VariableChooser : public QWidget class CORE_EXPORT VariableChooser : public QWidget
{ {
Q_OBJECT Q_OBJECT
@@ -53,11 +52,11 @@ public:
explicit VariableChooser(QWidget *parent = 0); explicit VariableChooser(QWidget *parent = 0);
~VariableChooser(); ~VariableChooser();
void addMacroExpanderProvider(const MacroExpanderProvider &provider); void addMacroExpanderProvider(const Utils::MacroExpanderProvider &provider);
void addSupportedWidget(QWidget *textcontrol, const QByteArray &ownName = QByteArray()); void addSupportedWidget(QWidget *textcontrol, const QByteArray &ownName = QByteArray());
protected: protected:
void keyPressEvent(QKeyEvent *ke); void keyPressEvent(QKeyEvent *ev);
bool eventFilter(QObject *, QEvent *event); bool eventFilter(QObject *, QEvent *event);
private: private:

View File

@@ -302,15 +302,13 @@ KitConfigWidget *DebuggerKitInformation::createConfigWidget(Kit *k) const
return new Internal::DebuggerKitConfigWidget(k, this); return new Internal::DebuggerKitConfigWidget(k, this);
} }
bool DebuggerKitInformation::resolveMacro(const Kit *kit, const QString &name, QString *ret) const void DebuggerKitInformation::addToMacroExpander(Kit *kit, MacroExpander *expander) const
{ {
const DebuggerItem *item = debugger(kit); expander->registerVariable("Debugger:engineType", tr("Type of Debugger Backend"),
if (name == QLatin1String("Debugger:engineType")) { [this, kit]() -> QString {
*ret = item ? item->engineTypeName() : tr("none"); const DebuggerItem *item = debugger(kit);
return true; return item ? item->engineTypeName() : tr("unknown");
} });
return false;
} }
KitInformation::ItemList DebuggerKitInformation::toUserOutput(const Kit *k) const KitInformation::ItemList DebuggerKitInformation::toUserOutput(const Kit *k) const

View File

@@ -63,7 +63,7 @@ public:
static bool isValidDebugger(const ProjectExplorer::Kit *k); static bool isValidDebugger(const ProjectExplorer::Kit *k);
ProjectExplorer::KitConfigWidget *createConfigWidget(ProjectExplorer::Kit *k) const; ProjectExplorer::KitConfigWidget *createConfigWidget(ProjectExplorer::Kit *k) const;
bool resolveMacro(const ProjectExplorer::Kit *kit, const QString &name, QString *ret) const; void addToMacroExpander(ProjectExplorer::Kit *kit, Utils::MacroExpander *expander) const;
ItemList toUserOutput(const ProjectExplorer::Kit *k) const; ItemList toUserOutput(const ProjectExplorer::Kit *k) const;

View File

@@ -61,6 +61,7 @@ const char MUTABLE_INFO_KEY[] = "PE.Profile.MutableInfo";
const char STICKY_INFO_KEY[] = "PE.Profile.StickyInfo"; const char STICKY_INFO_KEY[] = "PE.Profile.StickyInfo";
namespace ProjectExplorer { namespace ProjectExplorer {
namespace Internal {
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// KitMacroExpander: // KitMacroExpander:
@@ -69,26 +70,20 @@ namespace ProjectExplorer {
class KitMacroExpander : public MacroExpander class KitMacroExpander : public MacroExpander
{ {
public: public:
KitMacroExpander(Kit *kit) : m_kit(kit) {} explicit KitMacroExpander(Kit *kit)
bool resolveMacro(const QString &name, QString *ret)
{ {
setDisplayName(QCoreApplication::translate("ProjectExplorer::Kit", "Kit"));
setAccumulating(true);
foreach (KitInformation *ki, KitManager::kitInformation()) foreach (KitInformation *ki, KitManager::kitInformation())
if (ki->resolveMacro(m_kit, name, ret)) ki->addToMacroExpander(kit, this);
return true;
return false;
} }
private:
Kit *m_kit;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// KitPrivate // KitPrivate
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
namespace Internal {
class KitPrivate class KitPrivate
{ {
@@ -663,7 +658,7 @@ bool Kit::hasFeatures(const FeatureSet &features) const
return availableFeatures().contains(features); return availableFeatures().contains(features);
} }
AbstractMacroExpander *Kit::macroExpander() const MacroExpander *Kit::macroExpander() const
{ {
return &d->m_macroExpander; return &d->m_macroExpander;
} }

View File

@@ -40,8 +40,8 @@
#include <QVariant> #include <QVariant>
namespace Utils { namespace Utils {
class AbstractMacroExpander;
class Environment; class Environment;
class MacroExpander;
} // namespace Utils } // namespace Utils
namespace ProjectExplorer { namespace ProjectExplorer {
@@ -128,9 +128,9 @@ public:
QString displayNameForPlatform(const QString &platform) const; QString displayNameForPlatform(const QString &platform) const;
Core::FeatureSet availableFeatures() const; Core::FeatureSet availableFeatures() const;
bool hasFeatures(const Core::FeatureSet &features) const; bool hasFeatures(const Core::FeatureSet &features) const;
Utils::MacroExpander *macroExpander() const;
private: private:
Utils::AbstractMacroExpander *macroExpander() const;
void setSdkProvided(bool sdkProvided); void setSdkProvided(bool sdkProvided);
~Kit(); ~Kit();

View File

@@ -33,7 +33,7 @@
#include "projectexplorer_export.h" #include "projectexplorer_export.h"
#include <utils/stringutils.h> #include <utils/macroexpander.h>
namespace ProjectExplorer { namespace ProjectExplorer {
@@ -43,7 +43,7 @@ class Kit;
// KitInformationMacroExpander: // KitInformationMacroExpander:
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
class PROJECTEXPLORER_EXPORT KitInformationMacroExpander : public Utils::AbstractMacroExpander class PROJECTEXPLORER_EXPORT KitInformationMacroExpander : public Utils::MacroExpander
{ {
public: public:
KitInformationMacroExpander(const Kit *k); KitInformationMacroExpander(const Kit *k);

View File

@@ -585,12 +585,10 @@ FeatureSet KitInformation::availableFeatures(const Kit *k) const
return FeatureSet(); return FeatureSet();
} }
bool KitInformation::resolveMacro(const Kit *kit, const QString &name, QString *ret) const void KitInformation::addToMacroExpander(Kit *k, MacroExpander *expander) const
{ {
Q_UNUSED(kit); Q_UNUSED(k);
Q_UNUSED(name); Q_UNUSED(expander);
Q_UNUSED(ret);
return false;
} }
void KitInformation::notifyAboutUpdate(Kit *k) void KitInformation::notifyAboutUpdate(Kit *k)

View File

@@ -42,9 +42,9 @@
#include <functional> #include <functional>
namespace Utils { namespace Utils {
class AbstractMacroExpander;
class FileName;
class Environment; class Environment;
class FileName;
class MacroExpander;
} }
namespace ProjectExplorer { namespace ProjectExplorer {
@@ -99,7 +99,7 @@ public:
virtual QString displayNameForPlatform(const Kit *k, const QString &platform) const; virtual QString displayNameForPlatform(const Kit *k, const QString &platform) const;
virtual Core::FeatureSet availableFeatures(const Kit *k) const; virtual Core::FeatureSet availableFeatures(const Kit *k) const;
virtual bool resolveMacro(const Kit *kit, const QString &name, QString *ret) const; virtual void addToMacroExpander(ProjectExplorer::Kit *kit, Utils::MacroExpander *expander) const;
protected: protected:
void setId(Core::Id id) { m_id = id; } void setId(Core::Id id) { m_id = id; }

View File

@@ -35,9 +35,11 @@
#include "kitmanager.h" #include "kitmanager.h"
#include "task.h" #include "task.h"
#include <coreplugin/variablechooser.h>
#include <utils/detailswidget.h> #include <utils/detailswidget.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/stringutils.h> #include <utils/macroexpander.h>
#include <QAction> #include <QAction>
#include <QRegularExpression> #include <QRegularExpression>
@@ -118,6 +120,10 @@ KitManagerConfigWidget::KitManagerConfigWidget(Kit *k) :
this, &KitManagerConfigWidget::workingCopyWasUpdated); this, &KitManagerConfigWidget::workingCopyWasUpdated);
connect(km, &KitManager::kitUpdated, connect(km, &KitManager::kitUpdated,
this, &KitManagerConfigWidget::kitWasUpdated); this, &KitManagerConfigWidget::kitWasUpdated);
auto chooser = new Core::VariableChooser(this);
chooser->addSupportedWidget(m_nameEdit);
chooser->addMacroExpanderProvider([this]() { return m_modifiedKit->macroExpander(); });
} }
KitManagerConfigWidget::~KitManagerConfigWidget() KitManagerConfigWidget::~KitManagerConfigWidget()

View File

@@ -363,7 +363,7 @@ static QString variableValue(const char *variable)
} }
ProjectMacroExpander expander(projectFilePath, projectName, kit, buildConfigurationName); ProjectMacroExpander expander(projectFilePath, projectName, kit, buildConfigurationName);
QString result; QString result;
expander.resolveProjectMacro(QString::fromUtf8(variable), &result); expander.resolveMacro(QString::fromUtf8(variable), &result);
return result; return result;
} }

View File

@@ -43,7 +43,7 @@ ProjectMacroExpander::ProjectMacroExpander(const QString &projectFilePath, const
: m_projectFile(projectFilePath), m_projectName(projectName), m_kit(k), m_bcName(bcName) : m_projectFile(projectFilePath), m_projectName(projectName), m_kit(k), m_bcName(bcName)
{ } { }
bool ProjectMacroExpander::resolveProjectMacro(const QString &name, QString *ret) bool ProjectMacroExpander::resolveMacro(const QString &name, QString *ret)
{ {
QString result; QString result;
bool found = false; bool found = false;
@@ -93,15 +93,3 @@ bool ProjectMacroExpander::resolveProjectMacro(const QString &name, QString *ret
*ret = result; *ret = result;
return found; return found;
} }
// Try to resolve using local information, otherwise fall back to global variables.
bool ProjectMacroExpander::resolveMacro(const QString &name, QString *ret)
{
bool found = resolveProjectMacro(name, ret);
if (!found) {
QString result = Utils::globalMacroExpander()->value(name.toUtf8(), &found);
if (ret)
*ret = result;
}
return found;
}

View File

@@ -42,7 +42,6 @@ class PROJECTEXPLORER_EXPORT ProjectMacroExpander : public Utils::MacroExpander
{ {
public: public:
ProjectMacroExpander(const QString &projectFilePath, const QString &projectName, const Kit *k, const QString &bcName); ProjectMacroExpander(const QString &projectFilePath, const QString &projectName, const Kit *k, const QString &bcName);
bool resolveProjectMacro(const QString &name, QString *ret);
bool resolveMacro(const QString &name, QString *ret); bool resolveMacro(const QString &name, QString *ret);
private: private:

View File

@@ -115,7 +115,7 @@ QVariantMap VersionUpgrader::renameKeys(const QList<Change> &changes, QVariantMa
} // ProjectExplorer } // ProjectExplorer
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace Internal; using namespace ProjectExplorer::Internal;
namespace { namespace {

View File

@@ -234,6 +234,9 @@ void BaseQtVersion::ctor(const FileName &qmakePath)
void BaseQtVersion::setupExpander() void BaseQtVersion::setupExpander()
{ {
m_expander.setDisplayName(
QCoreApplication::translate("QtSupport::QtKitInformation", "Qt version"));
m_expander.registerVariable("Qt:version", m_expander.registerVariable("Qt:version",
QCoreApplication::translate("QtSupport::QtKitInformation", "The version string of the current Qt version."), QCoreApplication::translate("QtSupport::QtKitInformation", "The version string of the current Qt version."),
[this]() { return qtVersionString(); }); [this]() { return qtVersionString(); });
@@ -250,9 +253,6 @@ void BaseQtVersion::setupExpander()
// m_expander.registerVariable("Qt:name", // m_expander.registerVariable("Qt:name",
// QCoreApplication::translate("QtSupport::QtKitInformation", "The display name of the current Qt version."), // QCoreApplication::translate("QtSupport::QtKitInformation", "The display name of the current Qt version."),
// [this]() { return displayName(); }); // [this]() { return displayName(); });
m_expander.setDisplayName(
QCoreApplication::translate("QtSupport::QtKitInformation", "Qt version"));
} }
BaseQtVersion::~BaseQtVersion() BaseQtVersion::~BaseQtVersion()

View File

@@ -132,24 +132,19 @@ ProjectExplorer::IOutputParser *QtKitInformation::createOutputParser(const Proje
return 0; return 0;
} }
bool QtKitInformation::resolveMacro(const ProjectExplorer::Kit *kit, const QString &name, QString *ret) const void QtKitInformation::addToMacroExpander(Kit *kit, MacroExpander *expander) const
{ {
if (BaseQtVersion *version = qtVersion(kit)) { expander->registerSubProvider(
MacroExpander *expander = version->macroExpander(); [this, kit]() -> MacroExpander * {
if (expander->resolveMacro(name, ret)) BaseQtVersion *version = qtVersion(kit);
return true; return version ? version->macroExpander() : 0;
});
// FIXME: Handle in version expander once we can detect loops. expander->registerVariable("Qt:name", tr("Name of Qt Version"),
if (name == QLatin1String("Qt:name")) { [this, kit]() -> QString {
*ret = version->displayName(); BaseQtVersion *version = qtVersion(kit);
return true; return version ? version->displayName() : tr("unknown");
} });
}
if (Utils::globalMacroExpander()->resolveMacro(name, ret))
return true;
return false;
} }
Core::Id QtKitInformation::id() Core::Id QtKitInformation::id()

View File

@@ -62,8 +62,7 @@ public:
void addToEnvironment(const ProjectExplorer::Kit *k, Utils::Environment &env) const; void addToEnvironment(const ProjectExplorer::Kit *k, Utils::Environment &env) const;
ProjectExplorer::IOutputParser *createOutputParser(const ProjectExplorer::Kit *k) const; ProjectExplorer::IOutputParser *createOutputParser(const ProjectExplorer::Kit *k) const;
void addToMacroExpander(ProjectExplorer::Kit *kit, Utils::MacroExpander *expander) const;
bool resolveMacro(const ProjectExplorer::Kit *kit, const QString &name, QString *ret) const;
static Core::Id id(); static Core::Id id();
static int qtVersionId(const ProjectExplorer::Kit *k); static int qtVersionId(const ProjectExplorer::Kit *k);