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

@@ -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);
}
/*!