Marble

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

KDE's Doxygen guidelines are available online.