moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / infobox.cpp
blob0b14b0cc4ece47f3dbce1e26b97127107e1adbca
1 /***************************************************************************
2 infobox.cpp - description
3 -------------------
4 begin : Thu May 30 2002
5 copyright : (C) 2002 by Jason Harris
6 email : jharris@30doradus.org
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <kdebug.h>
19 #include <qpainter.h>
21 #include "infobox.h"
23 InfoBox::InfoBox(){
24 setText1( "" );
25 setText2( "" );
26 setText3( "" );
27 move( 0, 0 );
28 //Initialize text dimension variables to 0
29 FullTextWidth = 0;
30 FullTextHeight = 0;
31 ShadedTextWidth = 0;
32 ShadedTextHeight = 0;
34 AnchorFlag = AnchorNone;
35 Visible = true;
36 Shaded = false;
39 InfoBox::InfoBox( int x, int y, bool shade, QString t1, QString t2, QString t3 ) {
40 setText1( t1 );
41 setText2( t2 );
42 setText3( t3 );
43 move( x, y );
44 Shaded = shade;
45 //Initialize text dimension variables to 0
46 FullTextWidth = 0;
47 FullTextHeight = 0;
48 ShadedTextWidth = 0;
49 ShadedTextHeight = 0;
51 AnchorFlag = AnchorNone;
52 Visible = true;
55 InfoBox::InfoBox( QPoint pt, bool shade, QString t1, QString t2, QString t3 ) {
56 setText1( t1 );
57 setText2( t2 );
58 setText3( t3 );
59 move( pt );
60 Shaded = shade;
61 //Initialize text dimension variables to 0
62 FullTextWidth = 0;
63 FullTextHeight = 0;
64 ShadedTextWidth = 0;
65 ShadedTextHeight = 0;
67 AnchorFlag = AnchorNone;
68 Visible = true;
71 InfoBox::~InfoBox(){
74 void InfoBox::setAnchorRight( const bool ar ) {
75 if ( ar ) setAnchorFlag( anchorFlag() | AnchorRight );
76 else setAnchorFlag( anchorFlag() & ~AnchorRight );
79 void InfoBox::setAnchorBottom( const bool ab ) {
80 if ( ab ) setAnchorFlag( anchorFlag() | AnchorBottom );
81 else setAnchorFlag( anchorFlag() & ~AnchorBottom );
84 bool InfoBox::toggleShade() {
85 Shaded = !Shaded;
86 updateSize();
88 emit shaded( Shaded );
89 return Shaded;
92 void InfoBox::updateSize() {
93 int dh = ShadedTextHeight/2;
94 // kdDebug() << "Full=" << FullTextWidth + 2*padx() << "," << FullTextHeight - dh + 2*pady() << endl;
95 if ( Shaded ) resize( ShadedTextWidth + 2*padx(), ShadedTextHeight - dh + 2*pady() );
96 else resize( FullTextWidth + 2*padx(), FullTextHeight - dh + 2*pady() );
97 // kdDebug() << "Size=" << Size.width() << "," << Size.height() << endl;
100 bool InfoBox::constrain( QRect r, bool inside ) {
101 if ( inside ) {
102 //Place InfoBox within QRect r:
103 if ( x() < r.x() ) move( r.x(), y() );
104 if ( y() < r.y() ) move( x(), r.y() );
105 if ( x() + width() > r.right() ) move( r.right() - width(), y() );
106 if ( y() + height() > r.bottom() ) move( x(), r.bottom() - height() );
107 //The InfoBox is now within the bounds of QRect r, unless it is bigger than r.
108 //In that case, we cannot obey the constraint, but the current position is as
109 //close as we can get. Return false in this case.
110 if ( width() > r.width() || height() > r.height() ) return false;
111 else return true;
112 } else {
113 //FIXME...
114 //Place InfoBox outside QRect r. First, determine if InfoBox is within r:
115 // if ( rect().intersects( r ) ) {
116 //Move the InfoBox in all four directions until it no longer intersects r.
117 //Determine which displacement is shortest
119 return false;
123 void InfoBox::draw( QPainter &p, QColor BGColor, unsigned int BGMode ) {
124 QRect r;
125 int w,h;
127 r = p.boundingRect( x(), y(), p.window().width(), p.window().height(), Qt::AlignCenter, text1() );
128 ShadedTextWidth = r.width();
129 ShadedTextHeight = r.height();
131 w = ShadedTextWidth;
132 h = ShadedTextHeight;
134 if ( !text2().isEmpty() ) {
135 r = p.boundingRect( x(), y(), p.window().width(), p.window().height(), Qt::AlignCenter, text2() );
136 if ( r.width() > w ) w = r.width();
137 h += r.height();
140 if ( !text3().isEmpty() ) {
141 r = p.boundingRect( x(), y(), p.window().width(), p.window().height(), Qt::AlignCenter, text3() );
142 if ( r.width() > w ) w = r.width();
143 h += r.height();
146 FullTextWidth = w;
147 FullTextHeight = h;
149 updateSize();
150 constrain( QRect( 0, 0, p.window().width(), p.window().height() ) );
152 //Draw the box boundary and the text
153 if ( BGMode==1 ) p.fillRect( x(), y(), width(), height(), QBrush( BGColor, Dense4Pattern ) );
154 if ( BGMode==2 ) p.fillRect( x(), y(), width(), height(), QBrush( BGColor ) );
156 p.drawText( x() + padx(), y() + ShadedTextHeight/2 + pady(), text1() );
158 if ( !Shaded ) {
159 if ( !text2().isEmpty() ) p.drawText( x() + padx(), y() + 3*ShadedTextHeight/2 + pady(), text2() );
160 if ( !text3().isEmpty() ) p.drawText( x() + padx(), y() + 5*ShadedTextHeight/2 + pady(), text3() );
164 QRect InfoBox::rect() const {
165 return QRect( pos(), size() );
168 void InfoBox::move( int x, int y ) {
169 Pos.setX( x ); Pos.setY( y ); emit moved( QPoint(x,y) );
172 void InfoBox::move( QPoint p ) {
173 move( p.x(), p.y() ); emit moved( QPoint( p.x(), p.y() ) );
176 #include "infobox.moc"