swift
orderable.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/orderable.h"
5 
6 #include <limits>
7 
8 #include <QByteArray>
9 #include <QtGlobal>
10 
11 #include "misc/comparefunctions.h"
12 #include "misc/verify.h"
13 
14 namespace swift::misc
15 {
17 
18  IOrderable::IOrderable(int order) : m_order(order) {}
19 
21  {
22  if (this->hasValidOrder()) { return QString::number(this->getOrder()); }
23  return QStringLiteral("-");
24  }
25 
26  bool IOrderable::hasValidOrder() const { return this->getOrder() >= 0; }
27 
29  {
30  return (index >= static_cast<int>(IndexOrder)) && (index <= static_cast<int>(IndexOrderString));
31  }
32 
34  {
35  if (index.isEmpty()) { return false; }
36  const int i = index.frontCasted<int>();
37  return isAnyOrderIndex(i);
38  }
39 
41  {
42  if (!index.isEmpty())
43  {
44  const ColumnIndex i = index.frontCasted<ColumnIndex>();
45  switch (i)
46  {
47  case IndexOrder: return QVariant::fromValue(this->m_order);
48  case IndexOrderString: return QVariant::fromValue(this->getOrderAsString());
49  default: break;
50  }
51  }
52  const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
53  SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
54  return QVariant::fromValue(m);
55  }
56 
57  void IOrderable::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
58  {
59  if (!index.isEmpty())
60  {
61  const ColumnIndex i = index.frontCasted<ColumnIndex>();
62  switch (i)
63  {
64  case IndexOrder: this->setOrder(variant.toInt()); return;
65  case IndexOrderString:
66  default: break;
67  }
68  }
69  const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
70  SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
71  }
72 
73  int IOrderable::comparePropertyByIndex(CPropertyIndexRef index, const IOrderable &compareValue) const
74  {
75  Q_UNUSED(index)
76  static const int max = std::numeric_limits<int>::max();
77  const int o1 = this->hasValidOrder() ? this->getOrder() : max;
78  const int o2 = compareValue.hasValidOrder() ? compareValue.getOrder() : max;
79  return Compare::compare(o1, o2);
80  }
81 } // namespace swift::misc
Non-owning reference to a CPropertyIndex with a subset of its features.
QString toQString(bool i18n=false) const
Cast as QString.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
Entity with order attribute (can be manually ordered in views)
Definition: orderable.h:19
int m_order
order number
Definition: orderable.h:62
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
Definition: orderable.cpp:33
static bool isAnyOrderIndex(int index)
Any order index.
Definition: orderable.cpp:28
int comparePropertyByIndex(CPropertyIndexRef index, const IOrderable &compareValue) const
Compare for index.
Definition: orderable.cpp:73
void setOrder(int order)
Set order.
Definition: orderable.h:35
ColumnIndex
Properties by index.
Definition: orderable.h:23
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: orderable.cpp:57
QString getOrderAsString() const
Order as string.
Definition: orderable.cpp:20
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: orderable.cpp:40
IOrderable()
Constructor.
Definition: orderable.cpp:16
int getOrder() const
Order.
Definition: orderable.h:29
bool hasValidOrder() const
Valid order set?
Definition: orderable.cpp:26
Free functions in swift::misc.
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition: verify.h:26