summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/infoboxes.cpp
blob: 922380daa377e0789b448a0a11b66ed60fabc08f (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
/***************************************************************************
                          infoboxes.cpp  -  description
                             -------------------
    begin                : Wed Jun 5 2002
    copyright            : (C) 2002 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 <kglobal.h>
#include <kdebug.h>
//#include <tqpen.h>
#include <tqpainter.h>

#include "infoboxes.h"
#include "kstarsdatetime.h"
#include "dms.h"
#include "geolocation.h"
#include "skypoint.h"

InfoBoxes::InfoBoxes( int w, int h, TQPoint tp, bool tshade,
		TQPoint gp, bool gshade, TQPoint fp, bool fshade,
		TQColor colorText, TQColor colorGrab, TQColor colorBG ) :
		boxColor(colorText), grabColor(colorGrab), bgColor(colorBG),
		GeoBox(0), FocusBox(0), TimeBox(0)
{

	int tx = tp.x();
	int ty = tp.y();
	int gx = gp.x();
	int gy = gp.y();
	int fx = fp.x();
	int fy = fp.y();

	GrabbedBox = 0;
	GrabPos = TQPoint( 0, 0 );
	Visible = true;

	Width = w;
	Height = h;

//Create infoboxes
	GeoBox   = new InfoBox( gx, gy, gshade, "", "" );
	TimeBox  = new InfoBox( tx, ty, tshade, "", "", "" );
	FocusBox = new InfoBox( fx, fy, fshade, "", "", "" );

	fixCollisions( TimeBox );
	fixCollisions( FocusBox );

	resize( w, h );
}

InfoBoxes::InfoBoxes( int w, int h, int tx, int ty, bool tshade,
		int gx, int gy, bool gshade, int fx, int fy, bool fshade,
		TQColor colorText, TQColor colorGrab, TQColor colorBG ) :
		boxColor(colorText), grabColor(colorGrab), bgColor(colorBG) {

	GrabbedBox = 0;
	GrabPos = TQPoint( 0, 0 );
	Visible = true;

	Width = w;
	Height = h;

//Create infoboxes
	GeoBox   = new InfoBox( gx, gy, gshade, "", "" );
	TimeBox  = new InfoBox( tx, ty, tshade, "", "", "" );
	FocusBox = new InfoBox( fx, fy, fshade, "", "", "" );

	fixCollisions( TimeBox );
	fixCollisions( FocusBox );

	resize( w, h );
}

InfoBoxes::~InfoBoxes(){
	delete FocusBox;
	delete TimeBox;
	delete GeoBox;
}

void InfoBoxes::resize( int w, int h ) {
	Width = w;
	Height = h;
	checkBorders(false);
}

void InfoBoxes::drawBoxes( TQPainter &p, TQColor FGColor, TQColor grabColor,
		TQColor BGColor, unsigned int BGMode ) {
	if ( isVisible() ) {
		if ( GeoBox->isVisible() ) {
			p.setPen( TQPen( FGColor ) );
			if ( GrabbedBox == 1 ) {
				p.setPen( TQPen( grabColor ) );
				p.drawRect( GeoBox->x(), GeoBox->y(), GeoBox->width(), GeoBox->height() );
			}
			GeoBox->draw( p, BGColor, BGMode );
		}

		if ( TimeBox->isVisible() ) {
			p.setPen( TQPen( FGColor ) );
			if ( GrabbedBox == 2 ) {
				p.setPen( TQPen( grabColor ) );
				p.drawRect( TimeBox->x(), TimeBox->y(), TimeBox->width(), TimeBox->height() );
			}
			TimeBox->draw( p, BGColor, BGMode );
		}

		if ( FocusBox->isVisible() ) {
			p.setPen( TQPen( FGColor ) );
			if ( GrabbedBox == 3 ) {
				p.setPen( TQPen( grabColor ) );
				p.drawRect( FocusBox->x(), FocusBox->y(), FocusBox->width(), FocusBox->height() );
			}
			FocusBox->draw( p, BGColor, BGMode );
		}
	}
}

bool InfoBoxes::grabBox( TQMouseEvent *e ) {
	if ( GeoBox->rect().contains( e->pos() ) ) {
		GrabbedBox = 1;
		GrabPos.setX( e->x() - GeoBox->x() );
		GrabPos.setY( e->y() - GeoBox->y() );
		return true;
	} else if ( TimeBox->rect().contains( e->pos() ) ) {
		GrabbedBox = 2;
		GrabPos.setX( e->x() - TimeBox->x() );
		GrabPos.setY( e->y() - TimeBox->y() );
		return true;
	} else if ( FocusBox->rect().contains( e->pos() ) ) {
		GrabbedBox = 3;
		GrabPos.setX( e->x() - FocusBox->x() );
		GrabPos.setY( e->y() - FocusBox->y() );
		return true;
	} else {
		GrabbedBox = 0; //this is probably redundant, because mouseRelease should call unGrabBox()...
		return false;
	}
}

bool InfoBoxes::unGrabBox( void ) {
	if ( GrabbedBox ) {
		GrabbedBox = 0;
		checkBorders();
		return true;
	} else {
		return false;
	}
}

bool InfoBoxes::dragBox( TQMouseEvent *e ) {
	switch( GrabbedBox ) {
		case 1: //GeoBox
			GeoBox->move( e->x() - GrabPos.x(), e->y() - GrabPos.y() );
			fixCollisions( GeoBox );
			return true;
			break;
		case 2: //TimeBox
			TimeBox->move( e->x() - GrabPos.x(), e->y() - GrabPos.y() );
			fixCollisions( TimeBox );
			return true;
			break;
		case 3: //FocusBox
			FocusBox->move( e->x() - GrabPos.x(), e->y() - GrabPos.y() );
			fixCollisions( FocusBox );
			return true;
			break;
		default: //no box is grabbed; return false
			return false;
	}
}

bool InfoBoxes::shadeBox( TQMouseEvent *e ) {
	if ( GeoBox->rect().contains( e->pos() ) ) {
		GeoBox->toggleShade();
		if ( GeoBox->rect().bottom() > height() ) GeoBox->move( GeoBox->x(), height() - GeoBox->height() );
		if ( GeoBox->rect().right() > width() ) GeoBox->move( width() - GeoBox->width(), GeoBox->y() );
		if ( GeoBox->anchorBottom() ) GeoBox->move( GeoBox->x(), height() - GeoBox->height() );
		if ( GeoBox->anchorRight() ) GeoBox->move( width() - GeoBox->width(), GeoBox->y() );
		fixCollisions( TimeBox );
		fixCollisions( FocusBox );
		return true;
	} else if ( TimeBox->rect().contains( e->pos() ) ) {
		TimeBox->toggleShade();
		if ( TimeBox->rect().bottom() > height() ) TimeBox->move( TimeBox->x(), height() - TimeBox->height() );
		if ( TimeBox->rect().right() > width() ) TimeBox->move( width() - TimeBox->width(), TimeBox->y() );
		if ( TimeBox->anchorBottom() ) TimeBox->move( TimeBox->x(), height() - TimeBox->height() );
		if ( TimeBox->anchorRight() ) TimeBox->move( width() - TimeBox->width(), TimeBox->y() );
		fixCollisions( GeoBox );
		fixCollisions( FocusBox );
		return true;
	} else if ( FocusBox->rect().contains( e->pos() ) ) {
		FocusBox->toggleShade();
		if ( FocusBox->rect().bottom() > height() ) FocusBox->move( FocusBox->x(), height() - FocusBox->height() );
		if ( FocusBox->rect().right() > width() ) FocusBox->move( width() - FocusBox->width(), FocusBox->y() );
		if ( FocusBox->anchorBottom() ) FocusBox->move( FocusBox->x(), height() - FocusBox->height() );
		if ( FocusBox->anchorRight() ) FocusBox->move( width() - FocusBox->width(), FocusBox->y() );
		fixCollisions( TimeBox );
		fixCollisions( GeoBox );
		return true;
	}
	return false;
}

bool InfoBoxes::fixCollisions( InfoBox *target ) {
	int dLeft(0), dRight(0), dUp(0), dDown(0);
	TQRect area = TQRect( 0, 0, Width, Height );
	TQRect t = target->rect();
	TQRect Box1, Box2;
	
//Set Box1 and Box2 to the rects of the other two InfoBoxes, unless
//they are not visible (if so, set a null TQRect)
	if ( target == GeoBox ) {
		if ( FocusBox->isVisible() ) Box1 = FocusBox->rect();
		else Box1 = TQRect(0,0,0,0);

		if ( TimeBox->isVisible() ) Box2 = TimeBox->rect();
		else Box2 = TQRect(0,0,0,0);

	} else if ( target == FocusBox ) {
		if ( GeoBox->isVisible() ) Box1 = GeoBox->rect();
		else Box1 = TQRect(0,0,0,0);

		if ( TimeBox->isVisible() ) Box2 = TimeBox->rect();
		else Box2 = TQRect(0,0,0,0);

	} else if ( target == TimeBox ) {
		if ( FocusBox->isVisible() ) Box1 = FocusBox->rect();
		else Box1 = TQRect(0,0,0,0);

		if ( GeoBox->isVisible() ) Box2 = GeoBox->rect();
		else Box2 = TQRect(0,0,0,0);

	} else { return false; } //none of the Boxes match target!

//Shrink Box1 and Box2 by one pixel in each direction.  This will make the
//Edges of adjacent boxes line up more nicely.
	if ( Box1.width() ) Box1.setCoords( Box1.left()+1, Box1.top()+1, Box1.right()-1, Box1.bottom()-1 );
	if ( Box2.width() ) Box2.setCoords( Box2.left()+1, Box2.top()+1, Box2.right()-1, Box2.bottom()-1 );

//First, make sure target box is within area rect.
	if ( ! area.contains( t ) ) {
/*		if ( t.x() < area.x() ) target->move( area.x(), t.y() );
		if ( t.y() < area.y() ) target->move( t.x(), area.y() );
		if ( t.right() > area.right() ){ target->move( area.right() - t.width(), t.y() ); }
		if ( t.bottom() > area.bottom() ) target->move( t.x(), area.bottom() - t.height() );*/

		int x = t.x(), y = t.y();
		
		if ( t.x() < area.x() ) x = area.x();
		if ( t.y() < area.y() ) y = area.y();
		if ( t.right() > area.right() ) x = area.right() - t.width();
		if ( t.bottom() > area.bottom() ) y = area.bottom() - t.height();
		target->move(x,y);

		//Reset t
		t = target->rect();
	}

	TQRect upRect = t;
	TQRect downRect = t;
	TQRect leftRect = t;
	TQRect rightRect = t;

