summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/skymap.cpp
blob: caf645a62fd81ee6ecc174f3161a8fe5e79dbb28 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
/***************************************************************************
                          skymap.cpp  -  Trinity Desktop Planetarium
                             -------------------
    begin                : Sat Feb 10 2001
    copyright            : (C) 2001 by Jason Harris
    email                : jharris@30doradus.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <kapplication.h>
#include <kconfig.h>
#include <kiconloader.h>
#include <kstatusbar.h>
#include <kmessagebox.h>
#include <kaction.h>
#include <kstandarddirs.h>

#include <tqmemarray.h>
#include <tqpointarray.h>
#include <tqcursor.h>
#include <tqbitmap.h>
#include <tqpainter.h>

#include <math.h>
#include <stdlib.h>
#include <unistd.h>

#include "skymap.h"
#include "Options.h"
#include "kstars.h"
#include "kstarsdata.h"
#include "imageviewer.h"
#include "infoboxes.h"
#include "detaildialog.h"
#include "addlinkdialog.h"
#include "kspopupmenu.h"
#include "simclock.h"
#include "skyobject.h"
#include "deepskyobject.h"
#include "ksmoon.h"
#include "ksasteroid.h"
#include "kscomet.h"
#include "starobject.h"
#include "customcatalog.h"

SkyMap::SkyMap(KStarsData *d, TQWidget *parent, const char *name )
	: TQWidget (parent,name), computeSkymap(true), angularDistanceMode(false),
		ksw(0), data(d), pmenu(0), sky(0), sky2(0), IBoxes(0), 
		ClickedObject(0), FocusObject(0), TransientObject(0),
		starpix(0), pts(0), sp(0)
{
	if ( parent ) ksw = (KStars*) parent->parent();
	else ksw = 0;
	
	pts = new TQPointArray( 2000 );  // points for milkyway and horizon
	sp = new SkyPoint();            // needed by coordinate grid

	ZoomRect = TQRect();

	setDefaultMouseCursor();	// set the cross cursor

	// load the pixmaps of stars
	starpix = new StarPixmap( data->colorScheme()->starColorMode(), data->colorScheme()->starColorIntensity() );

	setBackgroundColor( TQColor( data->colorScheme()->colorNamed( "SkyColor" ) ) );
	setBackgroundMode( TQWidget::NoBackground );
	setFocusPolicy( TQ_StrongFocus );
	setMinimumSize( 380, 250 );
	setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ) );

	setMouseTracking (true); //Generate MouseMove events!
	midMouseButtonDown = false;
	mouseButtonDown = false;
	slewing = false;
	clockSlewing = false;

	ClickedObject = NULL;
	FocusObject = NULL;

	sky = new TQPixmap();
	sky2 = new TQPixmap();
	pmenu = new KSPopupMenu( ksw );
	
	//Initialize Transient label stuff
	TransientTimeout = 100; //fade label color every 0.2 sec
	connect( &HoverTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( slotTransientLabel() ) );
	connect( &TransientTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( slotTransientTimeout() ) );
	
	IBoxes = new InfoBoxes( Options::windowWidth(), Options::windowHeight(),
			Options::positionTimeBox(), Options::shadeTimeBox(),
			Options::positionGeoBox(), Options::shadeGeoBox(),
			Options::positionFocusBox(), Options::shadeFocusBox(),
			data->colorScheme()->colorNamed( "BoxTextColor" ),
			data->colorScheme()->colorNamed( "BoxGrabColor" ),
			data->colorScheme()->colorNamed( "BoxBGColor" ) );

	IBoxes->showTimeBox( Options::showTimeBox() );
	IBoxes->showFocusBox( Options::showFocusBox() );
	IBoxes->showGeoBox( Options::showGeoBox() );
	IBoxes->timeBox()->setAnchorFlag( Options::stickyTimeBox() );
	IBoxes->geoBox()->setAnchorFlag( Options::stickyGeoBox() );
	IBoxes->focusBox()->setAnchorFlag( Options::stickyFocusBox() );

	IBoxes->geoChanged( data->geo() );

	connect( IBoxes->timeBox(),  TQT_SIGNAL( shaded(bool) ), data, TQT_SLOT( saveTimeBoxShaded(bool) ) );
	connect( IBoxes->geoBox(),   TQT_SIGNAL( shaded(bool) ), data, TQT_SLOT( saveGeoBoxShaded(bool) ) );
	connect( IBoxes->focusBox(), TQT_SIGNAL( shaded(bool) ), data, TQT_SLOT( saveFocusBoxShaded(bool) ) );
	connect( IBoxes->timeBox(),  TQT_SIGNAL( moved(TQPoint) ), data, TQT_SLOT( saveTimeBoxPos(TQPoint) ) );
	connect( IBoxes->geoBox(),   TQT_SIGNAL( moved(TQPoint) ), data, TQT_SLOT( saveGeoBoxPos(TQPoint) ) );
	connect( IBoxes->focusBox(), TQT_SIGNAL( moved(TQPoint) ), data, TQT_SLOT( saveFocusBoxPos(TQPoint) ) );

	connect( this, TQT_SIGNAL( destinationChanged() ), this, TQT_SLOT( slewFocus() ) );

	//Initialize Refraction correction lookup table arrays.  RefractCorr1 is for calculating
	//the apparent altitude from the true altitude, and RefractCorr2 is for the reverse.
	for ( unsigned int index = 0; index <184; ++index ) {
		double alt = -1.75 + index*0.5;  //start at -1.75 degrees to get midpoint value for each interval.

		RefractCorr1[index] = 1.02 / tan( dms::PI*( alt + 10.3/(alt + 5.11) )/180.0 ) / 60.0; //correction in degrees.
		RefractCorr2[index] = -1.0 / tan( dms::PI*( alt + 7.31/(alt + 4.4) )/180.0 ) / 60.0;
	}
}

SkyMap::~SkyMap() {
	delete starpix;
	delete pts;
	delete sp;
	delete sky;
	delete sky2;
	delete pmenu;
	delete IBoxes;

//Deprecated...DeepSkyObject dtor now handles this itself.
/*//delete any remaining object Image pointers
	for ( DeepSkyObject *obj = data->deepSkyListMessier.first(); obj; obj = data->deepSkyListMessier.next() ) {
		if ( obj->image() ) obj->deleteImage();
  }
	for ( DeepSkyObject *obj = data->deepSkyListNGC.first(); obj; obj = data->deepSkyListNGC.next() ) {
		if ( obj->image() ) obj->deleteImage();
  }
	for ( DeepSkyObject *obj = data->deepSkyListIC.first(); obj; obj = data->deepSkyListIC.next() ) {
		if ( obj->image() ) obj->deleteImage();
  }
	for ( DeepSkyObject *obj = data->deepSkyListOther.first(); obj; obj = data->deepSkyListOther.next() ) {
		if ( obj->image() ) obj->deleteImage();
  }*/
}

