Warning, file /education/kalzium/src/psetable/elementitem.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     ElementItem - Element Item, part of the Periodic Table Graphics View for
0003     Avogadro
0004 
0005     SPDX-FileCopyrightText: 2007-2009 Marcus D. Hanwell <marcus@cryos.org>
0006     SPDX-FileContributor: 2010 Konstantin Tokarev
0007     SPDX-FileCopyrightText: 2010 Etienne Rebetez <etienne.rebetez@oberwallis.ch>
0008 
0009     This file is part of the Avogadro molecular editor project.
0010     For more information, see <https://avogadro.cc/>
0011 
0012     SPDX-License-Identifier: LGPL-2.1-or-later
0013 */
0014 
0015 #include "elementitem.h"
0016 
0017 #include <prefs.h>
0018 
0019 #include "kalzium_debug.h"
0020 #include <QFont>
0021 #include <QGraphicsSceneMouseEvent>
0022 #include <QPainter>
0023 #include <QStyleOption>
0024 
0025 #include <KLocalizedString>
0026 
0027 ElementItem::ElementItem(KalziumElementProperty *elProperty, int elementNumber)
0028     : m_width(40)
0029     , m_height(40)
0030     , m_element(elementNumber)
0031     , m_property(elProperty)
0032 {
0033     // Want these items to be selectable
0034     setFlags(QGraphicsItem::ItemIsSelectable);
0035     setAcceptHoverEvents(true);
0036 
0037     m_symbol = KalziumDataObject::instance()->element(m_element)->dataAsString(ChemicalDataObject::symbol);
0038 
0039     // Set some custom data to make it easy to figure out which element we are
0040     setData(0, m_element);
0041 }
0042 
0043 ElementItem::~ElementItem()
0044 {
0045 }
0046 
0047 QRectF ElementItem::boundingRect() const
0048 {
0049     return QRectF(0, 0, m_width, m_height);
0050 }
0051 
0052 QPainterPath ElementItem::shape() const
0053 {
0054     QPainterPath path;
0055     path.addRect(0, 0, m_width, m_height);
0056     return path;
0057 }
0058 
0059 void ElementItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
0060 {
0061     QPen pen;
0062     pen.setColor(m_borderColor);
0063     pen.setWidth(1);
0064     painter->setPen(pen);
0065 
0066     painter->setBrush(m_brush);
0067 
0068     painter->drawRoundedRect(boundingRect(), m_width / 10, m_width / 10);
0069 
0070     if (isSelected()) {
0071         QColor selectedBackgroundColor = m_borderColor;
0072         selectedBackgroundColor.setAlpha(160);
0073         painter->setBrush(QBrush(QColor(selectedBackgroundColor)));
0074         painter->drawRoundedRect(boundingRect(), m_width / 10, m_width / 10);
0075     }
0076 
0077     pen.setColor(m_textColor);
0078     painter->setPen(pen);
0079 
0080     QFont symbolFont;
0081 
0082     switch (m_property->getMode()) {
0083     case KalziumElementProperty::NORMAL:
0084         symbolFont.setPointSize(12);
0085         symbolFont.setBold(true);
0086         painter->setFont(symbolFont);
0087         painter->drawText(boundingRect(), Qt::AlignCenter, m_symbol);
0088         symbolFont.setPointSize(7);
0089         symbolFont.setBold(false);
0090         painter->setFont(symbolFont);
0091         painter->drawText(QRectF(m_width / 14, m_height / 20, m_width, m_height / 2), Qt::AlignLeft, QString::number(m_element));
0092         break;
0093 
0094     case KalziumElementProperty::GRADIENTVALUE:
0095         painter->drawText(QRectF(0, m_height / 20, m_width, m_height / 2), Qt::AlignCenter, m_symbol);
0096 
0097         symbolFont.setPointSize(7);
0098         painter->setFont(symbolFont);
0099 
0100         painter->drawText(QRectF(0, m_height / 2 - m_height / 20, m_width, m_height / 2), Qt::AlignCenter, m_textValue);
0101         break;
0102     }
0103 }
0104 
0105 void ElementItem::redraw()
0106 {
0107     m_brush = m_property->getElementBrush(m_element);
0108     m_textColor = m_property->getTextColor(m_element);
0109     m_borderColor = m_property->getBorderColor(m_element);
0110     m_textValue = getCurrentElementValue();
0111     update();
0112 }
0113 
0114 QString ElementItem::getCurrentElementValue()
0115 {
0116     double value = m_property->getValue(m_element);
0117 
0118     if (value == -1) {
0119         return i18n("n/a");
0120     }
0121 
0122     return QString::number(value);
0123 }
0124 
0125 void ElementItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
0126 {
0127     setZValue(200);
0128     moveBy(-m_width / 4, -m_height / 4);
0129     setScale(1.5);
0130     QGraphicsItem::hoverEnterEvent(event);
0131 }
0132 
0133 void ElementItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
0134 {
0135     resetTransform();
0136     moveBy(m_width / 4, m_height / 4);
0137     setZValue(100);
0138     setScale(1);
0139     QGraphicsItem::hoverLeaveEvent(event);
0140 }