summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/infobox.cpp
blob: 781500bd638dee16866d06c6e40477369ac03ce8 (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
/***************************************************************************
                          infobox.cpp  -  description
                             -------------------
    begin                : Thu May 30 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 <kdebug.h>
#include <tqpainter.h>

#include "infobox.h"

InfoBox::InfoBox(){
	setText1( "" );
	setText2( "" );
	setText3( "" );
	move( 0, 0 );
//Initialize text dimension variables to 0
	FullTextWidth  = 0;
	FullTextHeight = 0;
	ShadedTextWidth  = 0;
	ShadedTextHeight = 0;

	AnchorFlag = AnchorNone;
	Visible = true;
	Shaded = false;
}

InfoBox::InfoBox( int x, int y, bool shade, TQString t1, TQString t2, TQString t3 ) {
	setText1( t1 );
	setText2( t2 );
	setText3( t3 );
	move( x, y );
	Shaded = shade;
//Initialize text dimension variables to 0
	FullTextWidth  = 0;
	FullTextHeight = 0;
	ShadedTextWidth  = 0;
	ShadedTextHeight = 0;

	AnchorFlag = AnchorNone;
	Visible = true;
}

InfoBox::InfoBox( TQPoint pt, bool shade, TQString t1, TQString t2, TQString t3 ) {
	setText1( t1 );
	setText2( t2 );
	setText3( t3 );
	move( pt );
	Shaded = shade;
//Initialize text dimension variables to 0
	FullTextWidth  = 0;
	FullTextHeight = 0;
	ShadedTextWidth  = 0;
	ShadedTextHeight = 0;

	AnchorFlag = AnchorNone;
	Visible = true;
}

InfoBox::~InfoBox(){
}

void InfoBox::setAnchorRight( const bool ar ) {
	if ( ar ) setAnchorFlag( anchorFlag() | AnchorRight  );
	else      setAnchorFlag( anchorFlag() & ~AnchorRight );
}

void InfoBox::setAnchorBottom( const bool ab ) {
	if ( ab ) setAnchorFlag( anchorFlag() | AnchorBottom  );
	else      setAnchorFlag( anchorFlag() & ~AnchorBottom );
}

bool InfoBox::toggleShade() {
	Shaded = !Shaded;
	updateSize();

	emit shaded( Shaded );
	return Shaded;
}

void InfoBox::updateSize() {
	int dh = ShadedTextHeight/2;
//	kdDebug() << "Full=" << FullTextWidth + 2*padx() << "," << FullTextHeight - dh + 2*pady() << endl;
	if ( Shaded ) resize( ShadedTextWidth + 2*padx(), ShadedTextHeight - dh + 2*pady() );
	else resize( FullTextWidth + 2*padx(), FullTextHeight - dh + 2*pady() );
//	kdDebug() << "Size=" << Size.width() << "," << Size.height() << endl;
}

bool InfoBox::constrain( TQRect r, bool inside ) {
	if ( inside ) {
		//Place InfoBox within TQRect r:
		if ( x() < r.x() ) move( r.x(), y() );
		if ( y() < r.y() ) move( x(), r.y() );
		if ( x() + width() > r.right() ) move( r.right() - width(), y() );
		if ( y() + height() > r.bottom() ) move( x(), r.bottom() - height() );
		//The InfoBox is now within the bounds of TQRect r, unless it is bigger than r.
		//In that case, we cannot obey the constraint, but the current position is as
		//close as we can get.  Return false in this case.
		if ( width() > r.width() || height() > r.height() ) return false;
		else return true;
	} else {
//FIXME...
		//Place InfoBox outside TQRect r.  First, determine if InfoBox is within r:
//		if ( rect().intersects( r ) ) {
			//Move the InfoBox in all four directions until it no longer intersects r.
			//Determine which displacement is shortest
			//
		return false;
	}
}

void InfoBox::draw( TQPainter &p, TQColor BGColor, unsigned int BGMode ) {
	TQRect r;
	int w,h;

	r = p.boundingRect( x(), y(), p.window().width(), p.window().height(), TQt::AlignCenter, text1() );
	ShadedTextWidth  = r.width();
	ShadedTextHeight = r.height();

	w = ShadedTextWidth;
	h = ShadedTextHeight;

	if ( !text2().isEmpty() ) {
		r = p.boundingRect( x(), y(), p.window().width(), p.window().height(), TQt::AlignCenter, text2() );
		if ( r.width() > w ) w = r.width();
		h += r.height();
	}

	if ( !text3().isEmpty() ) {
		r = p.boundingRect( x(), y(), p.window().width(), p.window().height(), TQt::AlignCenter, text3() );
		if ( r.width() > w ) w = r.width();
		h += r.height();
	}

	FullTextWidth = w;
	FullTextHeight = h;

	updateSize();
	constrain( TQRect( 0, 0, p.window().width(), p.window().height() ) );

//Draw the box boundary and the text
	if ( BGMode==1 ) p.fillRect( x(), y(), width(), height(), TQBrush( BGColor, Dense4Pattern ) );
	if ( BGMode==2 ) p.fillRect( x(), y(), width(), height(), TQBrush( BGColor ) );

	p.drawText( x() + padx(), y() + ShadedTextHeight/2 + pady(), text1() );

	if ( !Shaded ) {
		if ( !text2().isEmpty() ) p.drawText( x() + padx(), y() + 3*ShadedTextHeight/2 + pady(), text2() );
		if ( !text3().isEmpty() ) p.drawText( x() + padx(), y() + 5*ShadedTextHeight/2 + pady(), text3() );
	}
}

TQRect InfoBox::rect() const {
	return TQRect( pos(), size() );
}

void InfoBox::move( int x, int y ) {
	Pos.setX( x ); Pos.setY( y ); emit moved( TQPoint(x,y) );
}

void InfoBox::move( TQPoint p ) {
	move( p.x(), p.y() ); emit moved( TQPoint( p.x(), p.y() ) );
}

#include "infobox.moc"