void SkyMap::setGeometry( int x, int y, int w, int h ) {
	TQWidget::setGeometry( x, y, w, h );
	sky->resize( w, h );
	sky2->resize( w, h );
}

void SkyMap::setGeometry( const TQRect &r ) {
	TQWidget::setGeometry( r );
	sky->resize( r.width(), r.height() );
	sky2->resize( r.width(), r.height() );
}


void SkyMap::showFocusCoords( bool coordsOnly ) {
	if ( ! coordsOnly ) {
		//display object info in infoBoxes
		TQString oname;
		oname = i18n( "nothing" );
		if ( focusObject() != NULL && Options::isTracking() ) 
			oname = focusObject()->translatedLongName();
		
		infoBoxes()->focusObjChanged(oname);
	}

	if ( Options::useAltAz() && Options::useRefraction() ) {
		SkyPoint corrFocus( *(focus()) );
		corrFocus.setAlt( refract( focus()->alt(), false ) );
		corrFocus.HorizontalToEquatorial( data->LST, data->geo()->lat() );
		corrFocus.setAlt( refract( focus()->alt(), true ) );
		infoBoxes()->focusCoordChanged( &corrFocus );
	} else {
		infoBoxes()->focusCoordChanged( focus() );
	}
}

SkyObject* SkyMap::objectNearest( SkyPoint *p ) {
	double r0 = 200.0/Options::zoomFactor();  //the maximum search radius
	double rmin = r0;

	//Search stars database for nearby object.
	double rstar_min = r0;
	double starmag_min = 20.0;      //absurd initial value
	int istar_min = -1;

	if ( Options::showStars() ) { //Can only click on a star if it's being drawn!

		//test RA and dec to see if this star is roughly nearby

		for ( register unsigned int i=0; i<data->starList.count(); ++i ) {
			SkyObject *test = (SkyObject *)data->starList.at(i);

			double dRA = test->ra()->Hours() - p->ra()->Hours();
			double dDec = test->dec()->Degrees() - p->dec()->Degrees();
			//determine angular distance between this object and mouse cursor
			double f = 15.0*cos( test->dec()->radians() );
			double r = f*f*dRA*dRA + dDec*dDec; //no need to take sqrt, we just want to ID smallest value.
			if (r < r0 && test->mag() < starmag_min ) {
				istar_min = i;
				rstar_min = r;
				starmag_min = test->mag();
			}
		}
	}

	//Next, find the nearest solar system body within r0
	double r = 0.0;
	double rsolar_min = r0;
	SkyObject *solarminobj = NULL;

	if ( Options::showPlanets() )
		solarminobj = data->PCat->findClosest( p, r );

	if ( r < r0 ) {
		rsolar_min = r;
	} else {
		solarminobj = NULL;
	}

	//Moon
	if ( Options::showMoon() ) {
		double dRA = data->Moon->ra()->Hours() - p->ra()->Hours();
		double dDec = data->Moon->dec()->Degrees() - p->dec()->Degrees();
		double f = 15.0*cos( data->Moon->dec()->radians() );
		r = f*f*dRA*dRA + dDec*dDec; //no need to take sqrt, we just want to ID smallest value.
		if (r < rsolar_min) {
			solarminobj= data->Moon;
			rsolar_min = r;
		}
	}

	//Asteroids
	if ( Options::showAsteroids() ) {
		for ( KSAsteroid *ast = data->asteroidList.first(); ast; ast = data->asteroidList.next() ) {
			//test RA and dec to see if this object is roughly nearby
			double dRA = ast->ra()->Hours() - p->ra()->Hours();
			double dDec = ast->dec()->Degrees() - p->dec()->Degrees();
			double f = 15.0*cos( ast->dec()->radians() );
			double r = f*f*dRA*dRA + dDec*dDec; //no need to take sqrt, we just want to ID smallest value.
			if ( r < rsolar_min && ast->mag() < Options::magLimitAsteroid() ) {
				solarminobj = ast;
				rsolar_min = r;
			}
		}
	}

	//Comets
	if ( Options::showComets() ) {
		for ( KSComet *com = data->cometList.first(); com; com = data->cometList.next() ) {
			//test RA and dec to see if this object is roughly nearby
			double dRA = com->ra()->Hours() - p->ra()->Hours();
			double dDec = com->dec()->Degrees() - p->dec()->Degrees();
			double f = 15.0*cos( com->dec()->radians() );
			double r = f*f*dRA*dRA + dDec*dDec; //no need to take sqrt, we just want to ID smallest value.
			if ( r < rsolar_min ) {
				solarminobj = com;
				rsolar_min = r;
			}
		}
	}

	//Next, search for nearest deep-sky object within r0
	double rmess_min = r0;
	double rngc_min = r0;
	double ric_min = r0;
	double rother_min = r0;
	int imess_min = -1;
	int ingc_min = -1;
	int iic_min = -1;
	int iother_min = -1;

	for ( DeepSkyObject *o = data->deepSkyList.first(); o; o = data->deepSkyList.next() ) {
		bool checkObject = false;
		if ( o->isCatalogM() &&
				( Options::showMessier() || Options::showMessierImages() ) ) checkObject = true;
		if ( o->isCatalogNGC() && Options::showNGC() ) checkObject = true;
		if ( o->isCatalogIC() && Options::showIC() ) checkObject = true;
		if ( o->catalog().isEmpty() && Options::showOther() ) checkObject = true;

		if ( checkObject ) {
			//test RA and dec to see if this object is roughly nearby
			double dRA = o->ra()->Hours() - p->ra()->Hours();
			double dDec = o->dec()->Degrees() - p->dec()->Degrees();
			double f = 15.0*cos( o->dec()->radians() );
			double r = f*f*dRA*dRA + dDec*dDec; //no need to take sqrt, we just want to ID smallest value.
			if ( o->isCatalogM() && r < rmess_min) {
				imess_min = data->deepSkyList.at();
				rmess_min = r;
			}
			if ( o->isCatalogNGC() && r < rngc_min) {
				ingc_min = data->deepSkyList.at();
				rngc_min = r;
			}
			if ( o->isCatalogIC() && r < ric_min) {
				iic_min = data->deepSkyList.at();
				ric_min = r;
			}
			if ( o->catalog().isEmpty() && r < rother_min) {
				iother_min = data->deepSkyList.at();
				rother_min = r;
			}
		}
	}

	//Next, search for nearest object within r0 among the custom catalogs
	double rcust_min = r0;
	int icust_min = -1;
	int icust_cat = -1;

	for ( register unsigned int j=0; j< data->CustomCatalogs.count(); ++j ) {
		if ( Options::showCatalog()[j] ) {
			TQPtrList<SkyObject> catList = data->CustomCatalogs.at(j)->objList();

			for ( register unsigned int i=0; i<catList.count(); ++i ) {
				//test RA and dec to see if this object is roughly nearby
				SkyObject *test = (SkyObject *)catList.at(i);
				double dRA = test->ra()->Hours()-p->ra()->Hours();
				double dDec = test->dec()->Degrees()-p->dec()->Degrees();
				double f = 15.0*cos( test->dec()->radians() );
				double r = f*f*dRA*dRA + dDec*dDec; //no need to take sqrt, we just want to ID smallest value.
				if (r < rcust_min) {
					icust_cat = j;
					icust_min = i;
					rcust_min = r;
				}
			}
		}
	}

	int jmin(-1);
	int icat(-1);

	//Among the objects selected within r0, prioritize the selection by catalog:
	//Planets, Messier, NGC, IC, stars
	if ( istar_min >= 0 && rstar_min < r0 ) {
		rmin = rstar_min;
		icat = 0; //set catalog to star
	}

	//IC object overrides star, unless star is twice as close as IC object
	if ( iic_min >= 0 && ric_min < r0 && rmin > 0.5*ric_min ) {
		rmin = ric_min;
		icat = 1; //set catalog to Deep Sky
		jmin = iic_min;
	}

	//NGC object overrides previous selection, unless previous is twice as close
	if ( ingc_min >= 0 && rngc_min < r0 && rmin > 0.5*rngc_min ) {
		rmin = rngc_min;
		icat = 1; //set catalog to Deep Sky
		jmin = ingc_min;
	}

	//"other" object overrides previous selection, unless previous is twice as close
	if ( iother_min >= 0 && rother_min < r0 && rmin > 0.5*rother_min ) {
		rmin = rother_min;
		icat = 1; //set catalog to Deep Sky
		jmin = iother_min;
	}

	//Messier object overrides previous selection, unless previous is twice as close
	if ( imess_min >= 0 && rmess_min < r0 && rmin > 0.5*rmess_min ) {
		rmin = rmess_min;
		icat = 1; //set catalog to Deep Sky
		jmin = imess_min;
	}

	//Custom object overrides previous selection, unless previous is twice as close
	if ( icust_min >= 0 && rcust_min < r0 && rmin > 0.5*rcust_min ) {
		rmin = rcust_min;
		icat = 2; //set catalog to Custom
	}

	//Solar system body overrides previous selection, unless previous selection is twice as close
	if ( solarminobj != NULL && rmin > 0.5*rsolar_min ) {
		rmin = rsolar_min;
		icat = 3; //set catalog to solar system
	}

	TQPtrList<SkyObject> cat;

	switch (icat) {
		case 0: //star
			return data->starList.at(istar_min);
			break;

		case 1: //Deep-Sky Objects
			return data->deepSkyList.at(jmin);
			break;

		case 2: //Custom Catalog Object
			cat = data->CustomCatalogs.at(icust_cat)->objList();
			return cat.at(icust_min);
			break;

		case 3: //solar system object
			return solarminobj;
			break;

		default: //no object found
			return NULL;
			break;
	}
}

