File indexing completed on 2022-11-29 12:50:42

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 "isotopescene.h"
0008 
0009 #include "isotopeitem.h"
0010 #include "kalziumdataobject.h"
0011 
0012 #include <element.h>
0013 #include <isotope.h>
0014 
0015 IsotopeScene::IsotopeScene(QObject *parent, int mode)
0016     : QGraphicsScene(parent)
0017 {
0018     m_mode = mode;
0019     m_isotopeGroup = new QGraphicsItemGroup();
0020     m_isotopeGroup->setHandlesChildEvents(false);
0021     addItem(m_isotopeGroup);
0022 
0023     m_itemSize = 10;
0024     drawIsotopes();
0025 }
0026 
0027 IsotopeScene::~IsotopeScene()
0028 {
0029     delete m_isotopeGroup;
0030 }
0031 
0032 void IsotopeScene::updateContextHelp(IsotopeItem *item)
0033 {
0034     Q_EMIT itemSelected(item);
0035 }
0036 
0037 void IsotopeScene::drawIsotopes()
0038 {
0039     for (auto item : m_isotopeGroup->childItems()) {
0040         m_isotopeGroup->removeFromGroup(item);
0041         delete item;
0042     }
0043 
0044     const QList<Element *> elist = KalziumDataObject::instance()->ElementList;
0045 
0046     for (Element *e : elist) {
0047         int elementNumber = e->dataAsVariant(ChemicalDataObject::atomicNumber).toInt();
0048 
0049         const QList<Isotope *> ilist = KalziumDataObject::instance()->isotopes(elementNumber);
0050         for (Isotope *i : ilist) {
0051             int x = elementNumber * m_itemSize;
0052             int y = (300 - i->nucleons()) * m_itemSize;
0053 
0054             if (m_mode == 0) {
0055                 // One part to the side of the other
0056                 int threshold = 60;
0057                 if (elementNumber > threshold) {
0058                     y += 120 * m_itemSize;
0059                     x += 5 * m_itemSize;
0060                 }
0061             } else if (m_mode == 1) {
0062                 // Both parts continuous
0063             } else if (m_mode == 2) {
0064                 // Horizontally
0065                 y = (elist.count() - elementNumber) * m_itemSize;
0066                 x = i->nucleons() * m_itemSize;
0067             } else if (m_mode == 3) {
0068                 // Horizontally (shifted)
0069                 y = (elist.count() - elementNumber) * m_itemSize;
0070                 x = (i->nucleons() - elementNumber) * m_itemSize;
0071             }
0072 
0073             auto item = new IsotopeItem(i, x, y, m_itemSize, m_itemSize);
0074             m_isotopeGroup->addToGroup(item);
0075         }
0076     }
0077 }
0078 
0079 void IsotopeScene::setMode(int mode)
0080 {
0081     m_mode = mode;
0082     drawIsotopes();
0083 }