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#include <QRegularExpression>
11
12SkyObjectListModel::SkyObjectListModel(QObject *parent) : QAbstractListModel(parent)
13{
14}
15
16QHash<int, QByteArray> SkyObjectListModel::roleNames() const
17{
19
20 roles[Qt::DisplayRole] = "name";
21 roles[SkyObjectRole] = "skyobject";
22 return roles;
23}
24
25int SkyObjectListModel::indexOf(const QString &objectName) const
26{
27 for (int i = 0; i < skyObjects.size(); ++i)
28 {
29 if (skyObjects[i].first == objectName)
30 {
31 return i;
32 }
33 }
34 return -1;
35}
36
37QVariant SkyObjectListModel::data(const QModelIndex &index, int role) const
38{
39 if (!index.isValid())
40 {
41 return QVariant();
42 }
43 if (role == Qt::DisplayRole)
44 {
45 return QVariant(skyObjects[index.row()].first);
46 }
47 else if (role == SkyObjectRole)
48 {
49 return QVariant::fromValue((void *)skyObjects[index.row()].second);
50 }
51 return QVariant();
52}
53
55{
56 QStringList filteredList;
57
58 for (auto &item : skyObjects)
59 {
60 if (regEx.match(item.first).hasMatch())
61 {
62 filteredList.append(item.first);
63 }
64 }
65 return filteredList;
66}
67
68void SkyObjectListModel::setSkyObjectsList(QVector<QPair<QString, const SkyObject *>> sObjects)
69{
71 skyObjects = sObjects;
73}
74
75void SkyObjectListModel::removeSkyObject(SkyObject *object)
76{
77 for (int i = 0; i < skyObjects.size(); ++i)
78 {
79 if (skyObjects[i].second == object)
80 {
81 skyObjects.remove(i);
82 return;
83 }
84 }
85}
QStringList filter(const QRegularExpression &regEx)
Filter the model.
int indexOf(const QString &objectName) const
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)
bool isValid() const const
int row() const const
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, MatchType matchType, MatchOptions matchOptions) const const
bool hasMatch() const const
DisplayRole
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.