• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Stellarium / stellarium / 6685397774

29 Oct 2023 07:37PM UTC coverage: 11.735% (-0.002%) from 11.737%
6685397774

push

github

10110111
Deduplicate title bar implementation

131 of 131 new or added lines in 56 files covered. (100.0%)

14842 of 126472 relevant lines covered (11.74%)

21617.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/plugins/TelescopeControl/src/gui/SlewDialog.cpp
1
/*
2
 * Stellarium Telescope Control Plug-in
3
 *
4
 * Copyright (C) 2010 Bogdan Marinov (this file)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
19
*/
20

21
#include <QDebug>
22

23
#include "Dialog.hpp"
24
#include "StelFileMgr.hpp"
25
#include "AngleSpinBox.hpp"
26
#include "StelApp.hpp"
27
#include "VecMath.hpp"
28
#include "StelJsonParser.hpp"
29
#include "StelUtils.hpp"
30
#include "StelCore.hpp"
31
#include "StelObjectMgr.hpp"
32
#include "StelModuleMgr.hpp"
33

34
#include "TelescopeControl.hpp"
35
#include "SlewDialog.hpp"
36
#include "ui_slewDialog.h"
37
#include "TelescopeClient.hpp"
38

39
SlewDialog::SlewDialog(const QString &dialogName, QObject *parent)
×
40
        : StelDialog(dialogName, parent)
41
        , storedPointsDialog(nullptr)
×
42
{
43
        ui = new Ui_slewDialog();
×
44
        
45
        //TODO: Fix this - it's in the same plugin
46
        telescopeManager = GETSTELMODULE(TelescopeControl);
×
47
}
×
48

49
SlewDialog::~SlewDialog()
×
50
{        
51
        delete ui;
×
52
        delete storedPointsDialog;
×
53
        storedPointsDialog = nullptr;
×
54
}
×
55

56
void SlewDialog::retranslate()
×
57
{
58
        if (dialog)
×
59
                ui->retranslateUi(dialog);
×
60
}
×
61

62
void SlewDialog::createDialogContent()
×
63
{
64
        ui->setupUi(dialog);
×
65
        
66
        //Inherited connect
67
        connect(&StelApp::getInstance(), SIGNAL(languageChanged()), this, SLOT(retranslate()));
×
68
        connect(ui->titleBar, &TitleBar::closeClicked, this, &StelDialog::close);
×
69
        connect(ui->titleBar, SIGNAL(movedTo(QPoint)), this, SLOT(handleMovedTo(QPoint)));
×
70

71
        connect(ui->radioButtonHMS, SIGNAL(toggled(bool)), this, SLOT(setFormatHMS(bool)));
×
72
        connect(ui->radioButtonDMS, SIGNAL(toggled(bool)), this, SLOT(setFormatDMS(bool)));
×
73
        connect(ui->radioButtonDecimal, SIGNAL(toggled(bool)), this, SLOT(setFormatDecimal(bool)));
×
74

75
        connect(ui->pushButtonSlew, SIGNAL(clicked()), this, SLOT(slew()));
×
76
        connect(ui->pushButtonSync, SIGNAL(clicked()), this, SLOT(sync()));
×
77
        connect(ui->pushButtonAbort, SIGNAL(clicked()), this, SLOT(abort()));
×
78
        connect(ui->pushButtonConfigure, SIGNAL(clicked()), this, SLOT(showConfiguration()));
×
79

80
        connect(telescopeManager, SIGNAL(clientConnected(int, QString)), this, SLOT(addTelescope(int, QString)));
×
81
        connect(telescopeManager, SIGNAL(clientDisconnected(int)), this, SLOT(removeTelescope(int)));
×
82

83
        connect(ui->comboBoxStoredPoints, SIGNAL(currentIndexChanged(int)), this, SLOT(getStoredPointInfo()));
×
84
        connect(ui->toolButtonStoredPoints, SIGNAL(clicked()), this, SLOT(editStoredPoints()));
×
85

86
        connect(ui->pushButtonCurrent, SIGNAL(clicked()), this, SLOT(getCurrentObjectInfo()));
×
87
        connect(ui->pushButtonCenter, SIGNAL(clicked()), this, SLOT(getCenterInfo()));
×
88

89
        QObject::connect(ui->comboBoxTelescope, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &SlewDialog::onCurrentTelescopeChanged);
×
90

91
        //Coordinates are in HMS by default:
92
        ui->radioButtonHMS->setChecked(true);
×
93

94
        storedPointsDialog = new StoredPointsDialog();
×
95
        // add point and remove
96
        connect(storedPointsDialog, SIGNAL(addStoredPoint(int, QString, double, double)), this, SLOT(addStoredPointToComboBox(int, QString, double, double)));
×
97
        // remove point
98
        connect(storedPointsDialog, SIGNAL(removeStoredPoint(int)), this, SLOT(removeStoredPointFromComboBox(int)));
×
99
        // clean points
100
        connect(storedPointsDialog, SIGNAL(clearStoredPoints()), this, SLOT(clearStoredPointsFromComboBox()));
×
101

102

103
        updateTelescopeList();
×
104
        updateStoredPointsList();
×
105
}
×
106