//Fix collisions
	if ( t.intersects( Box1 ) || t.intersects( Box2 ) ) {
		//move t to the left one pixel at a time until there is no
		//intersection with Box1 or Box2.
		while ( leftRect.intersects( Box1 ) || leftRect.intersects( Box2 ) ) {
			++dLeft;
			leftRect.moveTopLeft( TQPoint( t.x() - dLeft, t.y() ) );
		}
		//If leftRect is outside area, set dLeft to a nonsense large value
		if ( !area.contains( leftRect ) ) { dLeft = 100000; }
		//repeat for right, up and down directions.
		while ( rightRect.intersects( Box1 ) || rightRect.intersects( Box2 ) ) {
			++dRight;
			rightRect.moveTopLeft( TQPoint( t.x() + dRight, t.y() ) );
		}
		if ( !area.contains( rightRect ) ) { dRight = 100000; }

		while ( upRect.intersects( Box1 ) || upRect.intersects( Box2 ) ) {
			++dUp;
			upRect.moveTopLeft( TQPoint( t.x(), t.y() - dUp ) );
		}
		if ( !area.contains( upRect ) ) { dUp = 100000; }

		while ( downRect.intersects( Box1 ) || downRect.intersects( Box2 ) ) {
			++dDown;
			downRect.moveTopLeft( TQPoint( t.x(), t.y() + dDown ) );
		}
		if ( !area.contains( downRect ) ) { dDown = 100000; }


		//find the smallest displacement, and move the target box there.
		//if the smallest displacement is 100000, then the function has failed
		//to find any legal position; return false.
		int dmin = dLeft;
		if ( dRight < dmin ) dmin = dRight;
		if ( dDown < dmin ) dmin = dDown;
		if ( dUp < dmin ) dmin = dUp;

		if ( dmin == 100000 ) { return false; }
		else if ( dmin == dLeft ) {
			target->move( leftRect.x(), leftRect.y() );
		} else if ( dmin == dRight ) {
			target->move( rightRect.x(), rightRect.y() );
		} else if ( dmin == dUp ) {
			target->move( upRect.x(), upRect.y() );
		} else if ( dmin == dDown ) {
			target->move( downRect.x(), downRect.y() );
		}
	}
	else  // no intersection with other boxes
		return true;

	//Set Anchor flags based on new position
	if ( target->rect().right() >= width()-2 ) target->setAnchorRight( true );
	else target->setAnchorRight( false ); 
	if ( target->rect().bottom() >= height()-2 ) target->setAnchorBottom(true);
	else target->setAnchorBottom(false);

	//Final check to see if we're still inside area (we may not be if target
	//is bigger than area)
	if ( area.contains( target->rect() ) ) return true;
	else return false;
}

