Kstars

opssatellites.cpp
1/*
2 SPDX-FileCopyrightText: 2011 Jérôme SONRIER <jsid@emor3j.fr.eu.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "opssatellites.h"
8
9#include "kstars.h"
10#include "kstarsdata.h"
11#include "Options.h"
12#include "satellite.h"
13#include "skymapcomposite.h"
14#include "skycomponents/satellitescomponent.h"
15#include "skymap.h"
16
17#include <QStandardItemModel>
18#include <QStatusBar>
19
20static const char *satgroup_strings_context = "Satellite group name";
21
22SatelliteSortFilterProxyModel::SatelliteSortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
23{
24}
25
26bool SatelliteSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
27{
28 QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
29
31 {
32 for (int i = 0; i < sourceModel()->rowCount(index); ++i)
33 if (filterAcceptsRow(i, index))
34 return true;
35 return false;
36 }
37 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
39 #else
40 return sourceModel()->data(index).toString().contains(filterRegExp());
41 #endif
42
43}
44
46{
47 setupUi(this);
48
49 m_ConfigDialog = KConfigDialog::exists("settings");
50
51 //Set up the Table Views
52 m_Model = new QStandardItemModel(0, 1, this);
53 m_SortModel = new SatelliteSortFilterProxyModel(this);
54 m_SortModel->setSourceModel(m_Model);
55 SatListTreeView->setModel(m_SortModel);
56 SatListTreeView->setEditTriggers(QTreeView::NoEditTriggers);
57 SatListTreeView->setSortingEnabled(false);
58
59 // Populate satellites list
60 updateListView();
61
62 // Signals and slots connections
63 connect(UpdateTLEButton, SIGNAL(clicked()), this, SLOT(slotUpdateTLEs()));
64 connect(kcfg_ShowSatellites, SIGNAL(toggled(bool)), SLOT(slotShowSatellites(bool)));
65 connect(m_ConfigDialog->button(QDialogButtonBox::Apply), SIGNAL(clicked()), SLOT(slotApply()));
66 connect(m_ConfigDialog->button(QDialogButtonBox::Ok), SIGNAL(clicked()), SLOT(slotApply()));
67 connect(m_ConfigDialog->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), SLOT(slotCancel()));
68 connect(FilterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotFilterReg(QString)));
69 connect(m_Model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotItemChanged(QStandardItem*)));
70
71 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
72 connect(satelliteButtonGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonPressed), this,
73 [&]() { isDirty = true; });
74 #else
75 connect(satelliteButtonGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::idPressed), this,
76 [&]() { isDirty = true; });
77 #endif
78
79 isDirty = false;
80}
81
82void OpsSatellites::slotUpdateTLEs()
83{
84 // Save existing satellites
85 saveSatellitesList();
86
87 // Get new data files
88 KStarsData::Instance()->skyComposite()->satellites()->updateTLEs();
89
90 isDirty = true;
91
92 // Refresh satellites list
93 updateListView();
94}
95
96void OpsSatellites::updateListView()
97{
98 KStarsData *data = KStarsData::Instance();
99
100 // Clear satellites list
101 m_Model->clear();
102 SatListTreeView->reset();
103
104 m_Model->setHorizontalHeaderLabels(QStringList(i18n("Satellite Name")));
105
106 // Add each groups and satellites in the list
107 foreach (SatelliteGroup *sat_group, data->skyComposite()->satellites()->groups())
108 {
109 QStandardItem *group_item;
110 QStandardItem *sat_item;
111 bool all_sat_checked = true;
112 bool all_sat_unchecked = true;
113
114 // Add the group
115 group_item = new QStandardItem(i18nc(satgroup_strings_context, sat_group->name().toUtf8()));
116 group_item->setCheckable(true);
117 m_Model->appendRow(group_item);
118
119 // Add all satellites of the group
120 for (int i = 0; i < sat_group->count(); ++i)
121 {
122 sat_item = new QStandardItem(sat_group->at(i)->name());
123 sat_item->setCheckable(true);
124 if (Options::selectedSatellites().contains(sat_group->at(i)->name()))
125 {
126 sat_item->setCheckState(Qt::Checked);
127 all_sat_unchecked = false;
128 }
129 else
130 all_sat_checked = false;
131 group_item->setChild(i, sat_item);
132 }
133
134 // If all satellites of the group are selected, select the group
135 if (all_sat_checked)
136 group_item->setCheckState(Qt::Checked);
137 else if (all_sat_unchecked)
138 group_item->setCheckState(Qt::Unchecked);
139 else
141 }
142}
143
144void OpsSatellites::saveSatellitesList()
145{
146 KStarsData *data = KStarsData::Instance();
147 QString sat_name;
148 QStringList selected_satellites;
149 QModelIndex group_index, sat_index;
150 QStandardItem *group_item;
151 QStandardItem *sat_item;
152
153 // Retrieve each satellite in the list and select it if checkbox is checked
154 for (int i = 0; i < m_Model->rowCount(SatListTreeView->rootIndex()); ++i)
155 {
156 group_index = m_Model->index(i, 0, SatListTreeView->rootIndex());
157 group_item = m_Model->itemFromIndex(group_index);
158
159 for (int j = 0; j < m_Model->rowCount(group_item->index()); ++j)
160 {
161 sat_index = m_Model->index(j, 0, group_index);
162 sat_item = m_Model->itemFromIndex(sat_index);
163 sat_name = sat_item->data(0).toString();
164
165 Satellite *sat = data->skyComposite()->satellites()->findSatellite(sat_name);
166 if (sat)
167 {
168 if (sat_item->checkState() == Qt::Checked)
169 {
170 int rc = sat->updatePos();
171 // If position calculation fails, unselect it
172 if (rc == 0)
173 {
174 sat->setSelected(true);
175 selected_satellites.append(sat_name);
176 }
177 else
178 {
180 i18n("%1 position calculation error: %2.", sat->name(), sat->sgp4ErrorString(rc)), 0);
181 sat->setSelected(false);
182 sat_item->setCheckState(Qt::Unchecked);
183 }
184 }
185 else
186 {
187 sat->setSelected(false);
188 }
189 }
190 }
191 }
192
193 Options::setSelectedSatellites(selected_satellites);
194}
195
196void OpsSatellites::slotApply()
197{
198 if (isDirty == false)
199 return;
200
201 isDirty = false;
202
203 saveSatellitesList();
204
205 // update time for all objects because they might be not initialized
206 // it's needed when using horizontal coordinates
210}
211
212void OpsSatellites::slotCancel()
213{
214 // Update satellites list
215 updateListView();
216}
217
218void OpsSatellites::slotShowSatellites(bool on)
219{
220 isDirty = true;
221 kcfg_ShowVisibleSatellites->setEnabled(on);
222 kcfg_ShowSatellitesLabels->setEnabled(on);
223 kcfg_DrawSatellitesLikeStars->setEnabled(on);
224}
225
226void OpsSatellites::slotFilterReg(const QString &filter)
227{
229 m_SortModel->setFilterKeyColumn(-1);
230
231 isDirty = true;
232
233 // Expand all categories when the user use filter
234 if (filter.length() > 0)
235 SatListTreeView->expandAll();
236 else
237 SatListTreeView->collapseAll();
238}
239
240void OpsSatellites::slotItemChanged(QStandardItem *item)
241{
242 if (item->parent() == nullptr && !item->hasChildren())
243 {
244 return;
245 }
246
247 isDirty = true;
248
249 QModelIndex sat_index;
250 QStandardItem *sat_item;
251
252 disconnect(m_Model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotItemChanged(QStandardItem*)));
253
254 // If a group has been (un)checked, (un)check all satellites of the group
255 // else a satellite has been (un)checked, (un)check his group
256 if (item->hasChildren())
257 {
258 for (int i = 0; i < m_Model->rowCount(item->index()); ++i)
259 {
260 sat_index = m_Model->index(i, 0, item->index());
261 sat_item = m_Model->itemFromIndex(sat_index);
262
263 if (item->checkState() == Qt::Checked)
264 sat_item->setCheckState(Qt::Checked);
265 else
266 sat_item->setCheckState(Qt::Unchecked);
267 }
268 }
269 else
270 {
271 bool all_sat_checked = true;
272 bool all_sat_unchecked = true;
273
274 for (int i = 0; i < item->parent()->model()->rowCount(item->parent()->index()); ++i)
275 {
276 sat_index = m_Model->index(i, 0, item->parent()->index());
277 sat_item = m_Model->itemFromIndex(sat_index);
278
279 if (sat_item->checkState() == Qt::Checked)
280 all_sat_unchecked = false;
281 else
282 all_sat_checked = false;
283 }
284
285 if (all_sat_checked)
287 else if (all_sat_unchecked)
289 else
291 }
292
293 connect(m_Model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotItemChanged(QStandardItem*)));
294}
static KConfigDialog * exists(const QString &name)
QPushButton * button(QDialogButtonBox::StandardButton which) const
KStarsData is the backbone of KStars.
Definition kstarsdata.h:72
void setFullTimeUpdate()
The Sky is updated more frequently than the moon, which is updated more frequently than the planets.
SkyMapComposite * skyComposite()
Definition kstarsdata.h:166
This is the main window for KStars.
Definition kstars.h:89
SkyMap * map() const
Definition kstars.h:139
static KStars * Instance()
Definition kstars.h:121
KStarsData * data() const
Definition kstars.h:133
void updateTime(const bool automaticDSTchange=true)
Update time-dependent data and (possibly) repaint the sky map.
Definition kstars.cpp:592
OpsSatellites()
Constructor.
Represents a group of artificial satellites.
Represents an artificial satellites.
Definition satellite.h:23
int updatePos()
Update satellite position.
void setSelected(bool selected)
Select or not the satellite.
QString sgp4ErrorString(int code)
sgp4ErrorString Get error string associated with sgp4 calculation failure
Satellite * findSatellite(QString name)
Search a satellite by name.
QList< SatelliteGroup * > groups()
void updateTLEs()
Download new TLE files.
void forceUpdate(bool now=false)
Recalculates the positions of objects in the sky, and then repaints the sky map.
Definition skymap.cpp:1173
virtual QString name(void) const
Definition skyobject.h:145
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void buttonPressed(QAbstractButton *button)
void idPressed(int id)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
qsizetype count() const const
QStatusBar * statusBar() const const
QVariant data(int role) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
void setFilterKeyColumn(int column)
virtual bool hasChildren(const QModelIndex &parent) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
Qt::CheckState checkState() const const
virtual QVariant data(int role) const const
bool hasChildren() const const
QModelIndex index() const const
QStandardItem * parent() const const
void setCheckState(Qt::CheckState state)
void setCheckable(bool checkable)
void setChild(int row, QStandardItem *item)
void appendRow(QStandardItem *item)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
QStandardItem * itemFromIndex(const QModelIndex &index) const const
virtual int rowCount(const QModelIndex &parent) const const override
void setHorizontalHeaderLabels(const QStringList &labels)
void showMessage(const QString &message, int timeout)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QByteArray toUtf8() const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QString toString() const const
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.