Marble

CelestialSortFilterProxyModel.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2004-2007 Torsten Rahn <tackat@kde.org>
4// SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
5// SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
6// SPDX-FileCopyrightText: 2010 Bastian Holst <bastianholst@gmx.de>
7// SPDX-FileCopyrightText: 2011-2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
8// SPDX-FileCopyrightText: 2012 Illya Kovalevskyy <illya.kovalevskyy@gmail.com>
9//
10
11// Self
12#include "CelestialSortFilterProxyModel.h"
13
14namespace Marble
15{
16
17CelestialSortFilterProxyModel::CelestialSortFilterProxyModel()
18{
19 setupPriorities();
20 setupMoonsList();
21 setupDwarfsList();
22}
23
24CelestialSortFilterProxyModel::~CelestialSortFilterProxyModel() = default;
25
27{
29 if (role == Qt::DisplayRole && index.column() == 0) {
30 QString newOne = var.toString();
31 if (newOne == tr("Moon")) {
32 return QString(QLatin1StringView(" ") + tr("Moon"));
33 } else if (m_moons.contains(newOne.toLower())) {
34 return QString(QLatin1StringView(" ") + newOne + QLatin1StringView(" (") + tr("moon") + QLatin1Char(')'));
35 } else if (m_dwarfs.contains(newOne.toLower())) {
36 return QString(newOne + QLatin1StringView(" (") + tr("dwarf planet") + QLatin1Char(')'));
37 }
38 return newOne;
39 } else {
40 return var;
41 }
42}
43
44void CelestialSortFilterProxyModel::setupPriorities()
45{
46 // TODO: create priority on the model side (Planet Class) by taking the distance to the "home planet/home star" into account
47 // here we will set m_priority for default order
48 int prefix = 100;
49
50 m_priority[QStringLiteral("sun")] = prefix;
51 m_priority[QStringLiteral("mercury")] = prefix--;
52 m_priority[QStringLiteral("venus")] = prefix--;
53 m_priority[QStringLiteral("earth")] = prefix--;
54 m_priority[QStringLiteral("moon")] = prefix--;
55 m_priority[QStringLiteral("mars")] = prefix--;
56
57 m_priority[QStringLiteral("jupiter")] = prefix--;
58 // Moons of Jupiter
59 m_priority[QStringLiteral("io")] = prefix--;
60 m_priority[QStringLiteral("europa")] = prefix--;
61 m_priority[QStringLiteral("ganymede")] = prefix--;
62 m_priority[QStringLiteral("callisto")] = prefix--;
63
64 m_priority[QStringLiteral("saturn")] = prefix--;
65 // Moons of Saturn
66 m_priority[QStringLiteral("mimas")] = prefix--;
67 m_priority[QStringLiteral("enceladus")] = prefix--;
68 m_priority[QStringLiteral("thetys")] = prefix--;
69 m_priority[QStringLiteral("dione")] = prefix--;
70 m_priority[QStringLiteral("rhea")] = prefix--;
71 m_priority[QStringLiteral("titan")] = prefix--;
72 m_priority[QStringLiteral("iapetus")] = prefix--;
73
74 m_priority[QStringLiteral("uranus")] = prefix--;
75 m_priority[QStringLiteral("neptune")] = prefix--;
76 m_priority[QStringLiteral("pluto")] = prefix--;
77 m_priority[QStringLiteral("ceres")] = prefix--;
78}
79
80void CelestialSortFilterProxyModel::setupMoonsList()
81{
82 m_moons.push_back(QStringLiteral("moon"));
83 m_moons.push_back(QStringLiteral("europa"));
84 m_moons.push_back(QStringLiteral("ganymede"));
85 m_moons.push_back(QStringLiteral("callisto"));
86 m_moons.push_back(QStringLiteral("mimas"));
87 m_moons.push_back(QStringLiteral("enceladus"));
88 m_moons.push_back(QStringLiteral("thetys"));
89 m_moons.push_back(QStringLiteral("dione"));
90 m_moons.push_back(QStringLiteral("rhea"));
91 m_moons.push_back(QStringLiteral("titan"));
92 m_moons.push_back(QStringLiteral("iapetus"));
93}
94
95void CelestialSortFilterProxyModel::setupDwarfsList()
96{
97 m_dwarfs.push_back(QStringLiteral("pluto"));
98 m_dwarfs.push_back(QStringLiteral("ceres"));
99}
100
101bool CelestialSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
102{
103 const QString nameLeft = sourceModel()->index(left.row(), 1).data().toString();
104 const QString nameRight = sourceModel()->index(right.row(), 1).data().toString();
105 const QString first = nameLeft.toLower();
106 const QString second = nameRight.toLower();
107
108 // both are in the list
109 if (m_priority.contains(first) && m_priority.contains(second)) {
110 return m_priority[first] > m_priority[second];
111 }
112
113 // only left in the list
114 if (m_priority.contains(first) && !m_priority.contains(second)) {
115 return true;
116 }
117
118 // only right in the list
119 if (!m_priority.contains(first) && m_priority.contains(second)) {
120 return false;
121 }
122
123 return QSortFilterProxyModel::lessThan(left, right);
124}
125
126}
127
128#include "moc_CelestialSortFilterProxyModel.cpp"
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
A small trick to change names for dwarfs and moons.
Binds a QML item to a specific geodetic location in screen coordinates.
bool contains(const AT &value) const const
void push_back(parameter_type value)
bool contains(const Key &key) const const
int column() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const const
QChar * data()
QString toLower() const const
DisplayRole
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.