Kstars

skyobjectlistmodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Artem Fedoskin <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "skyobjectlistmodel.h"
8 
9 #include "skyobject.h"
10 
11 SkyObjectListModel::SkyObjectListModel(QObject *parent) : QAbstractListModel(parent)
12 {
13 }
14 
15 QHash<int, QByteArray> SkyObjectListModel::roleNames() const
16 {
18 
19  roles[Qt::DisplayRole] = "name";
20  roles[SkyObjectRole] = "skyobject";
21  return roles;
22 }
23 
24 int 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 
36 QVariant 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 
67 void SkyObjectListModel::setSkyObjectsList(QVector<QPair<QString, const SkyObject *>> sObjects)
68 {
70  skyObjects = sObjects;
71  endResetModel();
72 }
73 
74 void 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 }
void append(const T &value)
DisplayRole
QVariant fromValue(const T &value)
int indexOf(const QString &objectName) const
void remove(int i)
QStringList filter(const QRegExp &regEx)
Filter the model.
bool isValid() const const
bool exactMatch(const QString &str) const const
int row() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int size() const const
Information about an object in the sky.
Definition: skyobject.h:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Aug 11 2022 04:00:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.