Utils: Implement StringListAspect::addToLayout

Change-Id: I48806e397a8fa51c9b31860645a16c242e789a3d
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-05-28 13:54:37 +02:00
parent 01e0e43443
commit fbb2f70747
2 changed files with 149 additions and 5 deletions

View File

@@ -39,6 +39,7 @@
#include <QSpinBox> #include <QSpinBox>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QTextEdit> #include <QTextEdit>
#include <QTreeWidget>
#include <QUndoStack> #include <QUndoStack>
using namespace Layouting; using namespace Layouting;
@@ -931,6 +932,10 @@ public:
class StringListAspectPrivate class StringListAspectPrivate
{ {
public: public:
UndoableValue<QStringList> undoable;
bool allowAdding{true};
bool allowRemoving{true};
bool allowEditing{true};
}; };
class FilePathListAspectPrivate class FilePathListAspectPrivate
@@ -2629,13 +2634,115 @@ StringListAspect::StringListAspect(AspectContainer *container)
*/ */
StringListAspect::~StringListAspect() = default; StringListAspect::~StringListAspect() = default;
/*! bool StringListAspect::guiToBuffer()
\reimp {
*/ 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);
}
void StringListAspect::addToLayout(Layout &parent) void StringListAspect::addToLayout(Layout &parent)
{ {
Q_UNUSED(parent) d->undoable.setSilently(value());
// TODO - when needed.
auto editor = new QTreeWidget();
editor->setHeaderHidden(true);
editor->setRootIsDecorated(false);
editor->setEditTriggers(
d->allowEditing ? QAbstractItemView::AllEditTriggers : QAbstractItemView::NoEditTriggers);
QPushButton *add = d->allowAdding ? new QPushButton(Tr::tr("Add")) : nullptr;
QPushButton *remove = d->allowRemoving ? new QPushButton(Tr::tr("Remove")) : nullptr;
auto itemsToStringList = [editor] {
QStringList items;
const QTreeWidgetItem *rootItem = editor->invisibleRootItem();
for (int i = 0, count = rootItem->childCount(); i < count; ++i) {
auto expr = rootItem->child(i)->data(0, Qt::DisplayRole).toString();
items.append(expr);
}
return items;
};
auto populate = [editor, this] {
editor->clear();
for (const QString &entry : d->undoable.get()) {
auto item = new QTreeWidgetItem(editor, {entry});
item->setData(0, Qt::ToolTipRole, entry);
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
}
};
connect(add, &QPushButton::clicked, this, [this, populate, editor] {
d->undoable.setSilently(d->undoable.get() << "");
populate();
const QTreeWidgetItem *root = editor->invisibleRootItem();
QTreeWidgetItem *lastChild = root->child(root->childCount() - 1);
const QModelIndex index = editor->indexFromItem(lastChild, 0);
editor->edit(index);
});
connect(remove, &QPushButton::clicked, this, [this, editor, itemsToStringList] {
const QList<QTreeWidgetItem *> selected = editor->selectedItems();
QTC_ASSERT(selected.size() == 1, return);
editor->invisibleRootItem()->removeChild(selected.first());
delete selected.first();
d->undoable.set(undoStack(), itemsToStringList());
});
connect(
&d->undoable.m_signal, &UndoSignaller::changed, editor, [this, populate, itemsToStringList] {
if (itemsToStringList() != d->undoable.get())
populate();
handleGuiChanged();
});
connect(
editor->model(),
&QAbstractItemModel::dataChanged,
this,
[this,
itemsToStringList](const QModelIndex &tl, const QModelIndex &br, const QList<int> &roles) {
if (!roles.contains(Qt::DisplayRole))
return;
if (tl != br)
return;
d->undoable.set(undoStack(), itemsToStringList());
});
populate();
parent.addItem(
// clang-format off
Column {
createLabel(),
Row {
editor,
If { d->allowAdding || d->allowRemoving, {
Column {
If { d->allowAdding, {add}, {}},
If { d->allowRemoving, {remove}, {}},
st,
}
}, {}},
}
} // clang-format on
);
registerSubWidget(editor);
if (d->allowAdding)
registerSubWidget(add);
if (d->allowRemoving)
registerSubWidget(remove);
} }
void StringListAspect::appendValue(const QString &s, bool allowDuplicates) void StringListAspect::appendValue(const QString &s, bool allowDuplicates)
@@ -2671,6 +2778,32 @@ void StringListAspect::removeValues(const QStringList &values)
setValue(val); setValue(val);
} }
void StringListAspect::setUiAllowAdding(bool allowAdding)
{
d->allowAdding = allowAdding;
}
void StringListAspect::setUiAllowRemoving(bool allowRemoving)
{
d->allowRemoving = allowRemoving;
}
void StringListAspect::setUiAllowEditing(bool allowEditing)
{
d->allowEditing = allowEditing;
}
bool StringListAspect::uiAllowAdding() const
{
return d->allowAdding;
}
bool StringListAspect::uiAllowRemoving() const
{
return d->allowRemoving;
}
bool StringListAspect::uiAllowEditing() const
{
return d->allowEditing;
}
/*! /*!
\class Utils::FilePathListAspect \class Utils::FilePathListAspect
\inmodule QtCreator \inmodule QtCreator

View File

@@ -828,6 +828,9 @@ public:
StringListAspect(AspectContainer *container = nullptr); StringListAspect(AspectContainer *container = nullptr);
~StringListAspect() override; ~StringListAspect() override;
bool guiToBuffer() override;
void bufferToGui() override;
void addToLayout(Layouting::Layout &parent) override; void addToLayout(Layouting::Layout &parent) override;
void appendValue(const QString &value, bool allowDuplicates = true); void appendValue(const QString &value, bool allowDuplicates = true);
@@ -835,6 +838,14 @@ 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);
void setUiAllowAdding(bool allowAdding);
void setUiAllowRemoving(bool allowRemoving);
void setUiAllowEditing(bool allowEditing);
bool uiAllowAdding() const;
bool uiAllowRemoving() const;
bool uiAllowEditing() const;
private: private:
std::unique_ptr<Internal::StringListAspectPrivate> d; std::unique_ptr<Internal::StringListAspectPrivate> d;
}; };