void SkyMap::slotTransientLabel( void ) {
	//This function is only called if the HoverTimer manages to timeout.
	//(HoverTimer is restarted with every mouseMoveEvent; so if it times 
	//out, that means there was no mouse movement for HOVER_INTERVAL msec.)
	//Identify the object nearest to the mouse cursor as the
	//TransientObject.  The TransientObject is automatically labeled 
	//in SkyMap::paintEvent().
	//Note that when the TransientObject pointer is not NULL, the next 
	//mouseMoveEvent calls fadeTransientLabel(), which will fade out the 
	//TransientLabel and then set TransientObject to NULL.
	//
	//Do not show a transient label if the map is in motion, or if the mouse 
	//pointer is below the opaque horizon, or if the object has a permanent label
	if ( ! slewing && ! ( Options::useAltAz() && Options::showGround() && 
			mousePoint()->alt()->Degrees() < 0.0 ) ) {
		SkyObject *so = objectNearest( mousePoint() );
		
		if ( so && ! isObjectLabeled( so ) ) {
			setTransientObject( so );
			
			TransientColor = data->colorScheme()->colorNamed( "UserLabelColor" );
			if ( TransientTimer.isActive() ) TransientTimer.stop();
			update();
		}
	}
}


//Slots

void SkyMap::slotTransientTimeout( void ) {
	//Don't fade label if the transientObject is now the focusObject!
	if ( transientObject() == focusObject() && Options::useAutoLabel() ) {
		setTransientObject( NULL );
		TransientTimer.stop();
		return;
	}

	//to fade the labels, we will need to smoothly transition from UserLabelColor to SkyColor.
	TQColor c1 = data->colorScheme()->colorNamed( "UserLabelColor" );
	TQColor c2 = data->colorScheme()->colorNamed( "SkyColor" );
	
	int dRed =   ( c2.red()   - c1.red()   )/20;
	int dGreen = ( c2.green() - c1.green() )/20;
	int dBlue =  ( c2.blue()  - c1.blue()  )/20;
	int newRed   = TransientColor.red()   + dRed;
	int newGreen = TransientColor.green() + dGreen;
	int newBlue  = TransientColor.blue()  + dBlue;
	
	//Check to see if we have arrived at the target color (SkyColor).
	//If so, point TransientObject to NULL.
	if ( abs(newRed-c2.red()) < abs(dRed) || abs(newGreen-c2.green()) < abs(dGreen) || abs(newBlue-c2.blue()) < abs(dBlue) ) {
		setTransientObject( NULL );
		TransientTimer.stop();
	} else { 
		TransientColor.setRgb( newRed, newGreen, newBlue );
	}

	update();
}

void SkyMap::setFocusObject( SkyObject *o ) { 
	FocusObject = o; 
	
	if ( FocusObject ) 
		Options::setFocusObject( FocusObject->name() );
	else 
		Options::setFocusObject( i18n( "nothing" ) );
}

