template class EE::UI::Models::ObservableListModel

Overview

One-column model adapter for an ObservableVector. More…

#include <observablelistmodel.hpp>

template <typename T>
class ObservableListModel: public EE::UI::Models::Model {
public:
    // typedefs

    typedef std::function<Variant(const T&, ModelRole)> Formatter;
    typedef std::function<bool(const T&)> Predicate;

    // construction

    ObservableListModel(ObservableVector<T>& source, Formatter formatter = {});
    ~ObservableListModel();

    // methods

    static std::shared_ptr<ObservableListModel> create(ObservableVector<T>& source);
    static std::shared_ptr<ObservableListModel> create(ObservableVector<T>& source, Formatter formatter);
    virtual size_t rowCount(const ModelIndex& = ModelIndex()) const;
    virtual size_t columnCount(const ModelIndex& = ModelIndex()) const;
    virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const;
    virtual Variant data(const ModelIndex& index, ModelRole role = ModelRole::Display) const;
    void setFilter(Predicate predicate);
    void clearFilter();
    std::optional<std::size_t> sourceRow(const ModelIndex& index) const;
    const T* at(const ModelIndex& index) const;
};

Inherited Members

public:
    // typedefs

    typedef std::function<Variant(const ModelIndex&, const void*data)> ModelStyler;

    // enums

    enum UpdateFlag;

    // structs

    struct Operation;

    // classes

    class Client;

    // methods

    virtual bool hasChildren(const ModelIndex& modelIndex = ModelIndex()) const;
    virtual size_t rowCount(const ModelIndex& = ModelIndex()) const = 0;
    virtual size_t columnCount(const ModelIndex& = ModelIndex()) const = 0;
    virtual std::string columnName(const size_t&) const;
    virtual std::string rowName(const size_t&) const;
    virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const = 0;
    virtual void update();
    virtual ModelIndex parentIndex(const ModelIndex&) const;
    virtual ModelIndex index(int row, int column = 0, const ModelIndex& = ModelIndex()) const;
    virtual ModelIndex sibling(int row, int column, const ModelIndex& parent) const;
    virtual void setData(const ModelIndex&, const Variant&);
    virtual size_t treeColumn() const;
    virtual bool acceptsDrag(const ModelIndex&, const std::string& dataType);
    virtual bool isColumnSortable(const size_t&) const;
    virtual std::string dragDataType() const;
    virtual bool isEditable(const ModelIndex&) const;
    virtual bool isValid(const ModelIndex& index) const;
    virtual int keyColumn() const;
    virtual SortOrder sortOrder() const;
    virtual bool isSortable();
    virtual void sort(const size_t&, const SortOrder&);
    virtual bool classModelRoleEnabled();
    virtual bool tooltipModelRoleEnabled();
    void registerView(UIAbstractView*);
    void unregisterView(UIAbstractView*);
    void registerClient(Client*);
    void unregisterClient(Client*);
    void refreshView() const;
    void setOnUpdate(const std::function<void()>& onUpdate);
    virtual void invalidate(unsigned int flags = Model::UpdateFlag::InvalidateAllIndexes);
    std::weak_ptr<PersistentHandle> registerPersistentIndex(ModelIndex const&);
    void beginInsertRows(ModelIndex const& parent, int first, int last);
    void beginInsertColumns(ModelIndex const& parent, int first, int last);
    void beginMoveRows(ModelIndex const& sourceParent, int first, int last, ModelIndex const& targetParent, int target_index);
    void beginMoveColumns(ModelIndex const& sourceParent, int first, int last, ModelIndex const& targetParent, int target_index);
    bool beginDeleteRows(ModelIndex const& parent, int first, int last);
    bool beginDeleteRows(ModelIndex const& index);
    bool beginDeleteColumns(ModelIndex const& parent, int first, int last);
    void endInsertRows();
    void endInsertColumns();
    void endMoveRows();
    void endMoveColumns();
    void endDeleteRows();
    void endDeleteColumns();
    Mutex& resourceMutex();
    void acquireResourceMutex();
    void releaseResourceMutex();
    Uint32 subscribeModelStyler(const ModelStyler& styler);
    void unsubscribeModelStyler(Uint32 id);

Detailed Documentation

One-column model adapter for an ObservableVector.

Unfiltered sources preserve incremental model notifications. A filtered projection rebuilds its row mapping when source membership can change. Custom formatters allow domain types to remain independent from Variant. The model retains the source storage, so it remains safe when a view outlives the ObservableVector wrapper. Mutations naturally stop when that wrapper is destroyed.

Filtering changes model row numbers. Use sourceRow() or at() before mutating the source from a selection in the filtered view.

ObservableVector<Person> people;
auto model = ObservableListModel<Person>::create(
    people, []( const Person& person, ModelRole role ) {
        return role == ModelRole::Display ? Variant( person.name ) : Variant{};
    } );
model->setFilter( []( const Person& person ) { return person.active; } );

Typedefs

typedef std::function<Variant(const T&, ModelRole)> Formatter

Converts one source item and role into model data.

typedef std::function<bool(const T&)> Predicate

Returns true when a source item belongs in the visible projection.

Construction

ObservableListModel(ObservableVector<T>& source, Formatter formatter = {})

Creates an adapter over source, optionally using formatter for all roles.

The source storage is retained for the model’s lifetime.

Methods

static std::shared_ptr<ObservableListModel> create(ObservableVector<T>& source)

Creates a one-column adapter using Variant ‘s standard conversion for Display data.

static std::shared_ptr<ObservableListModel> create(ObservableVector<T>& source, Formatter formatter)

Creates a one-column adapter whose data() is supplied by formatter.

virtual size_t rowCount(const ModelIndex& = ModelIndex()) const

Returns:

The number of visible rows in the current projection.

virtual size_t columnCount(const ModelIndex& = ModelIndex()) const

Returns:

One; ObservableListModel is a flat, one-column model.

virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const

Returns:

A valid index for a visible row in column zero, or an invalid index.

virtual Variant data(const ModelIndex& index, ModelRole role = ModelRole::Display) const

Returns:

Formatted data for index and role, or an empty Variant when unavailable.

void setFilter(Predicate predicate)

Replaces the visible filter and invalidates all model indexes.

An empty predicate is equivalent to no filter. While filtered, source mutations rebuild the projection and invalidate indexes instead of emitting incremental row notifications.

void clearFilter()

Removes the active filter and exposes all source rows.

std::optional<std::size_t> sourceRow(const ModelIndex& index) const

Returns:

The ObservableVector row represented by index, or std::nullopt for an invalid or foreign index.

const T* at(const ModelIndex& index) const

Warning

The pointer is invalidated by mutations that reallocate or remove vector elements.

Returns:

The source item represented by index, or nullptr for an invalid or foreign index.