File indexing completed on 2022-11-22 12:51:02

0001 /*
0002     SPDX-FileCopyrightText: 2005-2006 Pino Toscano <toscano.pino@tiscali.it>
0003     SPDX-FileCopyrightText: 2003-2006 Carsten Niehaus <cniehaus@kde.org>
0004     SPDX-FileCopyrightText: 2007-2009 Marcus D. Hanwell <marcus@cryos.org>
0005     SPDX-FileCopyrightText: 2010 Etienne Rebetez <etienne.rebetez@oberwallis.ch>
0006 
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "periodictableview.h"
0012 
0013 #include <QTimer>
0014 #include <QSvgGenerator>
0015 
0016 #include "psetables.h"
0017 #include <prefs.h>
0018 
0019 PeriodicTableView::PeriodicTableView(QWidget *parent)
0020     : QGraphicsView(parent)
0021 {
0022     setRenderHint(QPainter::Antialiasing);
0023     setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
0024     setCacheMode(QGraphicsView::CacheBackground);
0025 
0026     setMouseTracking(true);
0027 
0028     m_currentTableInex = Prefs::table();
0029 
0030     m_tableScene = new PeriodicTableScene(this);
0031     setScene(m_tableScene);
0032     connect(m_tableScene, &PeriodicTableScene::freeSpaceClick, this, &PeriodicTableView::fitPseInView);
0033 
0034     m_tableStates = new PeriodicTableStates(createElementItems(), createNumerationItems());
0035 
0036     fitPseInView();
0037 }
0038 
0039 QList<NumerationItem *> PeriodicTableView::createNumerationItems() const
0040 {
0041     // Creating Nummerationitems here, we use the classic periodic table as reference (18 elements in a row)
0042     const int xMax = 18;
0043 
0044     QList<NumerationItem *> numerationItemList;
0045 
0046     for (int i = 0; i < xMax; ++i) {
0047         numerationItemList << new NumerationItem(i);
0048         m_tableScene->addItem(numerationItemList.at(i));
0049         connect(this, &PeriodicTableView::numerationChange, numerationItemList.at(i), &NumerationItem::setNumerationType);
0050     }
0051 
0052     return numerationItemList;
0053 }
0054 
0055 QList<ElementItem *> PeriodicTableView::createElementItems() const
0056 {
0057     QList<ElementItem *> elementItemList;
0058     KalziumElementProperty *elProperty = KalziumElementProperty::instance();
0059 
0060     foreach (int intElement, pseTables::instance()->getTabletype(0)->elements()) {
0061         auto item = new ElementItem(elProperty, intElement);
0062 
0063         connect(elProperty, &KalziumElementProperty::propertyChanged, item, &ElementItem::redraw);
0064         m_tableScene->addItem(item);
0065         elementItemList << item;
0066     }
0067 
0068     return elementItemList;
0069 }
0070 
0071 PeriodicTableScene *PeriodicTableView::pseScene() const
0072 {
0073     return m_tableScene;
0074 }
0075 
0076 int PeriodicTableView::table() const
0077 {
0078     return m_currentTableInex;
0079 }
0080 
0081 void PeriodicTableView::slotChangeTable(int table)
0082 {
0083     m_currentTableInex = table;
0084     Prefs::setTable(m_currentTableInex);
0085 
0086     setBiggerSceneRect();
0087     m_tableStates->setTableState(m_currentTableInex);
0088 
0089     QTimer::singleShot(RESIZE_SCENE_TIMEOUT, this, &PeriodicTableView::fitPseInView);
0090 }
0091 
0092 void PeriodicTableView::slotSelectOneElement(int element)
0093 {
0094     slotUnSelectElements();
0095     slotSelectAdditionalElement(element);
0096 }
0097 
0098 void PeriodicTableView::slotSelectAdditionalElement(int element)
0099 {
0100     if (element > 0) {
0101         m_tableScene->items().at(--element)->setSelected(true);
0102     }
0103 }
0104 
0105 void PeriodicTableView::slotUnSelectElements()
0106 {
0107     foreach (QGraphicsItem *item, m_tableScene->selectedItems()) {
0108         item->setSelected(false);
0109     }
0110 }
0111 
0112 void PeriodicTableView::setBiggerSceneRect()
0113 {
0114     QRectF newRect(sceneRect());
0115     QRectF pseRect(m_tableStates->pseRect(m_currentTableInex));
0116 
0117     if (sceneRect().width() < pseRect.width()) {
0118         newRect.setWidth(pseRect.width());
0119     }
0120 
0121     if (sceneRect().height() < pseRect.height()) {
0122         newRect.setHeight(pseRect.height());
0123     }
0124 
0125     setSceneRect(newRect);
0126 }
0127 
0128 void PeriodicTableView::resizeEvent(QResizeEvent *event)
0129 {
0130     fitPseInView();
0131     QGraphicsView::resizeEvent(event);
0132 }
0133 
0134 void PeriodicTableView::fitPseInView()
0135 {
0136     if (operator!=(sceneRect(), m_tableStates->pseRect(m_currentTableInex))) {
0137         setSceneRect(m_tableStates->pseRect(m_currentTableInex));
0138     }
0139 
0140     fitInView(sceneRect(), Qt::KeepAspectRatio);
0141 }
0142 
0143 bool PeriodicTableView::event(QEvent *e)
0144 {
0145     return QGraphicsView::event(e);
0146 }
0147 
0148 void PeriodicTableView::generateSvg(const QString &filename)
0149 {
0150     auto svgGen = new QSvgGenerator();
0151     svgGen->setFileName(filename);
0152 
0153     QPainter painter;
0154     painter.begin(svgGen);
0155     painter.rotate(180);
0156     render(&painter);
0157     painter.end();
0158 
0159     delete svgGen;
0160 }
0161 
0162 PeriodicTableView::~PeriodicTableView()
0163 {
0164     delete scene();
0165     delete m_tableStates;
0166 }