void SkyMap::slotCenter( void ) {
	setFocusPoint( clickedPoint() );
	if ( Options::useAltAz() ) 
		focusPoint()->EquatorialToHorizontal( data->LST, data->geo()->lat() );

	//clear the planet trail of old focusObject, if it was temporary
	if ( focusObject() && focusObject()->isSolarSystem() && data->temporaryTrail ) {
		((KSPlanetBase*)focusObject())->clearTrail();
		data->temporaryTrail = false;
	}

//If the requested object is below the opaque horizon, issue a warning message
//(unless user is already pointed below the horizon)
	if ( Options::useAltAz() && Options::showGround() &&
			focus()->alt()->Degrees() > -1.0 && focusPoint()->alt()->Degrees() < -1.0 ) {

		TQString caption = i18n( "Requested Position Below Horizon" );
		TQString message = i18n( "The requested position is below the horizon.\nWould you like to go there anyway?" );
		if ( KMessageBox::warningYesNo( this, message, caption,
				i18n("Go Anyway"), i18n("Keep Position"), "dag_focus_below_horiz" )==KMessageBox::No ) {
			setClickedObject( NULL );
			setFocusObject( NULL );
			Options::setIsTracking( false );

			return;
		}
	}

//set FocusObject before slewing.  Otherwise, KStarsData::updateTime() can reset
//destination to previous object...
	setFocusObject( ClickedObject );
	Options::setIsTracking( true );
	if ( ksw ) {
	  ksw->actionCollection()->action("track_object")->setIconSet( BarIcon( "encrypted" ) );
	  ksw->toolBar( "mainToolBar" )->setButtonIconSet( 4, BarIcon( "encrypted" ) );
	  ksw->actionCollection()->action("track_object")->setText( i18n( "Stop &Tracking" ) );
	}

	//If focusObject is a SS body and doesn't already have a trail, set the temporaryTrail
	if ( focusObject() && focusObject()->isSolarSystem()
	     && Options::useAutoTrail()
			&& ! ((KSPlanetBase*)focusObject())->hasTrail() ) {
		((KSPlanetBase*)focusObject())->addToTrail();
		data->temporaryTrail = true;
	}

	//update the destination to the selected coordinates
	if ( Options::useAltAz() ) { 
		if ( Options::useRefraction() )
			setDestinationAltAz( refract( focusPoint()->alt(), true ).Degrees(), focusPoint()->az()->Degrees() );
		else
			setDestinationAltAz( focusPoint()->alt()->Degrees(), focusPoint()->az()->Degrees() );
	} else {
		setDestination( focusPoint() );
	}

	focusPoint()->EquatorialToHorizontal( data->LST, data->geo()->lat() );

	//display coordinates in statusBar
	if ( ksw ) {
		TQString sX = focusPoint()->az()->toDMSString();
		TQString sY = focusPoint()->alt()->toDMSString(true);
		if ( Options::useAltAz() && Options::useRefraction() )
			sY = refract( focusPoint()->alt(), true ).toDMSString(true);
		TQString s = sX + ",  " + sY;
		ksw->statusBar()->changeItem( s, 1 );
		s = focusPoint()->ra()->toHMSString() + ",  " + focusPoint()->dec()->toDMSString(true);
		ksw->statusBar()->changeItem( s, 2 );
	}
	
	showFocusCoords(); //update FocusBox
}

void SkyMap::slotDSS( void ) {
	TQString URLprefix( "http://archive.stsci.edu/cgi-bin/dss_search?v=1" );
	TQString URLsuffix( "&e=J2000&h=15.0&w=15.0&f=gif&c=none&fov=NONE" );
	dms ra(0.0), dec(0.0);
	TQString RAString, DecString;
	char decsgn;

	//ra and dec must be the coordinates at J2000.  If we clicked on an object, just use the object's ra0, dec0 coords
	//if we clicked on empty sky, we need to precess to J2000.
	if ( clickedObject() ) {
		ra.setH( clickedObject()->ra0()->Hours() );
		dec.setD( clickedObject()->dec0()->Degrees() );
	} else {
		//move present coords temporarily to ra0,dec0 (needed for precessToAnyEpoch)
		clickedPoint()->setRA0( clickedPoint()->ra()->Hours() );
		clickedPoint()->setDec0( clickedPoint()->dec()->Degrees() );
		clickedPoint()->precessFromAnyEpoch( data->ut().djd(), J2000 );
		ra.setH( clickedPoint()->ra()->Hours() );
		dec.setD( clickedPoint()->dec()->Degrees() );

		//restore coords from present epoch
		clickedPoint()->setRA( clickedPoint()->ra0()->Hours() );
		clickedPoint()->setDec( clickedPoint()->dec0()->Degrees() );
	}

	RAString = RAString.sprintf( "&r=%02d+%02d+%02d", ra.hour(), ra.minute(), ra.second() );

	decsgn = '+';
	if ( dec.Degrees() < 0.0 ) decsgn = '-';
	int dd = abs( dec.degree() );
	int dm = abs( dec.arcmin() );
	int ds = abs( dec.arcsec() );
	DecString = DecString.sprintf( "&d=%c%02d+%02d+%02d", decsgn, dd, dm, ds );

	//concat all the segments into the kview command line:
	KURL url (URLprefix + RAString + DecString + URLsuffix);
	
	TQString message = i18n( "Digitized Sky Survey image provided by the Space Telescope Science Institute." );
	new ImageViewer (&url, message, this);
}

void SkyMap::slotDSS2( void ) {
	TQString URLprefix( "http://archive.stsci.edu/cgi-bin/dss_search?v=2r" );
	TQString URLsuffix( "&e=J2000&h=15.0&w=15.0&f=gif&c=none&fov=NONE" );
	dms ra(0.0), dec(0.0);
	TQString RAString, DecString;
	char decsgn;

	//ra and dec must be the coordinates at J2000.  If we clicked on an object, just use the object's ra0, dec0 coords
	//if we clicked on empty sky, we need to precess to J2000.
	if ( clickedObject() ) {
		ra.setH( clickedObject()->ra0()->Hours() );
		dec.setD( clickedObject()->dec0()->Degrees() );
	} else {
		//move present coords temporarily to ra0,dec0 (needed for precessToAnyEpoch)
		clickedPoint()->setRA0( clickedPoint()->ra()->Hours() );
		clickedPoint()->setDec0( clickedPoint()->dec()->Degrees() );
		clickedPoint()->precessFromAnyEpoch( data->ut().djd(), J2000 );
		ra.setH( clickedPoint()->ra()->Hours() );
		dec.setD( clickedPoint()->dec()->Degrees() );

		//restore coords from present epoch
		clickedPoint()->setRA( clickedPoint()->ra0()->Hours() );
		clickedPoint()->setDec( clickedPoint()->dec0()->Degrees() );
	}

	RAString = RAString.sprintf( "&r=%02d+%02d+%02d", ra.hour(), ra.minute(), ra.second() );

	decsgn = '+';
	if ( dec.Degrees() < 0.0 ) decsgn = '-';
	int dd = abs( dec.degree() );
	int dm = abs( dec.arcmin() );
	int ds = abs( dec.arcsec() );

	DecString = DecString.sprintf( "&d=%c%02d+%02d+%02d", decsgn, dd, dm, ds );

	//concat all the segments into the kview command line:
	KURL url (URLprefix + RAString + DecString + URLsuffix);
	
	TQString message = i18n( "Digitized Sky Survey image provided by the Space Telescope Science Institute." );
	new ImageViewer (&url, message, this);
}

void SkyMap::slotInfo( int id ) {
	TQStringList::Iterator it = clickedObject()->InfoList.at(id-200);
	TQString sURL = (*it);
	KURL url ( sURL );
	if (!url.isEmpty())
		kapp->invokeBrowser(sURL);
}

