forked from qt-creator/qt-creator
Utils: Add StringListAspect editor
Change-Id: I0d6713f75967fb56132f89772aa48fa1de2368af Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
#include "variablechooser.h"
|
#include "variablechooser.h"
|
||||||
|
|
||||||
#include <QAction>
|
#include <QAction>
|
||||||
|
#include <QApplication>
|
||||||
#include <QButtonGroup>
|
#include <QButtonGroup>
|
||||||
#include <QCheckBox>
|
#include <QCheckBox>
|
||||||
#include <QComboBox>
|
#include <QComboBox>
|
||||||
@@ -38,6 +39,8 @@
|
|||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QSpinBox>
|
#include <QSpinBox>
|
||||||
#include <QStandardItemModel>
|
#include <QStandardItemModel>
|
||||||
|
#include <QStringListModel>
|
||||||
|
#include <QStyledItemDelegate>
|
||||||
#include <QTextEdit>
|
#include <QTextEdit>
|
||||||
#include <QUndoStack>
|
#include <QUndoStack>
|
||||||
|
|
||||||
@@ -899,6 +902,7 @@ public:
|
|||||||
class StringListAspectPrivate
|
class StringListAspectPrivate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
UndoableValue<QStringList> undoable;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FilePathListAspectPrivate
|
class FilePathListAspectPrivate
|
||||||
@@ -2392,13 +2396,94 @@ StringListAspect::StringListAspect(AspectContainer *container)
|
|||||||
*/
|
*/
|
||||||
StringListAspect::~StringListAspect() = default;
|
StringListAspect::~StringListAspect() = default;
|
||||||
|
|
||||||
/*!
|
class StringListDelegate : public QStyledItemDelegate
|
||||||
\reimp
|
{
|
||||||
*/
|
public:
|
||||||
|
void paint(QPainter *painter,
|
||||||
|
const QStyleOptionViewItem &option,
|
||||||
|
const QModelIndex &index) const override
|
||||||
|
{
|
||||||
|
QStyleOptionViewItem opt = option;
|
||||||
|
initStyleOption(&opt, index);
|
||||||
|
if (opt.text.isEmpty()) {
|
||||||
|
opt.state &= ~QStyle::State_Enabled;
|
||||||
|
opt.state &= ~QStyle::State_Selected;
|
||||||
|
opt.text = Tr::tr("Double click to add new entry ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
const QWidget *widget = opt.widget;
|
||||||
|
QStyle *style = widget ? widget->style() : QApplication::style();
|
||||||
|
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
|
||||||
|
|
||||||
|
if (opt.state & QStyle::State_Selected) {
|
||||||
|
Utils::Icons::EDIT_CLEAR.icon().paint(painter,
|
||||||
|
opt.rect.adjusted(0, 2, -2, -2),
|
||||||
|
Qt::AlignRight | Qt::AlignVCenter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool eventFilter(QObject *object, QEvent *event) override
|
||||||
|
{
|
||||||
|
return QStyledItemDelegate::eventFilter(object, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool editorEvent(QEvent *event,
|
||||||
|
QAbstractItemModel *model,
|
||||||
|
const QStyleOptionViewItem &option,
|
||||||
|
const QModelIndex &index) override
|
||||||
|
{
|
||||||
|
if (event->type() == QEvent::MouseButtonRelease) {
|
||||||
|
auto mouseEvent = static_cast<QMouseEvent *>(event);
|
||||||
|
QRect r = option.rect;
|
||||||
|
r.setLeft(option.rect.width() - option.rect.height());
|
||||||
|
r.setRight(option.rect.width());
|
||||||
|
if (r.contains(mouseEvent->pos())) {
|
||||||
|
removeCallback(index.row());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return QStyledItemDelegate::editorEvent(event, model, option, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<void(int index)> removeCallback;
|
||||||
|
};
|
||||||
|
|
||||||
void StringListAspect::addToLayout(LayoutItem &parent)
|
void StringListAspect::addToLayout(LayoutItem &parent)
|
||||||
{
|
{
|
||||||
Q_UNUSED(parent)
|
QListView *listView = new QListView();
|
||||||
// TODO - when needed.
|
listView->setMaximumHeight(100);
|
||||||
|
listView->setMinimumHeight(100);
|
||||||
|
listView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||||
|
auto delegate = new StringListDelegate;
|
||||||
|
listView->setItemDelegate(delegate);
|
||||||
|
|
||||||
|
registerSubWidget(listView);
|
||||||
|
|
||||||
|
QStringListModel *model = new QStringListModel(listView);
|
||||||
|
model->setStringList(value() << "");
|
||||||
|
|
||||||
|
connect(&d->undoable.m_signal, &UndoSignaller::changed, model, [model, this] {
|
||||||
|
model->setStringList(d->undoable.get() << "");
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(model, &QStringListModel::dataChanged, [this, model] {
|
||||||
|
QStringList newList = model->stringList();
|
||||||
|
newList.removeIf([](const QString &s) { return s.isEmpty(); });
|
||||||
|
pushUndo(d->undoable.set(newList));
|
||||||
|
handleGuiChanged();
|
||||||
|
});
|
||||||
|
|
||||||
|
delegate->removeCallback = [this, model](int index) {
|
||||||
|
QStringList newList = model->stringList();
|
||||||
|
newList.removeAt(index);
|
||||||
|
newList.removeIf([](const QString &s) { return s.isEmpty(); });
|
||||||
|
pushUndo(d->undoable.set(newList));
|
||||||
|
handleGuiChanged();
|
||||||
|
};
|
||||||
|
|
||||||
|
listView->setModel(model);
|
||||||
|
|
||||||
|
parent.addItem(listView);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringListAspect::appendValue(const QString &s, bool allowDuplicates)
|
void StringListAspect::appendValue(const QString &s, bool allowDuplicates)
|
||||||
@@ -2434,6 +2519,21 @@ void StringListAspect::removeValues(const QStringList &values)
|
|||||||
setValue(val);
|
setValue(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool StringListAspect::guiToBuffer()
|
||||||
|
{
|
||||||
|
const QStringList newValue = d->undoable.get();
|
||||||
|
if (newValue != m_buffer) {
|
||||||
|
m_buffer = newValue;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringListAspect::bufferToGui()
|
||||||
|
{
|
||||||
|
d->undoable.setWithoutUndo(m_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\class Utils::FilePathListAspect
|
\class Utils::FilePathListAspect
|
||||||
\inmodule QtCreator
|
\inmodule QtCreator
|
||||||
|
|||||||
@@ -780,6 +780,9 @@ public:
|
|||||||
void appendValues(const QStringList &values, bool allowDuplicates = true);
|
void appendValues(const QStringList &values, bool allowDuplicates = true);
|
||||||
void removeValues(const QStringList &values);
|
void removeValues(const QStringList &values);
|
||||||
|
|
||||||
|
bool guiToBuffer() override;
|
||||||
|
void bufferToGui() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Internal::StringListAspectPrivate> d;
|
std::unique_ptr<Internal::StringListAspectPrivate> d;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user