107
void SlewDialog::showConfiguration()
×
108
{
109
        //Hack to work around having no direct way to do display the window
110
        telescopeManager->configureGui(true);
×
111
}
×
112

113
void SlewDialog::setFormatHMS(bool set)
×
114
{
115
        if (!set)
×
116
                return;
×
117

118
        ui->spinBoxRA->setDecimals(2);
×
119
        ui->spinBoxDec->setDecimals(2);
×
120
        ui->spinBoxRA->setDisplayFormat(AngleSpinBox::HMSLetters);
×
121
        ui->spinBoxDec->setDisplayFormat(AngleSpinBox::DMSSymbols);
×
122
}
123

124
void SlewDialog::setFormatDMS(bool set)
×
125
{
126
        if (!set)
×
127
                return;
×
128

129
        ui->spinBoxRA->setDecimals(2);
×
130
        ui->spinBoxDec->setDecimals(2);
×
131
        ui->spinBoxRA->setDisplayFormat(AngleSpinBox::DMSSymbols);
×
132
        ui->spinBoxDec->setDisplayFormat(AngleSpinBox::DMSSymbols);
×
133
}
134

135
void SlewDialog::setFormatDecimal(bool set)
×
136
{
137
        if (!set)
×
138
                return;
×
139

140
        ui->spinBoxRA->setDecimals(6);
×
141
        ui->spinBoxDec->setDecimals(6);
×
142
        ui->spinBoxRA->setDisplayFormat(AngleSpinBox::DecimalDeg);
×
143
        ui->spinBoxDec->setDisplayFormat(AngleSpinBox::DecimalDeg);
×
144
}
145

146
void SlewDialog::updateTelescopeList()
×
147
{
148
        connectedSlotsByName.clear();
×
149
        ui->comboBoxTelescope->clear();
×
150

151
        QHash<int, QString> connectedSlotsByNumber = telescopeManager->getConnectedClientsNames();
×
152
        QHash<int, QString> ::const_iterator it=connectedSlotsByNumber.constBegin();
×
153
        while (it!=connectedSlotsByNumber.constEnd())
×
154
        {
155
                QString telescopeName = connectedSlotsByNumber.value(it.key());
×
156
                connectedSlotsByName.insert(telescopeName, it.key());
×
157
                ui->comboBoxTelescope->addItem(telescopeName);
×
158
                ++it;
×
159
        }
×
160
        
161
        updateTelescopeControls();
×
162
}
×
163

164
void SlewDialog::updateTelescopeControls()
×
165
{
166
        bool connectedTelescopeAvailable = !connectedSlotsByName.isEmpty();
×
167
        ui->groupBoxSlew->setVisible(connectedTelescopeAvailable);
×
168
        ui->labelNoTelescopes->setVisible(!connectedTelescopeAvailable);
×
169
        if (connectedTelescopeAvailable)
×
170
                ui->comboBoxTelescope->setCurrentIndex(0);
×
171
}
×
172

173
void SlewDialog::addTelescope(int slot, QString name)
×
174
{
175
        if (slot <=0 || name.isEmpty())
×
176
                return;
×
177

178
        connectedSlotsByName.insert(name, slot);
×
179
        ui->comboBoxTelescope->addItem(name);
×
180

181
        updateTelescopeControls();
×
182
}
183