void SkyMap::slotBeginAngularDistance(void) {
	setPreviousClickedPoint( mousePoint() );
	angularDistanceMode = true;
	beginRulerPoint = getXY( previousClickedPoint(), Options::useAltAz(), Options::useRefraction() );
	endRulerPoint =  TQPoint( beginRulerPoint.x(),beginRulerPoint.y() );
}

void SkyMap::slotEndAngularDistance(void) {
	dms angularDistance;
	if(angularDistanceMode) {
		if ( SkyObject *so = objectNearest( mousePoint() ) ) {
			angularDistance = so->angularDistanceTo( previousClickedPoint() );
			ksw->statusBar()->changeItem( so->translatedLongName() + 
					"     " +
					i18n("Angular distance: " ) +
					angularDistance.toDMSString(), 0 );
		} else {
			angularDistance = mousePoint()->angularDistanceTo( previousClickedPoint() );
			ksw->statusBar()->changeItem( i18n("Angular distance: " ) +
				angularDistance.toDMSString(), 0 );
		}
		angularDistanceMode=false;
	}
}

void SkyMap::slotCancelAngularDistance(void) {
	angularDistanceMode=false;
}

void SkyMap::slotImage( int id ) {
	TQStringList::Iterator it = clickedObject()->ImageList.at(id-100);
	TQStringList::Iterator it2 = clickedObject()->ImageTitle.at(id-100);
	TQString sURL = (*it);
	TQString message = (*it2);
	KURL url ( sURL );
	if (!url.isEmpty())
		new ImageViewer (&url, clickedObject()->messageFromTitle(message), this);
}

bool SkyMap::isObjectLabeled( SkyObject *object ) {
	for ( SkyObject *o = data->ObjLabelList.first(); o; o = data->ObjLabelList.next() ) {
		if ( o == object ) return true;
	}

	return false;
}

void SkyMap::slotRemoveObjectLabel( void ) {
	for ( SkyObject *o = data->ObjLabelList.first(); o; o = data->ObjLabelList.next() ) {
		if ( o == clickedObject() ) {
			//remove object from list
			data->ObjLabelList.remove();
			break;
		}
	}

	forceUpdate();
}

void SkyMap::slotAddObjectLabel( void ) {
	data->ObjLabelList.append( clickedObject() );
	//Since we just added a permanent label, we don't want it to fade away!
	if ( transientObject() == clickedObject() ) setTransientObject( NULL );
	forceUpdate();
}

void SkyMap::slotRemovePlanetTrail( void ) {
	//probably don't need this if-statement, but just to be sure...
	if ( clickedObject() && clickedObject()->isSolarSystem() ) {
		((KSPlanetBase*)clickedObject())->clearTrail();
		forceUpdate();
	}
}

void SkyMap::slotAddPlanetTrail( void ) {
	//probably don't need this if-statement, but just to be sure...
	if ( clickedObject() && clickedObject()->isSolarSystem() ) {
		((KSPlanetBase*)clickedObject())->addToTrail();
		forceUpdate();
	}
}

void SkyMap::slotDetail( void ) {
// check if object is selected
	if ( !clickedObject() ) {
		KMessageBox::sorry( this, i18n("No object selected."), i18n("Object Details") );
		return;
	}
	DetailDialog detail( clickedObject(), data->ut(), data->geo(), ksw );
	detail.exec();
}

void SkyMap::slotClockSlewing() {
//If the current timescale exceeds slewTimeScale, set clockSlewing=true, and stop the clock.
	if ( fabs( data->clock()->scale() ) > Options::slewTimeScale() ) {
		if ( ! clockSlewing ) {
			clockSlewing = true;
			data->clock()->setManualMode( true );

			// don't change automatically the DST status
			if ( ksw ) ksw->updateTime( false );
		}
	} else {
		if ( clockSlewing ) {
			clockSlewing = false;
			data->clock()->setManualMode( false );

			// don't change automatically the DST status
			if ( ksw ) ksw->updateTime( false );
		}
	}
}

void SkyMap::setFocus( SkyPoint *p ) {
	setFocus( p->ra()->Hours(), p->dec()->Degrees() );
}

void SkyMap::setFocus( const dms &ra, const dms &dec ) {
	setFocus( ra.Hours(), dec.Degrees() );
}

void SkyMap::setFocus( double ra, double dec ) {
	Focus.set( ra, dec );
	focus()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
}

void SkyMap::setFocusAltAz( const dms &alt, const dms &az) {
	setFocusAltAz( alt.Degrees(), az.Degrees() );
}

void SkyMap::setFocusAltAz(double alt, double az) {
	focus()->setAlt(alt);
	focus()->setAz(az);
	focus()->HorizontalToEquatorial( data->LST, data->geo()->lat() );
	slewing = false;

	oldfocus()->set( focus()->ra(), focus()->dec() );
	oldfocus()->setAz( focus()->az()->Degrees() );
	oldfocus()->setAlt( focus()->alt()->Degrees() );

	double dHA = data->LST->Hours() - focus()->ra()->Hours();
	while ( dHA < 0.0 ) dHA += 24.0;
	data->HourAngle->setH( dHA );

	forceUpdate(); //need a total update, or slewing with the arrow keys doesn't work.
}

void SkyMap::setDestination( SkyPoint *p ) {
	Destination.set( p->ra(), p->dec() );
	destination()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
	emit destinationChanged();
}

void SkyMap::setDestination( const dms &ra, const dms &dec ) {
	Destination.set( ra, dec );
	destination()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
	emit destinationChanged();
}

void SkyMap::setDestination( double ra, double dec ) {
	Destination.set( ra, dec );
	destination()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
	emit destinationChanged();
}

void SkyMap::setDestinationAltAz( const dms &alt, const dms &az) {
	destination()->setAlt(alt);
	destination()->setAz(az);
	destination()->HorizontalToEquatorial( data->LST, data->geo()->lat() );
	emit destinationChanged();
}

void SkyMap::setDestinationAltAz(double alt, double az) {
	destination()->setAlt(alt);
	destination()->setAz(az);
	destination()->HorizontalToEquatorial( data->LST, data->geo()->lat() );
	emit destinationChanged();
}

