~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to src/detailedgraphicaloverview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 
 
3
copyright            : (C) 2004, 2005, 2006, 2007 by Carsten Niehaus
 
4
email                : cniehaus@kde.org
 
5
 ***************************************************************************/
 
6
 
 
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
 ***************************************************************************/
 
15
 
 
16
#include "detailedgraphicaloverview.h"
 
17
#include "kalziumdataobject.h"
 
18
#include "kalziumutils.h"
 
19
 
 
20
//KDE-Includes
 
21
#include <kdebug.h>
 
22
#include <klocale.h>
 
23
#include <kglobalsettings.h>
 
24
#include <kglobal.h>
 
25
#include <kstandarddirs.h>
 
26
 
 
27
//QT-Includes
 
28
#include <QFile>
 
29
#include <QPainter>
 
30
#include <QSvgRenderer>
 
31
#include <QRect>
 
32
 
 
33
#include <element.h>
 
34
#include "prefs.h"
 
35
 
 
36
DetailedGraphicalOverview::DetailedGraphicalOverview( QWidget *parent )
 
37
        : QWidget( parent ), m_element(0)
 
38
{
 
39
    setAttribute( Qt::WA_NoBackground, true );
 
40
 
 
41
    setMinimumSize( 300, 200 );
 
42
 
 
43
    // Set Hydrogen as inital Element.
 
44
    setElement(1);
 
45
}
 
46
 
 
47
void DetailedGraphicalOverview::setElement( int el )
 
48
{
 
49
    m_element = KalziumDataObject::instance()->element( el );
 
50
    setBackgroundColor( KalziumElementProperty::instance()->getElementColor( el ) );
 
51
    update();
 
52
}
 
53
 
 
54
void DetailedGraphicalOverview::setBackgroundColor( QColor bgColor )
 
55
{
 
56
    if ( bgColor == Qt::transparent) {
 
57
       bgColor = palette().background().color();
 
58
    }
 
59
 
 
60
    // add a gradient
 
61
    QLinearGradient grad(QPointF(0, 0), QPointF(0, height()));
 
62
    grad.setColorAt(0,bgColor);
 
63
    qreal h, s, v, a;
 
64
    bgColor.getHsvF(&h, &s, &v, &a);
 
65
    bgColor.setHsvF(h, s, v*0.6, a);
 
66
    grad.setColorAt(1,bgColor);
 
67
 
 
68
    m_backgroundBrush = QBrush(grad);
 
69
}
 
70
 
 
71
void DetailedGraphicalOverview::paintEvent( QPaintEvent* )
 
72
{
 
73
    QRect rect(0, 0, width(), height());
 
74
 
 
75
    QPixmap pm( width(), height() );
 
76
 
 
77
    QPainter p;
 
78
    p.begin( &pm );
 
79
 
 
80
    p.setBrush(Qt::SolidPattern);
 
81
 
 
82
    if ( !m_element )
 
83
    {
 
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() );
 
88
 
 
89
        QString pathname = KGlobal::dirs()->findResourceDir( "appdata", "data/iconsets/" ) + "data/iconsets/";
 
90
 
 
91
        int enumii = m_element->dataAsVariant( ChemicalDataObject::atomicNumber ).toInt();
 
92
 
 
93
        QString filename = pathname + "school" + '/' + QString::number( enumii )  + ".svg";
 
94
 
 
95
        QSvgRenderer svgrenderer;
 
96
        if ( QFile::exists(filename) && svgrenderer.load(filename) ) {
 
97
            QSize size = svgrenderer.defaultSize();
 
98
            size.scale( width(), height(), Qt::KeepAspectRatio );
 
99
 
 
100
            QRect bounds( QPoint( 0, 0 ), size );
 
101
            bounds.moveCenter( QPoint( width()/2, height()/2 ) );
 
102
            svgrenderer.render( &p, bounds );
 
103
        } else {
 
104
            p.drawText( rect, Qt::AlignCenter | Qt::TextWordWrap, i18n( "No graphic found" ) );
 
105
        }
 
106
    } else {
 
107
        const int h_t = 20; //height of the texts
 
108
 
 
109
        p.setBrush( m_backgroundBrush );
 
110
        p.drawRect( rect );
 
111
        p.setBrush( Qt::black );
 
112
        p.setBrush(Qt::NoBrush);
 
113
 
 
114
        QFont fA = KGlobalSettings::generalFont();
 
115
        QFont fB = KGlobalSettings::generalFont();
 
116
        QFont fC = KGlobalSettings::generalFont();
 
117
 
 
118
        fA.setPointSize( fA.pointSize() + 20 ); //Huge font
 
119
        fA.setBold( true );
 
120
        fB.setPointSize( fB.pointSize() + 6 ); //Big font
 
121
        fC.setPointSize( fC.pointSize() + 4 ); //Big font
 
122
        fC.setBold( true );
 
123
        QFontMetrics fmA = QFontMetrics( fA );
 
124
        QFontMetrics fmB = QFontMetrics( fB );
 
125
 
 
126
        //coordinates for element symbol: near the center
 
127
        int xA = 4 * width() / 10;
 
128
        int yA = height() / 2;
 
129
 
 
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;
 
133
 
 
134
        //Element Symbol
 
135
        p.setFont( fA );
 
136
        p.drawText( xA, yA , m_element->dataAsString( ChemicalDataObject::symbol) );
 
137
 
 
138
        //Atomic number
 
139
        p.setFont( fB );
 
140
        p.drawText( xB, yB, m_element->dataAsString( ChemicalDataObject::atomicNumber ) );
 
141
 
 
142
        //Name and other data
 
143
        fC.setPointSize( h_t );
 
144
        p.setFont( fC );
 
145
 
 
146
        //Name
 
147
        p.drawText( 1, 0, width(), height(), Qt::AlignLeft, m_element->dataAsString( ChemicalDataObject::name) );
 
148
 
 
149
        //TODO Oxidationstates -> not there yet
 
150
 
 
151
        //Mass
 
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 );
 
155
        p.setFont( fC );
 
156
        int offset = KalziumUtils::StringHeight( massString, fC, &p );
 
157
        p.drawText( 0,
 
158
                    height() - offset,
 
159
                    width(),
 
160
                    offset,
 
161
                    Qt::AlignRight,
 
162
                    massString
 
163
                  );
 
164
    }
 
165
 
 
166
    p.end();
 
167
 
 
168
    p.begin( this );
 
169
    p.drawPixmap( 0, 0, pm );
 
170
    p.end();
 
171
}
 
172
 
 
173
#include "detailedgraphicaloverview.moc"