1
/***************************************************************************
3
copyright : (C) 2004, 2005, 2006, 2007 by Carsten Niehaus
4
email : cniehaus@kde.org
5
***************************************************************************/
7
/***************************************************************************
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. *
14
***************************************************************************/
16
#include "detailedgraphicaloverview.h"
17
#include "kalziumdataobject.h"
18
#include "kalziumutils.h"
23
#include <kglobalsettings.h>
25
#include <kstandarddirs.h>
30
#include <QSvgRenderer>
36
DetailedGraphicalOverview::DetailedGraphicalOverview( QWidget *parent )
37
: QWidget( parent ), m_element(0)
39
setAttribute( Qt::WA_NoBackground, true );
41
setMinimumSize( 300, 200 );
43
// Set Hydrogen as inital Element.
47
void DetailedGraphicalOverview::setElement( int el )
49
m_element = KalziumDataObject::instance()->element( el );
50
setBackgroundColor( KalziumElementProperty::instance()->getElementColor( el ) );
54
void DetailedGraphicalOverview::setBackgroundColor( QColor bgColor )
56
if ( bgColor == Qt::transparent) {
57
bgColor = palette().background().color();
61
QLinearGradient grad(QPointF(0, 0), QPointF(0, height()));
62
grad.setColorAt(0,bgColor);
64
bgColor.getHsvF(&h, &s, &v, &a);
65
bgColor.setHsvF(h, s, v*0.6, a);
66
grad.setColorAt(1,bgColor);
68
m_backgroundBrush = QBrush(grad);
71
void DetailedGraphicalOverview::paintEvent( QPaintEvent* )
73
QRect rect(0, 0, width(), height());
75
QPixmap pm( width(), height() );
80
p.setBrush(Qt::SolidPattern);
84
pm.fill( palette().background().color() );
85
p.drawText( 0, 0, width(), height(), Qt::AlignCenter | Qt::TextWordWrap, i18n( "No element selected" ) );
86
} else if ( Prefs::colorschemebox() == 2) { //The iconic view is the 3rd view (0,1,2,...)
87
pm.fill( palette().background().color() );
89
QString pathname = KGlobal::dirs()->findResourceDir( "appdata", "data/iconsets/" ) + "data/iconsets/";
91
int enumii = m_element->dataAsVariant( ChemicalDataObject::atomicNumber ).toInt();
93
QString filename = pathname + "school" + '/' + QString::number( enumii ) + ".svg";
95
QSvgRenderer svgrenderer;
96
if ( QFile::exists(filename) && svgrenderer.load(filename) ) {
97
QSize size = svgrenderer.defaultSize();
98
size.scale( width(), height(), Qt::KeepAspectRatio );
100
QRect bounds( QPoint( 0, 0 ), size );
101
bounds.moveCenter( QPoint( width()/2, height()/2 ) );
102
svgrenderer.render( &p, bounds );
104
p.drawText( rect, Qt::AlignCenter | Qt::TextWordWrap, i18n( "No graphic found" ) );
107
const int h_t = 20; //height of the texts
109
p.setBrush( m_backgroundBrush );
111
p.setBrush( Qt::black );
112
p.setBrush(Qt::NoBrush);
114
QFont fA = KGlobalSettings::generalFont();
115
QFont fB = KGlobalSettings::generalFont();
116
QFont fC = KGlobalSettings::generalFont();
118
fA.setPointSize( fA.pointSize() + 20 ); //Huge font
120
fB.setPointSize( fB.pointSize() + 6 ); //Big font
121
fC.setPointSize( fC.pointSize() + 4 ); //Big font
123
QFontMetrics fmA = QFontMetrics( fA );
124
QFontMetrics fmB = QFontMetrics( fB );
126
//coordinates for element symbol: near the center
127
int xA = 4 * width() / 10;
128
int yA = height() / 2;
130
//coordinates for the atomic number: offset from element symbol to the upper left
131
int xB = xA - fmB.width( m_element->dataAsString( ChemicalDataObject::atomicNumber ) );
132
int yB = yA + fmB.height()/2;
136
p.drawText( xA, yA , m_element->dataAsString( ChemicalDataObject::symbol) );
140
p.drawText( xB, yB, m_element->dataAsString( ChemicalDataObject::atomicNumber ) );
142
//Name and other data
143
fC.setPointSize( h_t );
147
p.drawText( 1, 0, width(), height(), Qt::AlignLeft, m_element->dataAsString( ChemicalDataObject::name) );
149
//TODO Oxidationstates -> not there yet
152
QString massString = i18nc( "For example '1.0079u', the mass of an element in units", "%1 u", m_element->dataAsString( ChemicalDataObject::mass ) );
153
int size3 = KalziumUtils::maxSize( massString, rect , fC, &p);
154
fC.setPointSize( size3 );
156
int offset = KalziumUtils::StringHeight( massString, fC, &p );
169
p.drawPixmap( 0, 0, pm );
173
#include "detailedgraphicaloverview.moc"