void SkyMap::updateFocus() {
	if ( Options::isTracking() && focusObject() != NULL ) {
		if ( Options::useAltAz() ) {
			//Tracking any object in Alt/Az mode requires focus updates
			double dAlt = focusObject()->alt()->Degrees();
			if ( Options::useRefraction() ) 
				dAlt = refract( focusObject()->alt(), true ).Degrees();
			setFocusAltAz( dAlt, focusObject()->az()->Degrees() );
			focus()->HorizontalToEquatorial( data->LST, data->geo()->lat() );
			setDestination( focus() );
		} else {
			//Tracking in equatorial coords
			setFocus( focusObject() );
			focus()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
			setDestination( focus() );
		}
	} else if ( Options::isTracking() && focusPoint() != NULL ) {
		if ( Options::useAltAz() ) {
			//Tracking on empty sky in Alt/Az mode
			setFocus( focusPoint() );
			focus()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
			setDestination( focus() );
		}
	} else if ( ! slewing ) {
		//Not tracking and not slewing, let sky drift by
		if ( Options::useAltAz() ) {
			focus()->setAlt( destination()->alt()->Degrees() );
			focus()->setAz( destination()->az()->Degrees() );
			focus()->HorizontalToEquatorial( data->LST, data->geo()->lat() );
			//destination()->HorizontalToEquatorial( data->LST, data->geo()->lat() );
		} else {
			focus()->setRA( data->LST->Hours() - data->HourAngle->Hours() );
			setDestination( focus() );
			focus()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
			destination()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
		}
	}

	//Update the Hour Angle
	data->setHourAngle( data->LST->Hours() - focus()->ra()->Hours() );

	setOldFocus( focus() );
	oldfocus()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
}

void SkyMap::slewFocus( void ) {
	double dX, dY, fX, fY, r;
	double step = 1.0;
	SkyPoint newFocus;

//Don't slew if the mouse button is pressed
//Also, no animated slews if the Manual Clock is active
//08/2002: added possibility for one-time skipping of slew with snapNextFocus
	if ( !mouseButtonDown ) {
		bool goSlew = ( Options::useAnimatedSlewing() &&
			! data->snapNextFocus() ) &&
			!( data->clock()->isManualMode() && data->clock()->isActive() );
		if ( goSlew  ) {
			if ( Options::useAltAz() ) {
				dX = destination()->az()->Degrees() - focus()->az()->Degrees();
				dY = destination()->alt()->Degrees() - focus()->alt()->Degrees();
			} else {
				dX = destination()->ra()->Degrees() - focus()->ra()->Degrees();
				dY = destination()->dec()->Degrees() - focus()->dec()->Degrees();
			}

			//switch directions to go the short way around the celestial sphere, if necessary.
			if ( dX < -180.0 ) dX = 360.0 + dX;
			else if ( dX > 180.0 ) dX = -360.0 + dX;

			r = sqrt( dX*dX + dY*dY );

			while ( r > step ) {
				fX = dX / r;
				fY = dY / r;

				if ( Options::useAltAz() ) {
					focus()->setAlt( focus()->alt()->Degrees() + fY*step );
					focus()->setAz( dms( focus()->az()->Degrees() + fX*step ).reduce() );
					focus()->HorizontalToEquatorial( data->LST, data->geo()->lat() );
				} else {
					fX = fX/15.; //convert RA degrees to hours
					newFocus.set( focus()->ra()->Hours() + fX*step, focus()->dec()->Degrees() + fY*step );
					setFocus( &newFocus );
					focus()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
				}

				slewing = true;
				//since we are slewing, fade out the transient label
				if ( transientObject() && ! TransientTimer.isActive() ) 
					fadeTransientLabel();
				
				forceUpdate();
				kapp->processEvents(10); //keep up with other stuff

				if ( Options::useAltAz() ) {
					dX = destination()->az()->Degrees() - focus()->az()->Degrees();
					dY = destination()->alt()->Degrees() - focus()->alt()->Degrees();
				} else {
					dX = destination()->ra()->Degrees() - focus()->ra()->Degrees();
					dY = destination()->dec()->Degrees() - focus()->dec()->Degrees();
				}

				//switch directions to go the short way around the celestial sphere, if necessary.
				if ( dX < -180.0 ) dX = 360.0 + dX;
				else if ( dX > 180.0 ) dX = -360.0 + dX;

				r = sqrt( dX*dX + dY*dY );
			}
		}

		//Either useAnimatedSlewing==false, or we have slewed, and are within one step of destination
		//set focus=destination.
		if ( Options::useAltAz() ) {
			setFocusAltAz( destination()->alt()->Degrees(), destination()->az()->Degrees() );
			focus()->HorizontalToEquatorial( data->LST, data->geo()->lat() );
		} else {
			setFocus( destination() );
			focus()->EquatorialToHorizontal( data->LST, data->geo()->lat() );
		}
		
		data->HourAngle->setH( data->LST->Hours() - focus()->ra()->Hours() );
		slewing = false;

		//Turn off snapNextFocus, we only want it to happen once
		if ( data->snapNextFocus() ) {
			data->setSnapNextFocus(false);
		}

		//Start the HoverTimer. if the user leaves the mouse in place after a slew,
		//we want to attach a label to the nearest object.
		if ( Options::useHoverLabel() )
			HoverTimer.start( HOVER_INTERVAL, true );
		
		forceUpdate();
	}
}

void SkyMap::invokeKey( int key ) {
	TQKeyEvent *e = new TQKeyEvent( TQEvent::KeyPress, key, 0, 0 );
	keyPressEvent( e );
	delete e;
}

double SkyMap::findPA( SkyObject *o, int x, int y, double scale ) {
	//Find position angle of North using a test point displaced to the north
	//displace by 100/zoomFactor radians (so distance is always 100 pixels)
	//this is 5730/zoomFactor degrees
	double newDec = o->dec()->Degrees() + 5730.0/Options::zoomFactor();
	if ( newDec > 90.0 ) newDec = 90.0;
	SkyPoint test( o->ra()->Hours(), newDec );
	if ( Options::useAltAz() ) test.EquatorialToHorizontal( data->LST, data->geo()->lat() );
	TQPoint t = getXY( &test, Options::useAltAz(), Options::useRefraction(), scale );
	double dx = double( t.x() - x );  
	double dy = double( y - t.y() );  //backwards because TQWidget Y-axis increases to the bottom
	double north;
	if ( dy ) {
		north = atan( dx/dy )*180.0/dms::PI;
		//resolve atan ambiguity:
		if ( dy < 0.0 ) north += 180.0;
		if ( north >= 360.0 ) north -= 360.;
	} else {
		north = 90.0;
		if ( dx > 0 ) north = -90.0;
	}

	return ( north + o->pa() );
}

