File indexing completed on 2024-04-21 03:41:38

0001 /*
0002     SPDX-FileCopyrightText: 2005-2008 Carsten Niehaus <cniehaus@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "isotopeitem.h"
0008 
0009 #include "isotopescene.h"
0010 
0011 #include <element.h>
0012 #include <isotope.h>
0013 
0014 #include <QGraphicsSceneMouseEvent>
0015 #include <QPainter>
0016 #include <QStyleOptionGraphicsItem>
0017 
0018 #include <KLocalizedString>
0019 
0020 IsotopeItem::IsotopeItem(Isotope *i, qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent)
0021     : QAbstractGraphicsShapeItem(parent)
0022 {
0023     m_rect = QRectF(0, 0, width, height);
0024     setPos(x, y);
0025     m_isotope = i;
0026 
0027     m_type = getType(m_isotope);
0028 
0029     QBrush b;
0030     switch (m_type) {
0031     case alpha:
0032         b = QBrush(Qt::red);
0033         break;
0034     case ec:
0035         b = QBrush(Qt::blue);
0036         break;
0037     case multiple:
0038         b = QBrush(Qt::green);
0039         break;
0040     case bplus:
0041         b = QBrush(Qt::yellow);
0042         break;
0043     case bminus:
0044         b = QBrush(Qt::white);
0045         break;
0046     case unknown:
0047         b = QBrush(Qt::darkGray);
0048         break;
0049     case stable:
0050         b = QBrush(Qt::magenta);
0051         break;
0052     default:
0053         b = QBrush(Qt::darkGray);
0054         break;
0055     }
0056     setBrush(b);
0057 
0058     m_symbolFont = QFont(QStringLiteral("Arial"), 3, QFont::Bold);
0059     m_otherFont = QFont(QStringLiteral("Arial"), 1, QFont::Bold);
0060 
0061     setFlag(QGraphicsItem::ItemIsMovable, false);
0062     setFlag(QGraphicsItem::ItemIsSelectable, false);
0063     setCacheMode(QGraphicsItem::DeviceCoordinateCache);
0064 
0065     setToolTip(i18n("Isotope of Element %1 (%2)", m_isotope->parentElementNumber(), m_isotope->parentElementSymbol()));
0066 }
0067 
0068 void IsotopeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
0069 {
0070     Q_UNUSED(widget)
0071 
0072     const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
0073     if (lod > 0.3) {
0074         painter->setPen(pen());
0075     } else {
0076         painter->setPen(Qt::NoPen);
0077     }
0078     painter->setBrush(brush());
0079     painter->drawRect(m_rect);
0080 
0081     if (lod >= 1.0) {
0082         // FIXME: Get rid of magic numbers and rather dynamically calculate them
0083         const QRectF r1(m_rect.translated(0.0, 2.5));
0084 
0085         painter->setFont(m_symbolFont);
0086         painter->drawText(r1, Qt::AlignHCenter | Qt::TextDontClip, m_isotope->parentElementSymbol()); //, s->parentElementNumber()
0087 
0088         if (lod >= 4.0) {
0089             const QRectF r2(m_rect.topLeft() + QPointF(1.0, 0.5), m_rect.size() / 2.0);
0090             const QRectF r3(m_rect.topLeft() + QPointF(6.0, 0.5), m_rect.size() / 2.0);
0091 
0092             painter->setFont(m_otherFont);
0093             painter->drawText(r2, Qt::AlignHCenter | Qt::TextDontClip, QString::number(m_isotope->parentElementNumber()));
0094             painter->drawText(r3, Qt::AlignHCenter | Qt::TextDontClip, QString::number(m_isotope->nucleons()));
0095         }
0096     }
0097 }
0098 
0099 IsotopeItem::IsotopeType IsotopeItem::getType(Isotope *isotope)
0100 {
0101     // TODO Here I need a clever way to find out *what* to return.
0102     if (isotope->alphalikeliness() > 60.0) {
0103         return IsotopeItem::alpha;
0104     }
0105     if (isotope->betaminuslikeliness() > 60.0 || isotope->betaminusneutronlikeliness() > 60.0 || isotope->betaminusfissionlikeliness() > 60.0) {
0106         return IsotopeItem::bminus;
0107     }
0108     if (isotope->betapluslikeliness() > 60.0 || isotope->betaplusalphalikeliness() > 60.0 || isotope->betaplusalphalikeliness() > 60.0) {
0109         return IsotopeItem::bplus;
0110     }
0111     if (isotope->eclikeliness() > 60.0) {
0112         return IsotopeItem::ec;
0113     }
0114     if (!(isotope->halflife() > 0.0) && !(isotope->abundance().isEmpty())) {
0115         return IsotopeItem::stable;
0116     } else {
0117         return IsotopeItem::unknown;
0118     }
0119 }
0120 
0121 void IsotopeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
0122 {
0123     if (event->button() != Qt::RightButton) {
0124         event->ignore();
0125         return;
0126     }
0127 
0128     auto scene2 = static_cast<IsotopeScene *>(scene());
0129     scene2->updateContextHelp(this);
0130 }
0131 
0132 void IsotopeItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
0133 {
0134     if (event->button() != Qt::LeftButton) {
0135         event->ignore();
0136         return;
0137     }
0138 
0139     auto scene2 = static_cast<IsotopeScene *>(scene());
0140     scene2->updateContextHelp(this);
0141 }