moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / telescopewizardprocess.cpp
blobcffb2cf44f29d3168fa181f97441ac1d7be3364f
1 /* Telescope wizard
2 Copyright (C) 2003 Jasem Mutlaq (mutlaqja@ikarustech.com)
4 This application is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8 */
10 #include <qfile.h>
11 #include <qpixmap.h>
12 #include <qlabel.h>
13 #include <qpushbutton.h>
14 #include <qwidgetstack.h>
15 #include <qstring.h>
16 #include <qtimer.h>
17 #include <qtable.h>
18 #include <qtextedit.h>
19 #include <qradiobutton.h>
21 #include <klistview.h>
22 #include <klineedit.h>
23 #include <kmessagebox.h>
24 #include <kprogress.h>
26 #include "telescopewizardprocess.h"
27 #include "Options.h"
28 #include "kstars.h"
29 #include "kstarsdata.h"
30 #include "timedialog.h"
31 #include "ksutils.h"
32 #include "libkdeedu/extdate/extdatetime.h"
34 #include "indimenu.h"
35 #include "indidriver.h"
36 #include "indielement.h"
37 #include "indiproperty.h"
38 #include "indistd.h"
39 #include "indidevice.h"
41 #define TIMEOUT_THRESHHOLD 20
43 telescopeWizardProcess::telescopeWizardProcess( QWidget* parent, const char* name ) : telescopeWizard(parent, name)
45 currentPort = -1;
46 timeOutCount = 0;
47 indiDev = NULL;
48 progressScan = NULL;
49 linkRejected = false;
51 QString locStr;
52 QFile sideIMG;
54 if (KSUtils::openDataFile(sideIMG, "wizardside.png"))
55 wizardPix->setPixmap(QPixmap(sideIMG.name()));
57 backB->hide();
58 currentPage = INTRO_P;
60 ksw = (KStars *) parent;
62 ksw->establishINDI();
64 indimenu = ksw->getINDIMenu();
65 indidriver = ksw->getINDIDriver();
67 INDIMessageBar = Options::indiMessages();
68 Options::setIndiMessages( false );
70 QTime newTime( ksw->data()->lt().time() );
71 ExtDate newDate( ksw->data()->lt().date() );
73 timeOut->setText( QString().sprintf("%02d:%02d:%02d", newTime.hour(), newTime.minute(), newTime.second()));
74 dateOut->setText( QString().sprintf("%d-%02d-%02d", newDate.year(), newDate.month(), newDate.day()));
76 if (ksw->geo()->translatedProvince().isEmpty())
77 locationOut->setText( QString("%1, %2").arg(ksw->geo()->translatedName()).arg(ksw->geo()->translatedCountry()));
78 else
79 locationOut->setText( QString("%1, %2, %3").arg(ksw->geo()->translatedName())
80 .arg(ksw->geo()->translatedProvince())
81 .arg(ksw->geo()->translatedCountry()));
84 for (unsigned int i=0; i < indidriver->devices.size(); i++)
85 if (indidriver->devices[i]->deviceType == KSTARS_TELESCOPE)
86 telescopeCombo->insertItem(indidriver->devices[i]->label);
88 if ( !Options::indiTelescopePort().isEmpty())
89 portList << Options::indiTelescopePort();
91 portList << "/dev/ttyS0" << "/dev/ttyS1" << "/dev/ttyS2" << "/dev/ttyS3" << "/dev/ttyS4"
92 << "/dev/ttyUSB0" << "/dev/ttyUSB1" << "/dev/ttyUSB2" << "/dev/ttyUSB3";// << "/dev/ttyUSB4";
94 connect(helpB, SIGNAL(clicked()), parent, SLOT(appHelpActivated()));
95 connect(nextB, SIGNAL(clicked()), this, SLOT(processNext()));
96 connect(backB, SIGNAL(clicked()), this, SLOT(processBack()));
97 connect(setTimeB, SIGNAL(clicked()), this, SLOT(newTime()));
98 connect(setLocationB, SIGNAL(clicked()), this, SLOT(newLocation()));
100 newDeviceTimer = new QTimer(this);
101 QObject::connect( newDeviceTimer, SIGNAL(timeout()), this, SLOT(processPort()) );
105 telescopeWizardProcess::~telescopeWizardProcess()
107 if (progressScan)
108 if (progressScan->wasCancelled())
109 indidriver->processDeviceStatus(1);
111 Options::setIndiMessages( INDIMessageBar );
113 Reset();
116 void telescopeWizardProcess::processNext(void)
118 int linkResult=0;
120 switch (currentPage)
122 case INTRO_P:
123 currentPage++;
124 backB->show();
125 wizardContainer->raiseWidget(currentPage);
126 break;
127 case MODEL_P:
128 currentPage++;
129 wizardContainer->raiseWidget(currentPage);
130 break;
131 case TELESCOPE_P:
132 currentPage++;
133 wizardContainer->raiseWidget(currentPage);
134 break;
135 case LOCAL_P:
136 currentPage++;
137 wizardContainer->raiseWidget(currentPage);
138 break;
139 case PORT_P:
140 linkResult = establishLink();
141 if ( linkResult == 1)
143 progressScan = new KProgressDialog(this, "autoscan", i18n("Autoscan"), i18n("Please wait while KStars scan communication ports for attached telescopes.\nThis process might take few minutes to complete."), true);
144 progressScan->setAllowCancel(true);
145 progressScan->setAutoClose(true);
146 progressScan->setAutoReset(true);
147 progressScan->progressBar()->setTotalSteps(portList.count());
148 progressScan->progressBar()->setValue(0);
149 progressScan->show();
151 else if (linkResult == 2)
152 KMessageBox::queuedMessageBox(0, KMessageBox::Information, i18n("Please wait while KStars tries to connect to your telescope..."));
153 else if (linkResult == -1)
154 KMessageBox::error(0, i18n("Error. Unable to locate telescope drivers."));
155 break;
156 default:
157 break;
162 void telescopeWizardProcess::processBack(void)
164 // for now, just display the next page, and restart once we reached the end
166 switch (currentPage)
168 case INTRO_P:
169 // we shouldn't be here!
170 break;
171 break;
172 case MODEL_P:
173 currentPage--;
174 backB->hide();
175 wizardContainer->raiseWidget(currentPage);
176 break;
177 case TELESCOPE_P:
178 currentPage--;
179 wizardContainer->raiseWidget(currentPage);
180 break;
181 case LOCAL_P:
182 currentPage--;
183 wizardContainer->raiseWidget(currentPage);
184 break;
185 case PORT_P:
186 currentPage--;
187 wizardContainer->raiseWidget(currentPage);
188 break;
189 default:
190 break;
195 void telescopeWizardProcess::newTime()
197 TimeDialog timedialog (ksw->data()->lt(), ksw);
199 if ( timedialog.exec() == QDialog::Accepted )
201 KStarsDateTime dt( timedialog.selectedDate(), timedialog.selectedTime() );
202 ksw->data()->changeDateTime( dt );
204 timeOut->setText( QString().sprintf("%02d:%02d:%02d", dt.time().hour(), dt.time().minute(), dt.time().second()));
205 dateOut->setText( QString().sprintf("%d-%02d-%02d", dt.date().year(), dt.date().month(), dt.date().day()));
209 void telescopeWizardProcess::newLocation()
212 ksw->slotGeoLocator();
214 locationOut->setText( QString("%1, %2, %3").arg(ksw->geo()->translatedName())
215 .arg(ksw->geo()->translatedProvince())
216 .arg(ksw->geo()->translatedCountry()));
217 timeOut->setText( QString().sprintf("%02d:%02d:%02d", ksw->data()->lt().time().hour(), ksw->data()->lt().time().minute(), ksw->data()->lt().time().second()));
219 dateOut->setText( QString().sprintf("%d-%02d-%02d", ksw->data()->lt().date().year(),
220 ksw->data()->lt().date().month() ,ksw->data()->lt().date().day()));
226 int telescopeWizardProcess::establishLink()
229 if (!indidriver || !indimenu)
230 return (0);
232 QListViewItem *driverItem = NULL;
233 driverItem = indidriver->localListView->findItem(telescopeCombo->currentText(), 0);
234 if (driverItem == NULL) return -1;
236 // If device is already running, we need to shut it down first
237 if (indidriver->isDeviceRunning(telescopeCombo->currentText()))
239 indidriver->localListView->setSelected(driverItem, true);
240 indidriver->processDeviceStatus(1);
243 // Set custome label for device
244 indimenu->setCustomLabel(telescopeCombo->currentText());
245 currentDevice = indimenu->currentLabel;
246 // Select it
247 indidriver->localListView->setSelected(driverItem, true);
248 // Make sure we start is locally
249 indidriver->localR->setChecked(true);
250 // Run it
251 indidriver->processDeviceStatus(0);
253 if (!indidriver->isDeviceRunning(telescopeCombo->currentText()))
254 return (3);
256 newDeviceTimer->start(1500);
258 if (portIn->text().isEmpty())
259 return (1);
260 else
261 return (2);
265 void telescopeWizardProcess::processPort()
267 INDI_P * pp;
268 INDI_E * lp;
270 if (!indidriver || !indimenu)
271 return;
273 timeOutCount++;
275 if (timeOutCount >= TIMEOUT_THRESHHOLD)
277 indidriver->processDeviceStatus(1);
278 Reset();
279 KMessageBox::error(0, i18n("Error: connection timeout. Unable to communicate with an INDI server"));
280 close();
281 return;
284 indiDev = indimenu->findDeviceByLabel(currentDevice);
285 if (!indiDev) return;
287 // port empty, start autoscan
288 if (portIn->text().isEmpty())
290 newDeviceTimer->stop();
291 linkRejected = false;
292 connect(indiDev->stdDev, SIGNAL(linkRejected()), this, SLOT(scanPorts()));
293 connect(indiDev->stdDev, SIGNAL(linkAccepted()), this, SLOT(linkSuccess()));
294 scanPorts();
295 return;
298 pp = indiDev->findProp("DEVICE_PORT");
299 if (!pp) return;
300 lp = pp->findElement("PORT");
301 if (!lp) return;
303 lp->write_w->setText(portIn->text());
305 pp = indiDev->findProp("CONNECTION");
306 if (!pp) return;
308 newDeviceTimer->stop();
310 Options::setIndiMessages( INDIMessageBar );
312 pp->newSwitch(0);
314 timeOutCount = 0;
316 indimenu->show();
318 close();
322 void telescopeWizardProcess::scanPorts()
324 INDI_P * pp;
325 INDI_E *lp;
327 if (!indiDev || !indidriver || !indimenu || linkRejected)
328 return;
330 currentPort++;
332 progressScan->progressBar()->setValue(currentPort);
334 if ( (unsigned) currentPort >= portList.count())
336 KMessageBox::sorry(0, i18n("Sorry. KStars failed to detect any attached telescopes, please check your settings and try again."));
337 linkRejected = true;
338 indidriver->processDeviceStatus(1);
339 Reset();
340 return;
343 if (indiDev->msgST_w)
344 indiDev->msgST_w->clear();
346 pp = indiDev->findProp("DEVICE_PORT");
347 if (!pp) return;
348 lp = pp->findElement("PORT");
350 lp->write_w->setText(portList[currentPort]);
351 pp->newText();
353 pp = indiDev->findProp("CONNECTION");
354 if (!pp) return;
356 pp->newSwitch(0);
360 void telescopeWizardProcess::linkSuccess()
362 Reset();
364 indimenu->show();
366 close();
370 void telescopeWizardProcess::Reset()
373 currentPort = -1;
374 timeOutCount = 0;
376 if (progressScan)
377 progressScan->close();
379 indiDev = NULL;
383 #include "telescopewizardprocess.moc"