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 {
16  IOrderable::IOrderable(int order) : m_order(order) {}
17 
19  {
20  if (this->hasValidOrder()) { return QString::number(this->getOrder()); }
21  return QStringLiteral("-");
22  }
23 
24  bool IOrderable::hasValidOrder() const { return this->getOrder() >= 0; }
25 
27  {
28  return (index >= static_cast<int>(IndexOrder)) && (index <= static_cast<int>(IndexOrderString));
29  }
30 
32  {
33  if (index.isEmpty()) { return false; }
34  const int i = index.frontCasted<int>();
35  return isAnyOrderIndex(i);
36  }
37 
39  {
40  if (!index.isEmpty())
41  {
42  const auto i = index.frontCasted<ColumnIndex>();
43  switch (i)
44  {
45  case IndexOrder: return QVariant::fromValue(this->m_order);
46  case IndexOrderString: return QVariant::fromValue(this->getOrderAsString());
47  default: break;
48  }
49  }
50  const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
51  SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
52  return QVariant::fromValue(m);
53  }
54 
56  {
57  if (!index.isEmpty())
58  {
59  const auto i = index.frontCasted<ColumnIndex>();
60  switch (i)
61  {
62  case IndexOrder: this->setOrder(variant.toInt()); return;
63  case IndexOrderString:
64  default: break;
65  }
66  }
67  const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
68  SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
69  }
70 
71  int IOrderable::comparePropertyByIndex(CPropertyIndexRef index, const IOrderable &compareValue) const
72  {
73  Q_UNUSED(index)
74  static const int max = std::numeric_limits<int>::max();
75  const int o1 = this->hasValidOrder() ? this->getOrder() : max;
76  const int o2 = compareValue.hasValidOrder() ? compareValue.getOrder() : max;
77  return Compare::compare(o1, o2);
78  }
79 } // 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:31
IOrderable()=default
Constructor.
static bool isAnyOrderIndex(int index)
Any order index.
Definition: orderable.cpp:26
int comparePropertyByIndex(CPropertyIndexRef index, const IOrderable &compareValue) const
Compare for index.
Definition: orderable.cpp:71
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:55
QString getOrderAsString() const
Order as string.
Definition: orderable.cpp:18
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: orderable.cpp:38
int getOrder() const
Order.
Definition: orderable.h:29
bool hasValidOrder() const
Valid order set?
Definition: orderable.cpp:24
Free functions in swift::misc.
QString arg(Args &&... args) const const
QString number(double n, char format, int precision)
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition: verify.h:26