TQPoint SkyMap::getXY( SkyPoint *o, bool Horiz, bool doRefraction, double scale ) {
	TQPoint p;

	double Y, dX;
	double sindX, cosdX, sinY, cosY, sinY0, cosY0;

	int Width = int( width() * scale );
	int Height = int( height() * scale );

	double pscale = Options::zoomFactor() * scale;

	if ( Horiz ) {
		if ( doRefraction ) Y = refract( o->alt(), true ).radians(); //account for atmospheric refraction
		else Y = o->alt()->radians();

		if ( focus()->az()->Degrees() > 270.0 && o->az()->Degrees() < 90.0 ) {
			dX = 2*dms::PI + focus()->az()->radians() - o->az()->radians();
		} else {
			dX = focus()->az()->radians() - o->az()->radians();
		}

		focus()->alt()->SinCos( sinY0, cosY0 );

  } else {
		if (focus()->ra()->Hours() > 18.0 && o->ra()->Hours() < 6.0) {
			dX = 2*dms::PI + o->ra()->radians() - focus()->ra()->radians();
		} else {
			dX = o->ra()->radians() - focus()->ra()->radians();
		}
    Y = o->dec()->radians();
		focus()->dec()->SinCos( sinY0, cosY0 );
  }

	//Convert dX, Y coords to screen pixel coords.
	#if ( __GLIBC__ >= 2 && __GLIBC_MINOR__ >=1 ) && !defined(__UCLIBC__)
	//GNU version
	sincos( dX, &sindX, &cosdX );
	sincos( Y, &sinY, &cosY );
	#else
	//ANSI version
	sindX = sin(dX);
	cosdX = cos(dX);
	sinY  = sin(Y);
	cosY  = cos(Y);
	#endif

	double c = sinY0*sinY + cosY0*cosY*cosdX;

	if ( c < 0.0 ) { //Object is on "back side" of the celestial sphere; don't plot it.
		p.setX( -10000000 );
		p.setY( -10000000 );
		return p;
	}

	double k = sqrt( 2.0/( 1 + c ) );

	p.setX( int( 0.5*Width  - pscale*k*cosY*sindX ) );
	p.setY( int( 0.5*Height - pscale*k*( cosY0*sinY - sinY0*cosY*cosdX ) ) );

	return p;
}

SkyPoint SkyMap::dXdYToRaDec( double dx, double dy, bool useAltAz, dms *LST, const dms *lat, bool doRefract ) {
	//Determine RA and Dec of a point, given (dx, dy): it's pixel
	//coordinates in the SkyMap with the center of the map as the origin.

	SkyPoint result;
	double sinDec, cosDec, sinDec0, cosDec0, sinc, cosc, sinlat, coslat;
	double xx, yy;

	double r  = sqrt( dx*dx + dy*dy );
	dms centerAngle;
	centerAngle.setRadians( 2.0*asin(0.5*r) );

	focus()->dec()->SinCos( sinDec0, cosDec0 );
	centerAngle.SinCos( sinc, cosc );

	if ( useAltAz ) {
		dms HA;
		dms Dec, alt, az, alt0, az0;
		double A;
		double sinAlt, cosAlt, sinAlt0, cosAlt0, sinAz, cosAz;
//		double HA0 = LST - focus.ra();
		az0 = focus()->az()->Degrees();
		alt0 = focus()->alt()->Degrees();
		alt0.SinCos( sinAlt0, cosAlt0 );

		dx = -dx; //Flip East-west (Az goes in opposite direction of RA)
		yy = dx*sinc;
		xx = r*cosAlt0*cosc - dy*sinAlt0*sinc;

		A = atan( yy/xx );
		//resolve ambiguity of atan():
		if ( xx<0 ) A = A + dms::PI;
//		if ( xx>0 && yy<0 ) A = A + 2.0*dms::PI;

		dms deltaAz;
		deltaAz.setRadians( A );
		az = focus()->az()->Degrees() + deltaAz.Degrees();
		alt.setRadians( asin( cosc*sinAlt0 + ( dy*sinc*cosAlt0 )/r ) );

		if ( doRefract ) alt.setD( refract( &alt, false ).Degrees() );  //find true altitude from apparent altitude

		az.SinCos( sinAz, cosAz );
		alt.SinCos( sinAlt, cosAlt );
		lat->SinCos( sinlat, coslat );

		Dec.setRadians( asin( sinAlt*sinlat + cosAlt*coslat*cosAz ) );
		Dec.SinCos( sinDec, cosDec );

		HA.setRadians( acos( ( sinAlt - sinlat*sinDec )/( coslat*cosDec ) ) );
		if ( sinAz > 0.0 ) HA.setH( 24.0 - HA.Hours() );

		result.setRA( LST->Hours() - HA.Hours() );
		result.setRA( result.ra()->reduce() );
		result.setDec( Dec.Degrees() );

		return result;

  } else {
		yy = dx*sinc;
		xx = r*cosDec0*cosc - dy*sinDec0*sinc;

		double RARad = ( atan( yy / xx ) );
		//resolve ambiguity of atan():
		if ( xx<0 ) RARad = RARad + dms::PI;
//		if ( xx>0 && yy<0 ) RARad = RARad + 2.0*dms::PI;

		dms deltaRA, Dec;
		deltaRA.setRadians( RARad );
		Dec.setRadians( asin( cosc*sinDec0 + (dy*sinc*cosDec0)/r ) );

		result.setRA( focus()->ra()->Hours() + deltaRA.Hours() );
		result.setRA( result.ra()->reduce() );
		result.setDec( Dec.Degrees() );

		return result;
	}
}

dms SkyMap::refract( const dms *alt, bool findApparent ) {
	if ( alt->Degrees() <= -2.000 ) return dms( alt->Degrees() );

	int index = int( ( alt->Degrees() + 2.0 )*2. );  //RefractCorr arrays start at alt=-2.0 degrees.
	dms result;

	//Failsafe: if the index is out of range, return the original angle
	if ( index < 0 || index > 183 ) {
		return dms( alt->Degrees() );
	}

	if ( findApparent ) {
		result.setD( alt->Degrees() + RefractCorr1[index] );
	} else {
		result.setD( alt->Degrees() + RefractCorr2[index] );
	}

	return result;
}

//---------------------------------------------------------------------------


// force a new calculation of the skymap (used instead of update(), which may skip the redraw)
// if now=true, SkyMap::paintEvent() is run immediately, rather than being added to the event queue
// also, determine new coordinates of mouse cursor.
void SkyMap::forceUpdate( bool now )
{
	TQPoint mp( mapFromGlobal( TQCursor::pos() ) );
	double dx = ( 0.5*width()  - mp.x() )/Options::zoomFactor();
	double dy = ( 0.5*height() - mp.y() )/Options::zoomFactor();

	if (! unusablePoint (dx, dy)) {
		//determine RA, Dec of mouse pointer
		setMousePoint( dXdYToRaDec( dx, dy, Options::useAltAz(), data->LST, data->geo()->lat(), Options::useRefraction() ) );
	}

	computeSkymap = true;
	if ( now ) repaint();
	else update();
}

float SkyMap::fov() {
	if ( width() >= height() ) 
		return 28.65*width()/Options::zoomFactor();
	else
		return 28.65*height()/Options::zoomFactor();
}