184
void SlewDialog::removeTelescope(int slot)
×
185
{
186
        if (slot <= 0)
×
187
                return;
×
188

189
        QString name = connectedSlotsByName.key(slot, QString());
×
190
        if (name.isEmpty())
×
191
                return;
×
192

193
        connectedSlotsByName.remove(name);
×
194

195
        int index = ui->comboBoxTelescope->findText(name);
×
196
        if (index != -1)
×
197
        {
198
                ui->comboBoxTelescope->removeItem(index);
×
199
        }
200
        else
201
        {
202
                //Something very wrong just happened, so:
203
                updateTelescopeList();
×
204
                return;
×
205
        }
206

207
        updateTelescopeControls();
×
208
}
×
209

210
QSharedPointer<TelescopeClient> SlewDialog::currentTelescope() const
×
211
{
212
        int slot = connectedSlotsByName.value(ui->comboBoxTelescope->currentText());
×
213
        return telescopeManager->telescopeClient(slot);
×
214
}
215

216
void SlewDialog::slew()
×
217
{
218
        double radiansRA  = ui->spinBoxRA->valueRadians();
×
219
        double radiansDec = ui->spinBoxDec->valueRadians();
×
220

221
        Vec3d targetPosition;
×
222
        StelUtils::spheToRect(radiansRA, radiansDec, targetPosition);
×
223

224
        auto telescope = currentTelescope();
×
225
        if (!telescope)
×
226
                return;
×
227

228
        StelObjectP selectObject = nullptr;
×
229
        telescope->telescopeGoto(targetPosition, selectObject);
×
230
}
×
231

232
void SlewDialog::sync()
×
233
{
234
        double radiansRA  = ui->spinBoxRA->valueRadians();
×
235
        double radiansDec = ui->spinBoxDec->valueRadians();
×
236

237
        Vec3d targetPosition;
×
238
        StelUtils::spheToRect(radiansRA, radiansDec, targetPosition);
×
239

240
        auto telescope = currentTelescope();
×
241
        if (!telescope)
×
242
                return;
×
243

244
        StelObjectP selectObject = nullptr;
×
245
        telescope->telescopeSync(targetPosition, selectObject);
×
246
}
×
247

248
void SlewDialog::abort()
×
249
{
250
        auto telescope = currentTelescope();
×
251
        if (!telescope)
×
252
                return;
×
253

254
        telescope->telescopeAbortSlew();
×
255
}
×
256

257
void SlewDialog::getCurrentObjectInfo()
×
258
{
259
        const QList<StelObjectP>& selected = GETSTELMODULE(StelObjectMgr)->getSelectedObject();
×
260
        if (!selected.isEmpty()) {
×
261
                double dec_j2000 = 0;
×
262
                double ra_j2000 = 0;
×
263
                StelUtils::rectToSphe(&ra_j2000,&dec_j2000,selected[0]->getJ2000EquatorialPos(StelApp::getInstance().getCore()));
×
264
                ui->spinBoxRA->setRadians(ra_j2000);
×
265
                ui->spinBoxDec->setRadians(dec_j2000);
×
266
        }
267
}
×
268

269
void SlewDialog::getCenterInfo()
×
270
{
271
        StelCore *core = StelApp::getInstance().getCore();
×
272
        const StelProjectorP projector = core->getProjection(StelCore::FrameEquinoxEqu);
×
273
        Vec3d centerPosition;
×
274
        Vector2<qreal> center = projector->getViewportCenter();
×
275
        projector->unProject(center[0], center[1], centerPosition);
×
276
        double dec_j2000 = 0;
×
277
        double ra_j2000 = 0;
×
278
        StelUtils::rectToSphe(&ra_j2000,&dec_j2000,core->equinoxEquToJ2000(centerPosition, StelCore::RefractionOff));
×
279
        ui->spinBoxRA->setRadians(ra_j2000);
×
280
        ui->spinBoxDec->setRadians(dec_j2000);
×
281
}
×
282

283
void SlewDialog::editStoredPoints()
×
284
{
285
        storedPointsDialog->setVisible(true);
×
286
        QVariantList qvl;
×
287
        for (int i = 1;i< ui->comboBoxStoredPoints->count(); i++)
×
288
        {
289
                QVariant var = ui->comboBoxStoredPoints->itemData(i);
×
290
                qvl.append(var);
×
291
        }
×
292
        storedPointsDialog->populatePointsList(qvl);
×
293
}
×
294

295
void SlewDialog::updateStoredPointsList()
×
296
{
297
        //user point from file
298
        loadPointsFromFile();
×
299
}
×
300

301
void SlewDialog::addStoredPointToComboBox(int number, QString name, double radiansRA, double radiansDec)
×
302
{
303
        if (!ui->comboBoxStoredPoints->count())
×
304
        {
305
                ui->comboBoxStoredPoints->addItem(q_("Select one"));
×
306
        }
307
        storedPoint sp;
×
308
        sp.number = number;
×
309
        sp.name = name;
×
310
        sp.radiansRA = radiansRA;
×
311
        sp.radiansDec= radiansDec;
×
312

313
        QVariant var;
×
314
        var.setValue(sp);
×
315
        ui->comboBoxStoredPoints->addItem(name,var);
×
316

317
        savePointsToFile();
×
318
}
×
319

320
void SlewDialog::removeStoredPointFromComboBox(int number)
×
321
{
322
        ui->comboBoxStoredPoints->removeItem(number+1);
×
323
        if (1 == ui->comboBoxStoredPoints->count())
×
324
        {
325
                ui->comboBoxStoredPoints->removeItem(0);
×
326
        }
327
        savePointsToFile();
×
328
}
×
329

330
void SlewDialog::clearStoredPointsFromComboBox()
×
331
{
332
        ui->comboBoxStoredPoints->blockSignals(true);
×
333
        ui->comboBoxStoredPoints->clear();
×
334
        ui->comboBoxStoredPoints->addItem(q_("Select one"));
×
335
        ui->comboBoxStoredPoints->blockSignals(false);
×
336
        savePointsToFile();
×
337
}
×
338

339
void SlewDialog::getStoredPointInfo()
×
340
{
341
        QVariant var = ui->comboBoxStoredPoints->currentData();
×
342
        storedPoint sp = var.value<storedPoint>();
×
343

344
        ui->spinBoxRA->setRadians(sp.radiansRA);
×
345
        ui->spinBoxDec->setRadians(sp.radiansDec);
×
346
}
×
347

348
void SlewDialog::onMove(double angle, double speed)
×
349
{
350
        auto telescope = currentTelescope();
×
351
        if (telescope)
×
352
                telescope->move(angle, speed);
×
353
}
×
354

355
void SlewDialog::onCurrentTelescopeChanged()
×
356
{
357
        // remove previous controlWidget
358
        QLayoutItem* child;
359
        while ((child = ui->controlWidgetLayout->takeAt(0)) != nullptr)
×
360
        {
361
                delete child->widget();
×
362
                delete child;
×
363
        }
364

365
        auto telescope = currentTelescope();
×
366
        if (!telescope)
×
367
                return;
×
368

369
        auto controlWidget = telescope->createControlWidget(telescope);
×
370
        if (!controlWidget)
×
371
                return;
×
372

373
        ui->controlWidgetLayout->addWidget(controlWidget);
×
374
}
×
375

376
void SlewDialog::savePointsToFile()
×
377
{
378
        //Open/create the JSON file
379
        QString pointsJsonPath = StelFileMgr::findFile("modules/TelescopeControl", static_cast<StelFileMgr::Flags>(StelFileMgr::Directory|StelFileMgr::Writable)) + "/points.json";
×
380
        if (pointsJsonPath.isEmpty())
×
381
        {
382
                qWarning() << "SlewDialog: Error saving points";
×
383
                return;
×
384
        }
385
        QFile pointsJsonFile(pointsJsonPath);
×
386
        if(!pointsJsonFile.open(QFile::WriteOnly|QFile::Text))
×
387
        {
388
                qWarning() << "SlewDialog: Points can not be saved. A file can not be open for writing:"
×
389
                                   << QDir::toNativeSeparators(pointsJsonPath);
×
390
                return;
×
391
        }
392

393
        storedPointsDescriptions.clear();
×
394
        // All user stored points from comboBox
395
        for (int i = 1;i< ui->comboBoxStoredPoints->count(); i++)
×
396
        {
397
                QVariant var = ui->comboBoxStoredPoints->itemData(i);
×
398
                storedPoint sp = var.value<storedPoint>();
×
399
                QVariantMap point = {
400
                        {"number",        sp.number},
401
                        {"name",          sp.name},
402
                        {"radiansRA",     sp.radiansRA},
403
                        {"radiansDec",    sp.radiansDec}};
×
404

405
                storedPointsDescriptions.insert(QString::number(sp.number),point);
×
406
        }
×
407
        //Add the version:
408
        storedPointsDescriptions.insert("version", QString(TELESCOPE_CONTROL_CONFIG_VERSION));
×
409
        //Convert the tree to JSON
410
        StelJsonParser::write(storedPointsDescriptions, &pointsJsonFile);
×
411
        pointsJsonFile.flush();//Is this necessary?
×
412
        pointsJsonFile.close();
×
413
}
×
414

