File indexing completed on 2024-04-28 15:15:35

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2004-2007 Torsten Rahn <tackat@kde.org>
0004 // SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
0005 // SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
0006 // SPDX-FileCopyrightText: 2010 Bastian Holst <bastianholst@gmx.de>
0007 // SPDX-FileCopyrightText: 2011-2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0008 // SPDX-FileCopyrightText: 2012 Illya Kovalevskyy <illya.kovalevskyy@gmail.com>
0009 //
0010 
0011 // Self
0012 #include "CelestialSortFilterProxyModel.h"
0013 
0014 namespace Marble {
0015 
0016 CelestialSortFilterProxyModel::CelestialSortFilterProxyModel()
0017 {
0018     setupPriorities();
0019     setupMoonsList();
0020     setupDwarfsList();
0021 }
0022 
0023 CelestialSortFilterProxyModel::~CelestialSortFilterProxyModel() {}
0024 
0025 
0026 QVariant CelestialSortFilterProxyModel::data( const QModelIndex &index, int role ) const
0027 {
0028     QVariant var = QSortFilterProxyModel::data( index, role );
0029     if ( role == Qt::DisplayRole && index.column() == 0 ) {
0030         QString newOne = var.toString();
0031         if (newOne == tr("Moon")) {
0032             return QString(QLatin1String("  ") + tr("Moon"));
0033         } else if ( m_moons.contains( newOne.toLower() ) ) {
0034             return QString(QLatin1String("  ") + newOne + QLatin1String(" (") + tr("moon") + QLatin1Char(')'));
0035         } else if ( m_dwarfs.contains( newOne.toLower() ) ) {
0036             return QString(newOne + QLatin1String(" (") + tr("dwarf planet") + QLatin1Char(')'));
0037         }
0038         return newOne;
0039     } else {
0040         return var;
0041     }
0042 }
0043 
0044 
0045 void CelestialSortFilterProxyModel::setupPriorities()
0046 {
0047     // TODO: create priority on the model side (Planet Class) by taking the distance to the "home planet/home star" into account
0048     // here we will set m_priority for default order
0049     int prefix = 100;
0050 
0051     m_priority["sun"] = prefix;
0052     m_priority["mercury"] = prefix--;
0053     m_priority["venus"] = prefix--;
0054     m_priority["earth"] = prefix--;
0055     m_priority["moon"] = prefix--;
0056     m_priority["mars"] = prefix--;
0057 
0058     m_priority["jupiter"] = prefix--;
0059     // Moons of Jupiter
0060     m_priority["io"] = prefix--;
0061     m_priority["europa"] = prefix--;
0062     m_priority["ganymede"] = prefix--;
0063     m_priority["callisto"] = prefix--;
0064 
0065     m_priority["saturn"] = prefix--;
0066     // Moons of Saturn
0067     m_priority["mimas"] = prefix--;
0068     m_priority["enceladus"] = prefix--;
0069     m_priority["thetys"] = prefix--;
0070     m_priority["dione"] = prefix--;
0071     m_priority["rhea"] = prefix--;
0072     m_priority["titan"] = prefix--;
0073     m_priority["iapetus"] = prefix--;
0074 
0075     m_priority["uranus"] = prefix--;
0076     m_priority["neptune"] = prefix--;
0077     m_priority["pluto"] = prefix--;
0078     m_priority["ceres"] = prefix--;
0079 }
0080 
0081 void CelestialSortFilterProxyModel::setupMoonsList()
0082 {
0083     m_moons.push_back("moon");
0084     m_moons.push_back("europa");
0085     m_moons.push_back("ganymede");
0086     m_moons.push_back("callisto");
0087     m_moons.push_back("mimas");
0088     m_moons.push_back("enceladus");
0089     m_moons.push_back("thetys");
0090     m_moons.push_back("dione");
0091     m_moons.push_back("rhea");
0092     m_moons.push_back("titan");
0093     m_moons.push_back("iapetus");
0094 }
0095 
0096 void CelestialSortFilterProxyModel::setupDwarfsList()
0097 {
0098     m_dwarfs.push_back("pluto");
0099     m_dwarfs.push_back("ceres");
0100 }
0101 
0102 bool CelestialSortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
0103 {
0104     const QString nameLeft = sourceModel()->index( left.row(), 1 ).data().toString();
0105     const QString nameRight = sourceModel()->index( right.row(), 1 ).data().toString();
0106     const QString first = nameLeft.toLower();
0107     const QString second = nameRight.toLower();
0108 
0109     // both are in the list
0110     if ( m_priority.contains( first ) && m_priority.contains( second ) ) {
0111         return m_priority[first] > m_priority[second];
0112     }
0113 
0114     // only left in the list
0115     if ( m_priority.contains( first ) && !m_priority.contains( second ) ) {
0116         return true;
0117     }
0118 
0119     // only right in the list
0120     if (!m_priority.contains( first ) && m_priority.contains( second ) ) {
0121         return false;
0122     }
0123 
0124     return QSortFilterProxyModel::lessThan( left, right );
0125 }
0126 
0127 }
0128 
0129 #include "moc_CelestialSortFilterProxyModel.cpp"