File indexing completed on 2024-05-12 04:06:23

0001 /*
0002     SPDX-FileCopyrightText: 2010 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "interactor.h"
0008 
0009 #include <QGraphicsView>
0010 
0011 //BEGIN events
0012 
0013 Palapeli::MouseEvent::MouseEvent(QGraphicsView* view, const QPoint& pos_)
0014     : pos(pos_)
0015     , scenePos(view->mapToScene(pos_))
0016 {
0017 }
0018 
0019 Palapeli::MouseEvent::MouseEvent()
0020 {
0021 }
0022 
0023 Palapeli::WheelEvent::WheelEvent(QGraphicsView* view, const QPoint& pos_, int delta_)
0024     : pos(pos_)
0025     , scenePos(view->mapToScene(pos_))
0026     , delta(delta_)
0027 {
0028 }
0029 
0030 //END events
0031 
0032 Palapeli::Interactor::Interactor(int priority, Palapeli::InteractorType type, QGraphicsView* view)
0033     : m_type(type)
0034     , m_view(view)
0035     , m_scene(view ? view->scene() : nullptr)
0036     , m_active(false)
0037     , m_category(NoCategory)
0038     , m_priority(priority)
0039 {
0040 }
0041 
0042 Palapeli::Interactor::~Interactor()
0043 {
0044 }
0045 
0046 Palapeli::InteractorType Palapeli::Interactor::interactorType() const
0047 {
0048     return m_type;
0049 }
0050 
0051 int Palapeli::Interactor::priority() const
0052 {
0053     return m_priority;
0054 }
0055 
0056 Palapeli::Interactor::Category Palapeli::Interactor::category() const
0057 {
0058     return m_category;
0059 }
0060 
0061 QString Palapeli::Interactor::description() const
0062 {
0063     return m_description;
0064 }
0065 
0066 QIcon Palapeli::Interactor::icon() const
0067 {
0068     return m_icon;
0069 }
0070 
0071 void Palapeli::Interactor::setMetadata(Palapeli::Interactor::Category category, const QString& description, const QIcon& icon)
0072 {
0073     m_category = category;
0074     m_description = description;
0075     m_icon = icon;
0076 }
0077 
0078 void Palapeli::Interactor::updateScene()
0079 {
0080     QGraphicsScene* oldScene = m_scene;
0081     QGraphicsScene* newScene = m_view ? m_view->scene() : nullptr;
0082     if (oldScene != newScene)
0083     {
0084         setInactive();
0085         m_scene = newScene;
0086         sceneChangeEvent(oldScene, newScene);
0087     }
0088 }
0089 
0090 QGraphicsView* Palapeli::Interactor::view() const
0091 {
0092     return m_view;
0093 }
0094 
0095 QGraphicsScene* Palapeli::Interactor::scene() const
0096 {
0097     return m_scene;
0098 }
0099 
0100 bool Palapeli::Interactor::isActive() const
0101 {
0102     return m_active;
0103 }
0104 
0105 void Palapeli::Interactor::setInactive()
0106 {
0107     //resend last event as release event
0108     if (m_active)
0109     {
0110         stopInteraction(m_lastMouseEvent);
0111         m_active = false;
0112     }
0113 }
0114 
0115 void Palapeli::Interactor::sendEvent(const Palapeli::MouseEvent& event, Palapeli::EventProcessingFlags flags)
0116 {
0117     //conclude interaction
0118     if (flags & Palapeli::EventConcludesInteraction || !(flags & Palapeli::EventMatches))
0119     {
0120         setInactive();
0121         return;
0122     }
0123     //check if caller attempts to start new interaction chain while an old one is still in progress
0124     if (flags & Palapeli::EventStartsInteraction)
0125         setInactive();
0126     //handle event, thereby starting a new interaction if necessary
0127     if (m_active)
0128         continueInteraction(event);
0129     else
0130         m_active = startInteraction(event);
0131 }
0132 
0133 void Palapeli::Interactor::sendEvent(const Palapeli::WheelEvent& event)
0134 {
0135     doInteraction(event);
0136 }