diff options
author | Marcus Tillmanns <[email protected]> | 2023-09-12 09:42:18 +0200 |
---|---|---|
committer | Marcus Tillmanns <[email protected]> | 2023-09-12 08:51:20 +0000 |
commit | ed0935733efbd4ff25a2664e3e91b672f3a645c1 (patch) | |
tree | c40f126f2dd1c14d270e4c8529c3c19a3c47f371 /src | |
parent | 7bb8f59587b8e2a417a70b8e14da7fd5a7032256 (diff) |
Utils: Add FilePathListAspect
Change-Id: Iec89581e5818139bcc48ed807935c10421b7b664
Reviewed-by: hjk <[email protected]>
Diffstat (limited to 'src')
-rw-r--r-- | src/libs/utils/aspects.cpp | 124 | ||||
-rw-r--r-- | src/libs/utils/aspects.h | 29 |
2 files changed, 153 insertions, 0 deletions
diff --git a/src/libs/utils/aspects.cpp b/src/libs/utils/aspects.cpp index f1559b77167..800b0fb2995 100644 --- a/src/libs/utils/aspects.cpp +++ b/src/libs/utils/aspects.cpp @@ -10,6 +10,7 @@ #include "layoutbuilder.h" #include "passworddialog.h" #include "pathchooser.h" +#include "pathlisteditor.h" #include "qtcassert.h" #include "qtcolorbutton.h" #include "qtcsettings.h" @@ -600,6 +601,12 @@ void BaseAspect::registerSubWidget(QWidget *widget) 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, const QVariant &defaultValue, const Key &key) { @@ -885,6 +892,13 @@ class StringListAspectPrivate public: }; +class FilePathListAspectPrivate +{ +public: + UndoableValue<QStringList> undoable; + QString placeHolderText; +}; + class TextDisplayPrivate { public: @@ -2402,6 +2416,116 @@ void StringListAspect::removeValues(const QStringList &values) } /*! + \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 \internal \inmodule QtCreator diff --git a/src/libs/utils/aspects.h b/src/libs/utils/aspects.h index 35773ce7d25..0ffcaec7cb0 100644 --- a/src/libs/utils/aspects.h +++ b/src/libs/utils/aspects.h @@ -38,6 +38,7 @@ class BoolAspectPrivate; class ColorAspectPrivate; class DoubleAspectPrivate; class FilePathAspectPrivate; +class FilePathListAspectPrivate; class IntegerAspectPrivate; class MultiSelectionAspectPrivate; class SelectionAspectPrivate; @@ -243,6 +244,8 @@ protected: static void saveToMap(Store &data, const QVariant &value, const QVariant &defaultValue, const Key &key); + void forEachSubWidget(const std::function<void(QWidget *)> &func); + protected: template <class Value> static bool updateStorage(Value &target, const Value &val) @@ -774,6 +777,31 @@ private: 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>> { Q_OBJECT @@ -949,6 +977,7 @@ public: } void setSilently(const T &value) { m_value = value; } + void setWithoutUndo(const T &value) { setInternal(value); } T get() const { return m_value; } |