swift
Loading...
Searching...
No Matches
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
12#include "misc/verify.h"
13
14namespace swift::misc
15{
16 IOrderable::IOrderable(int order) : m_order(order) {}
17
19 {
20 if (this->hasValidOrder())
21 {
22 return QString::number(this->getOrder());
23 }
24 return QStringLiteral("-");
25 }
26
27 bool IOrderable::hasValidOrder() const { return this->getOrder() >= 0; }
28
30 {
31 return (index >= static_cast<int>(IndexOrder)) && (index <= static_cast<int>(IndexOrderString));
32 }
33
35 {
36 if (index.isEmpty())
37 {
38 return false;
39 }
40 const int i = index.frontCasted<int>();
41 return isAnyOrderIndex(i);
42 }
43
45 {
46 if (!index.isEmpty())
47 {
48 const auto i = index.frontCasted<ColumnIndex>();
49 switch (i)
50 {
51 case IndexOrder: return QVariant::fromValue(this->m_order);
52 case IndexOrderString: return QVariant::fromValue(this->getOrderAsString());
53 default: break;
54 }
55 }
56 const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
57 SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
58 return QVariant::fromValue(m);
59 }
60
62 {
63 if (!index.isEmpty())
64 {
65 const auto i = index.frontCasted<ColumnIndex>();
66 switch (i)
67 {
68 case IndexOrder: this->setOrder(variant.toInt()); return;
69 case IndexOrderString:
70 default: break;
71 }
72 }
73 const QString m = QStringLiteral("Cannot handle index %1").arg(index.toQString());
74 SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable(m));
75 }
76
78 {
79 Q_UNUSED(index)
80 static const int max = std::numeric_limits<int>::max();
81 const int o1 = this->hasValidOrder() ? this->getOrder() : max;
82 const int o2 = compareValue.hasValidOrder() ? compareValue.getOrder() : max;
83 return Compare::compare(o1, o2);
84 }
85} // 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.
int m_order
order number
Definition orderable.h:62
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
Definition orderable.cpp:34
IOrderable()=default
Constructor.
static bool isAnyOrderIndex(int index)
Any order index.
Definition orderable.cpp:29
int comparePropertyByIndex(CPropertyIndexRef index, const IOrderable &compareValue) const
Compare for index.
Definition orderable.cpp:77
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:61
QString getOrderAsString() const
Order as string.
Definition orderable.cpp:18
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition orderable.cpp:44
int getOrder() const
Order.
Definition orderable.h:29
bool hasValidOrder() const
Valid order set?
Definition orderable.cpp:27
int compare(T a, T b)
Compare arithmetic values.
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