moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / tools / lcgenerator.cpp
blob39a535f2cffe15dc5d1d3e20018ab4ab26efd5b5
1 /***************************************************************************
2 lcgenerator.cpp - description
3 -------------------
4 begin : Tue Oct 1 18:01:48 CDT 2002
5 copyright : (C) 2002 by Jasem Mutlaq
6 email : mutlaqja@ku.edu
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <qvariant.h>
19 #include <klineedit.h>
20 #include <klistbox.h>
21 #include <kpushbutton.h>
22 #include <qcheckbox.h>
23 #include <qgroupbox.h>
24 #include <qlabel.h>
25 #include <qpushbutton.h>
26 #include <qlayout.h>
27 #include <qtooltip.h>
28 #include <qwhatsthis.h>
29 #include <qfile.h>
31 #include <kio/netaccess.h>
32 #include <kmessagebox.h>
33 #include <kstandarddirs.h>
35 #include "lcgenerator.h"
36 #include "imageviewer.h"
37 #include "kstars.h"
38 #include "kstarsdata.h"
40 #include <kapplication.h>
42 LCGenerator::LCGenerator( QWidget* parent)
43 : KDialogBase( parent, "lcgenerator", false, i18n( "AAVSO Light Curve Generator"),0) , Hostprefix("http://www.aavso.org/cgi-bin/kstar.pl"), JDCutOff(2437600)
46 ksw = (KStars*) parent;
47 createGUI();
49 downloadJob = 0;
51 file = new QFile();
54 LCGenerator::~LCGenerator()
56 delete file;
57 delete downloadJob;
60 void LCGenerator::createGUI()
63 QWidget *page = new QWidget(this);
64 setMainWidget(page);
66 LCGeneratorDialogLayout = new QVBoxLayout( page, 11, 6, "LCGeneratorDialogLayout");
68 SDLayout = new QHBoxLayout( 0, 0, 6, "SDLayout");
70 StarInfoBox = new QGroupBox( page, "StarInfoBox" );
71 StarInfoBox->setColumnLayout(0, Qt::Vertical );
72 StarInfoBox->layout()->setSpacing( 6 );
73 StarInfoBox->layout()->setMargin( 11 );
74 StarInfoBoxLayout = new QVBoxLayout( StarInfoBox->layout() );
75 StarInfoBoxLayout->setAlignment( Qt::AlignTop );
77 DesignHLayout = new QHBoxLayout( 0, 0, 6, "DesignHLayout");
79 desigLabel = new QLabel( StarInfoBox, "desigLabel" );
80 desigLabel->setMinimumSize( QSize( 70, 0 ) );
81 DesignHLayout->addWidget( desigLabel );
83 DesignationIn = new KListBox( StarInfoBox, "DesignationIn" );
84 DesignHLayout->addWidget( DesignationIn );
85 StarInfoBoxLayout->addLayout( DesignHLayout );
87 // Fill stars designations
88 for (uint i=0; i< (ksw->data()->VariableStarsList.count()); i++)
89 DesignationIn->insertItem(ksw->data()->VariableStarsList.at(i)->Designation);
91 NameHLayout = new QHBoxLayout( 0, 0, 6, "NameHLayout");
93 nameLabel = new QLabel( StarInfoBox, "nameLabel" );
94 nameLabel->setMinimumSize( QSize( 70, 0 ) );
95 NameHLayout->addWidget( nameLabel );
97 NameIn = new KListBox( StarInfoBox, "NameIn" );
98 NameHLayout->addWidget( NameIn );
99 StarInfoBoxLayout->addLayout( NameHLayout );
101 // Fill star names
102 for (uint i=0; i<ksw->data()->VariableStarsList.count(); i++)
103 NameIn->insertItem(ksw->data()->VariableStarsList.at(i)->Name);
105 StartHLayout = new QHBoxLayout( 0, 0, 6, "StartHLayout");
107 startLabel = new QLabel( StarInfoBox, "startLabel" );
108 startLabel->setMinimumSize( QSize( 70, 0 ) );
109 StartHLayout->addWidget( startLabel );
111 StartDateIn = new KLineEdit( StarInfoBox, "StartDateIn" );
112 StartHLayout->addWidget( StartDateIn );
113 StarInfoBoxLayout->addLayout( StartHLayout );
115 EndHLayout = new QHBoxLayout( 0, 0, 6, "EndHLayout");
117 endLabel = new QLabel( StarInfoBox, "endLabel" );
118 endLabel->setMinimumSize( QSize( 70, 0 ) );
119 EndHLayout->addWidget( endLabel );
121 EndDateIn = new KLineEdit( StarInfoBox, "EndDateIn" );
122 EndHLayout->addWidget( EndDateIn );
123 StarInfoBoxLayout->addLayout( EndHLayout );
124 SDLayout->addWidget( StarInfoBox );
126 DataSelectBox = new QGroupBox( page, "DataSelectBox" );
127 DataSelectBox->setColumnLayout(0, Qt::Vertical );
128 DataSelectBox->layout()->setSpacing( 6 );
129 DataSelectBox->layout()->setMargin( 11 );
130 DataSelectBoxLayout = new QVBoxLayout( DataSelectBox->layout() );
131 DataSelectBoxLayout->setAlignment( Qt::AlignTop );
133 VisualCheck = new QCheckBox( DataSelectBox, "VisualCheck" );
134 VisualCheck->setChecked( TRUE );
135 DataSelectBoxLayout->addWidget( VisualCheck );
137 FainterCheck = new QCheckBox( DataSelectBox, "FainterCheck" );
138 FainterCheck->setChecked( TRUE );
139 DataSelectBoxLayout->addWidget( FainterCheck );
141 DiscrepantCheck = new QCheckBox( DataSelectBox, "DiscrepantCheck" );
142 DataSelectBoxLayout->addWidget( DiscrepantCheck );
144 CCDBCheck = new QCheckBox( DataSelectBox, "CCDBCheck" );
145 CCDBCheck->setChecked( TRUE );
146 DataSelectBoxLayout->addWidget( CCDBCheck );
148 CCDVCheck = new QCheckBox( DataSelectBox, "CCDVCheck" );
149 CCDVCheck->setChecked( TRUE );
150 DataSelectBoxLayout->addWidget( CCDVCheck );
152 CCDRCheck = new QCheckBox( DataSelectBox, "CCDRCheck" );
153 CCDRCheck->setChecked( TRUE );
154 DataSelectBoxLayout->addWidget( CCDRCheck );
156 CCDICheck = new QCheckBox( DataSelectBox, "CCDICheck" );
157 CCDICheck->setChecked( TRUE );
158 DataSelectBoxLayout->addWidget( CCDICheck );
160 PlotHLayout = new QHBoxLayout( 0, 0, 6, "PlotHLayout");
162 plotLabel = new QLabel( DataSelectBox, "plotLabel" );
163 PlotHLayout->addWidget( plotLabel );
165 AverageDayIn = new KLineEdit( DataSelectBox, "AverageDayIn" );
166 PlotHLayout->addWidget( AverageDayIn );
168 daysLabel = new QLabel( DataSelectBox, "daysLabel" );
169 PlotHLayout->addWidget( daysLabel );
170 DataSelectBoxLayout->addLayout( PlotHLayout );
171 SDLayout->addWidget( DataSelectBox );
172 LCGeneratorDialogLayout->addLayout( SDLayout );
174 ButtonHLayout = new QHBoxLayout( 0, 0, 6, "ButtonHLayout");
176 GetCurveButton = new KPushButton( page, "GetCurveButton" );
177 ButtonHLayout->addWidget( GetCurveButton );
179 UpdateListButton = new KPushButton( page, "UpdateListButton" );
180 ButtonHLayout->addWidget( UpdateListButton );
181 QSpacerItem* spacer = new QSpacerItem( 128, 16, QSizePolicy::Expanding, QSizePolicy::Minimum );
182 ButtonHLayout->addItem( spacer );
184 CloseButton = new KPushButton( page, "closeButton" );
185 ButtonHLayout->addWidget( CloseButton );
186 LCGeneratorDialogLayout->addLayout( ButtonHLayout );
189 StarInfoBox->setTitle( i18n( "Star Info" ) );
190 desigLabel->setText( i18n( "Designation:" ) );
191 nameLabel->setText( i18n( "Or name:" ) );
192 startLabel->setText( i18n( "Start date:" ) );
193 QWhatsThis::add( startLabel, i18n( "Start date for the light curve plot in mm/dd/yy or JD" ) );
194 endLabel->setText( i18n( "End date:" ) );
195 QWhatsThis::add( endLabel, i18n( "End date for the light curve plot in mm/dd/yy or JD" ) );
196 StartDateIn->setText( i18n( "default" ) );
197 EndDateIn->setText( i18n( "default" ) );
198 DataSelectBox->setTitle( i18n( "Data Selection" ) );
199 VisualCheck->setText( i18n( "Visual" ) );
200 FainterCheck->setText( i18n( "Fainter thans" ) );
201 DiscrepantCheck->setText( i18n( "Discrepant data" ) );
202 CCDBCheck->setText( i18n( "CCDB" ) );
203 CCDVCheck->setText( i18n( "CCDV" ) );
204 CCDRCheck->setText( i18n( "CCDR" ) );
205 CCDICheck->setText( i18n( "CCDI" ) );
206 plotLabel->setText( i18n( "Plot average:" ) );
207 daysLabel->setText( i18n( "days" ) );
208 GetCurveButton->setText( i18n( "Retrieve Curve" ) );
209 UpdateListButton->setText( i18n( "Update List" ) );
210 CloseButton->setText( i18n( "Close" ) );
212 resize( QSize(500, 360) );
214 // Signals/Slots
215 QObject::connect(CloseButton, SIGNAL(clicked()), this, SLOT(close()));
216 QObject::connect(GetCurveButton, SIGNAL(clicked()), this, SLOT(VerifyData()));
217 QObject::connect(UpdateListButton, SIGNAL(clicked()), this, SLOT(updateStarList()));
218 QObject::connect(DesignationIn, SIGNAL(highlighted(int)), this, SLOT(updateNameList(int)));
219 QObject::connect(NameIn, SIGNAL(highlighted(int)), this, SLOT(updateDesigList(int)));
224 void LCGenerator::VerifyData()
226 QString InitialStartDate, InitialEndDate;
227 QString FinalDesignation, FinalStartDate, FinalEndDate, AverageDays;
228 bool AverageDaysOK;
230 // Get initial user input
231 if ( StartDateIn->text().isEmpty() ) StartDateIn->setText( i18n( "default" ) );
232 if ( EndDateIn->text().isEmpty() ) EndDateIn->setText( i18n( "default" ) );
233 InitialStartDate = StartDateIn->text().lower();
234 InitialEndDate = EndDateIn->text().lower();
235 AverageDays = AverageDayIn->text();
236 FinalDesignation = DesignationIn->currentText();
238 // set Julian day
239 if (!setJD(InitialStartDate, &FinalStartDate, 0))
240 return;
241 if (!setJD(InitialEndDate, &FinalEndDate, 1))
242 return;
244 if (FinalEndDate.toInt() < FinalStartDate.toInt())
246 KMessageBox::error(this, i18n("End date must occur after start date."));
247 return;
250 // Check that we have an integer for average number of days, if data field empty, then make it 'default'
251 if (!AverageDays.isEmpty())
253 AverageDays.toInt(&AverageDaysOK);
254 if (!AverageDaysOK)
256 KMessageBox::error(this, i18n("Average days must be a positive integer."));
257 return;
259 else
261 if (AverageDays.toInt() < 0)
263 KMessageBox::error(this, i18n("Average days must be a positive integer."));
264 return;
269 else AverageDays = QString("default");
271 //Download the curve!
272 DownloadCurve(FinalStartDate, FinalEndDate, FinalDesignation, AverageDays);
276 bool LCGenerator::setJD(QString Date, QString *JD, int JDType)
278 uint i=0;
279 int TempJD=0;
280 int slashCount =0;
281 int slashRefrence[2];
283 int dateFormat[3];
284 bool isNumber;
286 const QString invalidFormatStartJD(i18n("Invalid date format. Correct format is mm/dd/yyyy or JD, leave 'default' to generate light curves for the past 500 days."));
287 const QString invalidFormatENDJD(i18n("Invalid date format. Correct format is mm/dd/yyyy or JD, leave 'default' to generate light curves until today."));
288 QString invalidFormatMsg(JDType ? invalidFormatENDJD : invalidFormatStartJD);
291 // check for "default" date
292 if (Date == i18n("default"))
294 *JD = "default";
295 return true;
298 // Get slashcount and and slash refrences
299 for (i=0; i<Date.length(); i++)
300 if (Date.at(i) == '/')
302 slashRefrence[slashCount++] = i;
303 if (slashCount > 2)
305 KMessageBox::error(this, invalidFormatMsg);
306 return false;
310 // check if the data appears to be in JD format
311 if (!slashCount)
313 TempJD = Date.toInt(&isNumber, 10);
314 if (!isNumber)
316 KMessageBox::error(this, invalidFormatMsg);
317 return false;
320 if (TempJD >= JDCutOff)
322 JD->setNum(TempJD, 10);
323 return true;
325 else
327 const char* invalidJD = I18N_NOOP("No data available for JD prior to %d");
328 KMessageBox::error(this, QString().sprintf(invalidJD, JDCutOff));
329 return false;
333 // If it's not a Julian day, check for the format of the date
334 // check if year is 4 digits
335 if ( (Date.length() - slashRefrence[1] - 1) != 4)
337 KMessageBox::error(this, invalidFormatMsg);
338 return false;
341 // form mm/dd/yyy fields
342 dateFormat[0] = Date.mid(0, slashRefrence[0]).toInt();
343 dateFormat[1] = Date.mid(slashRefrence[0]+1, slashRefrence[1] - (slashRefrence[0] +1)).toInt();
344 dateFormat[2] = Date.mid(slashRefrence[1]+1, Date.length()).toInt();
346 ExtDate tempdate(dateFormat[2], dateFormat[0], dateFormat[1]);
347 if (!tempdate.isValid())
349 KMessageBox::error(this, invalidFormatMsg);
350 return false;
353 // Convert to JD and verify its lower limit
354 TempJD = tempdate.jd();
356 if (TempJD >= JDCutOff)
358 JD->setNum(TempJD, 10);
359 return true;
361 else
363 const char* invalidJD = I18N_NOOP("No data available for JD prior to %d");
364 KMessageBox::error(this, QString().sprintf(invalidJD, JDCutOff));
365 return false;
370 void LCGenerator::DownloadCurve(QString FinalStartDate, QString FinalEndDate, QString FinalDesignation, QString AverageDay)
373 QString buf(Hostprefix);
374 QString Yes("yes");
375 QString No("no");
377 //FainterCheck;
378 //CCDVCheck;
379 //CCDICheck;
380 //CCDRCheck;
381 //CCDBCheck;
382 //VisualCheck;
383 //DiscrepantCheck;
386 buf.append("?"+FinalStartDate);
387 buf.append("?"+FinalEndDate);
388 buf.append("?"+FinalDesignation);
389 buf.append("?"+AverageDay);
390 buf.append("?"+ (FainterCheck->isOn() ? Yes : No));
391 buf.append("?"+ (CCDVCheck->isOn() ? Yes : No));
392 buf.append("?"+ (CCDICheck->isOn() ? Yes : No));
393 buf.append("?"+ (CCDRCheck->isOn() ? Yes : No));
394 buf.append("?"+ (CCDBCheck->isOn() ? Yes : No));
395 buf.append("?"+ (VisualCheck->isOn() ? Yes : No));
396 buf.append("?"+ (DiscrepantCheck->isOn() ? Yes : No));
399 KURL url(buf);
400 QString message = i18n( "Light Curve produced by the American Amateur Variable Star Observers" );
401 // parent of imageview is KStars
402 new ImageViewer(&url, message, ksw, "lightcurve");
406 void LCGenerator::updateDesigList(int index)
409 DesignationIn->setSelected(index, true);
410 DesignationIn->centerCurrentItem();
414 void LCGenerator::updateNameList(int index)
417 NameIn->setSelected(index, true);
418 NameIn->centerCurrentItem();
422 void LCGenerator::updateStarList()
424 file->setName( locateLocal( "appdata", "valaav.txt" ) );
426 KURL AAVSOFile("http://www.aavso.org/observing/aids/valaav.txt");
427 KURL saveFile (file->name());
429 downloadJob = KIO::file_copy (AAVSOFile, saveFile, -1, true);
430 connect (downloadJob, SIGNAL (result (KIO::Job *)), SLOT (downloadReady (KIO::Job *)));
433 void LCGenerator::downloadReady(KIO::Job * job)
436 downloadJob = 0;
438 if ( job->error() )
440 job->showErrorDialog();
441 closeEvent (0);
442 return; // exit this function
445 file->close(); // to get the newest informations of the file and not any informations from opening of the file
447 if ( file->exists() )
449 ksw->data()->readVARData();
451 DesignationIn->clear();
452 NameIn->clear();
454 // Fill stars designations
455 for (uint i=0; i< (ksw->data()->VariableStarsList.count()); i++)
456 DesignationIn->insertItem(ksw->data()->VariableStarsList.at(i)->Designation);
458 // Fill star names
459 for (uint i=0; i<ksw->data()->VariableStarsList.count(); i++)
460 NameIn->insertItem(ksw->data()->VariableStarsList.at(i)->Name);
462 KMessageBox::information(this, i18n("AAVSO Star list downloaded successfully."));
465 return;
467 closeEvent (0);
471 void LCGenerator::closeEvent (QCloseEvent *ev)
473 if (ev) // not if closeEvent (0) is called, because segfault
474 ev->accept(); // parent-widgets should not get this event, so it will be filtered
475 //this->~LCGenerator(); // destroy the object, so the object can only allocated with operator new, not as a global/local variable
478 #include "lcgenerator.moc"