1
/**********************************************************************
2
ElementItem - Element Item, part of the Periodic Table Graphics View for
5
Copyright (C) 2007-2009 by Marcus D. Hanwell
6
Some portions (C) 2010 by Konstantin Tokarev
7
Copyright (C) 2010 by Etienne Rebetez
9
This file is part of the Avogadro molecular editor project.
10
For more information, see <http://avogadro.openmolecules.net/>
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.
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.
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
27
**********************************************************************/
29
#include "elementitem.h"
34
#include <QGraphicsSceneMouseEvent>
36
#include <QStyleOption>
38
#include <QFontMetrics>
42
ElementItem::ElementItem(KalziumElementProperty *elProperty, int elementNumber) : m_width(40), m_height(40),
43
m_element(elementNumber), m_property(elProperty)
45
// Want these items to be selectable
46
setFlags(QGraphicsItem::ItemIsSelectable);
47
setAcceptsHoverEvents(true);
49
m_symbol = KalziumDataObject::instance()->element(m_element)->dataAsString( ChemicalDataObject::symbol );
51
// Set some custom data to make it easy to figure out which element we are
52
setData(0, m_element);
55
ElementItem::~ElementItem()
59
QRectF ElementItem::boundingRect() const
61
return QRectF(0, 0, m_width, m_height);
64
QPainterPath ElementItem::shape() const
67
path.addRect(0, 0, m_width, m_height);
71
void ElementItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
75
pen.setColor( m_borderColor );
77
painter->setPen( pen );
79
painter->setBrush( m_brush );
81
painter->drawRoundedRect( boundingRect(), m_width / 10, m_width / 10 );
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 );
90
pen.setColor( m_textColor );
91
painter->setPen( pen );
95
switch ( m_property->getMode() ) {
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));
109
case KalziumElementProperty::GRADIENTVALUE:
110
painter->drawText( QRectF(0, m_height / 20, m_width, m_height/2),
111
Qt::AlignCenter, m_symbol);
113
symbolFont.setPointSize(7);
114
painter->setFont(symbolFont);
116
painter->drawText( QRectF(0, m_height / 2 - m_height / 20, m_width, m_height/2),
117
Qt::AlignCenter, m_textValue);
122
void ElementItem::redraw()
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();
131
QString ElementItem::getCurrentElementValue()
133
double value = m_property->getValue(m_element);
138
return QString::number(value);
141
void ElementItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
144
moveBy(-m_width/4, -m_height/4);
146
QGraphicsItem::hoverEnterEvent(event);
149
void ElementItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
152
moveBy(m_width/4, m_height/4);
154
QGraphicsItem::hoverLeaveEvent(event);
157
#include "elementitem.moc"