• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

kalzium

detailedgraphicaloverview.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 
00003 copyright            : (C) 2004, 2005, 2006, 2007 by Carsten Niehaus
00004 email                : cniehaus@kde.org
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "detailedgraphicaloverview.h"
00017 #include "kalziumdataobject.h"
00018 #include "kalziumutils.h"
00019 
00020 //KDE-Includes
00021 #include <kdebug.h>
00022 #include <klocale.h>
00023 #include <kglobalsettings.h>
00024 #include <kglobal.h>
00025 #include <kstandarddirs.h>
00026 
00027 //QT-Includes
00028 #include <QFile>
00029 #include <QPainter>
00030 #include <QSvgRenderer>
00031 #include <QRect>
00032 
00033 #include <element.h>
00034 #include "prefs.h"
00035 
00036 DetailedGraphicalOverview::DetailedGraphicalOverview( QWidget *parent )
00037 : QWidget( parent )
00038 {
00039     setAttribute( Qt::WA_NoBackground, true );
00040 
00041     m_element = 0;
00042     setMinimumSize( 300, 200 );
00043 
00044     // last operation: setting the background color and scheduling an update()
00045     setBackgroundColor( QColor() );
00046 }
00047 
00048 void DetailedGraphicalOverview::setElement( int el )
00049 {
00050     m_element = KalziumDataObject::instance()->element( el );
00051     update();
00052 }
00053 
00054 void DetailedGraphicalOverview::setBackgroundColor( const QColor& bgColor )
00055 {
00056     m_backgroundColor = bgColor.isValid() ? bgColor : Qt::green;
00057     
00058     //this check is needed because a QBrush( QPixmap() ) constructs 
00059     //with a black brush. But black is a really bad color here ...
00060     if ( bgColor == QColor( 0, 0, 0 ) )
00061         m_backgroundColor = Qt::white;
00062     
00063     update();
00064 }
00065 
00066 void DetailedGraphicalOverview::paintEvent( QPaintEvent* )
00067 {
00068     int h = height();
00069     int w = width();
00070 
00071     QPixmap pm( w, h );
00072 
00073     QPainter p;
00074     p.begin( &pm );
00075 
00076     p.setBrush(Qt::SolidPattern);
00077 
00078     if ( !m_element )
00079     {
00080         pm.fill( palette().background().color() );
00081         p.drawText( 0, 0, w, h, Qt::AlignCenter | Qt::TextWordWrap, i18n( "No element selected" ) );
00082     } else if ( Prefs::colorschemebox() == 2) { //The iconic view is the 3rd view (0,1,2,...)
00083         pm.fill( palette().background().color() );
00084 
00085         QString pathname = KGlobal::dirs()->findResourceDir( "appdata", "data/iconsets/" ) + "data/iconsets/";
00086 
00087         int enumii = m_element->dataAsVariant( ChemicalDataObject::atomicNumber ).toInt();
00088 
00089         QString filename = pathname + "school" + '/' + QString::number( enumii )  + ".svg";
00090 
00091         QSvgRenderer svgrenderer;
00092         if ( QFile::exists(filename) && svgrenderer.load(filename) ) {
00093             QSize size = svgrenderer.defaultSize();
00094             size.scale( w, h, Qt::KeepAspectRatio );
00095 
00096             QRect bounds( QPoint( 0, 0 ), size );
00097             bounds.moveCenter( QPoint( w/2, h/2 ) );
00098             svgrenderer.render( &p, bounds );
00099         } else {
00100             p.drawText( 0, 0, w, h, Qt::AlignCenter | Qt::TextWordWrap, i18n( "No graphic found" ) );
00101         }
00102     } else
00103     {
00104         h_t = 20; //height of the texts
00105 
00106         x1 =  0;
00107         y1 =  0;
00108 
00109         x2 = w;
00110         y2 = h;
00111 
00112         p.setBrush( m_backgroundColor );
00113         p.drawRect( x1, y1, x2 - 1, y2 - 1 );
00114 
00115         p.setBrush( Qt::black );
00116         p.setBrush(Qt::NoBrush);
00117 
00118         QFont fA = KGlobalSettings::generalFont();
00119         QFont fB = KGlobalSettings::generalFont();
00120         QFont fC = KGlobalSettings::generalFont();
00121 
00122         fA.setPointSize( fA.pointSize() + 20 ); //Huge font
00123         fA.setBold( true );
00124         fB.setPointSize( fB.pointSize() + 6 ); //Big font
00125         fC.setPointSize( fC.pointSize() + 4 ); //Big font
00126         fC.setBold( true );
00127         QFontMetrics fmA = QFontMetrics( fA );
00128         QFontMetrics fmB = QFontMetrics( fB );
00129 
00130         //coordinates for element symbol: near the center
00131         int xA = 4 * w / 10;
00132         int yA = h / 2;
00133 
00134         //coordinates for the atomic number: offset from element symbol to the upper left
00135         int xB = xA - fmB.width( m_element->dataAsString( ChemicalDataObject::atomicNumber ) );
00136         int yB = yA + fmB.height()/2;
00137 
00138         //Element Symbol
00139         p.setFont( fA );
00140         p.drawText( xA, yA , m_element->dataAsString( ChemicalDataObject::symbol) ); 
00141 
00142         //Atomic number
00143         p.setFont( fB );
00144         p.drawText( xB, yB, m_element->dataAsString( ChemicalDataObject::atomicNumber ) );
00145 
00146         QRect rect( 0, 20, w/2, h );
00147 
00148         p.setFont( fC );
00149 
00150         int size = KalziumUtils::maxSize(m_element->dataAsString( ChemicalDataObject::name), rect , fC, &p);
00151 
00152 
00153         //Name and other data
00154         fC.setPointSize( size );
00155         p.setFont( fC );
00156 
00157         //Name
00158         p.drawText( 1, 0, w/2, h, Qt::AlignLeft, m_element->dataAsString( ChemicalDataObject::name) );
00159 
00160         //Oxidationstates
00161         p.setFont( fC );
00162 
00163         //Mass
00164         QString massString = i18nc( "For example '1.0079u', the mass of an element in units", "%1 u", m_element->dataAsString( ChemicalDataObject::mass ) );
00165         int size3 = KalziumUtils::maxSize( massString, rect , fC, &p);
00166         fC.setPointSize( size3 );
00167         p.setFont( fC );
00168         int offset = KalziumUtils::StringHeight( massString, fC, &p );
00169         p.drawText( w/2, 
00170                 h-offset, 
00171                 w/2, 
00172                 offset, 
00173                 Qt::AlignRight, 
00174                 massString
00175                 );
00176     }
00177 
00178     p.end();
00179 
00180     p.begin( this );
00181     p.drawPixmap( 0, 0, pm );
00182     p.end();
00183 }
00184 
00185 #include "detailedgraphicaloverview.moc"

kalzium

Skip menu "kalzium"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal