summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/tools/wutdialog.cpp
blob: 73d3d8a1997682f02c615cf70f02cf26a37d2f46 (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
/***************************************************************************
                          wutdialog.cpp  -  K Desktop Planetarium
                             -------------------
    begin                : Die Feb 25 2003
    copyright            : (C) 2003 by Thomas Kabelmann
    email                : tk78@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "wutdialog.h"
#include "wutdialogui.h"

#include "kstars.h"
#include "kstarsdata.h"
#include "skymap.h"
#include "ksnumbers.h"
#include "skyobjectname.h"
#include "objectnamelist.h"
#include "simclock.h"
#include "detaildialog.h"
#include "locationdialog.h"
#include "timedialog.h"
#include "kssun.h"
#include "ksmoon.h"

#include <kcombobox.h>
#include <klocale.h>
#include <klistbox.h>
#include <kpushbutton.h>

#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqframe.h>
#include <tqtabbar.h>
#include <tqtimer.h>
#include <tqcursor.h>

WUTDialog::WUTDialog(KStars *ks) :
	KDialogBase (KDialogBase::Plain, i18n("What's up Tonight"), Close, Close, (TQWidget*)ks),
	kstars(ks), EveningFlag(0) {

	TQFrame *page = plainPage();
	setMainWidget(page);
	TQVBoxLayout *vlay = new TQVBoxLayout( page, 0, spacingHint() );
	WUT = new WUTDialogUI( page );
	vlay->addWidget( WUT );

	objectList = &(ks->data()->ObjNames);
//	objectList->setLanguage( Options::useLatinConstellNames() );

	//initialize location and date to current KStars settings:
	geo = kstars->geo();
	
	T0 = kstars->data()->lt();
	//If the Time is earlier than 6:00 am, assume the user wants the night of the previous date
	if ( T0.time().hour() < 6 ) 
		T0 = T0.addDays( -1 );

	//Now, set time T0 to midnight (of the following day)
	T0.setTime( TQTime( 0, 0, 0 ) );
	T0 = T0.addDays( 1 );
	UT0 = geo->LTtoUT( T0 );
	
	//Set the Tomorrow date/time to Noon the following day
	Tomorrow = T0.addSecs( 12*3600 );
	TomorrowUT = geo->LTtoUT( Tomorrow );
	
	//Set the Evening date/time to 6:00 pm 
	Evening = T0.addSecs( -6*3600 );
	EveningUT = geo->LTtoUT( Evening );
	
	TQString sGeo = geo->translatedName();
	if ( ! geo->translatedProvince().isEmpty() ) sGeo += ", " + geo->translatedProvince();
	sGeo += ", " + geo->translatedCountry();
	WUT->LocationLabel->setText( i18n( "at %1" ).tqarg( sGeo ) );
	WUT->DateLabel->setText( i18n( "The night of %1" ).tqarg( Evening.date().toString( Qt::LocalDate ) ) );

	initCategories();

	makeConnections();

	TQTimer::singleShot(0, this, TQT_SLOT(init()));
}

WUTDialog::~WUTDialog(){
}

void WUTDialog::makeConnections() {
	connect( WUT->DateButton, TQT_SIGNAL( clicked() ), TQT_SLOT( slotChangeDate() ) );
	connect( WUT->LocationButton, TQT_SIGNAL( clicked() ), TQT_SLOT( slotChangeLocation() ) );
	connect( WUT->CenterButton, TQT_SIGNAL( clicked() ), TQT_SLOT( slotCenter() ) );
	connect( WUT->DetailButton, TQT_SIGNAL( clicked() ), TQT_SLOT( slotDetails() ) );
	connect( WUT->CategoryListBox, TQT_SIGNAL( highlighted(int) ), TQT_SLOT( slotLoadList(int) ) );
	connect( WUT->ObjectListBox, TQT_SIGNAL( selectionChanged(TQListBoxItem*) ),
			TQT_SLOT( slotDisplayObject(TQListBoxItem*) ) );
	connect( WUT->EveningMorningBox, TQT_SIGNAL( activated(int) ), TQT_SLOT( slotEveningMorning(int) ) );
}

void WUTDialog::initCategories() {
	WUT->CategoryListBox->insertItem( i18n( "Planets" ) );
	WUT->CategoryListBox->insertItem( i18n( "Comets" ) );
	WUT->CategoryListBox->insertItem( i18n( "Asteroids" ) );
	WUT->CategoryListBox->insertItem( i18n( "Stars" ) );
	WUT->CategoryListBox->insertItem( i18n( "Constellations" ) );
	WUT->CategoryListBox->insertItem( i18n( "Star Clusters" ) );
	WUT->CategoryListBox->insertItem( i18n( "Nebulae" ) );
	WUT->CategoryListBox->insertItem( i18n( "Galaxies" ) );
	WUT->CategoryListBox->setSelected(0, true);
}

void WUTDialog::init() {
	TQString sRise, sSet, sDuration;

	// reset all lists
	for (int i=0; i<NCATEGORY; i++) {
		lists.visibleList[i].clear();
		lists.initialized[i] = false;
	}

	// sun almanac information
	KSSun *oSun = (KSSun*) kstars->data()->PCat->planetSun();
	sunRiseTomorrow = oSun->riseSetTime( TomorrowUT, geo, true );
	sunSetToday = oSun->riseSetTime( EveningUT, geo, false );
	sunRiseToday = oSun->riseSetTime( EveningUT, geo, true );

	//check to see if Sun is circumpolar
	KSNumbers *num = new KSNumbers( UT0.djd() );
	KSNumbers *oldNum = new KSNumbers( kstars->data()->ut().djd() );
	dms LST = geo->GSTtoLST( T0.gst() );
	
	oSun->updateCoords( num, true, geo->lat(), &LST );
	if ( oSun->checkCircumpolar( geo->lat() ) ) {
		if ( oSun->alt()->Degrees() > 0.0 ) {
			sRise = i18n( "circumpolar" );
			sSet = i18n( "circumpolar" );
			sDuration = "00:00";
		} else {
			sRise = i18n( "does not rise" );
			sSet = i18n( "does not rise" );
			sDuration = "24:00";
		}
	} else {
		//Round times to the nearest minute by adding 30 seconds to the time
		sRise = sunRiseTomorrow.addSecs(30).toString("hh:mm");
		sSet = sunSetToday.addSecs(30).toString("hh:mm");

		float Dur = 24.0 + (float)sunRiseTomorrow.hour() 
				+ (float)sunRiseTomorrow.minute()/60.0 
				+ (float)sunRiseTomorrow.second()/3600.0 
				- (float)sunSetToday.hour() 
				- (float)sunSetToday.minute()/60.0
				- (float)sunSetToday.second()/3600.0;
		int hDur = int(Dur);
		int mDur = int(60.0*(Dur - (float)hDur));
		sDuration = TQString().sprintf( "%02d:%02d", hDur, mDur );
	}

	WUT->SunSetLabel->setText( i18n( "Sunset: %1" ).tqarg(sSet) );
	WUT->SunRiseLabel->setText( i18n( "Sunrise: %1" ).tqarg(sRise) );
	WUT->NightDurationLabel->setText( i18n( "Night duration: %1 hours" ).tqarg( sDuration ) );

	// moon almanac information
	KSMoon *oMoon = (KSMoon*) kstars->data()->Moon;
	moonRise = oMoon->riseSetTime( UT0, geo, true );
	moonSet = oMoon->riseSetTime( UT0, geo, false );

	//check to see if Moon is circumpolar
	oMoon->updateCoords( num, true, geo->lat(), &LST );
	if ( oMoon->checkCircumpolar( geo->lat() ) ) {
		if ( oMoon->alt()->Degrees() > 0.0 ) {
			sRise = i18n( "circumpolar" );
			sSet = i18n( "circumpolar" );
		} else {
			sRise = i18n( "does not rise" );
			sSet = i18n( "does not rise" );
		}
	} else {
		//Round times to the nearest minute by adding 30 seconds to the time
		sRise = moonRise.addSecs(30).toString("hh:mm");
		sSet = moonSet.addSecs(30).toString("hh:mm");
	}

	WUT->MoonRiseLabel->setText( i18n( "Moon rises at: %1" ).tqarg( sRise ) );
	WUT->MoonSetLabel->setText( i18n( "Moon sets at: %1" ).tqarg( sSet ) );
	oMoon->findPhase( oSun ); 
	WUT->MoonIllumLabel->setText( oMoon->phaseName() + TQString( " (%1%)" ).tqarg(
			int(100.0*oMoon->illum() ) ) );

	//Restore Sun's and Moon's coordinates, and recompute Moon's original Phase
	oMoon->updateCoords( oldNum, true, geo->lat(), kstars->LST() );
	oSun->updateCoords( oldNum, true, geo->lat(), kstars->LST() );
	oMoon->findPhase( oSun ); 
	
	splitObjectList();
	// load first list
	slotLoadList(0);

	delete num;
	delete oldNum;
}

void WUTDialog::splitObjectList() {
	// don't append objects which are never visible
	for (SkyObjectName *oname=objectList->first(); oname; oname=objectList->next()) {
		bool visible = true;
		SkyObject *o = oname->skyObject();
		// is object circumpolar or never visible
		if (o->checkCircumpolar(geo->lat()) == true) {
			// never visible
			if (o->alt()->Degrees() <= 0) visible = false;
		}
		if (visible == true) appendToList(oname);
	}
}

void WUTDialog::appendToList(SkyObjectName *o) {
	// split into several lists
	switch (o->skyObject()->type()) {
		//case SkyObject::CATALOG_STAR			: //Omitting CATALOG_STARs from list
		case SkyObject::PLANET            : lists.visibleList[0].append(o); break;
		case SkyObject::COMET             : lists.visibleList[1].append(o); break;
		case SkyObject::ASTEROID          : lists.visibleList[2].append(o); break;
		case SkyObject::STAR              : lists.visibleList[3].append(o); break;
		case SkyObject::CONSTELLATION     : lists.visibleList[4].append(o); break;
		case SkyObject::OPEN_CLUSTER      :
		case SkyObject::GLOBULAR_CLUSTER  : lists.visibleList[5].append(o); break;
		case SkyObject::GASEOUS_NEBULA    :
		case SkyObject::PLANETARY_NEBULA  :
		case SkyObject::SUPERNOVA_REMNANT : lists.visibleList[6].append(o); break;
		case SkyObject::GALAXY            : lists.visibleList[7].append(o); break;
	}
}

void WUTDialog::slotLoadList(int i) {
	WUT->ObjectListBox->clear();
	setCursor(TQCursor(TQt::WaitCursor));
	TQPtrList <SkyObjectName> invisibleList;
	for (SkyObjectName *oname=lists.visibleList[i].first(); oname; oname=lists.visibleList[i].next()) {
		bool visible = true;
		if (lists.initialized[i] == false) {
			if (i == 0) {  //planets, sun and moon
				if (oname->skyObject()->name() == "Sun" ) visible = false;  // don't ever display the sun
				else visible = checkVisibility(oname);
			}
			if (visible == false) {
				// collect all invisible objects
				invisibleList.append(oname);
			}
		}
		// append to listbox
		if (visible == true) new SkyObjectNameListItem(WUT->ObjectListBox, oname);
	}
	// remove all invisible objects from list
	if (invisibleList.isEmpty() == false) {
		for (SkyObjectName *o=invisibleList.first(); o; o=invisibleList.next()) {
			lists.visibleList[i].removeRef(o);
		}
	}
	setCursor(TQCursor(TQt::ArrowCursor));
	lists.initialized[i] = true;

	// highlight first item
	if ( WUT->ObjectListBox->count() ) {
		WUT->ObjectListBox->setSelected(0, true);
		WUT->ObjectListBox->setFocus();
	}
}

bool WUTDialog::checkVisibility(SkyObjectName *oname) {
	bool visible( false );
	double minAlt = 6.0; //An object is considered 'visible' if it is above horizon during civil twilight.

	//Initial values for T1, T2 assume all night option of EveningMorningBox
	KStarsDateTime T1 = Evening;
	T1.setTime( sunSetToday );
	KStarsDateTime T2 = Tomorrow;
	T2.setTime( sunRiseTomorrow );

	//Check Evening/Morning only state:
	if ( EveningFlag==0 ) { //Evening only
		T2 = T0; //midnight
	} else if ( EveningFlag==1 ) { //Morning only
		T1 = T0; //midnight
	}

	for ( KStarsDateTime test = T1; test < T2; test = test.addSecs(3600) ) {
		//Need LST of the test time, expressed as a dms object.
		KStarsDateTime ut = geo->LTtoUT( test );
		dms LST = geo->GSTtoLST( ut.gst() );
		SkyPoint sp = oname->skyObject()->recomputeCoords( ut, geo );
		
		//check altitude of object at this time.
		sp.EquatorialToHorizontal( &LST, geo->lat() );

		if ( sp.alt()->Degrees() > minAlt ) {
			visible = true;
			break;
		}
	}

	return visible;
}

void WUTDialog::slotDisplayObject(TQListBoxItem *item) {
	TQTime tRise, tSet, tTransit;
	TQString sRise, sTransit, sSet;

	if ( item==0 ) { //no object selected
		WUT->ObjectBox->setTitle( i18n( "No Object Selected" ) );

		sRise = "--:--";
		sTransit = "--:--";
		sSet = "--:--";
		WUT->DetailButton->setEnabled( false );
	} else {
		SkyObject *o = ((SkyObjectNameListItem*)item)->objName()->skyObject();
		WUT->ObjectBox->setTitle( o->name() );

		if ( o->checkCircumpolar( geo->lat() ) ) {
			if ( o->alt()->Degrees() > 0.0 ) {
				sRise = i18n( "circumpolar" );
				sSet = i18n( "circumpolar" );
			} else {
				sRise = i18n( "does not rise" );
				sSet = i18n( "does not rise" );
			}
		} else {
			tRise = o->riseSetTime( T0, geo, true );
			tSet = o->riseSetTime( T0, geo, false );
//			if ( tSet < tRise ) 
//				tSet = o->riseSetTime( JDTomorrow, geo, false );
			
			sRise = TQString().sprintf( "%02d:%02d", tRise.hour(), tRise.minute() );
			sSet = TQString().sprintf( "%02d:%02d", tSet.hour(), tSet.minute() );
		}

		tTransit = o->transitTime( T0, geo );
//		if ( tTransit < tRise ) 
//			tTransit = o->transitTime( JDTomorrow, geo );
		
		sTransit = TQString().sprintf( "%02d:%02d", tTransit.hour(), tTransit.minute() );

		WUT->DetailButton->setEnabled( true );
	}

	WUT->ObjectRiseLabel->setText( i18n( "Rises at: %1" ).tqarg( sRise ) );
	WUT->ObjectTransitLabel->setText( i18n( "Transits at: %1" ).tqarg( sTransit ) );
	WUT->ObjectSetLabel->setText( i18n( "Sets at: %1" ).tqarg( sSet ) );
}

void WUTDialog::slotCenter() {
	SkyObject *o = 0;
	// get selected item
	if (WUT->ObjectListBox->selectedItem() != 0) {
		o = ((SkyObjectNameListItem*)WUT->ObjectListBox->selectedItem())->objName()->skyObject();
	}
	if (o != 0) {
		kstars->map()->setFocusPoint( o );
		kstars->map()->setFocusObject( o );
		kstars->map()->setDestination( kstars->map()->focusPoint() );
	}
}

void WUTDialog::slotDetails() {
	SkyObject *o = 0;
	// get selected item
	if (WUT->ObjectListBox->selectedItem() != 0) {
		o = ((SkyObjectNameListItem*)WUT->ObjectListBox->selectedItem())->objName()->skyObject();
	}
	if (o != 0) {
		DetailDialog detail(o, kstars->data()->LTime, geo, kstars);
		detail.exec();
	}
}

void WUTDialog::slotChangeDate() {
	TimeDialog td( T0, this );
	if ( td.exec() == TQDialog::Accepted ) {
		T0 = td.selectedDateTime();
		//If the Time is earlier than 6:00 am, assume the user wants the night of the previous date
		if ( T0.time().hour() < 6 ) 
			T0 = T0.addDays( -1 );
		
		//Now, set time T0 to midnight (of the following day)
		T0.setTime( TQTime( 0, 0, 0 ) );
		T0 = T0.addDays( 1 );
		UT0 = geo->LTtoUT( T0 );
		
		//Set the Tomorrow date/time to Noon the following day
		Tomorrow = T0.addSecs( 12*3600 );
		TomorrowUT = geo->LTtoUT( Tomorrow );
		
		//Set the Evening date/time to 6:00 pm 
		Evening = T0.addSecs( -6*3600 );
		EveningUT = geo->LTtoUT( Evening );
		
		WUT->DateLabel->setText( i18n( "The night of %1" ).tqarg( Evening.date().toString() ) );

		int i = WUT->CategoryListBox->currentItem();
		init();
		slotLoadList( i );
	}
}

void WUTDialog::slotChangeLocation() {
	LocationDialog ld( kstars );
	if ( ld.exec() == TQDialog::Accepted ) {
		GeoLocation *newGeo = ld.selectedCity();
		if ( newGeo ) {
			geo = newGeo;
			UT0 = geo->LTtoUT( T0 );
			TomorrowUT = geo->LTtoUT( Tomorrow );
			EveningUT = geo->LTtoUT( Evening );
			
			WUT->LocationLabel->setText( i18n( "at %1" ).tqarg( geo->fullName() ) );
			
			int i = WUT->CategoryListBox->currentItem();
			init();
			slotLoadList( i );
		}
	}
}

void WUTDialog::slotEveningMorning( int index ) {
	if ( EveningFlag != index ) {
		EveningFlag = index;
//		splitObjectList();
//		slotLoadList( WUT->CategoryListBox->currentItem() );
		int i = WUT->CategoryListBox->currentItem();
		init();
		slotLoadList( i );
	}
}

#include "wutdialog.moc"