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
)
39 setAttribute( Qt::WA_NoBackground
, true );
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
);
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
;
66 void DetailedGraphicalOverview::paintEvent( QPaintEvent
* )
76 p
.setBrush(Qt::SolidPattern
);
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
);
100 p
.drawText( 0, 0, w
, h
, Qt::AlignCenter
| Qt::TextWordWrap
, i18n( "No graphic found" ) );
104 h_t
= 20; //height of the texts
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
124 fB
.setPointSize( fB
.pointSize() + 6 ); //Big font
125 fC
.setPointSize( fC
.pointSize() + 4 ); //Big font
127 QFontMetrics fmA
= QFontMetrics( fA
);
128 QFontMetrics fmB
= QFontMetrics( fB
);
130 //coordinates for element symbol: near the center
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;
140 p
.drawText( xA
, yA
, m_element
->dataAsString( ChemicalDataObject::symbol
) );
144 p
.drawText( xB
, yB
, m_element
->dataAsString( ChemicalDataObject::atomicNumber
) );
146 QRect
rect( 0, 20, w
/2, h
);
150 int size
= KalziumUtils::maxSize(m_element
->dataAsString( ChemicalDataObject::name
), rect
, fC
, &p
);
153 //Name and other data
154 fC
.setPointSize( size
);
158 p
.drawText( 1, 0, w
/2, h
, Qt::AlignLeft
, m_element
->dataAsString( ChemicalDataObject::name
) );
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
);
168 int offset
= KalziumUtils::StringHeight( massString
, fC
, &p
);
181 p
.drawPixmap( 0, 0, pm
);
185 #include "detailedgraphicaloverview.moc"