summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/tools/lcgenerator.cpp
blob: 39a535f2cffe15dc5d1d3e20018ab4ab26efd5b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/***************************************************************************
                          lcgenerator.cpp  -  description
                             -------------------
    begin                : Tue Oct  1 18:01:48 CDT 2002
    copyright            : (C) 2002 by Jasem Mutlaq
    email                : mutlaqja@ku.edu
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qvariant.h>
#include <klineedit.h>
#include <klistbox.h>
#include <kpushbutton.h>
#include <qcheckbox.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qfile.h>

#include <kio/netaccess.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>

#include "lcgenerator.h"
#include "imageviewer.h"
#include "kstars.h"
#include "kstarsdata.h"

#include <kapplication.h>

LCGenerator::LCGenerator( QWidget* parent)
    : KDialogBase( parent, "lcgenerator", false, i18n( "AAVSO Light Curve Generator"),0) , Hostprefix("http://www.aavso.org/cgi-bin/kstar.pl"), JDCutOff(2437600)
{

  ksw = (KStars*) parent;
  createGUI();
  
  downloadJob = 0;
  
  file = new QFile();
}

LCGenerator::~LCGenerator()
{
   delete file;
   delete downloadJob;
}

void LCGenerator::createGUI()
{

    QWidget *page = new QWidget(this);
    setMainWidget(page);
    
    LCGeneratorDialogLayout = new QVBoxLayout( page, 11, 6, "LCGeneratorDialogLayout"); 

    SDLayout = new QHBoxLayout( 0, 0, 6, "SDLayout"); 

    StarInfoBox = new QGroupBox( page, "StarInfoBox" );
    StarInfoBox->setColumnLayout(0, Qt::Vertical );
    StarInfoBox->layout()->setSpacing( 6 );
    StarInfoBox->layout()->setMargin( 11 );
    StarInfoBoxLayout = new QVBoxLayout( StarInfoBox->layout() );
    StarInfoBoxLayout->setAlignment( Qt::AlignTop );

    DesignHLayout = new QHBoxLayout( 0, 0, 6, "DesignHLayout"); 

    desigLabel = new QLabel( StarInfoBox, "desigLabel" );
    desigLabel->setMinimumSize( QSize( 70, 0 ) );
    DesignHLayout->addWidget( desigLabel );

    DesignationIn = new KListBox( StarInfoBox, "DesignationIn" );
    DesignHLayout->addWidget( DesignationIn );
    StarInfoBoxLayout->addLayout( DesignHLayout );
    
    // Fill stars designations
    for (uint i=0; i< (ksw->data()->VariableStarsList.count()); i++)
     DesignationIn->insertItem(ksw->data()->VariableStarsList.at(i)->Designation);

    NameHLayout = new QHBoxLayout( 0, 0, 6, "NameHLayout"); 

    nameLabel = new QLabel( StarInfoBox, "nameLabel" );
    nameLabel->setMinimumSize( QSize( 70, 0 ) );
    NameHLayout->addWidget( nameLabel );

    NameIn = new KListBox( StarInfoBox, "NameIn" );
    NameHLayout->addWidget( NameIn );
    StarInfoBoxLayout->addLayout( NameHLayout );
    
    // Fill star names
    for (uint i=0; i<ksw->data()->VariableStarsList.count(); i++)
     NameIn->insertItem(ksw->data()->VariableStarsList.at(i)->Name);

    StartHLayout = new QHBoxLayout( 0, 0, 6, "StartHLayout"); 

    startLabel = new QLabel( StarInfoBox, "startLabel" );
    startLabel->setMinimumSize( QSize( 70, 0 ) );
    StartHLayout->addWidget( startLabel );

    StartDateIn = new KLineEdit( StarInfoBox, "StartDateIn" );
    StartHLayout->addWidget( StartDateIn );
    StarInfoBoxLayout->addLayout( StartHLayout );

    EndHLayout = new QHBoxLayout( 0, 0, 6, "EndHLayout"); 

    endLabel = new QLabel( StarInfoBox, "endLabel" );
    endLabel->setMinimumSize( QSize( 70, 0 ) );
    EndHLayout->addWidget( endLabel );

    EndDateIn = new KLineEdit( StarInfoBox, "EndDateIn" );
    EndHLayout->addWidget( EndDateIn );
    StarInfoBoxLayout->addLayout( EndHLayout );
    SDLayout->addWidget( StarInfoBox );

    DataSelectBox = new QGroupBox( page, "DataSelectBox" );
    DataSelectBox->setColumnLayout(0, Qt::Vertical );
    DataSelectBox->layout()->setSpacing( 6 );
    DataSelectBox->layout()->setMargin( 11 );
    DataSelectBoxLayout = new QVBoxLayout( DataSelectBox->layout() );
    DataSelectBoxLayout->setAlignment( Qt::AlignTop );

    VisualCheck = new QCheckBox( DataSelectBox, "VisualCheck" );
    VisualCheck->setChecked( TRUE );
    DataSelectBoxLayout->addWidget( VisualCheck );

    FainterCheck = new QCheckBox( DataSelectBox, "FainterCheck" );
    FainterCheck->setChecked( TRUE );
    DataSelectBoxLayout->addWidget( FainterCheck );

    DiscrepantCheck = new QCheckBox( DataSelectBox, "DiscrepantCheck" );
    DataSelectBoxLayout->addWidget( DiscrepantCheck );

    CCDBCheck = new QCheckBox( DataSelectBox, "CCDBCheck" );
    CCDBCheck->setChecked( TRUE );
    DataSelectBoxLayout->addWidget( CCDBCheck );

    CCDVCheck = new QCheckBox( DataSelectBox, "CCDVCheck" );
    CCDVCheck->setChecked( TRUE );
    DataSelectBoxLayout->addWidget( CCDVCheck );

    CCDRCheck = new QCheckBox( DataSelectBox, "CCDRCheck" );
    CCDRCheck->setChecked( TRUE );
    DataSelectBoxLayout->addWidget( CCDRCheck );

    CCDICheck = new QCheckBox( DataSelectBox, "CCDICheck" );
    CCDICheck->setChecked( TRUE );
    DataSelectBoxLayout->addWidget( CCDICheck );

    PlotHLayout = new QHBoxLayout( 0, 0, 6, "PlotHLayout"); 

    plotLabel = new QLabel( DataSelectBox, "plotLabel" );
    PlotHLayout->addWidget( plotLabel );

    AverageDayIn = new KLineEdit( DataSelectBox, "AverageDayIn" );
    PlotHLayout->addWidget( AverageDayIn );

    daysLabel = new QLabel( DataSelectBox, "daysLabel" );
    PlotHLayout->addWidget( daysLabel );
    DataSelectBoxLayout->addLayout( PlotHLayout );
    SDLayout->addWidget( DataSelectBox );
    LCGeneratorDialogLayout->addLayout( SDLayout );

    ButtonHLayout = new QHBoxLayout( 0, 0, 6, "ButtonHLayout"); 

    GetCurveButton = new KPushButton( page, "GetCurveButton" );
    ButtonHLayout->addWidget( GetCurveButton );

    UpdateListButton = new KPushButton( page, "UpdateListButton" );
    ButtonHLayout->addWidget( UpdateListButton );
    QSpacerItem* spacer = new QSpacerItem( 128, 16, QSizePolicy::Expanding, QSizePolicy::Minimum );
    ButtonHLayout->addItem( spacer );

    CloseButton = new KPushButton( page, "closeButton" );
    ButtonHLayout->addWidget( CloseButton );
    LCGeneratorDialogLayout->addLayout( ButtonHLayout );
    
    
    StarInfoBox->setTitle( i18n( "Star Info" ) );
    desigLabel->setText( i18n( "Designation:" ) );
    nameLabel->setText( i18n( "Or name:" ) );
    startLabel->setText( i18n( "Start date:" ) );
    QWhatsThis::add( startLabel, i18n( "Start date for the light curve plot in mm/dd/yy or JD" ) );
    endLabel->setText( i18n( "End date:" ) );
    QWhatsThis::add( endLabel, i18n( "End date for the light curve plot in mm/dd/yy or JD" ) );
    StartDateIn->setText( i18n( "default" ) );
    EndDateIn->setText( i18n( "default" ) );
    DataSelectBox->setTitle( i18n( "Data Selection" ) );
    VisualCheck->setText( i18n( "Visual" ) );
    FainterCheck->setText( i18n( "Fainter thans" ) );
    DiscrepantCheck->setText( i18n( "Discrepant data" ) );
    CCDBCheck->setText( i18n( "CCDB" ) );
    CCDVCheck->setText( i18n( "CCDV" ) );
    CCDRCheck->setText( i18n( "CCDR" ) );
    CCDICheck->setText( i18n( "CCDI" ) );
    plotLabel->setText( i18n( "Plot average:" ) );
    daysLabel->setText( i18n( "days" ) );
    GetCurveButton->setText( i18n( "Retrieve Curve" ) );
    UpdateListButton->setText( i18n( "Update List" ) );
    CloseButton->setText( i18n( "Close" ) );
    
    resize( QSize(500, 360) );
        
    // Signals/Slots
    QObject::connect(CloseButton, SIGNAL(clicked()), this, SLOT(close()));
    QObject::connect(GetCurveButton, SIGNAL(clicked()), this, SLOT(VerifyData()));
    QObject::connect(UpdateListButton, SIGNAL(clicked()), this, SLOT(updateStarList()));
    QObject::connect(DesignationIn, SIGNAL(highlighted(int)), this, SLOT(updateNameList(int)));
    QObject::connect(NameIn, SIGNAL(highlighted(int)), this, SLOT(updateDesigList(int)));
      
    
}

void LCGenerator::VerifyData()
{
    QString InitialStartDate, InitialEndDate;
    QString FinalDesignation, FinalStartDate, FinalEndDate, AverageDays;
    bool AverageDaysOK;

    // Get initial user input
    if ( StartDateIn->text().isEmpty() ) StartDateIn->setText( i18n( "default" ) );
    if ( EndDateIn->text().isEmpty() ) EndDateIn->setText( i18n( "default" ) );
    InitialStartDate     = StartDateIn->text().lower();
    InitialEndDate       = EndDateIn->text().lower();
    AverageDays       =  AverageDayIn->text();
    FinalDesignation = DesignationIn->currentText();

    // set Julian day
    if (!setJD(InitialStartDate, &FinalStartDate, 0))
        return;
    if (!setJD(InitialEndDate, &FinalEndDate, 1))
        return;

    if (FinalEndDate.toInt() < FinalStartDate.toInt())
    {
        KMessageBox::error(this, i18n("End date must occur after start date."));
        return;
    }

   // Check that we have an integer for average number of days, if data field empty, then make it 'default'
   if (!AverageDays.isEmpty())
   {
        AverageDays.toInt(&AverageDaysOK);
        if (!AverageDaysOK)
        {
            KMessageBox::error(this, i18n("Average days must be a positive integer."));
            return;
         }
	 else
	 {
	  if (AverageDays.toInt() < 0)
	  {
	  	KMessageBox::error(this, i18n("Average days must be a positive integer."));
            	return;
	  }
	 }
	  
   }
   else AverageDays = QString("default");

    //Download the curve!
   DownloadCurve(FinalStartDate, FinalEndDate, FinalDesignation, AverageDays);
  
}

bool LCGenerator::setJD(QString Date, QString *JD, int JDType)
{
    uint i=0;
    int TempJD=0;
    int slashCount =0;
    int slashRefrence[2];

    int dateFormat[3];
    bool isNumber;
    
    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."));
    const QString invalidFormatENDJD(i18n("Invalid date format. Correct format is mm/dd/yyyy or JD, leave 'default' to generate light curves until today."));
    QString invalidFormatMsg(JDType ? invalidFormatENDJD : invalidFormatStartJD);
    

    // check for "default" date
    if (Date == i18n("default"))
    {
       *JD = "default";
       return true;
    }

    // Get slashcount and and slash refrences
    for (i=0; i<Date.length(); i++)
      if (Date.at(i) == '/')
      {
          slashRefrence[slashCount++] = i;
          if (slashCount > 2)
          {
             KMessageBox::error(this, invalidFormatMsg);
             return false;
          }
      }

    // check if the data appears to be in JD format
    if (!slashCount)
    {
        TempJD = Date.toInt(&isNumber, 10);
        if (!isNumber)
        {
             KMessageBox::error(this, invalidFormatMsg);
             return false;
        }

         if (TempJD >= JDCutOff)
        {
          JD->setNum(TempJD, 10);
          return true;
        }
        else
        {
             const char* invalidJD = I18N_NOOP("No data available for JD prior to %d");
             KMessageBox::error(this, QString().sprintf(invalidJD, JDCutOff));
             return false;
        }
    }

    // If it's not a Julian day, check for the format of the date
    // check if year is 4 digits
    if ( (Date.length() - slashRefrence[1] - 1) != 4)
     {
          KMessageBox::error(this, invalidFormatMsg);
          return false;
     }

    // form mm/dd/yyy fields
    dateFormat[0] = Date.mid(0, slashRefrence[0]).toInt();
    dateFormat[1] = Date.mid(slashRefrence[0]+1, slashRefrence[1] - (slashRefrence[0] +1)).toInt();
    dateFormat[2] = Date.mid(slashRefrence[1]+1, Date.length()).toInt();

    ExtDate tempdate(dateFormat[2], dateFormat[0], dateFormat[1]);
    if (!tempdate.isValid())
    {
        KMessageBox::error(this, invalidFormatMsg);
        return false;
     }

     // Convert to JD and verify its lower limit
     TempJD = tempdate.jd();

     if (TempJD >= JDCutOff)
        {
          JD->setNum(TempJD, 10);
          return true;
        }
        else
        {
             const char* invalidJD = I18N_NOOP("No data available for JD prior to %d");
             KMessageBox::error(this, QString().sprintf(invalidJD, JDCutOff));
             return false;
        }
  
}

void LCGenerator::DownloadCurve(QString FinalStartDate, QString FinalEndDate, QString FinalDesignation, QString AverageDay)
{

	QString buf(Hostprefix);
	QString Yes("yes");
	QString No("no");
	
	//FainterCheck;
	//CCDVCheck;
	//CCDICheck;
	//CCDRCheck;
	//CCDBCheck;
	//VisualCheck;
	//DiscrepantCheck;
    

	buf.append("?"+FinalStartDate);
	buf.append("?"+FinalEndDate);
	buf.append("?"+FinalDesignation);
	buf.append("?"+AverageDay);
	buf.append("?"+ (FainterCheck->isOn() ? Yes : No));
	buf.append("?"+ (CCDVCheck->isOn() ? Yes : No));
	buf.append("?"+ (CCDICheck->isOn() ? Yes : No));
	buf.append("?"+ (CCDRCheck->isOn() ? Yes : No));
	buf.append("?"+ (CCDBCheck->isOn() ? Yes : No));
	buf.append("?"+ (VisualCheck->isOn() ? Yes : No));
	buf.append("?"+ (DiscrepantCheck->isOn() ? Yes : No));
	

	KURL url(buf);
	QString message = i18n( "Light Curve produced by the American Amateur Variable Star Observers" );
	// parent of imageview is KStars
	new ImageViewer(&url, message, ksw, "lightcurve");
        
}

void LCGenerator::updateDesigList(int index)
{

    DesignationIn->setSelected(index, true);
    DesignationIn->centerCurrentItem();
    
}

void LCGenerator::updateNameList(int index)
{

    NameIn->setSelected(index, true);
    NameIn->centerCurrentItem();
    
}

void LCGenerator::updateStarList()
{
	file->setName( locateLocal( "appdata", "valaav.txt" ) );
	
	KURL AAVSOFile("http://www.aavso.org/observing/aids/valaav.txt");
	KURL saveFile (file->name());
	
	downloadJob = KIO::file_copy (AAVSOFile, saveFile, -1, true);
	connect (downloadJob, SIGNAL (result (KIO::Job *)), SLOT (downloadReady (KIO::Job *)));
}

void LCGenerator::downloadReady(KIO::Job * job)
{

downloadJob = 0;

	if ( job->error() )
	{
		job->showErrorDialog();
		closeEvent (0);
		return;		// exit this function
	}

	file->close(); // to get the newest informations of the file and not any informations from opening of the file

	if ( file->exists() )
	{
		ksw->data()->readVARData();
		
		DesignationIn->clear();
                NameIn->clear();
		
		// Fill stars designations
                for (uint i=0; i< (ksw->data()->VariableStarsList.count()); i++)
                DesignationIn->insertItem(ksw->data()->VariableStarsList.at(i)->Designation);

                // Fill star names
                for (uint i=0; i<ksw->data()->VariableStarsList.count(); i++)
                NameIn->insertItem(ksw->data()->VariableStarsList.at(i)->Name);
		
                KMessageBox::information(this, i18n("AAVSO Star list downloaded successfully."));

		
		return;
	}
	closeEvent (0);

}

void LCGenerator::closeEvent (QCloseEvent *ev)
{
	if (ev)	// not if closeEvent (0) is called, because segfault
		ev->accept();	// parent-widgets should not get this event, so it will be filtered
	//this->~LCGenerator();	// destroy the object, so the object can only allocated with operator new, not as a global/local variable
}

#include "lcgenerator.moc"