summaryrefslogtreecommitdiffstats
path: root/kalzium/src/kalziumtip.cpp
blob: c7b3ff26ec5e77ecb0cbeb0237755f6804db09ee (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
/***************************************************************************
 *   Copyright (C) 2005 by Zack Rusin <zrusin@kde.org>			   *
 *  Sami Kyostil <skyostil@kempele.fi>			 		   *
 *   Aaron J. Seigo <aseigo@kde.org>		 			   *
 *		     Martin Pfeiffer <hubipete@gmx.net>                    *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "kalziumtip.h"
#include "kalziumutils.h"
#include "element.h"

#include <tqapplication.h>
#include <tqpixmap.h>
#include <tqimage.h>
#include <tqsimplerichtext.h>
#include <tqpainter.h>
#include <tqdesktopwidget.h>

#include <kdialog.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kstandarddirs.h>

KalziumTip::KalziumTip( TQWidget * parent, const char * name, WFlags f ) : TQWidget( parent, name, f ) 
{
	setFocusPolicy(TQ_NoFocus); //the widget don't get the keyboard focus
	setBackgroundMode(NoBackground); // widget has no background
	resize(0,0);
	hide(); //initailly hide it
	m_richText = 0;
	m_noElemIcon = TDEGlobal::iconLoader()->loadIcon( "orbits", TDEIcon::NoGroup, 64 );
	setMouseTracking(true); // receice mouse move events
	
	connect(&m_frameTimer, TQT_SIGNAL(timeout()), TQT_SLOT(internalUpdate()));
}

void KalziumTip::showTip( TQPoint mouse, Element* element, int visibleWidth, int visibleHeight )
{
	TQWidget *p = 0;
        if ( dynamic_cast<TQWidget*>( parent() ) )
                p = TQT_TQWIDGET( parent() );

        if ( p )
        {
	 	if ( mouse.y() + size().height() > visibleHeight )
                        mouse.setY( mouse.y() - size().height() );
               	if ( mouse.x() + size().width() > visibleWidth )
                       	mouse.setX( mouse.x() - size().width() );
	
		if ( p->width() < visibleWidth )
		{	 
                	if ( mouse.x() + size().width() > p->width() )
                        	mouse.setX( mouse.x() - size().width() );
		}

		if ( p->height() < visibleHeight ) 
		{
			if ( mouse.y() + size().height() > p->height() )
                        	mouse.setY( mouse.y() - size().height() );
		}
        }
	
	m_mousePointer = mouse;
	
	if( element == m_tippedElement )
	{
		// Avoid moving out of the current screen
		if (m_mousePointer.x()+width() > tqApp->desktop()->width())
				m_mousePointer.setX(tqApp->desktop()->width() - width());

		move(m_mousePointer); //do not paint again if already painted
	}
	else
	{
		if ( element == 0)
			return;	
	
		m_tippedElement = element;
		
		loadIcon(); //load icon	
		display();
	}
}

void KalziumTip::paintEvent(TQPaintEvent* e)
{
	if (m_dirty)
    	{
        	displayInternal();
        	m_dirty = false;
    	}

	TQPainter p(this);
   	p.drawPixmap(e->rect().topLeft(), m_pixmap, e->rect());
}

void KalziumTip::mouseMoveEvent(TQMouseEvent * e)
{
	// delegate the mouse move event to the parent (actually the elements table)
	// so that this tooltip doesn't stop to be updated
	e->ignore();
}

void KalziumTip::display()
{
	if( !m_tippedElement )
		return;

	delete m_richText;
	
	TQString elementname = m_tippedElement->elname();
	
	TQString number = i18n( "Number: %1" )
			.arg( TQString::number(m_tippedElement->number()) );
	
	TQString mass = i18n( "Mass: %1" )
			.arg( KalziumUtils::localizedValue(m_tippedElement->mass(), 6) );

	m_richText = new TQSimpleRichText("<qt><h1>" + elementname + "</h1><p>"
						    + number + "</p><p>"
						    + mass  +"</p></qt>", font());

	m_richText->setWidth(400);

	m_maskEffect = isVisible() ? Plain : Dissolve;
    	m_dissolveSize = 24;
    	m_dissolveDelta = -1;

    	displayInternal();
	m_frameTimer.start(500/30);	

	move(m_mousePointer);
	show();
}

void KalziumTip::displayInternal()
{
    	if (!m_richText)
        	return;
    	

	// determine text rectangel sizes
	TQRect textRect(0,0,0,0);
	textRect.setWidth(m_richText->widthUsed());
	textRect.setHeight(m_richText->height());

	//moves the rect
	textRect.moveBy(-textRect.left(), -textRect.top());
	// add the new coordinates
	textRect.addCoords(0,0,2,2);

	int margin = KDialog::marginHint();
	int height = TQMAX(m_icon.height(), textRect.height()) + 2 * margin;
    	int textX = 2 + m_icon.width() + 2 * margin;
    	int width = textX + textRect.width() + margin;
    	int textY = (height - textRect.height()) / 2;

	//resize pixmap, mask and widget
	m_mask.resize(width, height);
	m_pixmap.resize(width, height);
	resize(width, height);

	// create and set transparency mask
    	switch(m_maskEffect)
    	{
        	case Plain:
            		plainMask();
            		break;

        	case Dissolve:
            		dissolveMask();
            		break;
    	}

	// draw background
    	TQPainter bufferPainter(&m_pixmap);
    	bufferPainter.setPen(TQt::black);
    	bufferPainter.setBrush(backgroundColor());
    	bufferPainter.drawRoundRect(0, 0, width, height,
                                1600 / width, 1600 / height);

    	// draw icon if present
    	if (!m_icon.isNull())
    	{
        	bufferPainter.drawPixmap(margin,
                                 	margin,
                                 	m_icon, 0, 0,
                                 	m_icon.width(), m_icon.height());
    	}
	
	// draw text shadow
        TQColorGroup cg = colorGroup();
        cg.setColor(TQColorGroup::Text, cg.background().dark(115));
        int shadowOffset = TQApplication::reverseLayout() ? -1 : 1;
        m_richText->draw(&bufferPainter, 5 + textX + shadowOffset, textY + 1, TQRect(), cg);

        // draw text
        cg = colorGroup();
        m_richText->draw(&bufferPainter, 5 + textX, textY, rect(), cg);
}

void KalziumTip::dissolveMask()
{
    TQPainter maskPainter(&m_mask);

    m_mask.fill(TQt::black);

    maskPainter.setBrush(TQt::white);
    maskPainter.setPen(TQt::white);
    maskPainter.drawRoundRect(m_mask.rect(), 1600 / m_mask.rect().width(),
                              1600 / m_mask.rect().height());

    m_dissolveSize += m_dissolveDelta;

    if (m_dissolveSize > 0)
    {
        maskPainter.setRasterOp(TQt::EraseROP);

        int x, y, s;
        const int size = 16;
	const int heightsize = size + height();

        for (y = 0; y < heightsize; y += size)
        {
            x = width();
            s = m_dissolveSize * x / 128;
            for (; x > -size; x -= size, s -= 2)
            {
                if (s < 0)
                {
                    s = 0;
                }
                maskPainter.drawEllipse(x - s / 2, y - s / 2, s, s);
            }
        }
    }
    else if (m_dissolveSize < 0)
    {
        m_frameTimer.stop();
        m_dissolveDelta = 1;
    }

    setMask(m_mask);
}

void KalziumTip::hide()
{
	m_frameTimer.stop();
    	m_tippedElement = 0;
    	TQWidget::hide();
}

void KalziumTip::plainMask()
{
    	TQPainter maskPainter(&m_mask);

    	m_mask.fill(TQt::black);

    	maskPainter.setBrush(TQt::white);
    	maskPainter.setPen(TQt::white);
    	maskPainter.drawRoundRect(m_mask.rect(), 1600 / m_mask.rect().width(),
                              1600 / m_mask.rect().height());
    	setMask(m_mask);
    	m_frameTimer.stop();
}

void KalziumTip::internalUpdate()
{
	m_dirty = true;
    	update();
}

void KalziumTip::loadIcon()
{
	TQString iconpath = locate(  "data" , "kalzium/elempics/" + m_tippedElement->symbol() + ".jpg" );
	if ( !iconpath.isEmpty() )
	{
		TQImage img ( iconpath, "JPEG" );
		img = img.smoothScale ( 128, 128, TQ_ScaleMin );
		m_icon.convertFromImage( img );

	}
	else
	{
		m_icon = m_noElemIcon;
	}
}

#include "kalziumtip.moc"