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

« back to all changes in this revision

Viewing changes to src/psetable/elementitem.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
 ElementItem - Element Item, part of the Periodic Table Graphics View for
 
3
 Avogadro
 
4
 
 
5
 Copyright (C) 2007-2009 by Marcus D. Hanwell
 
6
 Some portions (C) 2010 by Konstantin Tokarev
 
7
 Copyright (C) 2010 by Etienne Rebetez
 
8
 
 
9
 This file is part of the Avogadro molecular editor project.
 
10
 For more information, see <http://avogadro.openmolecules.net/>
 
11
 
 
12
 Avogadro is free software; you can redistribute it and/or modify
 
13
 it under the terms of the GNU Lesser General Public License as published by
 
14
 the Free Software Foundation; either version 2.1 of the License, or
 
15
 (at your option) any later version.
 
16
 
 
17
 Avogadro is distributed in the hope that it will be useful,
 
18
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 GNU General Public License for more details.
 
21
 
 
22
 You should have received a copy of the GNU General Public License
 
23
 along with this program; if not, write to the Free Software
 
24
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
25
 02110-1301, USA.
 
26
 
 
27
 **********************************************************************/
 
28
 
 
29
#include "elementitem.h"
 
30
 
 
31
#include <prefs.h>
 
32
#include <klocale.h>
 
33
 
 
34
#include <QGraphicsSceneMouseEvent>
 
35
#include <QPainter>
 
36
#include <QStyleOption>
 
37
#include <QFont>
 
38
#include <QFontMetrics>
 
39
#include <QDebug>
 
40
 
 
41
 
 
42
ElementItem::ElementItem(KalziumElementProperty *elProperty, int elementNumber) : m_width(40), m_height(40),
 
43
        m_element(elementNumber), m_property(elProperty)
 
44
{
 
45
    // Want these items to be selectable
 
46
    setFlags(QGraphicsItem::ItemIsSelectable);
 
47
    setAcceptsHoverEvents(true);
 
48
 
 
49
    m_symbol = KalziumDataObject::instance()->element(m_element)->dataAsString( ChemicalDataObject::symbol );
 
50
 
 
51
    // Set some custom data to make it easy to figure out which element we are
 
52
    setData(0, m_element);
 
53
}
 
54
 
 
55
ElementItem::~ElementItem()
 
56
{
 
57
}
 
58
 
 
59
QRectF ElementItem::boundingRect() const
 
60
{
 
61
    return QRectF(0, 0, m_width, m_height);
 
62
}
 
63
 
 
64
QPainterPath ElementItem::shape() const
 
65
{
 
66
    QPainterPath path;
 
67
    path.addRect(0, 0, m_width, m_height);
 
68
    return path;
 
69
}
 
70
 
 
71
void ElementItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
 
72
                        QWidget *)
 
73
{
 
74
    QPen pen;
 
75
    pen.setColor( m_borderColor );
 
76
    pen.setWidth( 1 );
 
77
    painter->setPen( pen );
 
78
 
 
79
    painter->setBrush( m_brush );
 
80
 
 
81
    painter->drawRoundedRect( boundingRect(), m_width / 10, m_width / 10 );
 
82
 
 
83
    if ( isSelected() ) {
 
84
        QColor selectedBackgroundColor = m_borderColor;
 
85
        selectedBackgroundColor.setAlpha(120);
 
86
        painter->setBrush( QBrush( QColor( selectedBackgroundColor ) ) );
 
87
        painter->drawRoundedRect( boundingRect(), m_width / 10, m_width / 10 );
 
88
    }
 
89
 
 
90
    pen.setColor( m_textColor );
 
91
    painter->setPen( pen );
 
92
 
 
93
    QFont symbolFont;
 
94
 
 
95
    switch ( m_property->getMode() ) {
 
96
 
 
97
    case KalziumElementProperty::NORMAL:
 
98
        symbolFont.setPointSize(12);
 
99
        symbolFont.setBold(true);
 
100
        painter->setFont(symbolFont);
 
101
        painter->drawText( boundingRect(), Qt::AlignCenter, m_symbol );
 
102
        symbolFont.setPointSize(7);
 
103
        symbolFont.setBold(false);
 
104
        painter->setFont(symbolFont);
 
105
        painter->drawText( QRectF(m_width / 14, m_height / 20, m_width, m_height/2),
 
106
                           Qt::AlignLeft, QString::number(m_element));
 
107
        break;
 
108
 
 
109
    case KalziumElementProperty::GRADIENTVALUE:
 
110
        painter->drawText( QRectF(0, m_height / 20, m_width, m_height/2),
 
111
                           Qt::AlignCenter, m_symbol);
 
112
 
 
113
        symbolFont.setPointSize(7);
 
114
        painter->setFont(symbolFont);
 
115
 
 
116
        painter->drawText( QRectF(0, m_height / 2 - m_height / 20, m_width, m_height/2),
 
117
                           Qt::AlignCenter, m_textValue);
 
118
        break;
 
119
    }
 
120
}
 
121
 
 
122
void ElementItem::redraw()
 
123
{
 
124
    m_brush = m_property->getElementBrush(m_element);
 
125
    m_textColor = m_property->getTextColor(m_element);
 
126
    m_borderColor = m_property->getBorderColor(m_element);
 
127
    m_textValue = getCurrentElementValue();
 
128
    update();
 
129
}
 
130
 
 
131
QString ElementItem::getCurrentElementValue()
 
132
{
 
133
    double value = m_property->getValue(m_element);
 
134
 
 
135
    if (value == -1)
 
136
        return i18n("n/a");
 
137
 
 
138
    return QString::number(value);
 
139
}
 
140
 
 
141
void ElementItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
 
142
{
 
143
    setZValue(200);
 
144
    moveBy(-m_width/4, -m_height/4);
 
145
    scale(1.5, 1.5);
 
146
    QGraphicsItem::hoverEnterEvent(event);
 
147
}
 
148
 
 
149
void ElementItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
 
150
{
 
151
    resetTransform();
 
152
    moveBy(m_width/4, m_height/4);
 
153
    setZValue(100);
 
154
    QGraphicsItem::hoverLeaveEvent(event);
 
155
}
 
156
 
 
157
#include "elementitem.moc"