415
void SlewDialog::loadPointsFromFile()
×
416
{
417
        QVariantMap result;
×
418
        QString pointsJsonPath = StelFileMgr::findFile("modules/TelescopeControl", static_cast<StelFileMgr::Flags>(StelFileMgr::Directory|StelFileMgr::Writable)) + "/points.json";
×
419

420
        if (pointsJsonPath.isEmpty())
×
421
        {
422
                qWarning() << "SlewDialog: Error loading points";
×
423
                return;
×
424
        }
425
        if(!QFileInfo::exists(pointsJsonPath))
×
426
        {
427
                qWarning() << "SlewDialog::loadPointsFromFile(): No points loaded. File is missing:"
×
428
                                   << QDir::toNativeSeparators(pointsJsonPath);
×
429
                storedPointsDescriptions = result;
×
430
                return;
×
431
        }
432

433
        QFile pointsJsonFile(pointsJsonPath);
×
434

435
        QVariantMap map;
×
436

437
        if(!pointsJsonFile.open(QFile::ReadOnly))
×
438
        {
439
                qWarning() << "SlewDialog: No points loaded. Can't open for reading"
×
440
                                   << QDir::toNativeSeparators(pointsJsonPath);
×
441
                storedPointsDescriptions = result;
×
442
                return;
×
443
        }
444
        else
445
        {
446
                map = StelJsonParser::parse(&pointsJsonFile).toMap();
×
447
                pointsJsonFile.close();
×
448
        }
449

450
        //File contains any points?
451
        if(map.isEmpty())
×
452
        {
453
                storedPointsDescriptions = result;
×
454
                return;
×
455
        }
456

457
        QString version = map.value("version", "0.0.0").toString();
×
458
        if(version < QString(TELESCOPE_CONTROL_CONFIG_VERSION))
×
459
        {
460
                QString newName = pointsJsonPath + ".backup." + QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss");
×
461
                if(pointsJsonFile.rename(newName))
×
462
                {
463
                        qWarning() << "SlewDialog: The existing version of points.json is obsolete. Backing it up as "
×
464
                                           << QDir::toNativeSeparators(newName);
×
465
                        qWarning() << "SlewDialog: A blank points.json file will have to be created.";
×
466
                        storedPointsDescriptions = result;
×
467
                        return;
×
468
                }
469
                else
470
                {
471
                        qWarning() << "SlewDialog: The existing version of points.json is obsolete. Unable to rename.";
×
472
                        storedPointsDescriptions = result;
×
473
                        return;
×
474
                }
475
        }
×
476

477
        //Read points, if any
478
        QMapIterator<QString, QVariant> node(map);
×
479

480
        if(node.hasNext())
×
481
        {
482
                ui->comboBoxStoredPoints->addItem(q_("Select one"));
×
483
                do
484
                {
485
                        node.next();
×
486

487
                        QVariantMap point = node.value().toMap();
×
488
                        storedPoint sp;
×
489
                        sp.name       = point.value("name").toString();
×
490
                        sp.number     = point.value("number").toInt();
×
491
                        sp.radiansRA  = point.value("radiansRA").toDouble();
×
492
                        sp.radiansDec = point.value("radiansDec").toDouble();
×
493

494
                        QVariant var;
×
495
                        var.setValue(sp);
×
496
                        ui->comboBoxStoredPoints->addItem(sp.name,var);
×
497
                }
×
498
                while (node.hasNext());
×
499
        }
500
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc