6 #ifndef SWIFT_GUI_MODELS_LISTMODELBASE_H
7 #define SWIFT_GUI_MODELS_LISTMODELBASE_H
11 #include <QJsonDocument>
12 #include <QJsonObject>
13 #include <QModelIndex>
14 #include <QModelIndexList>
33 template <
typename T,
bool UseCompare = false>
50 QVariant
data(
const QModelIndex &
index,
int role)
const override;
53 bool setData(
const QModelIndex &
index,
const QVariant &value,
int role = Qt::EditRole)
final;
56 QMimeData *
mimeData(
const QModelIndexList &indexes)
const final;
59 void sort(
int column, Qt::SortOrder order)
final;
62 int rowCount(
const QModelIndex &parentIndex = QModelIndex()) const final;
66 const QModelIndex &
parent) const final;
70 const QModelIndex &
parent) final;
77 QJsonObject
toJson(
bool selectedOnly = false) const override;
80 QString
toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented,
81 bool selectedOnly = false) const override;
135 void truncate(
int maxNumber,
bool forceSort = false);
161 template <class K0, class V0, class... KeysValues>
162 int removeIf(K0 k0, V0 v0, KeysValues... keysValues)
171 virtual void clear();
199 void onDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomLeft,
200 const QVector<int> &roles)
override;
214 std::unique_ptr<IModelFilter<ContainerType>>
m_filter;
221 template <
class ObjectType>
226 const int c = a.comparePropertyByIndex(index, b);
231 return compareForModelSort<ObjectType>(a, b, order, tieBreakers.
front(),
236 return (order == Qt::AscendingOrder) ? (c < 0) : (c > 0);
240 template <
typename ObjectType>
247 if (!tieBreakers.
isEmpty() && aQv == bQv)
249 return compareForModelSort<ObjectType>(a, b, order, tieBreakers.
front(), tieBreakers.
copyFrontRemoved(),
252 return (order == Qt::AscendingOrder) ? (aQv < bQv) : (bQv < aQv);
const ContainerType & container() const
Used container data.
virtual void clear()
Clear the list.
~CListModelBase()=default
Destructor.
virtual const ObjectType & at(const QModelIndex &index) const
Object at row position.
QMimeData * mimeData(const QModelIndexList &indexes) const final
void sort()
Sort by given sort order.
virtual bool isEmpty() const
Empty?
void removeFilter()
Remove filter.
QVariant data(const QModelIndex &index, int role) const
bool dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, const QModelIndex &parent) final
ISelectionModel< ContainerType > * m_selectionModel
selection model
virtual void push_back(const ObjectType &object)
Similar to ContainerType::push_back.
bool hasFilter() const
Filter available.
void takeFilterOwnership(std::unique_ptr< IModelFilter< ContainerType >> &filter)
Set the filter.
QString toJsonString(QJsonDocument::JsonFormat format=QJsonDocument::Indented, bool selectedOnly=false) const
Convert to JSON string.
virtual void insert(const ObjectType &object)
Similar to ContainerType::insert here inserts at first position.
void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomLeft, const QVector< int > &roles)
Feedback when QStandardItemModel::dataChanged was called.
bool isOrderable() const
Orderable, normally use a container swift::misc::IOrderableList.
virtual swift::misc::CWorker * updateAsync(const ContainerType &container, bool sort=true)
Asynchronous update.
std::unique_ptr< IModelFilter< ContainerType > > m_filter
used filter
void setSelectionModel(swift::gui::models::ISelectionModel< ContainerType > *selectionModel)
Set the selection model.
void truncate(int maxNumber, bool forceSort=false)
Truncate to given number.
void onChangedDigest()
Feedback when QStandardItemModel::dataChanged was called.
T ContainerType
Container type.
typename T::value_type ObjectType
Container element type.
int removeIf(K0 k0, V0 v0, KeysValues... keysValues)
Remove elements matching some particular key/value pair(s).
virtual void remove(const ObjectType &object)
Remove object.
virtual bool isValidIndex(const QModelIndex &index) const
Valid index (in range)
ContainerType m_container
used container
void updateFilteredContainer()
Update filtered container.
ContainerType m_containerFiltered
cache for filtered container data
int rowCount(const QModelIndex &parentIndex=QModelIndex()) const final
QJsonObject toJson(bool selectedOnly=false) const
Convert to JSON.
ContainerType sortContainerByColumn(const ContainerType &container, int column, Qt::SortOrder order) const
Sort container by given column / order. This is used by sort() but als for asynchronous updates in th...
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) final
const ContainerType & containerOrFilteredContainer(bool *filtered=nullptr) const
Full container or cached filtered container as approproiate.
const ContainerType & containerFiltered() const
Used container data.
virtual void moveItems(const ContainerType &items, int position)
Move items to position, normally called from dropMimeData.
void emitModelDataChanged()
Model changed.
void resort()
Sort by given sort order.
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const final
virtual int update(const ContainerType &container, bool sort=true)
Update by new container.
CListModelBase(const QString &translationContext, QObject *parent=nullptr)
Constructor.
virtual void updateContainerMaybeAsync(const ContainerType &container, bool sort=true)
Update by new container.
bool setInContainer(const QModelIndex &index, const ObjectType &obj)
Simple set of data in container, using class is responsible for firing signals etc.
Non templated base class, allows Q_OBJECT and signals to be used.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const final
QModelIndex parent(const QModelIndex &child) const final
Allow to get and select objects.
Value object encapsulating a list of property indexes.
Q_REQUIRED_RESULT CPropertyIndexList copyFrontRemoved() const
List without front element, or empty list if not applicable.
reference front()
Access the first element.
bool isEmpty() const
Synonym for empty.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
bool compareForModelSort(const ObjectType &a, const ObjectType &b, Qt::SortOrder order, const swift::misc::CPropertyIndex &index, const swift::misc::CPropertyIndexList &tieBreakers, std::true_type)
Sort with compare function.
Models to be used with views, mainly QTableView.
auto MemberEqual(Ts... vs)
Predicate which tests whether some member functions return some values.
Free functions in swift::misc.