File indexing completed on 2024-11-24 04:15:36

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "floorlevelmodel.h"
0008 
0009 #include <KOSMIndoorMap/MapData>
0010 
0011 using namespace KOSMIndoorMap;
0012 
0013 FloorLevelModel::FloorLevelModel(QObject *parent)
0014     : QAbstractListModel(parent)
0015 {
0016     connect(this, &QAbstractItemModel::modelReset, this, &FloorLevelModel::contentChanged);
0017 }
0018 
0019 FloorLevelModel::~FloorLevelModel() = default;
0020 
0021 void FloorLevelModel::setMapData(MapData *data)
0022 {
0023     beginResetModel();
0024     m_level.clear();
0025     if (data) {
0026         for (const auto &l : data->levelMap()) {
0027             if (l.first.isFullLevel()) {
0028                 m_level.push_back(l.first);
0029             }
0030         }
0031     }
0032     endResetModel();
0033 }
0034 
0035 int FloorLevelModel::rowCount(const QModelIndex &parent) const
0036 {
0037     if (parent.isValid()) {
0038         return 0;
0039     }
0040     return m_level.size();
0041 }
0042 
0043 QVariant FloorLevelModel::data(const QModelIndex &index, int role) const
0044 {
0045     if (!index.isValid()) {
0046         return {};
0047     }
0048 
0049     switch (role) {
0050         case Qt::DisplayRole:
0051             return m_level[index.row()].name();
0052         case MapLevelRole:
0053             return QVariant::fromValue(m_level[index.row()]);
0054     }
0055 
0056     return {};
0057 }
0058 
0059 int FloorLevelModel::rowForLevel(int level) const
0060 {
0061     for (auto it = m_level.begin(); it != m_level.end(); ++it) {
0062         if ((*it).numericLevel() == level) {
0063             return std::distance(m_level.begin(), it);
0064         }
0065     }
0066     return -1;
0067 }
0068 
0069 int FloorLevelModel::levelForRow(int row) const
0070 {
0071     return m_level[row].numericLevel();
0072 }
0073 
0074 bool FloorLevelModel::hasFloorLevelBelow(int level) const
0075 {
0076     if (m_level.empty()) {
0077         return false;
0078     }
0079     return m_level.back().numericLevel() != level;
0080 }
0081 
0082 int FloorLevelModel::floorLevelBelow(int level) const
0083 {
0084     for (auto it = m_level.begin(); it != m_level.end(); ++it) {
0085         if ((*it).numericLevel() == level && std::next(it) != m_level.end()) {
0086             ++it;
0087             return (*it).numericLevel();
0088         }
0089     }
0090     return 0;
0091 }
0092 
0093 bool FloorLevelModel::hasFloorLevelAbove(int level) const
0094 {
0095     if (m_level.empty()) {
0096         return false;
0097     }
0098 
0099     return m_level.front().numericLevel() != level;
0100 }
0101 
0102 int FloorLevelModel::floorLevelAbove(int level) const
0103 {
0104     for (auto it = std::next(m_level.begin()); it != m_level.end(); ++it) {
0105         if ((*it).numericLevel() == level) {
0106             --it;
0107             return (*it).numericLevel();
0108         }
0109     }
0110     return 0;
0111 }
0112 
0113 bool FloorLevelModel::hasName(int level) const
0114 {
0115     for (auto it = m_level.begin(); it != m_level.end(); ++it) {
0116         if ((*it).numericLevel() == level) {
0117             return (*it).hasName();
0118         }
0119     }
0120     return false;
0121 }
0122 
0123 QString FloorLevelModel::name(int level) const
0124 {
0125     for (auto it = m_level.begin(); it != m_level.end(); ++it) {
0126         if ((*it).numericLevel() == level) {
0127             return (*it).name();
0128         }
0129     }
0130     return {};
0131 }
0132 
0133 bool FloorLevelModel::hasFloorLevels() const
0134 {
0135     return rowCount() > 1;
0136 }
0137 
0138 #include "moc_floorlevelmodel.cpp"