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

0001 /*
0002     PeriodicTableScene - Periodic Table Graphics Scene for Kalzium
0003 
0004     SPDX-FileCopyrightText: 2005-2006 Pino Toscano <toscano.pino@tiscali.it>
0005     SPDX-FileCopyrightText: 2003-2006 Carsten Niehaus <cniehaus@kde.org>
0006     SPDX-FileCopyrightText: 2007-2009 Marcus D. Hanwell <marcus@cryos.org>
0007     SPDX-FileCopyrightText: 2010 Etienne Rebetez <etienne.rebetez@oberwallis.ch>
0008 
0009     SPDX-License-Identifier: LGPL-2.1-or-later
0010 */
0011 
0012 #include "periodictablescene.h"
0013 
0014 #include <QApplication>
0015 #include <QDrag>
0016 #include <QGraphicsSceneDragDropEvent>
0017 #include <QGraphicsSceneMouseEvent>
0018 #include <QMimeData>
0019 #include <QPainter>
0020 #include <QPixmap>
0021 #include <QStyleOptionGraphicsItem>
0022 
0023 PeriodicTableScene::PeriodicTableScene(QObject *parent)
0024     : QGraphicsScene(parent)
0025     , m_prevHoverElement(-1)
0026 {
0027     QPalette widgetPalette = palette();
0028     setBackgroundBrush(QBrush(widgetPalette.window()));
0029 
0030     setItemIndexMethod(QGraphicsScene::NoIndex);
0031 
0032     m_hoverTimer.setSingleShot(true);
0033     connect(&m_hoverTimer, &QTimer::timeout, this, &PeriodicTableScene::slotMouseover);
0034 }
0035 
0036 PeriodicTableScene::~PeriodicTableScene() = default;
0037 
0038 bool PeriodicTableScene::event(QEvent *e)
0039 {
0040     return QGraphicsScene::event(e);
0041 }
0042 
0043 void PeriodicTableScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
0044 {
0045     if (event->button() != Qt::LeftButton) {
0046         return;
0047     }
0048 
0049     QGraphicsItem *item = QGraphicsScene::itemAt(event->scenePos(), QTransform());
0050     if (item->data(0).toInt() > 0 && item->data(0).toInt() < 119) {
0051         m_eventPos = event->scenePos();
0052     } else {
0053         Q_EMIT freeSpaceClick();
0054     }
0055 
0056     QGraphicsScene::mousePressEvent(event);
0057 }
0058 
0059 void PeriodicTableScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
0060 {
0061     QGraphicsItem *item = QGraphicsScene::itemAt(m_eventPos, QTransform());
0062 
0063     if ((QApplication::mouseButtons() & Qt::LeftButton) && (event->pos() - m_eventPos).manhattanLength() > QApplication::startDragDistance()
0064         && item->data(0).toInt() > 0) {
0065         Element *pointedElement = KalziumDataObject::instance()->element(item->data(0).toInt());
0066 
0067         auto drag = new QDrag(event->widget());
0068         auto mimeData = new QMimeData;
0069 
0070         mimeData->setText(pointedElement->dataAsString(ChemicalDataObject::name));
0071         drag->setMimeData(mimeData);
0072 
0073         QPixmap pix(item->boundingRect().width() + 1, item->boundingRect().height() + 1);
0074         pix.fill(palette().color(QPalette::Window));
0075 
0076         QPainter painter(&pix);
0077         item->paint(&painter, new QStyleOptionGraphicsItem());
0078 
0079         drag->setPixmap(pix);
0080         drag->exec(Qt::CopyAction | Qt::MoveAction);
0081 
0082         m_eventPos = QPoint();
0083     } else {
0084         m_eventPos = event->scenePos();
0085 
0086         if (m_hoverTimer.isActive()) {
0087             m_hoverTimer.stop();
0088         }
0089         m_hoverTimer.start(100);
0090     }
0091 
0092     QGraphicsScene::mouseMoveEvent(event);
0093 }
0094 
0095 void PeriodicTableScene::slotMouseover()
0096 {
0097     QGraphicsItem *item = QGraphicsScene::itemAt(m_eventPos, QTransform());
0098 
0099     if (item->data(0).toInt() > 0 && item->data(0).toInt() < 119) {
0100         int num = item->data(0).toInt();
0101         if ((num > 0) && (num != m_prevHoverElement)) {
0102             Q_EMIT elementHovered(num);
0103         }
0104         m_prevHoverElement = num;
0105     }
0106 }
0107 
0108 void PeriodicTableScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
0109 {
0110     if (event->button() != Qt::LeftButton) {
0111         return;
0112     }
0113 
0114     QGraphicsItem *item = QGraphicsScene::itemAt(event->scenePos(), QTransform());
0115     if (item->data(0).toInt() > 0 && item->data(0).toInt() < 119) {
0116         Q_EMIT(elementChanged(item->data(0).toInt()));
0117     }
0118     QGraphicsScene::mouseReleaseEvent(event);
0119 }