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{
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
38 return sourceModel()->data(index).toString().contains(filterRegExp());
39}
40
42{
43 setupUi(this);
44
45 m_ConfigDialog = KConfigDialog::exists("settings");
46
47 //Set up the Table Views
48 m_Model = new QStandardItemModel(0, 1, this);
49 m_SortModel = new SatelliteSortFilterProxyModel(this);
50 m_SortModel->setSourceModel(m_Model);
51 SatListTreeView->setModel(m_SortModel);
53 SatListTreeView->setSortingEnabled(false);
54
55 // Populate satellites list
56 updateListView();
57
58 // Signals and slots connections
59 connect(UpdateTLEButton, SIGNAL(clicked()), this, SLOT(slotUpdateTLEs()));
60 connect(kcfg_ShowSatellites, SIGNAL(toggled(bool)), SLOT(slotShowSatellites(bool)));
61 connect(m_ConfigDialog->button(QDialogButtonBox::Apply), SIGNAL(clicked()), SLOT(slotApply()));
62 connect(m_ConfigDialog->button(QDialogButtonBox::Ok), SIGNAL(clicked()), SLOT(slotApply()));
63 connect(m_ConfigDialog->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), SLOT(slotCancel()));
64 connect(FilterEdit, SIGNAL(textChanged(QString)), this, SLOT(slotFilterReg(QString)));
65 connect(m_Model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotItemChanged(QStandardItem*)));
66
67 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
68 connect(satelliteButtonGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonPressed), this,
69 [&]() { isDirty = true; });
70 #else
71 connect(satelliteButtonGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::idPressed), this,
72 [&]() { isDirty = true; });
73 #endif
74
75 isDirty = false;
76}
77
78void OpsSatellites::slotUpdateTLEs()
79{
80 // Save existing satellites
81 saveSatellitesList();
82
83 // Get new data files
84 KStarsData::Instance()->skyComposite()->satellites()->updateTLEs();
85
86 isDirty = true;
87
88 // Refresh satellites list
89 updateListView();
90}
91
92void OpsSatellites::updateListView()
93{
94 KStarsData *data = KStarsData::Instance();
95
96 // Clear satellites list
97 m_Model->clear();
98 SatListTreeView->reset();
99
100 m_Model->setHorizontalHeaderLabels(QStringList(i18n("Satellite Name")));
101
102 // Add each groups and satellites in the list
103 foreach (SatelliteGroup *sat_group, data->skyComposite()->satellites()->groups())
104 {
107 bool all_sat_checked = true;
108 bool all_sat_unchecked = true;
109
110 // Add the group
111 group_item = new QStandardItem(i18nc(satgroup_strings_context, sat_group->name().toUtf8()));
112 group_item->setCheckable(true);
113 m_Model->appendRow(group_item);
114
115 // Add all satellites of the group
116 for (int i = 0; i < sat_group->count(); ++i)
117 {
118 sat_item = new QStandardItem(sat_group->at(i)->name());
119 sat_item->setCheckable(true);
120 if (Options::selectedSatellites().contains(sat_group->at(i)->name()))
121 {
122 sat_item->setCheckState(Qt::Checked);
123 all_sat_unchecked = false;
124 }
125 else
126 all_sat_checked = false;
127 group_item->setChild(i, sat_item);
128 }
129
130 // If all satellites of the group are selected, select the group
131 if (all_sat_checked)
132 group_item->setCheckState(Qt::Checked);
133 else if (all_sat_unchecked)
134 group_item->setCheckState(Qt::Unchecked);
135 else
136 group_item->setCheckState(Qt::PartiallyChecked);
137 }
138}
139
140void OpsSatellites::saveSatellitesList()
141{
142 KStarsData *data = KStarsData::Instance();
148
149 // Retrieve each satellite in the list and select it if checkbox is checked
150 for (int i = 0; i < m_Model->rowCount(SatListTreeView->rootIndex()); ++i)
151 {
152 group_index = m_Model->index(i, 0, SatListTreeView->rootIndex());
154
155 for (int j = 0; j < m_Model->rowCount(group_item->index()); ++j)
156 {
157 sat_index = m_Model->index(j, 0, group_index);
158 sat_item = m_Model->itemFromIndex(sat_index);
159 sat_name = sat_item->data(0).toString();
160
161 Satellite *sat = data->skyComposite()->satellites()->findSatellite(sat_name);
162 if (sat)
163 {
164 if (sat_item->checkState() == Qt::Checked)
165 {
166 int rc = sat->updatePos();
167 // If position calculation fails, unselect it
168 if (rc == 0)
169 {
170 sat->setSelected(true);
172 }
173 else
174 {
175 KStars::Instance()->statusBar()->showMessage(
176 i18n("%1 position calculation error: %2.", sat->name(), sat->sgp4ErrorString(rc)), 0);
177 sat->setSelected(false);
178 sat_item->setCheckState(Qt::Unchecked);
179 }
180 }
181 else
182 {
183 sat->setSelected(false);
184 }
185 }
186 }
187 }
188
189 Options::setSelectedSatellites(selected_satellites);
190}
191
192void OpsSatellites::slotApply()
193{
194 if (isDirty == false)
195 return;
196
197 isDirty = false;
198
199 saveSatellitesList();
200
201 // update time for all objects because they might be not initialized
202 // it's needed when using horizontal coordinates
203 KStars::Instance()->data()->setFullTimeUpdate();
204 KStars::Instance()->updateTime();
205 KStars::Instance()->map()->forceUpdate();
206}
207
208void OpsSatellites::slotCancel()
209{
210 // Update satellites list
211 updateListView();
212}
213
214void OpsSatellites::slotShowSatellites(bool on)
215{
216 isDirty = true;
217 kcfg_ShowVisibleSatellites->setEnabled(on);
218 kcfg_ShowSatellitesLabels->setEnabled(on);
219 kcfg_DrawSatellitesLikeStars->setEnabled(on);
220}
221
222void OpsSatellites::slotFilterReg(const QString &filter)
223{
225 m_SortModel->setFilterKeyColumn(-1);
226
227 isDirty = true;
228
229 // Expand all categories when the user use filter
230 if (filter.length() > 0)
231 SatListTreeView->expandAll();
232 else
233 SatListTreeView->collapseAll();
234}
235
236void OpsSatellites::slotItemChanged(QStandardItem *item)
237{
238 if (item->parent() == nullptr && !item->hasChildren())
239 {
240 return;
241 }
242
243 isDirty = true;
244
247
248 disconnect(m_Model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotItemChanged(QStandardItem*)));
249
250 // If a group has been (un)checked, (un)check all satellites of the group
251 // else a satellite has been (un)checked, (un)check his group
252 if (item->hasChildren())
253 {
254 for (int i = 0; i < m_Model->rowCount(item->index()); ++i)
255 {
256 sat_index = m_Model->index(i, 0, item->index());
257 sat_item = m_Model->itemFromIndex(sat_index);
258
259 if (item->checkState() == Qt::Checked)
260 sat_item->setCheckState(Qt::Checked);
261 else
262 sat_item->setCheckState(Qt::Unchecked);
263 }
264 }
265 else
266 {
267 bool all_sat_checked = true;
268 bool all_sat_unchecked = true;
269
270 for (int i = 0; i < item->parent()->model()->rowCount(item->parent()->index()); ++i)
271 {
272 sat_index = m_Model->index(i, 0, item->parent()->index());
273 sat_item = m_Model->itemFromIndex(sat_index);
274
275 if (sat_item->checkState() == Qt::Checked)
276 all_sat_unchecked = false;
277 else
278 all_sat_checked = false;
279 }
280
281 if (all_sat_checked)
283 else if (all_sat_unchecked)
285 else
287 }
288
289 connect(m_Model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(slotItemChanged(QStandardItem*)));
290}
static KConfigDialog * exists(const QString &name)
QPushButton * button(QDialogButtonBox::StandardButton which) const
KStarsData is the backbone of KStars.
Definition kstarsdata.h:72
SkyMapComposite * skyComposite()
Definition kstarsdata.h:166
This is the main window for KStars.
Definition kstars.h:91
static KStars * Instance()
Definition kstars.h:123
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()
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)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
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
void setFilterRegExp(const QString &pattern)
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
Qt::CheckState checkState() const const
bool hasChildren() const const
QModelIndex index() const const
QStandardItemModel * model() const const
QStandardItem * parent() const const
void setCheckState(Qt::CheckState state)
void appendRow(const QList< QStandardItem * > &items)
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)
CaseInsensitive
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:45:35 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.