Utils: Add FilePathListAspect

Change-Id: Iec89581e5818139bcc48ed807935c10421b7b664
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-09-12 09:42:18 +02:00
parent 7bb8f59587
commit ed0935733e
2 changed files with 153 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
#include "layoutbuilder.h" #include "layoutbuilder.h"
#include "passworddialog.h" #include "passworddialog.h"
#include "pathchooser.h" #include "pathchooser.h"
#include "pathlisteditor.h"
#include "qtcassert.h" #include "qtcassert.h"
#include "qtcolorbutton.h" #include "qtcolorbutton.h"
#include "qtcsettings.h" #include "qtcsettings.h"
@@ -600,6 +601,12 @@ void BaseAspect::registerSubWidget(QWidget *widget)
widget->setVisible(d->m_visible); widget->setVisible(d->m_visible);
} }
void BaseAspect::forEachSubWidget(const std::function<void(QWidget *)> &func)
{
for (auto w : d->m_subWidgets)
func(w);
}
void BaseAspect::saveToMap(Store &data, const QVariant &value, void BaseAspect::saveToMap(Store &data, const QVariant &value,
const QVariant &defaultValue, const Key &key) const QVariant &defaultValue, const Key &key)
{ {
@@ -885,6 +892,13 @@ class StringListAspectPrivate
public: public:
}; };
class FilePathListAspectPrivate
{
public:
UndoableValue<QStringList> undoable;
QString placeHolderText;
};
class TextDisplayPrivate class TextDisplayPrivate
{ {
public: public:
@@ -2401,6 +2415,116 @@ void StringListAspect::removeValues(const QStringList &values)
setValue(val); setValue(val);
} }
/*!
\class Utils::FilePathListAspect
\inmodule QtCreator
\brief A filepath list aspect represents a property of some object
that is a list of filepathList.
*/
FilePathListAspect::FilePathListAspect(AspectContainer *container)
: TypedAspect(container)
, d(new Internal::FilePathListAspectPrivate)
{
setDefaultValue(QStringList());
}
FilePathListAspect::~FilePathListAspect() = default;
FilePaths FilePathListAspect::operator()() const
{
return Utils::transform(m_internal, &FilePath::fromUserInput);
}
bool FilePathListAspect::guiToBuffer()
{
const QStringList newValue = d->undoable.get();
if (newValue != m_buffer) {
m_buffer = newValue;
return true;
}
return false;
}
void FilePathListAspect::bufferToGui()
{
d->undoable.setWithoutUndo(m_buffer);
}
void FilePathListAspect::addToLayout(LayoutItem &parent)
{
d->undoable.setSilently(value());
PathListEditor *editor = new PathListEditor;
editor->setPathList(value());
connect(editor, &PathListEditor::changed, this, [this, editor] {
pushUndo(d->undoable.set(editor->pathList()));
});
connect(&d->undoable.m_signal, &UndoSignaller::changed, this, [this, editor] {
if (editor->pathList() != d->undoable.get())
editor->setPathList(d->undoable.get());
handleGuiChanged();
});
editor->setToolTip(toolTip());
editor->setMaximumHeight(100);
editor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
editor->setPlaceholderText(d->placeHolderText);
registerSubWidget(editor);
parent.addItem(editor);
}
void FilePathListAspect::setPlaceHolderText(const QString &placeHolderText)
{
d->placeHolderText = placeHolderText;
forEachSubWidget([placeHolderText](QWidget *widget) {
if (auto pathListEditor = qobject_cast<PathListEditor *>(widget)) {
pathListEditor->setPlaceholderText(placeHolderText);
}
});
}
void FilePathListAspect::appendValue(const FilePath &path, bool allowDuplicates)
{
const QString asString = path.toUserOutput();
QStringList val = value();
if (allowDuplicates || !val.contains(asString))
val.append(asString);
setValue(val);
}
void FilePathListAspect::removeValue(const FilePath &s)
{
QStringList val = value();
val.removeAll(s.toUserOutput());
setValue(val);
}
void FilePathListAspect::appendValues(const FilePaths &paths, bool allowDuplicates)
{
QStringList val = value();
for (const FilePath &path : paths) {
const QString asString = path.toUserOutput();
if (allowDuplicates || !val.contains(asString))
val.append(asString);
}
setValue(val);
}
void FilePathListAspect::removeValues(const FilePaths &paths)
{
QStringList val = value();
for (const FilePath &path : paths)
val.removeAll(path.toUserOutput());
setValue(val);
}
/*! /*!
\class Utils::IntegerListAspect \class Utils::IntegerListAspect
\internal \internal

View File

@@ -38,6 +38,7 @@ class BoolAspectPrivate;
class ColorAspectPrivate; class ColorAspectPrivate;
class DoubleAspectPrivate; class DoubleAspectPrivate;
class FilePathAspectPrivate; class FilePathAspectPrivate;
class FilePathListAspectPrivate;
class IntegerAspectPrivate; class IntegerAspectPrivate;
class MultiSelectionAspectPrivate; class MultiSelectionAspectPrivate;
class SelectionAspectPrivate; class SelectionAspectPrivate;
@@ -243,6 +244,8 @@ protected:
static void saveToMap(Store &data, const QVariant &value, static void saveToMap(Store &data, const QVariant &value,
const QVariant &defaultValue, const Key &key); const QVariant &defaultValue, const Key &key);
void forEachSubWidget(const std::function<void(QWidget *)> &func);
protected: protected:
template <class Value> template <class Value>
static bool updateStorage(Value &target, const Value &val) static bool updateStorage(Value &target, const Value &val)
@@ -774,6 +777,31 @@ private:
std::unique_ptr<Internal::StringListAspectPrivate> d; std::unique_ptr<Internal::StringListAspectPrivate> d;
}; };
class QTCREATOR_UTILS_EXPORT FilePathListAspect : public TypedAspect<QStringList>
{
Q_OBJECT
public:
FilePathListAspect(AspectContainer *container = nullptr);
~FilePathListAspect() override;
FilePaths operator()() const;
bool guiToBuffer() override;
void bufferToGui() override;
void addToLayout(Layouting::LayoutItem &parent) override;
void setPlaceHolderText(const QString &placeHolderText);
void appendValue(const FilePath &path, bool allowDuplicates = true);
void removeValue(const FilePath &path);
void appendValues(const FilePaths &values, bool allowDuplicates = true);
void removeValues(const FilePaths &values);
private:
std::unique_ptr<Internal::FilePathListAspectPrivate> d;
};
class QTCREATOR_UTILS_EXPORT IntegersAspect : public TypedAspect<QList<int>> class QTCREATOR_UTILS_EXPORT IntegersAspect : public TypedAspect<QList<int>>
{ {
Q_OBJECT Q_OBJECT
@@ -949,6 +977,7 @@ public:
} }
void setSilently(const T &value) { m_value = value; } void setSilently(const T &value) { m_value = value; }
void setWithoutUndo(const T &value) { setInternal(value); }
T get() const { return m_value; } T get() const { return m_value; }