File indexing completed on 2024-04-14 03:40:17

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     setProperty("_breeze_force_frame", false);
0027 
0028     setMouseTracking(true);
0029 
0030     m_currentTableInex = Prefs::table();
0031 
0032     m_tableScene = new PeriodicTableScene(this);
0033     setScene(m_tableScene);
0034     connect(m_tableScene, &PeriodicTableScene::freeSpaceClick, this, &PeriodicTableView::fitPseInView);
0035 
0036     m_tableStates = new PeriodicTableStates(createElementItems(), createNumerationItems());
0037 
0038     fitPseInView();
0039 }
0040 
0041 QList<NumerationItem *> PeriodicTableView::createNumerationItems() const
0042 {
0043     // Creating Nummerationitems here, we use the classic periodic table as reference (18 elements in a row)
0044     const int xMax = 18;
0045 
0046     QList<NumerationItem *> numerationItemList;
0047 
0048     for (int i = 0; i < xMax; ++i) {
0049         numerationItemList << new NumerationItem(i);
0050         m_tableScene->addItem(numerationItemList.at(i));
0051         connect(this, &PeriodicTableView::numerationChange, numerationItemList.at(i), &NumerationItem::setNumerationType);
0052     }
0053 
0054     return numerationItemList;
0055 }
0056 
0057 QList<ElementItem *> PeriodicTableView::createElementItems() const
0058 {
0059     QList<ElementItem *> elementItemList;
0060     KalziumElementProperty *elProperty = KalziumElementProperty::instance();
0061 
0062     const auto elements = pseTables::instance()->getTabletype(0)->elements();
0063     for (int intElement : elements) {
0064         auto item = new ElementItem(elProperty, intElement);
0065 
0066         connect(elProperty, &KalziumElementProperty::propertyChanged, item, &ElementItem::redraw);
0067         m_tableScene->addItem(item);
0068         elementItemList << item;
0069     }
0070 
0071     return elementItemList;
0072 }
0073 
0074 PeriodicTableScene *PeriodicTableView::pseScene() const
0075 {
0076     return m_tableScene;
0077 }
0078 
0079 int PeriodicTableView::table() const
0080 {
0081     return m_currentTableInex;
0082 }
0083 
0084 void PeriodicTableView::slotChangeTable(int table)
0085 {
0086     m_currentTableInex = table;
0087     Prefs::setTable(m_currentTableInex);
0088 
0089     setBiggerSceneRect();
0090     m_tableStates->setTableState(m_currentTableInex);
0091 
0092     QTimer::singleShot(RESIZE_SCENE_TIMEOUT, this, &PeriodicTableView::fitPseInView);
0093 }
0094 
0095 void PeriodicTableView::slotSelectOneElement(int element)
0096 {
0097     slotUnSelectElements();
0098     slotSelectAdditionalElement(element);
0099 }
0100 
0101 void PeriodicTableView::slotSelectAdditionalElement(int element)
0102 {
0103     if (element > 0) {
0104         m_tableScene->items().at(--element)->setSelected(true);
0105     }
0106 }
0107 
0108 void PeriodicTableView::slotUnSelectElements()
0109 {
0110     const auto selectedItems = m_tableScene->selectedItems();
0111     for (QGraphicsItem *item : selectedItems) {
0112         item->setSelected(false);
0113     }
0114 }
0115 
0116 void PeriodicTableView::setBiggerSceneRect()
0117 {
0118     QRectF newRect(sceneRect());
0119     QRectF pseRect(m_tableStates->pseRect(m_currentTableInex));
0120 
0121     if (sceneRect().width() < pseRect.width()) {
0122         newRect.setWidth(pseRect.width());
0123     }
0124 
0125     if (sceneRect().height() < pseRect.height()) {
0126         newRect.setHeight(pseRect.height());
0127     }
0128 
0129     setSceneRect(newRect);
0130 }
0131 
0132 void PeriodicTableView::resizeEvent(QResizeEvent *event)
0133 {
0134     fitPseInView();
0135     QGraphicsView::resizeEvent(event);
0136 }
0137 
0138 void PeriodicTableView::fitPseInView()
0139 {
0140     if (operator!=(sceneRect(), m_tableStates->pseRect(m_currentTableInex))) {
0141         setSceneRect(m_tableStates->pseRect(m_currentTableInex));
0142     }
0143 
0144     fitInView(sceneRect(), Qt::KeepAspectRatio);
0145 }
0146 
0147 bool PeriodicTableView::event(QEvent *e)
0148 {
0149     return QGraphicsView::event(e);
0150 }
0151 
0152 void PeriodicTableView::generateSvg(const QString &filename)
0153 {
0154     auto svgGen = new QSvgGenerator();
0155     svgGen->setFileName(filename);
0156 
0157     QPainter painter;
0158     painter.begin(svgGen);
0159     painter.rotate(180);
0160     render(&painter);
0161     painter.end();
0162 
0163     delete svgGen;
0164 }
0165 
0166 PeriodicTableView::~PeriodicTableView()
0167 {
0168     delete scene();
0169     delete m_tableStates;
0170 }
0171 
0172 #include "moc_periodictableview.cpp"