Kstars

flagmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Jerome SONRIER <jsid@emor3j.fr.eu.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "flagmanager.h"
8
9#include "config-kstars.h"
10
11#include "kspaths.h"
12#include "ksnotification.h"
13#include "kstars.h"
14#include "kstars_debug.h"
15#include "kstarsdata.h"
16#include "Options.h"
17#include "skymap.h"
18#include "skycomponents/flagcomponent.h"
19#include "skycomponents/skymapcomposite.h"
20
21#ifdef HAVE_INDI
22#include <basedevice.h>
23#include "indi/indilistener.h"
24#include "indi/indistd.h"
25#include "indi/indimount.h"
26#include "indi/driverinfo.h"
27#endif
28
29#include <KMessageBox>
30
31#include <QStandardItemModel>
32#include <QSortFilterProxyModel>
33
34FlagManagerUI::FlagManagerUI(QWidget *p) : QFrame(p)
35{
36 setupUi(this);
37}
38
39FlagManager::FlagManager(QWidget *ks) : QDialog(ks)
40{
41#ifdef Q_OS_MACOS
42 setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
43#endif
45 QList<QImage> imageList;
46 QStringList flagNames;
47 int i;
48
49 ui = new FlagManagerUI(this);
50
51 setWindowTitle(i18nc("@title:window", "Flag Manager"));
52
53 QVBoxLayout *mainLayout = new QVBoxLayout;
54 mainLayout->addWidget(ui);
55 setLayout(mainLayout);
56
58 mainLayout->addWidget(buttonBox);
59 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
60
61 m_Ks = KStars::Instance();
62
63 ui->hintLabel->setText(i18n("To add custom icons, just add images in %1. File names must begin with flag. "
64 "For example, the file <i>flagSmall_red_cross.png</i> will be shown as <b>Small red "
65 "cross</b> in the combo box.",
66 KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation)));
67 //Set up the Table Views
68 m_Model = new QStandardItemModel(0, 5, this);
69 m_Model->setHorizontalHeaderLabels(QStringList() << i18nc("Right Ascension", "RA") << i18nc("Declination", "Dec")
70 << i18n("Epoch") << i18n("Icon") << i18n("Label"));
71 m_SortModel = new QSortFilterProxyModel(this);
72 m_SortModel->setSourceModel(m_Model);
73 m_SortModel->setDynamicSortFilter(true);
74 ui->flagList->setModel(m_SortModel);
75 ui->flagList->horizontalHeader()->setStretchLastSection(true);
76
77 ui->flagList->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
78
79 ui->saveButton->setEnabled(false);
80
81 ui->raBox->setUnits(dmsBox::HOURS);
82
83 //Fill the list
84 imageList = m_Ks->data()->skyComposite()->flags()->imageList();
85 flagNames = m_Ks->data()->skyComposite()->flags()->getNames();
86
87 FlagComponent *flags = m_Ks->data()->skyComposite()->flags();
88 QPixmap pixmap;
89
90 for (i = 0; i < m_Ks->data()->skyComposite()->flags()->size(); ++i)
91 {
92 QStandardItem *labelItem = new QStandardItem(flags->label(i));
93 labelItem->setForeground(QBrush(flags->labelColor(i)));
94
95 itemList << new QStandardItem(flags->pointList().at(i)->ra0().toHMSString())
96 << new QStandardItem(flags->pointList().at(i)->dec0().toDMSString())
97 << new QStandardItem(flags->epoch(i))
98 << new QStandardItem(QIcon(pixmap.fromImage(flags->image(i))), flags->imageName(i)) << labelItem;
99 m_Model->appendRow(itemList);
100 itemList.clear();
101 }
102
103 //Fill the combobox
104 for (i = 0; i < imageList.size(); ++i)
105 {
106 ui->flagCombobox->addItem(QIcon(pixmap.fromImage(flags->imageList(i))), flagNames.at(i), flagNames.at(i));
107 }
108
109 //Connect buttons
110 connect(ui->addButton, SIGNAL(clicked()), this, SLOT(slotAddFlag()));
111 connect(ui->delButton, SIGNAL(clicked()), this, SLOT(slotDeleteFlag()));
112 connect(ui->CenterButton, SIGNAL(clicked()), this, SLOT(slotCenterFlag()));
113 connect(ui->ScopeButton, SIGNAL(clicked()), this, SLOT(slotCenterTelescope()));
114 connect(ui->flagList, SIGNAL(clicked(QModelIndex)), this, SLOT(slotSetShownFlag(QModelIndex)));
115 connect(ui->flagList, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(slotCenterFlag()));
116
117 connect(ui->saveButton, SIGNAL(clicked()), this, SLOT(slotSaveChanges()));
118}
119
120void FlagManager::setRaDec(const dms &ra, const dms &dec)
121{
122 ui->raBox->show(ra);
123 ui->decBox->show(dec);
124}
125
126void FlagManager::clearFields()
127{
128 ui->raBox->clear();
129 ui->decBox->clear();
130
131 ui->epochBox->setText("2000.0");
132 ui->flagLabel->clear();
133 ui->flagLabel->setFocus();
134
135 //disable "Save Changes" button
136 ui->saveButton->setEnabled(false);
137
138 //unselect item from flagList
139 ui->flagList->clearSelection();
140}
141
142void FlagManager::showFlag(int flagIdx)
143{
144 if (flagIdx < 0 || flagIdx >= m_Model->rowCount())
145 {
146 return;
147 }
148
149 else
150 {
151 ui->raBox->setText(m_Model->data(m_Model->index(flagIdx, 0)).toString());
152 ui->decBox->setText(m_Model->data(m_Model->index(flagIdx, 1)).toString());
153 ui->epochBox->setText(m_Model->data(m_Model->index(flagIdx, 2)).toString());
154
155 //ui->flagCombobox->setCurrentItem( m_Model->data( m_Model->index( flagIdx, 3) ).toString() );
156 ui->flagCombobox->setCurrentText(m_Model->data(m_Model->index(flagIdx, 3)).toString());
157 ui->flagLabel->setText(m_Model->data(m_Model->index(flagIdx, 4)).toString());
158
159 QColor labelColor = m_Model->item(flagIdx, 4)->foreground().color();
160 ui->labelColorcombo->setColor(labelColor);
161 }
162
163 ui->flagList->selectRow(flagIdx);
164 ui->saveButton->setEnabled(true);
165}
166
167bool FlagManager::validatePoint()
168{
169 bool raOk(false), decOk(false);
170 dms ra(ui->raBox->createDms(&raOk));
171 dms dec(ui->decBox->createDms(&decOk));
172
173 QString message;
174
175 //check if ra & dec values were successfully converted
176 if (!raOk || !decOk)
177 {
178 KSNotification::error(i18n("Invalid coordinates."));
179 return false;
180 }
181
182 //make sure values are in valid range
183 if (ra.Hours() < 0.0 || ra.Degrees() > 360.0)
184 message = i18n("The Right Ascension value must be between 0.0 and 24.0.");
185 if (dec.Degrees() < -90.0 || dec.Degrees() > 90.0)
186 message += '\n' + i18n("The Declination value must be between -90.0 and 90.0.");
187 if (!message.isEmpty())
188 {
189 KSNotification::sorry(message, i18n("Invalid Coordinate Data"));
190 return false;
191 }
192
193 //all checks passed
194 return true;
195}
196
197void FlagManager::deleteFlagItem(int flagIdx)
198{
199 if (flagIdx < m_Model->rowCount())
200 {
201 m_Model->removeRow(flagIdx);
202 }
203}
204
206{
207 if (validatePoint() == false)
208 return;
209
210 dms ra(ui->raBox->createDms());
211 dms dec(ui->decBox->createDms());
212
213 insertFlag(true);
214
215 FlagComponent *flags = m_Ks->data()->skyComposite()->flags();
216 //Add flag in FlagComponent
217 SkyPoint flagPoint(ra, dec);
218 flags->add(flagPoint, ui->epochBox->text(), ui->flagCombobox->currentText(), ui->flagLabel->text(),
219 ui->labelColorcombo->color());
220
221 ui->flagList->selectRow(m_Model->rowCount() - 1);
222 ui->saveButton->setEnabled(true);
223
224 flags->saveToFile();
225
226 //Redraw map
227 m_Ks->map()->forceUpdate(false);
228}
229
231{
232 int flag = ui->flagList->currentIndex().row();
233
234 //Remove from FlagComponent
235 m_Ks->data()->skyComposite()->flags()->remove(flag);
236
237 //Remove from list
238 m_Model->removeRow(flag);
239
240 //Clear form fields
241 clearFields();
242
243 //Remove from file
244 m_Ks->data()->skyComposite()->flags()->saveToFile();
245
246 //Redraw map
247 m_Ks->map()->forceUpdate(false);
248}
249
251{
252 if (ui->flagList->currentIndex().isValid())
253 {
254 m_Ks->map()->setClickedObject(nullptr);
255 m_Ks->map()->setClickedPoint(
256 m_Ks->data()->skyComposite()->flags()->pointList().at(ui->flagList->currentIndex().row()).get());
257 m_Ks->map()->slotCenter();
258 }
259}
260
262{
263#ifdef HAVE_INDI
264
265 if (INDIListener::Instance()->size() == 0)
266 {
267 KSNotification::sorry(i18n("No connected mounts found."));
268 return;
269 }
270
271 for (auto oneDevice : INDIListener::devices())
272 {
273 if (!(oneDevice->getDriverInterface() & INDI::BaseDevice::TELESCOPE_INTERFACE))
274 continue;
275
276 if (oneDevice->isConnected() == false)
277 {
278 KSNotification::error(
279 i18n("Telescope %1 is offline. Please connect and retry again.", oneDevice->getDeviceName()));
280 return;
281 }
282
283 auto mount = oneDevice->getMount();
284 if (!mount)
285 continue;
286
287 mount->Slew(m_Ks->data()->skyComposite()->flags()->pointList().at(ui->flagList->currentIndex().row()).get());
288
289 return;
290 }
291
292 KSNotification::sorry(i18n("No connected mounts found."));
293
294#endif
295}
296
297void FlagManager::slotSaveChanges()
298{
299 int row = ui->flagList->currentIndex().row();
300
301 if (validatePoint() == false)
302 return;
303
304 insertFlag(false, row);
305
306 m_Ks->map()->forceUpdate();
307
308 dms ra(ui->raBox->createDms());
309 dms dec(ui->decBox->createDms());
310
311 SkyPoint flagPoint(ra, dec);
312
313 //Update FlagComponent
314 m_Ks->data()->skyComposite()->flags()->updateFlag(row, flagPoint, ui->epochBox->text(),
315 ui->flagCombobox->currentText(), ui->flagLabel->text(),
316 ui->labelColorcombo->color());
317
318 //Save changes to file
319 m_Ks->data()->skyComposite()->flags()->saveToFile();
320
321 ui->flagList->selectRow(row);
322}
323
324void FlagManager::slotSetShownFlag(QModelIndex idx)
325{
326 showFlag(idx.row());
327}
328
329void FlagManager::insertFlag(bool isNew, int row)
330{
331 dms ra(ui->raBox->createDms());
332 dms dec(ui->decBox->createDms());
333 SkyPoint flagPoint(ra, dec);
334
335 // Add flag in the list
336 QList<QStandardItem *> itemList;
337
338 QStandardItem *labelItem = new QStandardItem(ui->flagLabel->text());
339 labelItem->setForeground(QBrush(ui->labelColorcombo->color()));
340
341 FlagComponent *flags = m_Ks->data()->skyComposite()->flags();
342
343 QPixmap pixmap;
344 itemList << new QStandardItem(flagPoint.ra0().toHMSString()) << new QStandardItem(flagPoint.dec0().toDMSString())
345 << new QStandardItem(ui->epochBox->text())
346 << new QStandardItem(QIcon(pixmap.fromImage(flags->imageList(ui->flagCombobox->currentIndex()))),
347 ui->flagCombobox->currentText())
348 << labelItem;
349
350 if (isNew)
351 {
352 m_Model->appendRow(itemList);
353 }
354
355 else
356 {
357 for (int i = 0; i < m_Model->columnCount(); i++)
358 {
359 m_Model->setItem(row, i, itemList.at(i));
360 }
361 }
362}
Represents a flag on the sky map.
void saveToFile()
Save flags to flags.dat file.
QColor labelColor(int index)
Get label color.
void remove(int index)
Remove a flag.
QList< QImage > imageList()
Get images.
QString label(int index)
Get label.
void add(const SkyPoint &flagPoint, QString epoch, QString image, QString label, QColor labelColor)
Add a flag.
QString epoch(int index)
Get epoch.
QString imageName(int index)
Get image name.
void updateFlag(int index, const SkyPoint &flagPoint, QString epoch, QString image, QString label, QColor labelColor)
Update a flag.
QImage image(int index)
Get image.
void slotCenterTelescope()
Center the selected flag in the telescope.
void slotCenterFlag()
Center the selected flag in the display.
void slotAddFlag()
Verify coordinates and add a flag.
void slotDeleteFlag()
Delete a flag.
SkyMapComposite * skyComposite()
Definition kstarsdata.h:168
SkyMap * map() const
Definition kstars.h:139
static KStars * Instance()
Definition kstars.h:121
KStarsData * data() const
Definition kstars.h:133
void setClickedPoint(const SkyPoint *f)
Set the ClickedPoint to the skypoint given as an argument.
Definition skymap.cpp:1008
void setClickedObject(SkyObject *o)
Set the ClickedObject pointer to the argument.
Definition skymap.cpp:366
void forceUpdate(bool now=false)
Recalculates the positions of objects in the sky, and then repaints the sky map.
Definition skymap.cpp:1173
void slotCenter()
Center the display at the point ClickedPoint.
Definition skymap.cpp:380
The sky coordinates of a point in the sky.
Definition skypoint.h:45
An angle, stored as degrees, but expressible in many ways.
Definition dms.h:38
double Hours() const
Definition dms.h:168
const double & Degrees() const
Definition dms.h:141
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
bool removeRow(int row, const QModelIndex &parent)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
const QColor & color() const const
const_reference at(qsizetype i) const const
void clear()
pointer data()
qsizetype size() const const
int row() const const
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
void appendRow(QStandardItem *item)
QBrush foreground() const const
void setForeground(const QBrush &brush)
void appendRow(QStandardItem *item)
virtual int columnCount(const QModelIndex &parent) const const override
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
QStandardItem * item(int row, int column) const const
virtual int rowCount(const QModelIndex &parent) const const override
void setItem(int row, QStandardItem *item)
bool isEmpty() const const
QTextStream & dec(QTextStream &stream)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
void setEnabled(bool)
void setFocus()
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.