Test change - can I push OK?
[kdeedu-porting.git] / kalzium / src / detailedgraphicaloverview.cpp
bloba9b93b70bf516a1270bc432e01cd7d06ad9fa580
1 /***************************************************************************
3 copyright : (C) 2004, 2005, 2006, 2007 by Carsten Niehaus
4 email : cniehaus@kde.org
5 ***************************************************************************/
7 /***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
16 #include "detailedgraphicaloverview.h"
17 #include "kalziumdataobject.h"
18 #include "kalziumutils.h"
20 //KDE-Includes
21 #include <kdebug.h>
22 #include <klocale.h>
23 #include <kglobalsettings.h>
24 #include <kglobal.h>
25 #include <kstandarddirs.h>
27 //QT-Includes
28 #include <QFile>
29 #include <QPainter>
30 #include <QSvgRenderer>
31 #include <QRect>
33 #include <element.h>
34 #include "prefs.h"
36 DetailedGraphicalOverview::DetailedGraphicalOverview( QWidget *parent )
37 : QWidget( parent )
39 setAttribute( Qt::WA_NoBackground, true );
41 m_element = 0;
42 setMinimumSize( 300, 200 );
44 // last operation: setting the background color and scheduling an update()
45 setBackgroundColor( QColor() );
48 void DetailedGraphicalOverview::setElement( int el )
50 m_element = KalziumDataObject::instance()->element( el );
51 update();
54 void DetailedGraphicalOverview::setBackgroundColor( const QColor& bgColor )
56 m_backgroundColor = bgColor.isValid() ? bgColor : Qt::green;
58 //this check is needed because a QBrush( QPixmap() ) constructs
59 //with a black brush. But black is a really bad color here ...
60 if ( bgColor == QColor( 0, 0, 0 ) )
61 m_backgroundColor = Qt::white;
63 update();
66 void DetailedGraphicalOverview::paintEvent( QPaintEvent* )
68 int h = height();
69 int w = width();
71 QPixmap pm( w, h );
73 QPainter p;
74 p.begin( &pm );
76 p.setBrush(Qt::SolidPattern);
78 if ( !m_element )
80 pm.fill( palette().background().color() );
81 p.drawText( 0, 0, w, h, Qt::AlignCenter | Qt::TextWordWrap, i18n( "No element selected" ) );
82 } else if ( Prefs::colorschemebox() == 2) { //The iconic view is the 3rd view (0,1,2,...)
83 pm.fill( palette().background().color() );
85 QString pathname = KGlobal::dirs()->findResourceDir( "appdata", "data/iconsets/" ) + "data/iconsets/";
87 int enumii = m_element->dataAsVariant( ChemicalDataObject::atomicNumber ).toInt();
89 QString filename = pathname + "school" + '/' + QString::number( enumii ) + ".svg";
91 QSvgRenderer svgrenderer;
92 if ( QFile::exists(filename) && svgrenderer.load(filename) ) {
93 QSize size = svgrenderer.defaultSize();
94 size.scale( w, h, Qt::KeepAspectRatio );
96 QRect bounds( QPoint( 0, 0 ), size );
97 bounds.moveCenter( QPoint( w/2, h/2 ) );
98 svgrenderer.render( &p, bounds );
99 } else {
100 p.drawText( 0, 0, w, h, Qt::AlignCenter | Qt::TextWordWrap, i18n( "No graphic found" ) );
102 } else
104 h_t = 20; //height of the texts
106 x1 = 0;
107 y1 = 0;
109 x2 = w;
110 y2 = h;
112 p.setBrush( m_backgroundColor );
113 p.drawRect( x1, y1, x2 - 1, y2 - 1 );
115 p.setBrush( Qt::black );
116 p.setBrush(Qt::NoBrush);
118 QFont fA = KGlobalSettings::generalFont();
119 QFont fB = KGlobalSettings::generalFont();
120 QFont fC = KGlobalSettings::generalFont();
122 fA.setPointSize( fA.pointSize() + 20 ); //Huge font
123 fA.setBold( true );
124 fB.setPointSize( fB.pointSize() + 6 ); //Big font
125 fC.setPointSize( fC.pointSize() + 4 ); //Big font
126 fC.setBold( true );
127 QFontMetrics fmA = QFontMetrics( fA );
128 QFontMetrics fmB = QFontMetrics( fB );
130 //coordinates for element symbol: near the center
131 int xA = 4 * w / 10;
132 int yA = h / 2;
134 //coordinates for the atomic number: offset from element symbol to the upper left
135 int xB = xA - fmB.width( m_element->dataAsString( ChemicalDataObject::atomicNumber ) );
136 int yB = yA + fmB.height()/2;
138 //Element Symbol
139 p.setFont( fA );
140 p.drawText( xA, yA , m_element->dataAsString( ChemicalDataObject::symbol) );
142 //Atomic number
143 p.setFont( fB );
144 p.drawText( xB, yB, m_element->dataAsString( ChemicalDataObject::atomicNumber ) );
146 QRect rect( 0, 20, w/2, h );
148 p.setFont( fC );
150 int size = KalziumUtils::maxSize(m_element->dataAsString( ChemicalDataObject::name), rect , fC, &p);
153 //Name and other data
154 fC.setPointSize( size );
155 p.setFont( fC );
157 //Name
158 p.drawText( 1, 0, w/2, h, Qt::AlignLeft, m_element->dataAsString( ChemicalDataObject::name) );
160 //Oxidationstates
161 p.setFont( fC );
163 //Mass
164 QString massString = i18nc( "For example '1.0079u', the mass of an element in units", "%1 u", m_element->dataAsString( ChemicalDataObject::mass ) );
165 int size3 = KalziumUtils::maxSize( massString, rect , fC, &p);
166 fC.setPointSize( size3 );
167 p.setFont( fC );
168 int offset = KalziumUtils::StringHeight( massString, fC, &p );
169 p.drawText( w/2,
170 h-offset,
171 w/2,
172 offset,
173 Qt::AlignRight,
174 massString
178 p.end();
180 p.begin( this );
181 p.drawPixmap( 0, 0, pm );
182 p.end();
185 #include "detailedgraphicaloverview.moc"