Commit c6ef3add authored by hjk's avatar hjk

Make expanders work with subexpanders

Change-Id: I30bad85ce2fbaf1f02043b3d97f657461f5a1995
Reviewed-by: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent 58be8e64
......@@ -30,6 +30,8 @@
#include "macroexpander.h"
#include "algorithm.h"
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
......@@ -46,10 +48,15 @@ const char kFileBaseNamePostfix[] = ":FileBaseName";
class MacroExpanderPrivate
{
public:
MacroExpanderPrivate() : m_accumulating(false) {}
QHash<QByteArray, MacroExpander::StringFunction> m_map;
QHash<QByteArray, MacroExpander::PrefixFunction> m_prefixMap;
QMap<QByteArray, QString> m_descriptions;
QString m_displayName;
QVector<MacroExpanderProvider> m_subProviders;
QVector<MacroExpander *> m_subExpanders; // Not owned
bool m_accumulating;
};
} // Internal
......@@ -184,7 +191,18 @@ bool MacroExpander::resolveMacro(const QString &name, QString *ret)
{
bool 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 res = Utils::expandMacros(stringWithVariables, this);
if (this != globalMacroExpander())
res = Utils::expandMacros(res, this);
QString res = stringWithVariables;
Utils::expandMacros(&res, this);
return res;
}
......@@ -329,6 +346,17 @@ QString MacroExpander::variableDescription(const QByteArray &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
{
return d->m_displayName;
......@@ -339,6 +367,20 @@ void MacroExpander::setDisplayName(const QString &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
{
......
......@@ -35,12 +35,22 @@
#include <functional>
#include <QList>
#include <QVector>
#include <QCoreApplication>
namespace Utils {
namespace Internal { class MacroExpanderPrivate; }
class MacroExpander;
typedef std::function<MacroExpander *()> MacroExpanderProvider;
typedef QVector<MacroExpander *> MacroExpanders;
class QTCREATOR_UTILS_EXPORT MacroExpander : public AbstractMacroExpander
{
Q_DECLARE_TR_FUNCTIONS("MacroExpander")
public:
explicit MacroExpander();
~MacroExpander();
......@@ -71,9 +81,16 @@ public:
QList<QByteArray> variables();
QString variableDescription(const QByteArray &variable);
MacroExpanders subExpanders() const;
QString displayName() const;
void setDisplayName(const QString &displayName);
void registerSubProvider(const MacroExpanderProvider &provider);
bool isAccumulating() const;
void setAccumulating(bool on);
private:
MacroExpander(const MacroExpander &) Q_DECL_EQ_DELETE;
void operator=(const MacroExpander &) Q_DECL_EQ_DELETE;
......
......@@ -32,6 +32,7 @@
#include "coreconstants.h"
#include <utils/fancylineedit.h> // IconButton
#include <utils/headerviewstretcher.h> // IconButton
#include <utils/macroexpander.h>
#include <utils/treemodel.h>
#include <utils/qtcassert.h>
......@@ -42,6 +43,7 @@
#include <QLabel>
#include <QLineEdit>
#include <QListWidgetItem>
#include <QMenu>
#include <QPlainTextEdit>
#include <QPointer>
#include <QTextEdit>
......@@ -55,6 +57,31 @@ using namespace Utils;
namespace Core {
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
{
public:
......@@ -73,7 +100,7 @@ public:
void updateDescription(const QModelIndex &index);
void updateCurrentEditor(QWidget *old, QWidget *widget);
void handleItemActivated(const QModelIndex &index);
void insertVariable(const QString &variable);
void insertText(const QString &variable);
void updatePositionAndShow(bool);
QWidget *currentWidget();
......@@ -87,40 +114,17 @@ public:
QPointer<QPlainTextEdit> m_plainTextEdit;
QPointer<Utils::IconButton> m_iconButton;
QTreeView *m_variableTree;
VariableTreeView *m_variableTree;
QLabel *m_variableDescription;
QString m_defaultDescription;
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
{
public:
VariableGroupItem(VariableChooserPrivate *chooser)
: m_chooser(chooser), m_expander(0)
VariableGroupItem()
: m_chooser(0), m_expander(0)
{
setLazy(true);
}
......@@ -138,30 +142,87 @@ public:
if (column == 0 && ensureExpander())
return m_expander->displayName();
}
return QVariant();
}
void populate()
{
if (ensureExpander()) {
foreach (const QByteArray &variable, m_expander->variables()) {
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);
}
}
if (ensureExpander())
populateGroup(m_expander);
}
void populateGroup(MacroExpander *expander);
public:
VariableChooserPrivate *m_chooser; // Not owned.
MacroExpanderProvider m_provider;
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)
: q(parent),
m_lineEdit(0),
......@@ -170,12 +231,8 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
{
m_defaultDescription = VariableChooser::tr("Select a variable to insert.");
m_variableTree = new QTreeView(q);
m_variableTree->setAttribute(Qt::WA_MacSmallSize);
m_variableTree->setAttribute(Qt::WA_MacShowFocusRect, false);
m_variableTree = new VariableTreeView(q, this);
m_variableTree->setModel(&m_model);
m_variableTree->header()->hide();
m_variableTree->header()->setStretchLastSection(true);
m_variableDescription = new QLabel(q);
m_variableDescription->setText(m_defaultDescription);
......@@ -189,8 +246,6 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
verticalLayout->addWidget(m_variableTree);
verticalLayout->addWidget(m_variableDescription);
// connect(m_variableList, &QTreeView::currentChanged,
// this, &VariableChooserPrivate::updateDescription);
connect(m_variableTree, &QTreeView::clicked,
this, &VariableChooserPrivate::updateDescription);
connect(m_variableTree, &QTreeView::activated,
......@@ -200,6 +255,29 @@ VariableChooserPrivate::VariableChooserPrivate(VariableChooser *parent)
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
using namespace Internal;
......@@ -274,7 +352,8 @@ VariableChooser::~VariableChooser()
void VariableChooser::addMacroExpanderProvider(const MacroExpanderProvider &provider)
{
auto *item = new VariableGroupItem(d);
auto item = new VariableGroupItem;
item->m_chooser = d;
item->m_provider = provider;
d->m_model.rootItem()->prependChild(item);
}
......@@ -378,6 +457,7 @@ void VariableChooserPrivate::updatePositionAndShow(bool)
q->show();
q->raise();
q->activateWindow();
m_variableTree->expandAll();
}
/*!
......@@ -397,15 +477,16 @@ QWidget *VariableChooserPrivate::currentWidget()
*/
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
*/
void VariableChooserPrivate::insertVariable(const QString &variable)
void VariableChooserPrivate::insertText(const QString &text)
{
const QString text = QLatin1String("%{") + variable + QLatin1Char('}');
if (m_lineEdit) {
m_lineEdit->insert(text);
m_lineEdit->activateWindow();
......@@ -434,9 +515,9 @@ static bool handleEscapePressed(QKeyEvent *ke, QWidget *widget)
/*!
* \internal
*/
void VariableChooser::keyPressEvent(QKeyEvent *ke)
void VariableChooser::keyPressEvent(QKeyEvent *ev)
{
handleEscapePressed(ke, this);
handleEscapePressed(ev, this);
}
/*!
......
......@@ -35,6 +35,7 @@
#include <QWidget>
#include <utils/macroexpander.h>
#include <functional>
namespace Utils { class MacroExpander; }
......@@ -43,8 +44,6 @@ namespace Core {
namespace Internal { class VariableChooserPrivate; }
typedef std::function<Utils::MacroExpander *()> MacroExpanderProvider;
class CORE_EXPORT VariableChooser : public QWidget
{
Q_OBJECT
......@@ -53,11 +52,11 @@ public:
explicit VariableChooser(QWidget *parent = 0);
~VariableChooser();
void addMacroExpanderProvider(const MacroExpanderProvider &provider);
void addMacroExpanderProvider(const Utils::MacroExpanderProvider &provider);
void addSupportedWidget(QWidget *textcontrol, const QByteArray &ownName = QByteArray());
protected:
void keyPressEvent(QKeyEvent *ke);
void keyPressEvent(QKeyEvent *ev);
bool eventFilter(QObject *, QEvent *event);
private:
......
......@@ -302,15 +302,13 @@ KitConfigWidget *DebuggerKitInformation::createConfigWidget(Kit *k) const
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);
if (name == QLatin1String("Debugger:engineType")) {
*ret = item ? item->engineTypeName() : tr("none");
return true;
}
return false;
expander->registerVariable("Debugger:engineType", tr("Type of Debugger Backend"),
[this, kit]() -> QString {
const DebuggerItem *item = debugger(kit);
return item ? item->engineTypeName() : tr("unknown");
});
}
KitInformation::ItemList DebuggerKitInformation::toUserOutput(const Kit *k) const
......
......@@ -63,7 +63,7 @@ public:
static bool isValidDebugger(const ProjectExplorer::Kit *k);
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;
......
......@@ -61,6 +61,7 @@ const char MUTABLE_INFO_KEY[] = "PE.Profile.MutableInfo";
const char STICKY_INFO_KEY[] = "PE.Profile.StickyInfo";
namespace ProjectExplorer {
namespace Internal {
// --------------------------------------------------------------------
// KitMacroExpander:
......@@ -69,26 +70,20 @@ namespace ProjectExplorer {
class KitMacroExpander : public MacroExpander
{
public:
KitMacroExpander(Kit *kit) : m_kit(kit) {}
bool resolveMacro(const QString &name, QString *ret)
explicit KitMacroExpander(Kit *kit)
{
foreach (KitInformation *ki, KitManager::kitInformation())
if (ki->resolveMacro(m_kit, name, ret))
return true;
setDisplayName(QCoreApplication::translate("ProjectExplorer::Kit", "Kit"));
setAccumulating(true);
return false;
foreach (KitInformation *ki, KitManager::kitInformation())
ki->addToMacroExpander(kit, this);
}
private:
Kit *m_kit;
};
// -------------------------------------------------------------------------
// KitPrivate
// -------------------------------------------------------------------------
namespace Internal {
class KitPrivate
{
......@@ -663,7 +658,7 @@ bool Kit::hasFeatures(const FeatureSet &features) const
return availableFeatures().contains(features);
}
AbstractMacroExpander *Kit::macroExpander() const
MacroExpander *Kit::macroExpander() const
{
return &d->m_macroExpander;
}
......
......@@ -40,8 +40,8 @@
#include <QVariant>
namespace Utils {
class AbstractMacroExpander;
class Environment;
class MacroExpander;
} // namespace Utils
namespace ProjectExplorer {
......@@ -128,9 +128,9 @@ public:
QString displayNameForPlatform(const QString &platform) const;
Core::FeatureSet availableFeatures() const;
bool hasFeatures(const Core::FeatureSet &features) const;
Utils::MacroExpander *macroExpander() const;
private:
Utils::AbstractMacroExpander *macroExpander() const;
void setSdkProvided(bool sdkProvided);
~Kit();
......
......@@ -33,7 +33,7 @@
#include "projectexplorer_export.h"
#include <utils/stringutils.h>
#include <utils/macroexpander.h>
namespace ProjectExplorer {
......@@ -43,7 +43,7 @@ class Kit;
// KitInformationMacroExpander:
// --------------------------------------------------------------------------
class PROJECTEXPLORER_EXPORT KitInformationMacroExpander : public Utils::AbstractMacroExpander
class PROJECTEXPLORER_EXPORT KitInformationMacroExpander : public Utils::MacroExpander
{
public:
KitInformationMacroExpander(const Kit *k);
......
......@@ -585,12 +585,10 @@ FeatureSet KitInformation::availableFeatures(const Kit *k) const
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(name);
Q_UNUSED(ret);
return false;
Q_UNUSED(k);
Q_UNUSED(expander);
}
void KitInformation::notifyAboutUpdate(Kit *k)
......
......@@ -42,9 +42,9 @@
#include <functional>
namespace Utils {
class AbstractMacroExpander;
class FileName;
class Environment;
class FileName;
class MacroExpander;
}
namespace ProjectExplorer {
......@@ -99,7 +99,7 @@ public:
virtual QString displayNameForPlatform(const Kit *k, const QString &platform) 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:
void setId(Core::Id id) { m_id = id; }
......
......@@ -35,9 +35,11 @@
#include "kitmanager.h"
#include "task.h"
#include <coreplugin/variablechooser.h>
#include <utils/detailswidget.h>
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <utils/macroexpander.h>
#include <QAction>
#include <QRegularExpression>
......@@ -118,6 +120,10 @@ KitManagerConfigWidget::KitManagerConfigWidget(Kit *k) :
this, &KitManagerConfigWidget::workingCopyWasUpdated);
connect(km, &KitManager::kitUpdated,
this, &KitManagerConfigWidget::kitWasUpdated);
auto chooser = new Core::VariableChooser(this);
chooser->addSupportedWidget(m_nameEdit);
chooser->addMacroExpanderProvider([this]() { return m_modifiedKit->macroExpander(); });
}
KitManagerConfigWidget::~KitManagerConfigWidget()
......
......@@ -363,7 +363,7 @@ static QString variableValue(const char *variable)
}
ProjectMacroExpander expander(projectFilePath, projectName, kit, buildConfigurationName);
QString result;
expander.resolveProjectMacro(QString::fromUtf8(variable), &result);
expander.resolveMacro(QString::fromUtf8(variable), &result);
return result;
}
......
......@@ -43,7 +43,7 @@ ProjectMacroExpander::ProjectMacroExpander(const QString &projectFilePath, const
: 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;
bool found = false;
......@@ -93,15 +93,3 @@ bool ProjectMacroExpander::resolveProjectMacro(const QString &name, QString *ret
*ret = result;
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);