00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <kglobal.h>
00019 #include <kdebug.h>
00020
00021 #include <qpainter.h>
00022
00023 #include "infoboxes.h"
00024 #include "kstarsdatetime.h"
00025 #include "dms.h"
00026 #include "geolocation.h"
00027 #include "skypoint.h"
00028
00029 InfoBoxes::InfoBoxes( int w, int h, QPoint tp, bool tshade,
00030 QPoint gp, bool gshade, QPoint fp, bool fshade,
00031 QColor colorText, QColor colorGrab, QColor colorBG ) :
00032 boxColor(colorText), grabColor(colorGrab), bgColor(colorBG),
00033 GeoBox(0), FocusBox(0), TimeBox(0)
00034 {
00035
00036 int tx = tp.x();
00037 int ty = tp.y();
00038 int gx = gp.x();
00039 int gy = gp.y();
00040 int fx = fp.x();
00041 int fy = fp.y();
00042
00043 GrabbedBox = 0;
00044 GrabPos = QPoint( 0, 0 );
00045 Visible = true;
00046
00047 Width = w;
00048 Height = h;
00049
00050
00051 GeoBox = new InfoBox( gx, gy, gshade, "", "" );
00052 TimeBox = new InfoBox( tx, ty, tshade, "", "", "" );
00053 FocusBox = new InfoBox( fx, fy, fshade, "", "", "" );
00054
00055 fixCollisions( TimeBox );
00056 fixCollisions( FocusBox );
00057
00058 resize( w, h );
00059 }
00060
00061 InfoBoxes::InfoBoxes( int w, int h, int tx, int ty, bool tshade,
00062 int gx, int gy, bool gshade, int fx, int fy, bool fshade,
00063 QColor colorText, QColor colorGrab, QColor colorBG ) :
00064 boxColor(colorText), grabColor(colorGrab), bgColor(colorBG) {
00065
00066 GrabbedBox = 0;
00067 GrabPos = QPoint( 0, 0 );
00068 Visible = true;
00069
00070 Width = w;
00071 Height = h;
00072
00073
00074 GeoBox = new InfoBox( gx, gy, gshade, "", "" );
00075 TimeBox = new InfoBox( tx, ty, tshade, "", "", "" );
00076 FocusBox = new InfoBox( fx, fy, fshade, "", "", "" );
00077
00078 fixCollisions( TimeBox );
00079 fixCollisions( FocusBox );
00080
00081 resize( w, h );
00082 }
00083
00084 InfoBoxes::~InfoBoxes(){
00085 delete FocusBox;
00086 delete TimeBox;
00087 delete GeoBox;
00088 }
00089
00090 void InfoBoxes::resize( int w, int h ) {
00091 Width = w;
00092 Height = h;
00093 checkBorders(false);
00094 }
00095
00096 void InfoBoxes::drawBoxes( QPainter &p, QColor FGColor, QColor grabColor,
00097 QColor BGColor, unsigned int BGMode ) {
00098 if ( isVisible() ) {
00099 if ( GeoBox->isVisible() ) {
00100 p.setPen( QPen( FGColor ) );
00101 if ( GrabbedBox == 1 ) {
00102 p.setPen( QPen( grabColor ) );
00103 p.drawRect( GeoBox->x(), GeoBox->y(), GeoBox->width(), GeoBox->height() );
00104 }
00105 GeoBox->draw( p, BGColor, BGMode );
00106 }
00107
00108 if ( TimeBox->isVisible() ) {
00109 p.setPen( QPen( FGColor ) );
00110 if ( GrabbedBox == 2 ) {
00111 p.setPen( QPen( grabColor ) );
00112 p.drawRect( TimeBox->x(), TimeBox->y(), TimeBox->width(), TimeBox->height() );
00113 }
00114 TimeBox->draw( p, BGColor, BGMode );
00115 }
00116
00117 if ( FocusBox->isVisible() ) {
00118 p.setPen( QPen( FGColor ) );
00119 if ( GrabbedBox == 3 ) {
00120 p.setPen( QPen( grabColor ) );
00121 p.drawRect( FocusBox->x(), FocusBox->y(), FocusBox->width(), FocusBox->height() );
00122 }
00123 FocusBox->draw( p, BGColor, BGMode );
00124 }
00125 }
00126 }
00127
00128 bool InfoBoxes::grabBox( QMouseEvent *e ) {
00129 if ( GeoBox->rect().contains( e->pos() ) ) {
00130 GrabbedBox = 1;
00131 GrabPos.setX( e->x() - GeoBox->x() );
00132 GrabPos.setY( e->y() - GeoBox->y() );
00133 return true;
00134 } else if ( TimeBox->rect().contains( e->pos() ) ) {
00135 GrabbedBox = 2;
00136 GrabPos.setX( e->x() - TimeBox->x() );
00137 GrabPos.setY( e->y() - TimeBox->y() );
00138 return true;
00139 } else if ( FocusBox->rect().contains( e->pos() ) ) {
00140 GrabbedBox = 3;
00141 GrabPos.setX( e->x() - FocusBox->x() );
00142 GrabPos.setY( e->y() - FocusBox->y() );
00143 return true;
00144 } else {
00145 GrabbedBox = 0;
00146 return false;
00147 }
00148 }
00149
00150 bool InfoBoxes::unGrabBox( void ) {
00151 if ( GrabbedBox ) {
00152 GrabbedBox = 0;
00153 checkBorders();
00154 return true;
00155 } else {
00156 return false;
00157 }
00158 }
00159
00160 bool InfoBoxes::dragBox( QMouseEvent *e ) {
00161 switch( GrabbedBox ) {
00162 case 1:
00163 GeoBox->move( e->x() - GrabPos.x(), e->y() - GrabPos.y() );
00164 fixCollisions( GeoBox );
00165 return true;
00166 break;
00167 case 2:
00168 TimeBox->move( e->x() - GrabPos.x(), e->y() - GrabPos.y() );
00169 fixCollisions( TimeBox );
00170 return true;
00171 break;
00172 case 3:
00173 FocusBox->move( e->x() - GrabPos.x(), e->y() - GrabPos.y() );
00174 fixCollisions( FocusBox );
00175 return true;
00176 break;
00177 default:
00178 return false;
00179 }
00180 }
00181
00182 bool InfoBoxes::shadeBox( QMouseEvent *e ) {
00183 if ( GeoBox->rect().contains( e->pos() ) ) {
00184 GeoBox->toggleShade();
00185 if ( GeoBox->rect().bottom() > height() ) GeoBox->move( GeoBox->x(), height() - GeoBox->height() );
00186 if ( GeoBox->rect().right() > width() ) GeoBox->move( width() - GeoBox->width(), GeoBox->y() );
00187 if ( GeoBox->anchorBottom() ) GeoBox->move( GeoBox->x(), height() - GeoBox->height() );
00188 if ( GeoBox->anchorRight() ) GeoBox->move( width() - GeoBox->width(), GeoBox->y() );
00189 fixCollisions( TimeBox );
00190 fixCollisions( FocusBox );
00191 return true;
00192 } else if ( TimeBox->rect().contains( e->pos() ) ) {
00193 TimeBox->toggleShade();
00194 if ( TimeBox->rect().bottom() > height() ) TimeBox->move( TimeBox->x(), height() - TimeBox->height() );
00195 if ( TimeBox->rect().right() > width() ) TimeBox->move( width() - TimeBox->width(), TimeBox->y() );
00196 if ( TimeBox->anchorBottom() ) TimeBox->move( TimeBox->x(), height() - TimeBox->height() );
00197 if ( TimeBox->anchorRight() ) TimeBox->move( width() - TimeBox->width(), TimeBox->y() );
00198 fixCollisions( GeoBox );
00199 fixCollisions( FocusBox );
00200 return true;
00201 } else if ( FocusBox->rect().contains( e->pos() ) ) {
00202 FocusBox->toggleShade();
00203 if ( FocusBox->rect().bottom() > height() ) FocusBox->move( FocusBox->x(), height() - FocusBox->height() );
00204 if ( FocusBox->rect().right() > width() ) FocusBox->move( width() - FocusBox->width(), FocusBox->y() );
00205 if ( FocusBox->anchorBottom() ) FocusBox->move( FocusBox->x(), height() - FocusBox->height() );
00206 if ( FocusBox->anchorRight() ) FocusBox->move( width() - FocusBox->width(), FocusBox->y() );
00207 fixCollisions( TimeBox );
00208 fixCollisions( GeoBox );
00209 return true;
00210 }
00211 return false;
00212 }
00213
00214 bool InfoBoxes::fixCollisions( InfoBox *target ) {
00215 int dLeft(0), dRight(0), dUp(0), dDown(0);
00216 QRect area = QRect( 0, 0, Width, Height );
00217 QRect t = target->rect();
00218 QRect Box1, Box2;
00219
00220
00221
00222 if ( target == GeoBox ) {
00223 if ( FocusBox->isVisible() ) Box1 = FocusBox->rect();
00224 else Box1 = QRect(0,0,0,0);
00225
00226 if ( TimeBox->isVisible() ) Box2 = TimeBox->rect();
00227 else Box2 = QRect(0,0,0,0);
00228
00229 } else if ( target == FocusBox ) {
00230 if ( GeoBox->isVisible() ) Box1 = GeoBox->rect();
00231 else Box1 = QRect(0,0,0,0);
00232
00233 if ( TimeBox->isVisible() ) Box2 = TimeBox->rect();
00234 else Box2 = QRect(0,0,0,0);
00235
00236 } else if ( target == TimeBox ) {
00237 if ( FocusBox->isVisible() ) Box1 = FocusBox->rect();
00238 else Box1 = QRect(0,0,0,0);
00239
00240 if ( GeoBox->isVisible() ) Box2 = GeoBox->rect();
00241 else Box2 = QRect(0,0,0,0);
00242
00243 } else { return false; }
00244
00245
00246
00247 if ( Box1.width() ) Box1.setCoords( Box1.left()+1, Box1.top()+1, Box1.right()-1, Box1.bottom()-1 );
00248 if ( Box2.width() ) Box2.setCoords( Box2.left()+1, Box2.top()+1, Box2.right()-1, Box2.bottom()-1 );
00249
00250
00251 if ( ! area.contains( t ) ) {
00252
00253
00254
00255
00256
00257 int x = t.x(), y = t.y();
00258
00259 if ( t.x() < area.x() ) x = area.x();
00260 if ( t.y() < area.y() ) y = area.y();
00261 if ( t.right() > area.right() ) x = area.right() - t.width();
00262 if ( t.bottom() > area.bottom() ) y = area.bottom() - t.height();
00263 target->move(x,y);
00264
00265
00266 t = target->rect();
00267 }
00268
00269 QRect upRect = t;
00270 QRect downRect = t;
00271 QRect leftRect = t;
00272 QRect rightRect = t;
00273
00274
00275 if ( t.intersects( Box1 ) || t.intersects( Box2 ) ) {
00276
00277
00278 while ( leftRect.intersects( Box1 ) || leftRect.intersects( Box2 ) ) {
00279 ++dLeft;
00280 leftRect.moveTopLeft( QPoint( t.x() - dLeft, t.y() ) );
00281 }
00282
00283 if ( !area.contains( leftRect ) ) { dLeft = 100000; }
00284
00285 while ( rightRect.intersects( Box1 ) || rightRect.intersects( Box2 ) ) {
00286 ++dRight;
00287 rightRect.moveTopLeft( QPoint( t.x() + dRight, t.y() ) );
00288 }
00289 if ( !area.contains( rightRect ) ) { dRight = 100000; }
00290
00291 while ( upRect.intersects( Box1 ) || upRect.intersects( Box2 ) ) {
00292 ++dUp;
00293 upRect.moveTopLeft( QPoint( t.x(), t.y() - dUp ) );
00294 }
00295 if ( !area.contains( upRect ) ) { dUp = 100000; }
00296
00297 while ( downRect.intersects( Box1 ) || downRect.intersects( Box2 ) ) {
00298 ++dDown;
00299 downRect.moveTopLeft( QPoint( t.x(), t.y() + dDown ) );
00300 }
00301 if ( !area.contains( downRect ) ) { dDown = 100000; }
00302
00303
00304
00305
00306
00307 int dmin = dLeft;
00308 if ( dRight < dmin ) dmin = dRight;
00309 if ( dDown < dmin ) dmin = dDown;
00310 if ( dUp < dmin ) dmin = dUp;
00311
00312 if ( dmin == 100000 ) { return false; }
00313 else if ( dmin == dLeft ) {
00314 target->move( leftRect.x(), leftRect.y() );
00315 } else if ( dmin == dRight ) {
00316 target->move( rightRect.x(), rightRect.y() );
00317 } else if ( dmin == dUp ) {
00318 target->move( upRect.x(), upRect.y() );
00319 } else if ( dmin == dDown ) {
00320 target->move( downRect.x(), downRect.y() );
00321 }
00322 }
00323 else
00324 return true;
00325
00326
00327 if ( target->rect().right() >= width()-2 ) target->setAnchorRight( true );
00328 else target->setAnchorRight( false );
00329 if ( target->rect().bottom() >= height()-2 ) target->setAnchorBottom(true);
00330 else target->setAnchorBottom(false);
00331
00332
00333
00334 if ( area.contains( target->rect() ) ) return true;
00335 else return false;
00336 }
00337
00338 bool InfoBoxes::timeChanged( const KStarsDateTime &ut, const KStarsDateTime <, dms *lst ) {
00339 QString ot1 = TimeBox->text1();
00340 QString ot2 = TimeBox->text2();
00341 QString ot3 = TimeBox->text3();
00342
00343 TimeBox->setText1( i18n( "Local Time", "LT: " ) + lt.time().toString()
00344 + " " + lt.date().toString( "%d %b %Y" ) );
00345 TimeBox->setText2( i18n( "Universal Time", "UT: " ) + ut.time().toString()
00346 + " " + ut.date().toString( "%d %b %Y" ) );
00347
00348 QString STString;
00349 STString = STString.sprintf( "%02d:%02d:%02d ", lst->hour(), lst->minute(), lst->second() );
00350
00351
00352
00353 QString JDString = QString::number( ut.djd(), 'f', 2 );
00354 JDString.replace( ".", KGlobal::locale()->decimalSymbol() );
00355
00356 TimeBox->setText3( i18n( "Sidereal Time", "ST: " ) + STString +
00357 i18n( "Julian Day", "JD: " ) + JDString );
00358
00359 if ( ot1 == TimeBox->text1() && ot2 == TimeBox->text2() &&
00360 ot3 == TimeBox->text3() )
00361 return false;
00362 else {
00363 checkBorders();
00364 return true;
00365 }
00366 }
00367
00368 bool InfoBoxes::geoChanged(const GeoLocation *geo) {
00369 QString ot1 = GeoBox->text1();
00370 QString ot2 = GeoBox->text2();
00371
00372 QString name = geo->translatedName() + ", ";
00373 if ( ! geo->province().isEmpty() ) name += geo->translatedProvince() + ", ";
00374 name += geo->translatedCountry();
00375 GeoBox->setText1( name );
00376
00377 GeoBox->setText2( i18n( "Longitude", "Long:" ) + " " +
00378 KGlobal::locale()->formatNumber( geo->lng()->Degrees(),3) + " " +
00379 i18n( "Latitude", "Lat:" ) + " " +
00380 KGlobal::locale()->formatNumber( geo->lat()->Degrees(),3) );
00381
00382 if ( ot1 == GeoBox->text1() && ot2 == GeoBox->text2() )
00383 return false;
00384 else {
00385 checkBorders();
00386 return true;
00387 }
00388 }
00389
00390 bool InfoBoxes::focusObjChanged( const QString &n ) {
00391 QString ot1 = FocusBox->text1();
00392
00393 FocusBox->setText1( i18n( "Focused on: " ) + n );
00394 if ( ot1 == FocusBox->text1() ) return false;
00395 else {
00396 checkBorders();
00397 return true;
00398 }
00399 }
00400
00401 bool InfoBoxes::focusCoordChanged(const SkyPoint *p) {
00402 QString ot2 = FocusBox->text2();
00403 QString ot3 = FocusBox->text3();
00404
00405 FocusBox->setText2( i18n( "Right Ascension", "RA" ) + ": " + p->ra()->toHMSString() +
00406 " " + i18n( "Declination", "Dec" ) + ": " + p->dec()->toDMSString(true) );
00407 FocusBox->setText3( i18n( "Azimuth", "Az" ) + ": " + p->az()->toDMSString(true) +
00408 " " + i18n( "Altitude", "Alt" ) + ": " + p->alt()->toDMSString(true) );
00409
00410 if ( ot2 == FocusBox->text2() && ot3 == FocusBox->text3() )
00411 return false;
00412 else {
00413 checkBorders();
00414 return true;
00415 }
00416 }
00417
00418 void InfoBoxes::checkBorders(bool resetToDefault) {
00419 if (resetToDefault) {
00420 TimeBox->setAnchorFlag( InfoBox::AnchorNone );
00421 GeoBox->setAnchorFlag( InfoBox::AnchorNone );
00422 FocusBox->setAnchorFlag( InfoBox::AnchorNone );
00423 }
00424
00425 if ( GeoBox->rect().right() >= Width-2 ) GeoBox->setAnchorRight( true );
00426 if ( TimeBox->rect().right() >= Width-2 ) TimeBox->setAnchorRight( true );
00427 if ( FocusBox->rect().right() >= Width-2 ) FocusBox->setAnchorRight( true );
00428 if ( GeoBox->rect().bottom() >= Height-2 ) GeoBox->setAnchorBottom( true );
00429 if ( TimeBox->rect().bottom() >= Height-2 ) TimeBox->setAnchorBottom( true );
00430 if ( FocusBox->rect().bottom() >= Height-2 ) FocusBox->setAnchorBottom( true );
00431
00432 if ( GeoBox->anchorRight() ) GeoBox->move( Width, GeoBox->y() );
00433 if ( TimeBox->anchorRight() ) TimeBox->move( Width, TimeBox->y() );
00434 if ( FocusBox->anchorRight() ) FocusBox->move( Width, FocusBox->y() );
00435 if ( GeoBox->anchorBottom() ) GeoBox->move( GeoBox->x(), Height );
00436 if ( TimeBox->anchorBottom() ) TimeBox->move( TimeBox->x(), Height );
00437 if ( FocusBox->anchorBottom() ) FocusBox->move( FocusBox->x(), Height );
00438 }
00439
00440 #include "infoboxes.moc"