summaryrefslogtreecommitdiffstats
path: root/kalzium/src/detailedgraphicaloverview.cpp
blob: 56c086ff87fed980a4b40fb374ba246adddb706d (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
/***************************************************************************

copyright            : (C) 2004, 2005 by Carsten Niehaus
email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "detailedgraphicaloverview.h"
#include "element.h"
#include "kalziumutils.h"

//KDE-Includes
#include <kdebug.h>
#include <klocale.h>
#include <kglobalsettings.h>
#include <kglobal.h>

//QT-Includes
#include <qpainter.h>
#include <qstring.h>
#include <qpixmap.h>
#include <qrect.h>

DetailedGraphicalOverview::DetailedGraphicalOverview( Element *el, QWidget *parent, const char *name ) 
: QWidget( parent, name )
{
	init( el );
}

DetailedGraphicalOverview::DetailedGraphicalOverview( QWidget *parent, const char *name ) 
: QWidget( parent, name )
{
	init( 0L );
}

void DetailedGraphicalOverview::init( Element *el )
{
	setBackgroundMode( NoBackground );

	m_element = el;
	setMinimumSize( 300, 200 );
	update();
}

void DetailedGraphicalOverview::paintEvent( QPaintEvent* )
{
	int h = height();
	int w = width();

	QPixmap pm( w, h );

	QPainter p;
	p.begin( &pm );

	p.setBrush(Qt::SolidPattern);

	if ( !m_element )
	{
		pm.fill( paletteBackgroundColor() );
		p.drawText( 0, 0, w, h, Qt::AlignCenter | Qt::WordBreak, i18n( "No element selected" ) );
	}
	else
	{

		h_t = 20; //height of the texts

		x1 =  0;
		y1 =  0;

		x2 = w;
		y2 = h;

		p.setBrush( m_element->elementColor() );
		p.drawRect( x1 , y1 , x2 , y2 );

		p.setBrush( Qt::black );
		p.setBrush(Qt::NoBrush);

		QFont fA = KGlobalSettings::generalFont();
		QFont fB = KGlobalSettings::generalFont();
		QFont fC = KGlobalSettings::generalFont();

		QString strLocalizedMass = KalziumUtils::localizedValue( m_element->mass(), 6 );

		fA.setPointSize( fA.pointSize() + 20 ); //Huge font
		fA.setBold( true );
		fB.setPointSize( fB.pointSize() + 6 ); //Big font
		fC.setPointSize( fC.pointSize() + 4 ); //Big font
		fC.setBold( true );
		QFontMetrics fmA = QFontMetrics( fA );
		QFontMetrics fmB = QFontMetrics( fB );

		//coordinates for element symbol: near the center
		int xA = 4 * w / 10;
		int yA = h / 2;

		//coordinates for the atomic number: offset from element symbol to the upper left
		int xB = xA - fmB.width( QString::number( m_element->number() ) );
		int yB = yA - fmA.height() + fmB.height();

		//Element Symbol
		p.setFont( fA );
		p.drawText( xA, yA , m_element->symbol() ); 

		//Atomic number
		p.setFont( fB );
		p.drawText( xB, yB, QString::number( m_element->number() ));

		QRect rect( 0, 20, w/2, h );
		
		p.setFont( fC );
				
		int size = KalziumUtils::maxSize(m_element->elname(), rect , fC, &p);
		int size2 = KalziumUtils::maxSize(m_element->oxstage(), rect, fC, &p);
		int size3 = KalziumUtils::maxSize( strLocalizedMass , rect , fC, &p);

		//Name and other data
		fC.setPointSize( size );
		p.setFont( fC );
		
		//Name
		p.drawText( 1, 0, w/2, h, Qt::AlignLeft, m_element->elname() );
		
		//Oxidationstates
		fC.setPointSize( size2 );
		p.setFont( fC );
		int offsetOx = KalziumUtils::StringHeight( strLocalizedMass , fC, &p );
		p.drawText( 1, h-offsetOx, w/2, offsetOx, Qt::AlignLeft, m_element->oxstage() );

		//Mass
		fC.setPointSize( size3 );
		p.setFont( fC );
		
		int offset = KalziumUtils::StringHeight( strLocalizedMass, fC, &p );
		p.drawText( w/2, h-offset, w/2, offset, Qt::AlignRight, strLocalizedMass );

		drawBiologicalSymbol( &p );
	}

	p.end();


	bitBlt( this, 0, 0, &pm );
}

void DetailedGraphicalOverview::drawBiologicalSymbol( QPainter *p )
{
	if ( !m_element ) return;
	const int db = h_t;        //diameter of the big circle
	const int ds = db/2;      //diameter of the inner circle

	int d_ds = ( db/2 )-( ds/2 ); //the delta-x/y of the inner circle

	int pos_x = width() - 4 - db;
	int pos_y = 4;

	if ( m_element->biological() > 0 )
	{
		const int radius = db + 8;
		p->setBrush( Qt::SolidPattern );
		p->setBrush( Qt::white );
		p->setPen( Qt::black );
		p->drawRoundRect( QRect( width() - radius, 
					-radius, 
					2 * radius, 
					2 * radius ), 70, 70 );
	}
	
	switch ( m_element->biological() )
	{
		case 0:        //nothing
			break;
		case 1:        //red, red
			p->setBrush( Qt::red );
			p->setBrush(Qt::NoBrush);
			p->setPen( Qt::red );
			p->drawEllipse( pos_x,pos_y,db,db );
			p->setBrush(Qt::SolidPattern);
			p->setBrush( Qt::red );
			p->drawEllipse( pos_x+d_ds, pos_y+d_ds, ds, ds );
			break;
		case 2:        //green, red
			p->setBrush( Qt::red );
			p->setBrush(Qt::NoBrush);
			p->setPen( Qt::red );
			p->drawEllipse( pos_x,pos_y,db,db );
			p->setBrush(Qt::SolidPattern);
			p->setBrush( Qt::green );
			p->setPen( Qt::green );
			p->drawEllipse( pos_x+d_ds, pos_y+d_ds, ds, ds );
			break;
		case 3:        //green
			p->setBrush(Qt::SolidPattern);
			p->setBrush( Qt::green );
			p->setPen( Qt::green );
			p->drawEllipse( pos_x+d_ds, pos_y+d_ds, ds, ds );
			break;
		case 4:        //green, blue
			p->setBrush( Qt::blue );
			p->setBrush(Qt::NoBrush);
			p->setPen( Qt::blue );
			p->drawEllipse( pos_x,pos_y,db,db );
			p->setBrush(Qt::SolidPattern);
			p->setBrush( Qt::green );
			p->setPen( Qt::green );
			p->drawEllipse( pos_x+d_ds, pos_y+d_ds, ds, ds );
			break;
		case 5:        //blue
			p->setBrush(Qt::SolidPattern);
			p->setBrush( Qt::blue );
			p->setPen( Qt::blue );
			p->drawEllipse( pos_x+d_ds, pos_y+d_ds, ds, ds );
			break;
		case 6:        //blue, blue
			p->setBrush( Qt::blue );
			p->setBrush(Qt::NoBrush);
			p->setPen( Qt::blue );
			p->drawEllipse( pos_x,pos_y,db,db );
			p->setBrush(Qt::SolidPattern);
			p->drawEllipse( pos_x+d_ds, pos_y+d_ds, ds, ds );
			break;
	}
}

#include "detailedgraphicaloverview.moc"