Kstars

skyobjectlistmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "skyobjectlistmodel.h"
8
9#include "skyobject.h"
10
11SkyObjectListModel::SkyObjectListModel(QObject *parent) : QAbstractListModel(parent)
12{
13}
14
15QHash<int, QByteArray> SkyObjectListModel::roleNames() const
16{
18
19 roles[Qt::DisplayRole] = "name";
20 roles[SkyObjectRole] = "skyobject";
21 return roles;
22}
23
24int SkyObjectListModel::indexOf(const QString &objectName) const
25{
26 for (int i = 0; i < skyObjects.size(); ++i)
27 {
28 if (skyObjects[i].first == objectName)
29 {
30 return i;
31 }
32 }
33 return -1;
34}
35
36QVariant SkyObjectListModel::data(const QModelIndex &index, int role) const
37{
38 if (!index.isValid())
39 {
40 return QVariant();
41 }
42 if (role == Qt::DisplayRole)
43 {
44 return QVariant(skyObjects[index.row()].first);
45 }
46 else if (role == SkyObjectRole)
47 {
48 return QVariant::fromValue((void *)skyObjects[index.row()].second);
49 }
50 return QVariant();
51}
52
54{
55 QStringList filteredList;
56
57 for (auto &item : skyObjects)
58 {
59 if (regEx.exactMatch(item.first))
60 {
61 filteredList.append(item.first);
62 }
63 }
64 return filteredList;
65}
66
67void SkyObjectListModel::setSkyObjectsList(QVector<QPair<QString, const SkyObject *>> sObjects)
68{
70 skyObjects = sObjects;
72}
73
74void SkyObjectListModel::removeSkyObject(SkyObject *object)
75{
76 for (int i = 0; i < skyObjects.size(); ++i)
77 {
78 if (skyObjects[i].second == object)
79 {
80 skyObjects.remove(i);
81 return;
82 }
83 }
84}
int indexOf(const QString &objectName) const
QStringList filter(const QRegExp &regEx)
Filter the model.
Provides all necessary information about an object in the sky: its coordinates, name(s),...
Definition skyobject.h:42
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void append(QList< T > &&value)
DisplayRole
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:48:25 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.