1 /*
2     SPDX-FileCopyrightText: 2012 Jasem Mutlaq <mutlaqja@ikartech.com>
3     SPDX-FileCopyrightText: 2021 Wolfgang Reissenberger <sterne-jaeger@openfuture.de>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "guidemanager.h"
9 
10 #include "kstarsdata.h"
11 #include "Options.h"
12 
13 namespace Ekos
14 {
GuideManager(QWidget * parent)15 GuideManager::GuideManager(QWidget *parent) : QWidget(parent)
16 {
17     setupUi(this);
18     guideStateWidget = new GuideStateWidget();
19     guideTitleLayout->addWidget(guideStateWidget, 0, Qt::AlignRight);
20 }
21 
init(Guide * guideProcess)22 void GuideManager::init(Guide *guideProcess)
23 {
24     // guide details buttons
25     connect(guideDetailNextButton, &QPushButton::clicked, [this]() {
26         const int pos = guideDetailWidget->currentIndex();
27         if (pos == 0 || (pos == 1 && guideStarPixmap.get() != nullptr))
28             guideDetailWidget->setCurrentIndex(pos+1);
29         else if (pos > 0)
30             guideDetailWidget->setCurrentIndex(0);
31 
32         updateGuideDetailView();
33     });
34 
35     connect(guideDetailPrevButton, &QPushButton::clicked, [this]() {
36         const int pos = guideDetailWidget->currentIndex();
37         if (pos > 0)
38             guideDetailWidget->setCurrentIndex(pos-1);
39         else if (guideStarPixmap.get() != nullptr)
40             guideDetailWidget->setCurrentIndex(2);
41         else
42             guideDetailWidget->setCurrentIndex(1);
43 
44         updateGuideDetailView();
45     });
46 
47     // feed guide state widget
48     connect(guideProcess, &Ekos::Guide::newStatus, guideStateWidget, &Ekos::GuideStateWidget::updateGuideStatus);
49 
50     if (!guidePI)
51     {
52         guidePI = new QProgressIndicator(guideProcess);
53         guideTitleLayout->insertWidget(2, guidePI);
54     }
55 
56     // initialize the target rings
57     targetPlot->buildTarget(Options::guiderAccuracyThreshold());
58 
59 
60     // establish connections to receive guiding data
61     driftGraph->connectGuider(guideProcess->getGuider());
62     targetPlot->connectGuider(guideProcess->getGuider());
63 
64     // connect to Guide UI controls
65     //This connects all the buttons and slider below the guide plots.
66     connect(guideProcess->showRAPlotCheck, &QCheckBox::toggled, [this](bool isChecked) {driftGraph->toggleShowPlot(GuideGraph::G_RA, isChecked);});
67     connect(guideProcess->showDECPlotCheck, &QCheckBox::toggled, [this](bool isChecked) {driftGraph->toggleShowPlot(GuideGraph::G_DEC, isChecked);});
68     connect(guideProcess->showRACorrectionsCheck, &QCheckBox::toggled, [this](bool isChecked) {driftGraph->toggleShowPlot(GuideGraph::G_RA_PULSE, isChecked);});
69     connect(guideProcess->showDECorrectionsCheck, &QCheckBox::toggled, [this](bool isChecked) {driftGraph->toggleShowPlot(GuideGraph::G_DEC_PULSE, isChecked);});
70     connect(guideProcess->showSNRPlotCheck, &QCheckBox::toggled, [this](bool isChecked) {driftGraph->toggleShowPlot(GuideGraph::G_SNR, isChecked);});
71     connect(guideProcess->showRMSPlotCheck, &QCheckBox::toggled, [this](bool isChecked) {driftGraph->toggleShowPlot(GuideGraph::G_RMS, isChecked);});
72     connect(guideProcess->correctionSlider, &QSlider::sliderMoved, driftGraph, &GuideDriftGraph::setCorrectionGraphScale);
73 
74     connect(guideProcess->latestCheck, &QCheckBox::toggled, targetPlot, &GuideTargetPlot::setLatestGuidePoint);
75     connect(guideProcess->accuracyRadiusSpin, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), targetPlot,
76             &GuideTargetPlot::buildTarget);
77 
78 
79 }
80 
81 
updateGuideStatus(Ekos::GuideState status)82 void GuideManager::updateGuideStatus(Ekos::GuideState status)
83 {
84     guideStatus->setText(Ekos::getGuideStatusString(status));
85 
86     switch (status)
87     {
88         case Ekos::GUIDE_IDLE:
89         case Ekos::GUIDE_CALIBRATION_ERROR:
90         case Ekos::GUIDE_ABORTED:
91         case Ekos::GUIDE_SUSPENDED:
92         case Ekos::GUIDE_DITHERING_ERROR:
93         case Ekos::GUIDE_CALIBRATION_SUCESS:
94             if (guidePI->isAnimated())
95                 guidePI->stopAnimation();
96             break;
97 
98         case Ekos::GUIDE_CALIBRATING:
99             guidePI->setColor(QColor(KStarsData::Instance()->colorScheme()->colorNamed("TargetColor")));
100             if (guidePI->isAnimated() == false)
101                 guidePI->startAnimation();
102             break;
103         case Ekos::GUIDE_GUIDING:
104             guidePI->setColor(Qt::darkGreen);
105             if (guidePI->isAnimated() == false)
106                 guidePI->startAnimation();
107             targetPlot->clear();
108             driftGraph->clear();
109             break;
110         case Ekos::GUIDE_DITHERING:
111             guidePI->setColor(QColor(KStarsData::Instance()->colorScheme()->colorNamed("TargetColor")));
112             if (guidePI->isAnimated() == false)
113                 guidePI->startAnimation();
114             break;
115         case Ekos::GUIDE_DITHERING_SUCCESS:
116             guidePI->setColor(Qt::darkGreen);
117             if (guidePI->isAnimated() == false)
118                 guidePI->startAnimation();
119             break;
120 
121         default:
122             if (guidePI->isAnimated())
123                 guidePI->stopAnimation();
124             break;
125     }
126 
127 }
128 
129 
130 
updateGuideStarPixmap(QPixmap & starPix)131 void GuideManager::updateGuideStarPixmap(QPixmap &starPix)
132 {
133     if (starPix.isNull())
134         return;
135 
136     guideStarPixmap.reset(new QPixmap(starPix));
137     updateGuideDetailView();
138 }
139 
updateSigmas(double ra,double de)140 void GuideManager::updateSigmas(double ra, double de)
141 {
142     errRA->setText(QString::number(ra, 'f', 2) + "\"");
143     errDEC->setText(QString::number(de, 'f', 2) + "\"");
144 }
145 
updateGuideDetailView()146 void GuideManager::updateGuideDetailView()
147 {
148     const int pos = guideDetailWidget->currentIndex();
149     if (pos == 2 && guideStarPixmap.get() != nullptr)
150         guideStarView->setPixmap(guideStarPixmap.get()->scaled(guideStarView->width(), guideStarView->height(),
151                                                                  Qt::KeepAspectRatio, Qt::SmoothTransformation));
152 }
153 
reset()154 void GuideManager::reset()
155 {
156     guideStatus->setText(i18n("Idle"));
157 
158     if (guidePI)
159         guidePI->stopAnimation();
160 }
161 
162 }
163