File indexing completed on 2024-10-13 03:27:31
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.base())); 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 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) 0041 if (e->type() == QEvent::ApplicationPaletteChange) { 0042 QPalette widgetPalette = qApp->palette(); 0043 setBackgroundBrush(QBrush(widgetPalette.base())); 0044 } 0045 #endif 0046 return QGraphicsScene::event(e); 0047 } 0048 0049 void PeriodicTableScene::mousePressEvent(QGraphicsSceneMouseEvent *event) 0050 { 0051 if (event->button() != Qt::LeftButton) { 0052 return; 0053 } 0054 0055 QGraphicsItem *item = QGraphicsScene::itemAt(event->scenePos(), QTransform()); 0056 if (item->data(0).toInt() > 0 && item->data(0).toInt() < 119) { 0057 m_eventPos = event->scenePos(); 0058 } else { 0059 Q_EMIT freeSpaceClick(); 0060 } 0061 0062 QGraphicsScene::mousePressEvent(event); 0063 } 0064 0065 void PeriodicTableScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) 0066 { 0067 QGraphicsItem *item = QGraphicsScene::itemAt(m_eventPos, QTransform()); 0068 0069 if ((QApplication::mouseButtons() & Qt::LeftButton) && (event->pos() - m_eventPos).manhattanLength() > QApplication::startDragDistance() 0070 && item->data(0).toInt() > 0) { 0071 Element *pointedElement = KalziumDataObject::instance()->element(item->data(0).toInt()); 0072 0073 auto drag = new QDrag(event->widget()); 0074 auto mimeData = new QMimeData; 0075 0076 mimeData->setText(pointedElement->dataAsString(ChemicalDataObject::name)); 0077 drag->setMimeData(mimeData); 0078 0079 QPixmap pix(item->boundingRect().width() + 1, item->boundingRect().height() + 1); 0080 pix.fill(palette().color(QPalette::Window)); 0081 0082 QPainter painter(&pix); 0083 item->paint(&painter, new QStyleOptionGraphicsItem()); 0084 0085 drag->setPixmap(pix); 0086 drag->exec(Qt::CopyAction | Qt::MoveAction); 0087 0088 m_eventPos = QPoint(); 0089 } else { 0090 m_eventPos = event->scenePos(); 0091 0092 if (m_hoverTimer.isActive()) { 0093 m_hoverTimer.stop(); 0094 } 0095 m_hoverTimer.start(100); 0096 } 0097 0098 QGraphicsScene::mouseMoveEvent(event); 0099 } 0100 0101 void PeriodicTableScene::slotMouseover() 0102 { 0103 QGraphicsItem *item = QGraphicsScene::itemAt(m_eventPos, QTransform()); 0104 0105 if (item->data(0).toInt() > 0 && item->data(0).toInt() < 119) { 0106 int num = item->data(0).toInt(); 0107 if ((num > 0) && (num != m_prevHoverElement)) { 0108 Q_EMIT elementHovered(num); 0109 } 0110 m_prevHoverElement = num; 0111 } 0112 } 0113 0114 void PeriodicTableScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) 0115 { 0116 if (event->button() != Qt::LeftButton) { 0117 return; 0118 } 0119 0120 QGraphicsItem *item = QGraphicsScene::itemAt(event->scenePos(), QTransform()); 0121 if (item->data(0).toInt() > 0 && item->data(0).toInt() < 119) { 0122 Q_EMIT(elementChanged(item->data(0).toInt())); 0123 } 0124 QGraphicsScene::mouseReleaseEvent(event); 0125 } 0126 0127 #include "moc_periodictablescene.cpp"