swift
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 
4 #include "listmodeldbobjects.h"
5 
6 #include <type_traits>
7 
8 #include <QBrush>
9 #include <QModelIndex>
10 
11 #include "gui/models/allmodelcontainers.h"
18 #include "misc/aviation/livery.h"
20 #include "misc/country.h"
21 #include "misc/countrylist.h"
23 #include "misc/orderable.h"
28 
29 using namespace swift::misc;
30 using namespace swift::misc::aviation;
31 using namespace swift::misc::db;
32 
33 namespace swift::gui::models
34 {
35  template <typename T, typename K, bool UseCompare>
36  CListModelDbObjects<T, K, UseCompare>::CListModelDbObjects(const QString &translationContext, QObject *parent)
37  : CListModelBase<ContainerType, UseCompare>(translationContext, parent)
38  {
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>
50  QVariant CListModelDbObjects<T, K, UseCompare>::data(const QModelIndex &index, int role) const
51  {
52  if (role != Qt::BackgroundRole) { return CListModelBase<ContainerType, UseCompare>::data(index, role); }
53  if (isHighlightedIndex(index)) { return QBrush(m_highlightColor); }
55  }
56 
57  template <typename T, typename K, bool UseCompare>
58  K CListModelDbObjects<T, K, UseCompare>::dbKeyForIndex(const QModelIndex &index) const
59  {
60  if (!index.isValid()) { return ObjectType::invalidDbKey(); }
61  return this->at(index).getDbKey();
62  }
63 
64  template <typename T, typename K, bool UseCompare>
65  bool CListModelDbObjects<T, K, UseCompare>::isHighlightedIndex(const QModelIndex &index) const
66  {
67  if (!index.isValid()) { return false; }
68  if (m_highlightKeys.isEmpty()) { return false; }
69  return m_highlightKeys.contains(dbKeyForIndex(index));
70  }
71 
72  template <typename T, typename K, bool UseCompare>
74  QObject *parent)
75  : CListModelDbObjects<ContainerType, KeyType, UseCompare>(translationContext, parent)
76  {}
77 
78  template <typename T, typename K, bool UseCompare>
80  {
81  if (items.isEmpty()) { return; }
82  ContainerType container(this->container());
83  int order = 0;
84  if (position >= 0 && position < container.size()) { order = container[position].getOrder(); }
85  this->setSortColumnToOrder();
86  container.moveTo(items, order);
87 
88  // update container
89  this->updateContainerMaybeAsync(container);
90  }
91 
92  template <typename T, typename K, bool UseCompare>
94  {
95  // force sorted by order, otherwise display looks confusing
96  return this->setSorting(IOrderable::IndexOrder);
97  }
98 
99  template <typename T, typename K, bool UseCompare>
101  {
102  if (container.needsOrder())
103  {
104  ContainerType orderable(container);
105  orderable.resetOrder();
107  }
109  }
110 
120 
121 } // namespace swift::gui::models
QVariant data(const QModelIndex &index, int role) const
virtual int update(const ContainerType &container, bool sort=true)
Update by new container.
swift::misc::CPropertyIndexList m_sortTieBreakers
how column values are sorted if equal, if no value is given this is random
KeyType dbKeyForIndex(const QModelIndex &index) const
DB key for given index.
bool isHighlightedIndex(const QModelIndex &index) const
Highlight index.
virtual 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.
virtual int update(const ContainerType &container, bool sort)
Update by new container.
COrderableListModelDbObjects(const QString &translationContext, QObject *parent=nullptr)
Constructor.
virtual void moveItems(const ContainerType &items, int position)
Move items to position, normally called from dropMimeData.
void push_front(const T &value)
Insert as first element.
Definition: sequence.h:308
Models to be used with views, mainly QTableView.
Free functions in swift::misc.