bool SkyMap::checkVisibility( SkyPoint *p, float FOV, double XMax ) {
	double dX, dY;
	bool useAltAz = Options::useAltAz();

	//Skip objects below the horizon if:
	// + usingQt::Horizontal coords,
	// + the ground is drawn,
	// + and either of the following is true:
	//   - focus is above the horizon
	//   - field of view is larger than 50 degrees
	if ( useAltAz && Options::showGround() && p->alt()->Degrees() < -2.0 
				&& ( focus()->alt()->Degrees() > 0. || FOV > 50. ) ) return false;

	if ( useAltAz ) {
		dY = fabs( p->alt()->Degrees() - focus()->alt()->Degrees() );
	} else {
		dY = fabs( p->dec()->Degrees() - focus()->dec()->Degrees() );
	}
	if ( isPoleVisible ) dY *= 0.75; //increase effective FOV when pole visible.
	if ( dY > FOV ) return false;
	if ( isPoleVisible ) return true;

	if ( useAltAz ) {
		dX = fabs( p->az()->Degrees() - focus()->az()->Degrees() );
	} else {
		dX = fabs( p->ra()->Degrees() - focus()->ra()->Degrees() );
	}
	if ( dX > 180.0 ) dX = 360.0 - dX; // take shorter distance around sky

	if ( dX < XMax ) {
		return true;
	} else {
		return false;
	}
}

bool SkyMap::unusablePoint (double dx, double dy)
{
	if (dx >= 1.41 || dx <= -1.41 || dy >= 1.41 || dy <= -1.41)
		return true;
	else
		return false;
}

void SkyMap::setZoomMouseCursor()
{
	mouseMoveCursor = false;	// no mousemove cursor
	
	TQPainter p;
	TQPixmap cursorPix (32, 32); // size 32x32 (this size is compatible to all systems)
// the center of the pixmap
	int mx = cursorPix.	width() / 2;
	int my = cursorPix.	height() / 2;

	cursorPix.fill (white);  // white background
	p.begin (&cursorPix);
	p.setPen (TQPen (black, 2));	// black lines

	p.drawEllipse( mx - 7, my - 7, 14, 14 );
	p.drawLine( mx + 5, my + 5, mx + 11, my + 11 );
	p.end();

// create a mask to make parts of the pixmap invisible
	TQBitmap mask (32, 32);
	mask.fill (color0);	// all is invisible

	p.begin (&mask);
// paint over the parts which should be visible
	p.setPen (TQPen (color1, 3));
	p.drawEllipse( mx - 7, my - 7, 14, 14 );
	p.drawLine( mx + 5, my + 5, mx + 12, my + 12 );
	p.end();
	
	cursorPix.setMask (mask);	// set the mask
	TQCursor cursor (cursorPix);
	setCursor (cursor);
}

void SkyMap::setDefaultMouseCursor()
{
	mouseMoveCursor = false;	// no mousemove cursor
	
	TQPainter p;
	TQPixmap cursorPix (32, 32); // size 32x32 (this size is compatible to all systems)
// the center of the pixmap
	int mx = cursorPix.	width() / 2;
	int my = cursorPix.	height() / 2;

	cursorPix.fill (white);  // white background
	p.begin (&cursorPix);
	p.setPen (TQPen (black, 2));	// black lines
// 1. diagonal
	p.drawLine (mx - 2, my - 2, mx - 8, mx - 8);
	p.drawLine (mx + 2, my + 2, mx + 8, mx + 8);
// 2. diagonal
	p.drawLine (mx - 2, my + 2, mx - 8, mx + 8);
	p.drawLine (mx + 2, my - 2, mx + 8, mx - 8);
	p.end();

// create a mask to make parts of the pixmap invisible
	TQBitmap mask (32, 32);
	mask.fill (color0);	// all is invisible

	p.begin (&mask);
// paint over the parts which should be visible
	p.setPen (TQPen (color1, 3));
// 1. diagonal
	p.drawLine (mx - 2, my - 2, mx - 8, mx - 8);
	p.drawLine (mx + 2, my + 2, mx + 8, mx + 8);
// 2. diagonal
	p.drawLine (mx - 2, my + 2, mx - 8, mx + 8);
	p.drawLine (mx + 2, my - 2, mx + 8, mx - 8);
	p.end();

	cursorPix.setMask (mask);	// set the mask
	TQCursor cursor (cursorPix);
	setCursor (cursor);
}

void SkyMap::setMouseMoveCursor()
{
	if (mouseButtonDown)
	{
		setCursor (9);	// cursor shape defined in qt
		mouseMoveCursor = true;
	}
}

void SkyMap::addLink( void ) {
	AddLinkDialog adialog( this, clickedObject()->name() );
	TQString entry;
  TQFile file;

	if ( adialog.exec()==TQDialog::Accepted ) {
		if ( adialog.isImageLink() ) {
			//Add link to object's ImageList, and descriptive text to its ImageTitle list
			clickedObject()->ImageList.append( adialog.url() );
			clickedObject()->ImageTitle.append( adialog.desc() );

			//Also, update the user's custom image links database
			//check for user's image-links database.  If it doesn't exist, create it.
			file.setName( locateLocal( "appdata", "image_url.dat" ) ); //determine filename in local user KDE directory tree.

			if ( !file.open( IO_ReadWrite | IO_Append ) ) {
				TQString message = i18n( "Custom image-links file could not be opened.\nLink cannot be recorded for future sessions." );
				KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
				return;
			} else {
				entry = clickedObject()->name() + ":" + adialog.desc() + ":" + adialog.url();
				TQTextStream stream( &file );
				stream << entry << endl;
				file.close();
				emit linkAdded();
			}
		} else {
			clickedObject()->InfoList.append( adialog.url() );
			clickedObject()->InfoTitle.append( adialog.desc() );

			//check for user's image-links database.  If it doesn't exist, create it.
			file.setName( locateLocal( "appdata", "info_url.dat" ) ); //determine filename in local user KDE directory tree.

			if ( !file.open( IO_ReadWrite | IO_Append ) ) {
				TQString message = i18n( "Custom information-links file could not be opened.\nLink cannot be recorded for future sessions." );						KMessageBox::sorry( 0, message, i18n( "Could not Open File" ) );
				return;
			} else {
				entry = clickedObject()->name() + ":" + adialog.desc() + ":" + adialog.url();
				TQTextStream stream( &file );
				stream << entry << endl;
				file.close();
				emit linkAdded();
			}
		}
	}
}

void SkyMap::updateAngleRuler() {
	if ( Options::useAltAz() ) PreviousClickedPoint.EquatorialToHorizontal( data->LST, data->geo()->lat() );
	beginRulerPoint = getXY( previousClickedPoint(), Options::useAltAz(), Options::useRefraction() );

//	endRulerPoint =  TQPoint(e->x(), e->y());
	endRulerPoint = mapFromGlobal( TQCursor::pos() );
}

#include "skymap.moc"