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 #ifndef PALAPELI_INTERACTOR_H
0008 #define PALAPELI_INTERACTOR_H
0009 
0010 class QGraphicsScene;
0011 class QGraphicsView;
0012 #include <QIcon>
0013 
0014 namespace Palapeli
0015 {
0016     struct MouseEvent
0017     {
0018         public:
0019             MouseEvent(QGraphicsView* view, const QPoint& pos);
0020             QPoint pos;
0021             QPointF scenePos;
0022         protected:
0023             friend class Interactor;
0024             MouseEvent();
0025     };
0026 
0027     struct WheelEvent
0028     {
0029         public:
0030             WheelEvent(QGraphicsView* view, const QPoint& pos, int delta);
0031             QPoint pos;
0032             QPointF scenePos;
0033             int delta;
0034     };
0035 
0036     enum InteractorType
0037     {
0038         MouseInteractor = 1,
0039         WheelInteractor = 2
0040     };
0041 
0042     enum EventProcessingFlag {
0043         EventMatches = 1 << 0,
0044         EventMatchesExactly = (1 << 1) + EventMatches,
0045         EventStartsInteraction = 1 << 2,
0046         EventConcludesInteraction = 1 << 3
0047     };
0048     Q_DECLARE_FLAGS(EventProcessingFlags, EventProcessingFlag)
0049     Q_DECLARE_OPERATORS_FOR_FLAGS(EventProcessingFlags)
0050 
0051     class Interactor
0052     {
0053         //TODO: add a flag for interactors which misbehave extremely with "NoButton;NoModifier" or "NoButton;*" triggers
0054         Q_DISABLE_COPY(Interactor)
0055         public:
0056             virtual ~Interactor();
0057 
0058             enum Category { NoCategory, PieceInteraction, TableInteraction, ViewportInteraction };
0059             Category category() const;
0060             QString description() const;
0061             QIcon icon() const;
0062             Palapeli::InteractorType interactorType() const;
0063             int priority() const;
0064 
0065             bool isActive() const;
0066             void setInactive();
0067             void sendEvent(const Palapeli::MouseEvent& event, Palapeli::EventProcessingFlags flags);
0068             void sendEvent(const Palapeli::WheelEvent& event);
0069 
0070             ///Call this method when the view for this interactor sets a different scene.
0071             void updateScene();
0072         protected:
0073             Interactor(int priority, Palapeli::InteractorType type, QGraphicsView* view);
0074             void setMetadata(Category category, const QString& description, const QIcon& icon);
0075 
0076             QGraphicsView* view() const;
0077             QGraphicsScene* scene() const;
0078 
0079             ///This corresponds to a mousePressEvent. Return false unless you want to accept this interaction. The default implementation does nothing and accepts all interactions.
0080             virtual bool startInteraction(const Palapeli::MouseEvent& event) { Q_UNUSED(event) return true; }
0081             ///This corresponds to a mouseMoveEvent.
0082             virtual void continueInteraction(const Palapeli::MouseEvent& event) { Q_UNUSED(event) }
0083             ///This corresponds to a mouseRelease.
0084             virtual void stopInteraction(const Palapeli::MouseEvent& event) { Q_UNUSED(event) }
0085             ///This corresponds to a wheelEvent
0086             virtual void doInteraction(const Palapeli::WheelEvent& event) { Q_UNUSED(event) }
0087             ///This method is called whenever the view() changes its scene. Both parameters may be null pointers, indicating that the view had no scene up to now, or that the view will have no scene after this operation.
0088             virtual void sceneChangeEvent(QGraphicsScene* oldScene, QGraphicsScene* newScene) { Q_UNUSED(oldScene) Q_UNUSED(newScene) }
0089         private:
0090             Palapeli::InteractorType m_type;
0091             //context
0092             QGraphicsView* m_view;
0093             QGraphicsScene* m_scene;
0094             //state
0095             bool m_active;
0096             Palapeli::MouseEvent m_lastMouseEvent;
0097             //metadata
0098             Category m_category;
0099             QString m_description;
0100             QIcon m_icon;
0101             int m_priority;
0102     };
0103 }
0104 
0105 #endif // PALAPELI_INTERACTOR_H