File indexing completed on 2024-04-21 14:44:24

0001 /*
0002     SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "skyobjectlistmodel.h"
0008 
0009 #include "skyobject.h"
0010 
0011 SkyObjectListModel::SkyObjectListModel(QObject *parent) : QAbstractListModel(parent)
0012 {
0013 }
0014 
0015 QHash<int, QByteArray> SkyObjectListModel::roleNames() const
0016 {
0017     QHash<int, QByteArray> roles;
0018 
0019     roles[Qt::DisplayRole] = "name";
0020     roles[SkyObjectRole]   = "skyobject";
0021     return roles;
0022 }
0023 
0024 int SkyObjectListModel::indexOf(const QString &objectName) const
0025 {
0026     for (int i = 0; i < skyObjects.size(); ++i)
0027     {
0028         if (skyObjects[i].first == objectName)
0029         {
0030             return i;
0031         }
0032     }
0033     return -1;
0034 }
0035 
0036 QVariant SkyObjectListModel::data(const QModelIndex &index, int role) const
0037 {
0038     if (!index.isValid())
0039     {
0040         return QVariant();
0041     }
0042     if (role == Qt::DisplayRole)
0043     {
0044         return QVariant(skyObjects[index.row()].first);
0045     }
0046     else if (role == SkyObjectRole)
0047     {
0048         return QVariant::fromValue((void *)skyObjects[index.row()].second);
0049     }
0050     return QVariant();
0051 }
0052 
0053 QStringList SkyObjectListModel::filter(const QRegExp &regEx)
0054 {
0055     QStringList filteredList;
0056 
0057     for (auto &item : skyObjects)
0058     {
0059         if (regEx.exactMatch(item.first))
0060         {
0061             filteredList.append(item.first);
0062         }
0063     }
0064     return filteredList;
0065 }
0066 
0067 void SkyObjectListModel::setSkyObjectsList(QVector<QPair<QString, const SkyObject *>> sObjects)
0068 {
0069     beginResetModel();
0070     skyObjects = sObjects;
0071     endResetModel();
0072 }
0073 
0074 void SkyObjectListModel::removeSkyObject(SkyObject *object)
0075 {
0076     for (int i = 0; i < skyObjects.size(); ++i)
0077     {
0078         if (skyObjects[i].second == object)
0079         {
0080             skyObjects.remove(i);
0081             return;
0082         }
0083     }
0084 }