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 <QFont>
0020 #include <QGraphicsSceneMouseEvent>
0021 #include <QPainter>
0022 #include <QStyleOption>
0023 
0024 #include <KLocalizedString>
0025 
0026 ElementItem::ElementItem(KalziumElementProperty *elProperty, int elementNumber)
0027     : m_width(40)
0028     , m_height(40)
0029     , m_element(elementNumber)
0030     , m_property(elProperty)
0031 {
0032     // Want these items to be selectable
0033     setFlags(QGraphicsItem::ItemIsSelectable);
0034     setAcceptHoverEvents(true);
0035 
0036     m_symbol = KalziumDataObject::instance()->element(m_element)->dataAsString(ChemicalDataObject::symbol);
0037 
0038     // Set some custom data to make it easy to figure out which element we are
0039     setData(0, m_element);
0040 }
0041 
0042 ElementItem::~ElementItem()
0043 {
0044 }
0045 
0046 QRectF ElementItem::boundingRect() const
0047 {
0048     return QRectF(0, 0, m_width, m_height);
0049 }
0050 
0051 QPainterPath ElementItem::shape() const
0052 {
0053     QPainterPath path;
0054     path.addRect(0, 0, m_width, m_height);
0055     return path;
0056 }
0057 
0058 void ElementItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
0059 {
0060     QPen pen;
0061     pen.setColor(m_borderColor);
0062     pen.setWidth(1);
0063     painter->setPen(pen);
0064 
0065     painter->setBrush(m_brush);
0066 
0067     painter->drawRoundedRect(boundingRect(), m_width / 10, m_width / 10);
0068 
0069     if (isSelected()) {
0070         QColor selectedBackgroundColor = m_borderColor;
0071         selectedBackgroundColor.setAlpha(160);
0072         painter->setBrush(QBrush(QColor(selectedBackgroundColor)));
0073         painter->drawRoundedRect(boundingRect(), m_width / 10, m_width / 10);
0074     }
0075 
0076     pen.setColor(m_textColor);
0077     painter->setPen(pen);
0078 
0079     QFont symbolFont;
0080 
0081     switch (m_property->getMode()) {
0082     case KalziumElementProperty::NORMAL:
0083         symbolFont.setPointSize(12);
0084         symbolFont.setBold(true);
0085         painter->setFont(symbolFont);
0086         painter->drawText(boundingRect(), Qt::AlignCenter, m_symbol);
0087         symbolFont.setPointSize(7);
0088         symbolFont.setBold(false);
0089         painter->setFont(symbolFont);
0090         painter->drawText(QRectF(m_width / 14, m_height / 20, m_width, m_height / 2), Qt::AlignLeft, QString::number(m_element));
0091         break;
0092 
0093     case KalziumElementProperty::GRADIENTVALUE:
0094         painter->drawText(QRectF(0, m_height / 20, m_width, m_height / 2), Qt::AlignCenter, m_symbol);
0095 
0096         symbolFont.setPointSize(7);
0097         painter->setFont(symbolFont);
0098 
0099         painter->drawText(QRectF(0, m_height / 2 - m_height / 20, m_width, m_height / 2), Qt::AlignCenter, m_textValue);
0100         break;
0101     }
0102 }
0103 
0104 void ElementItem::redraw()
0105 {
0106     m_brush = m_property->getElementBrush(m_element);
0107     m_textColor = m_property->getTextColor(m_element);
0108     m_borderColor = m_property->getBorderColor(m_element);
0109     m_textValue = getCurrentElementValue();
0110     update();
0111 }
0112 
0113 QString ElementItem::getCurrentElementValue()
0114 {
0115     double value = m_property->getValue(m_element);
0116 
0117     if (value == -1) {
0118         return i18n("n/a");
0119     }
0120 
0121     return QString::number(value);
0122 }
0123 
0124 void ElementItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
0125 {
0126     setZValue(200);
0127     moveBy(-m_width / 4, -m_height / 4);
0128     setScale(1.5);
0129     QGraphicsItem::hoverEnterEvent(event);
0130 }
0131 
0132 void ElementItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
0133 {
0134     resetTransform();
0135     moveBy(m_width / 4, m_height / 4);
0136     setZValue(100);
0137     setScale(1);
0138     QGraphicsItem::hoverLeaveEvent(event);
0139 }