swift
Loading...
Searching...
No Matches
listmodeldbobjects.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2053 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <type_traits>
7
8#include <QBrush>
9#include <QModelIndex>
10
11#include "gui/models/allmodelcontainers.h"
20#include "misc/country.h"
21#include "misc/countrylist.h"
23#include "misc/orderable.h"
28
29using namespace swift::misc;
30using namespace swift::misc::aviation;
31using namespace swift::misc::db;
32
33namespace swift::gui::models
34{
35 template <typename T, typename K, bool UseCompare>
37 : CListModelBase<ContainerType, UseCompare>(translationContext, parent)
38 {
39 CListModelBaseNonTemplate::m_sortTieBreakers.push_front(ObjectType::keyIndex());
40
41 constexpr bool hasIntegerKey =
42 std::is_base_of_v<IDatastoreObjectWithIntegerKey, ObjectType> && std::is_same_v<int, KeyType>;
43 constexpr bool hasStringKey =
44 std::is_base_of_v<IDatastoreObjectWithStringKey, ObjectType> && std::is_base_of_v<QString, KeyType>;
45 static_assert(hasIntegerKey || hasStringKey,
46 "ObjectType needs to implement IDatastoreObjectWithXXXXKey and have appropriate KeyType");
47 }
48
49 template <typename T, typename K, bool UseCompare>
51 {
52 if (role != Qt::BackgroundRole)
53 {
55 }
57 {
58 return QBrush(m_highlightColor);
63 template <typename T, typename K, bool UseCompare>
65 {
66 if (!index.isValid())
67 {
68 return ObjectType::invalidDbKey();
69 }
70 return this->at(index).getDbKey();
71 }
72
73 template <typename T, typename K, bool UseCompare>
75 {
76 if (!index.isValid())
77 {
78 return false;
79 }
80 if (m_highlightKeys.isEmpty())
81 {
82 return false;
83 }
84 return m_highlightKeys.contains(dbKeyForIndex(index));
85 }
86
87 template <typename T, typename K, bool UseCompare>
92
93 template <typename T, typename K, bool UseCompare>
95 {
96 if (items.isEmpty())
97 {
98 return;
99 }
100 ContainerType container(this->container());
101 int order = 0;
102 if (position >= 0 && position < container.size())
103 {
104 order = container[position].getOrder();
106 this->setSortColumnToOrder();
107 container.moveTo(items, order);
108
109 // update container
111 }
112
113 template <typename T, typename K, bool UseCompare>
115 {
116 // force sorted by order, otherwise display looks confusing
117 return this->setSorting(IOrderable::IndexOrder);
118 }
119
120 template <typename T, typename K, bool UseCompare>
131
141
142} // namespace swift::gui::models
QVariant data(const QModelIndex &index, int role) const
virtual int update(const ContainerType &container, bool sort=true)
CListModelBase(const QString &translationContext, QObject *parent=nullptr)
virtual void updateContainerMaybeAsync(const ContainerType &container, bool sort=true)
void sort(int column, Qt::SortOrder order) final
virtual bool setSorting(const swift::misc::CPropertyIndex &propertyIndex, Qt::SortOrder order=Qt::AscendingOrder)
Sorting.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const final
QModelIndex parent(const QModelIndex &child) const final
swift::misc::CPropertyIndexList m_sortTieBreakers
how column values are sorted if equal, if no value is given this is random
CListModelDbObjects(const QString &translationContext, QObject *parent=nullptr)
Constructor.
KeyType dbKeyForIndex(const QModelIndex &index) const
DB key for given index.
bool isHighlightedIndex(const QModelIndex &index) const
Highlight index.
QVariant data(const QModelIndex &index, int role) const
Get data for index and role.
bool setSortColumnToOrder()
Sort order to order property swift::misc::IOrderable::IndexOrder.
int update(const ContainerType &container, bool sort)
Update by new container.
COrderableListModelDbObjects(const QString &translationContext, QObject *parent=nullptr)
Constructor.
void moveItems(const ContainerType &items, int position)
Move items to position, normally called from dropMimeData.
Models to be used with views, mainly QTableView.
Free functions in swift::misc.
QObject(QObject *parent)
BackgroundRole