void QEditorScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){ QExGraphicsScene::mouseReleaseEvent(event); switch(work_state) { case wsEditing: { //обработка в режиме редактора } break; case wsRunningSHN: { //обработка в режиме исполнения } break; //и так далее для всех режимов }}
class QExGraphicsScene : public QGraphicsScene{ Q_OBJECTprotected: void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);...};void QExGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event){ emit sigMousePressEvent(event); QGraphicsScene::mousePressEvent(event);}void QExGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){ QGraphicsScene::mouseReleaseEvent(event); emit sigMouseReleaseEvent(event);}void QExGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){ QGraphicsScene::mouseMoveEvent(event); emit sigMouseMoveEvent(event);}
void CArmEditor::createLogic(){connect(fproject->fieldView()->scene(),SIGNAL(sigContextMenu(QGraphicsSceneContextMenuEvent*)),this,SLOT(fieldContextMenu(QGraphicsSceneContextMenuEvent*)),Qt::DirectConnection); connect(fproject->fieldView()->scene(),SIGNAL(sigMousePressEvent(QGraphicsSceneMouseEvent*)),this,SLOT(sceneMousePressed(QGraphicsSceneMouseEvent*)),Qt::DirectConnection); connect(fproject->fieldView()->scene(),SIGNAL(sigMouseMoveEvent(QGraphicsSceneMouseEvent*)),this,SLOT(sceneMouseMoved(QGraphicsSceneMouseEvent*)),Qt::DirectConnection); connect(fproject->fieldView()->scene(),SIGNAL(sigMouseReleaseEvent(QGraphicsSceneMouseEvent*)),this,SLOT(sceneMouseReleased(QGraphicsSceneMouseEvent*)),Qt::DirectConnection);
как сделал сейчасclass QExGraphicsScene : public QGraphicsScene{ Q_OBJECTprotected: void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);...};void QExGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event){ emit sigMousePressEvent(event); QGraphicsScene::mousePressEvent(event);}...
C++ (Qt)struct ActorBase{ virtual void mousePressEvent (QExGraphicScene *scene, QGraphicsSceneMouseEvent *event) = 0; virtual void mouseReleaseEvent (QExGraphicScene *scene, QGraphicsSceneMouseEvent *event) = 0; ...}; struct ActorEdit : public ActorBase { ... };struct ActorView : public ActorBase { ... }; struct QExGraphicsScene : public QGraphicScene{ ActorBase *m_actor; void setEditMode () { delete m_actor; m_actor = new ActorEdit (); } ... void mousePressEvent (QGraphicsSceneMouseEvent *event) {m_actor->mousePressEvent (this, event);} ...};
#ifndef QEXGRAPHICSSCENE_H#define QEXGRAPHICSSCENE_H#include "config.h"#include <QGraphicsScene>#include <QGraphicsItem>class QExGraphicsScene : public QGraphicsScene{ Q_OBJECTprotected: void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);public: explicit QExGraphicsScene(QObject *parent = 0); void detachDelegates();//делегаты void (*__mousePressEvent)(QExGraphicsScene * scene, QGraphicsSceneMouseEvent *event); void (*__mouseReleaseEvent)(QExGraphicsScene * scene, QGraphicsSceneMouseEvent *event); void (*__mouseMoveEvent)(QExGraphicsScene * scene, QGraphicsSceneMouseEvent *event); void (*__contextMenuEvent)(QExGraphicsScene * scene, QGraphicsSceneContextMenuEvent *event);signals:};#endif // QEXGRAPHICSSCENE_H
#include <QGraphicsSceneMouseEvent>#include "qexgraphicsscene.h"QExGraphicsScene::QExGraphicsScene(QObject *parent) : QGraphicsScene(parent){ detachDelegates();}void QExGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event){ QGraphicsScene::mousePressEvent(event); if(__mousePressEvent) __mousePressEvent(this,event);}void QExGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){ QGraphicsScene::mouseReleaseEvent(event); if(__mouseReleaseEvent) __mouseReleaseEvent(this,event);}void QExGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event){ QGraphicsScene::mouseMoveEvent(event); if(__mouseMoveEvent) __mouseMoveEvent(this,event);}void QExGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event){ if(__contextMenuEvent) __contextMenuEvent(this,event);}void QExGraphicsScene::detachDelegates(){ __mousePressEvent = NULL; __mouseReleaseEvent = NULL; __mouseMoveEvent = NULL; __contextMenuEvent = NULL;}
... CArmEditor(QObject * parent = 0); ~CArmEditor(); static void fieldMousePressed(QExGraphicsScene * scene, QGraphicsSceneMouseEvent *event); static void fieldMouseMoved(QExGraphicsScene * scene, QGraphicsSceneMouseEvent *event); static void fieldMouseReleased(QExGraphicsScene * scene, QGraphicsSceneMouseEvent *event); static void fieldContextMenu(QExGraphicsScene * scene, QGraphicsSceneContextMenuEvent *event);
void CArmEditor::createLogic(){ fproject->fieldView()->scene()->__mousePressEvent = &CArmEditor::fieldMousePressed; fproject->fieldView()->scene()->__mouseMoveEvent = &CArmEditor::fieldMouseMoved; fproject->fieldView()->scene()->__mouseReleaseEvent = &CArmEditor::fieldMouseReleased; fproject->fieldView()->scene()->__contextMenuEvent = &CArmEditor::fieldContextMenu; /*старый вариант со слотами connect(fproject->fieldView()->scene(),SIGNAL(sigContextMenu(QGraphicsSceneContextMenuEvent*)),this,SLOT(fieldContextMenu(QGraphicsSceneContextMenuEvent*)),Qt::DirectConnection); connect(fproject->fieldView()->scene(),SIGNAL(sigMousePressEvent(QGraphicsSceneMouseEvent*)),this,SLOT(sceneMousePressed(QGraphicsSceneMouseEvent*)),Qt::DirectConnection); connect(fproject->fieldView()->scene(),SIGNAL(sigMouseMoveEvent(QGraphicsSceneMouseEvent*)),this,SLOT(sceneMouseMoved(QGraphicsSceneMouseEvent*)),Qt::DirectConnection); connect(fproject->fieldView()->scene(),SIGNAL(sigMouseReleaseEvent(QGraphicsSceneMouseEvent*)),this,SLOT(sceneMouseReleased(QGraphicsSceneMouseEvent*)),Qt::DirectConnection); connect(fproject->modulView()->scene(),SIGNAL(sigContextMenu(QGraphicsSceneContextMenuEvent*)),this,SLOT(modulesContextMenu(QGraphicsSceneContextMenuEvent*)),Qt::DirectConnection); */...}
void CArmEditor::fieldMousePressed(QExGraphicsScene * scene, QGraphicsSceneMouseEvent *event){ QEditorScene * fscene = (QEditorScene*)scene; switch(fscene->sceneAction) { case saAddingItem: { if(event->button()==Qt::LeftButton) switch(fscene->addingObject) { case otLight: fscene->createLight(event->scenePos()); break; case otSimpleIndicator: fscene->createIndicator(event->scenePos()); break;...}