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

« back to all changes in this revision

Viewing changes to src/isotopetable/isotopeitem.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
 *   Copyright (C) 2005, 2006, 2007, 2008 by Carsten Niehaus               *
 
3
 *   cniehaus@kde.org                                                      *
 
4
 *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU General Public License as published by  *
 
8
 *   the Free Software Foundation; either version 2 of the License, or     *
 
9
 *   (at your option) any later version.                                   *
 
10
 *                                                                         *
 
11
 *   This program is distributed in the hope that it will be useful,       *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU General Public License for more details.                          *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU General Public License     *
 
17
 *   along with this program; if not, write to the                         *
 
18
 *   Free Software Foundation, Inc.,                                       *
 
19
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 
20
 ***************************************************************************/
 
21
 
 
22
 
 
23
#include "isotopeitem.h"
 
24
#include "isotopescene.h"
 
25
 
 
26
#include <element.h>
 
27
#include <isotope.h>
 
28
 
 
29
#include <QGraphicsSceneMouseEvent>
 
30
#include <QPainter>
 
31
#include <QStyleOptionGraphicsItem>
 
32
 
 
33
#include <klocale.h>
 
34
 
 
35
    IsotopeItem::IsotopeItem( Isotope * i, qreal x, qreal y, qreal width, qreal height,   QGraphicsItem *parent)
 
36
:  QAbstractGraphicsShapeItem(parent)
 
37
{
 
38
    m_rect = QRectF( 0, 0, width, height );
 
39
    setPos( x, y );
 
40
    m_isotope = i;
 
41
 
 
42
    m_type = getType( m_isotope );
 
43
 
 
44
    QBrush b;
 
45
    switch (m_type) {
 
46
        case alpha:
 
47
            b = QBrush( Qt::red );
 
48
            break;
 
49
        case ec:
 
50
            b = QBrush( Qt::blue );
 
51
            break;
 
52
        case multiple:
 
53
            b = QBrush( Qt::green );
 
54
            break;
 
55
        case bplus:
 
56
            b = QBrush( Qt::yellow );
 
57
            break;
 
58
        case bminus:
 
59
            b = QBrush( Qt::white );
 
60
            break;
 
61
        case stable:
 
62
            b = QBrush( Qt::lightGray );
 
63
            break;
 
64
        default:
 
65
            b = QBrush( Qt::darkGray );
 
66
            break;
 
67
    }
 
68
    setBrush(b);
 
69
 
 
70
    m_symbolFont = QFont( "Arial", 3 ,QFont::Bold );
 
71
    m_otherFont = QFont( "Arial", 1 ,QFont::Bold );
 
72
 
 
73
    setFlag(QGraphicsItem::ItemIsMovable, false);
 
74
    setFlag(QGraphicsItem::ItemIsSelectable, false);
 
75
    setCacheMode(QGraphicsItem::DeviceCoordinateCache);
 
76
 
 
77
    setToolTip( i18n("Isotope of Element %1 (%2)", m_isotope->parentElementNumber(), m_isotope->parentElementSymbol() ) );
 
78
}
 
79
 
 
80
void IsotopeItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
 
81
{
 
82
    Q_UNUSED(widget)
 
83
 
 
84
    const qreal lod = option->levelOfDetailFromTransform( painter->worldTransform() );
 
85
    if ( lod > 0.3 )
 
86
        painter->setPen( pen() );
 
87
    else
 
88
        painter->setPen( Qt::NoPen );
 
89
    painter->setBrush( brush() );
 
90
    painter->drawRect( m_rect );
 
91
 
 
92
    if ( lod >= 1.0 ) {
 
93
        // FIXME: Get rid of magic numbers and rather dynamically calculate them
 
94
        QRectF r1( m_rect.translated( 0.0, 2.5 ) );
 
95
 
 
96
        painter->setFont( m_symbolFont );
 
97
        painter->drawText( r1, Qt::AlignHCenter | Qt::TextDontClip, m_isotope->parentElementSymbol() );//, s->parentElementNumber()
 
98
 
 
99
        if ( lod >= 4.0 ) {
 
100
            QRectF r2( m_rect.topLeft() + QPointF( 1.0, 0.5 ), m_rect.size() / 2.0 );
 
101
            QRectF r3( m_rect.topLeft() + QPointF( 6.0, 0.5 ) , m_rect.size() / 2.0 );
 
102
 
 
103
            painter->setFont( m_otherFont );
 
104
            painter->drawText( r2, Qt::AlignHCenter | Qt::TextDontClip, QString::number( m_isotope->parentElementNumber() ) );
 
105
            painter->drawText( r3, Qt::AlignHCenter | Qt::TextDontClip, QString::number( m_isotope->nucleons() ) );
 
106
        }
 
107
    }
 
108
}
 
109
 
 
110
IsotopeItem::IsotopeType IsotopeItem::getType( Isotope * isotope )
 
111
{
 
112
    //TODO Here I need a clever way to find out *what* to return.
 
113
    if (isotope->alphalikeliness() > 60.0 )
 
114
        return IsotopeItem::alpha;
 
115
    if (isotope->betaminuslikeliness() > 60.0 )
 
116
        return IsotopeItem::bminus;
 
117
    if (isotope->betapluslikeliness() > 60.0 )
 
118
        return IsotopeItem::bminus;
 
119
    if (isotope->eclikeliness() > 60.0 )
 
120
        return IsotopeItem::ec;
 
121
    else
 
122
        return IsotopeItem::stable;
 
123
}
 
124
 
 
125
void IsotopeItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
 
126
{
 
127
    if (event->button() != Qt::RightButton) {
 
128
        event->ignore();
 
129
        return;
 
130
    }
 
131
 
 
132
    IsotopeScene *scene2 = static_cast<IsotopeScene*>(scene());
 
133
    scene2->updateContextHelp( this );
 
134
}