bool InfoBoxes::timeChanged( const KStarsDateTime &ut, const KStarsDateTime &lt, dms *lst ) {
	TQString ot1 = TimeBox->text1();
	TQString ot2 = TimeBox->text2();
	TQString ot3 = TimeBox->text3();
	
	TimeBox->setText1( i18n( "Local Time", "LT: " ) + lt.time().toString()
		+ "   " + lt.date().toString( "%d %b %Y" ) );
	TimeBox->setText2( i18n( "Universal Time", "UT: " ) + ut.time().toString()
		+ "   " + ut.date().toString( "%d %b %Y" ) );
	
	TQString STString;
	STString = STString.sprintf( "%02d:%02d:%02d   ", lst->hour(), lst->minute(), lst->second() );
	
	//Don't use KLocale::formatNumber() for Julian Day because we don't want 
	//thousands-place separators
	TQString JDString = TQString::number( ut.djd(), 'f', 2 );
	JDString.replace( ".", TDEGlobal::locale()->decimalSymbol() );
	
	TimeBox->setText3( i18n( "Sidereal Time", "ST: " ) + STString +
			i18n( "Julian Day", "JD: " ) + JDString );

	if ( ot1 == TimeBox->text1() && ot2 == TimeBox->text2() &&
			ot3 == TimeBox->text3() ) 
		return false;
	else {
		checkBorders(); 
		return true;
	}
}

bool InfoBoxes::geoChanged(const GeoLocation *geo) {
	TQString ot1 = GeoBox->text1();
	TQString ot2 = GeoBox->text2();
	
	TQString name = geo->translatedName() + ", ";
	if ( ! geo->province().isEmpty() ) name += geo->translatedProvince() + ",  ";
	name += geo->translatedCountry();
	GeoBox->setText1( name );

	GeoBox->setText2( i18n( "Longitude", "Long:" ) + " " +
		TDEGlobal::locale()->formatNumber( geo->lng()->Degrees(),3) + "   " +
		i18n( "Latitude", "Lat:" ) + " " +
		TDEGlobal::locale()->formatNumber( geo->lat()->Degrees(),3) );
	
	if ( ot1 == GeoBox->text1() && ot2 == GeoBox->text2() )
		return false;
	else {
		checkBorders();
		return true;
	}
}

bool InfoBoxes::focusObjChanged( const TQString &n ) {
	TQString ot1 = FocusBox->text1();
	
	FocusBox->setText1( i18n( "Focused on: " ) + n );
	if ( ot1 == FocusBox->text1() ) return false;
	else {
		checkBorders();
		return true;
	}
}

bool InfoBoxes::focusCoordChanged(const SkyPoint *p) {
	TQString ot2 = FocusBox->text2();
	TQString ot3 = FocusBox->text3();
	
	FocusBox->setText2( i18n( "Right Ascension", "RA" ) + ": " + p->ra()->toHMSString() +
		"  " + i18n( "Declination", "Dec" ) +  ": " + p->dec()->toDMSString(true) );
	FocusBox->setText3( i18n( "Azimuth", "Az" ) + ": " + p->az()->toDMSString(true) + 
		"  " + i18n( "Altitude", "Alt" ) + ": " + p->alt()->toDMSString(true) );

	if ( ot2 == FocusBox->text2() && ot3 == FocusBox->text3() ) 
		return false;
	else {
		checkBorders();
		return true;
	}
}

void InfoBoxes::checkBorders(bool resetToDefault) {
	if (resetToDefault) {
		TimeBox->setAnchorFlag( InfoBox::AnchorNone );
		GeoBox->setAnchorFlag( InfoBox::AnchorNone );
		FocusBox->setAnchorFlag( InfoBox::AnchorNone );
	}
	
	if ( GeoBox->rect().right()    >= Width-2  ) GeoBox->setAnchorRight( true );
	if ( TimeBox->rect().right()   >= Width-2  ) TimeBox->setAnchorRight( true );
	if ( FocusBox->rect().right()  >= Width-2  ) FocusBox->setAnchorRight( true );
	if ( GeoBox->rect().bottom()   >= Height-2 ) GeoBox->setAnchorBottom( true );
	if ( TimeBox->rect().bottom()  >= Height-2 ) TimeBox->setAnchorBottom( true );
	if ( FocusBox->rect().bottom() >= Height-2 ) FocusBox->setAnchorBottom( true );

	if ( GeoBox->anchorRight()    ) GeoBox->move( Width, GeoBox->y() );
	if ( TimeBox->anchorRight()   ) TimeBox->move( Width, TimeBox->y() );
	if ( FocusBox->anchorRight()  ) FocusBox->move( Width, FocusBox->y() );
	if ( GeoBox->anchorBottom()   ) GeoBox->move( GeoBox->x(), Height );
	if ( TimeBox->anchorBottom()  ) TimeBox->move( TimeBox->x(), Height );
	if ( FocusBox->anchorBottom() ) FocusBox->move( FocusBox->x(), Height );
}

#include "infoboxes.moc"