Refactoring VPuzzleGraphicsPiece

This commit is contained in:
Ronan Le Tiec 2020-05-23 14:50:22 +02:00
parent e43f970ea8
commit 45be7271bb
5 changed files with 39 additions and 39 deletions

View File

@ -11,9 +11,9 @@ SOURCES += \
$$PWD/vpcarrouselpiecepreview.cpp \ $$PWD/vpcarrouselpiecepreview.cpp \
$$PWD/vpcommandline.cpp \ $$PWD/vpcommandline.cpp \
$$PWD/vpcommands.cpp \ $$PWD/vpcommands.cpp \
$$PWD/vpgraphicspiece.cpp \
$$PWD/vpgraphicssheet.cpp \ $$PWD/vpgraphicssheet.cpp \
$$PWD/vpmainwindow.cpp \ $$PWD/vpmainwindow.cpp \
$$PWD/vpuzzlegraphicspiece.cpp \
$$PWD/vpuzzlelayout.cpp \ $$PWD/vpuzzlelayout.cpp \
$$PWD/vpuzzlelayer.cpp \ $$PWD/vpuzzlelayer.cpp \
$$PWD/vpuzzlemaingraphicsview.cpp \ $$PWD/vpuzzlemaingraphicsview.cpp \
@ -35,10 +35,10 @@ HEADERS += \
$$PWD/vpcarrouselpiecepreview.h \ $$PWD/vpcarrouselpiecepreview.h \
$$PWD/vpcommandline.h \ $$PWD/vpcommandline.h \
$$PWD/vpcommands.h \ $$PWD/vpcommands.h \
$$PWD/vpgraphicspiece.h \
$$PWD/vpgraphicssheet.h \ $$PWD/vpgraphicssheet.h \
$$PWD/vpmainwindow.h \ $$PWD/vpmainwindow.h \
$$PWD/vpstable.h \ $$PWD/vpstable.h \
$$PWD/vpuzzlegraphicspiece.h \
$$PWD/vpuzzlelayout.h \ $$PWD/vpuzzlelayout.h \
$$PWD/vpuzzlelayer.h \ $$PWD/vpuzzlelayer.h \
$$PWD/vpuzzlemaingraphicsview.h \ $$PWD/vpuzzlemaingraphicsview.h \

View File

@ -1,6 +1,6 @@
/************************************************************************ /************************************************************************
** **
** @file vpuzzlegraphicspiece.cpp ** @file vpgraphicspiece.cpp
** @author Ronan Le Tiec ** @author Ronan Le Tiec
** @date 4 5, 2020 ** @date 4 5, 2020
** **
@ -26,7 +26,7 @@
** **
*************************************************************************/ *************************************************************************/
#include "vpuzzlegraphicspiece.h" #include "vpgraphicspiece.h"
#include <QPen> #include <QPen>
#include <QBrush> #include <QBrush>
@ -47,7 +47,7 @@
Q_LOGGING_CATEGORY(pGraphicsPiece, "p.graphicsPiece") Q_LOGGING_CATEGORY(pGraphicsPiece, "p.graphicsPiece")
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPuzzleGraphicsPiece::VPuzzleGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent) : VPGraphicsPiece::VPGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent) :
QGraphicsObject(parent), QGraphicsObject(parent),
m_piece(piece), m_piece(piece),
m_cuttingLine(QPainterPath()), m_cuttingLine(QPainterPath()),
@ -59,13 +59,13 @@ VPuzzleGraphicsPiece::VPuzzleGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *p
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPuzzleGraphicsPiece::~VPuzzleGraphicsPiece() VPGraphicsPiece::~VPGraphicsPiece()
{ {
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::Init() void VPGraphicsPiece::Init()
{ {
// set some infos // set some infos
setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges); setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
@ -102,19 +102,19 @@ void VPuzzleGraphicsPiece::Init()
// TODO : initialises the other elements labels, passmarks etc. // TODO : initialises the other elements labels, passmarks etc.
// Initialises the connectors // Initialises the connectors
connect(m_piece, &VPuzzlePiece::SelectionChanged, this, &VPuzzleGraphicsPiece::on_PieceSelectionChanged); connect(m_piece, &VPuzzlePiece::SelectionChanged, this, &VPGraphicsPiece::on_PieceSelectionChanged);
connect(m_piece, &VPuzzlePiece::PositionChanged, this, &VPuzzleGraphicsPiece::on_PiecePositionChanged); connect(m_piece, &VPuzzlePiece::PositionChanged, this, &VPGraphicsPiece::on_PiecePositionChanged);
connect(m_piece, &VPuzzlePiece::RotationChanged, this, &VPuzzleGraphicsPiece::on_PieceRotationChanged); connect(m_piece, &VPuzzlePiece::RotationChanged, this, &VPGraphicsPiece::on_PieceRotationChanged);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPuzzlePiece* VPuzzleGraphicsPiece::GetPiece() VPuzzlePiece* VPGraphicsPiece::GetPiece()
{ {
return m_piece; return m_piece;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QRectF VPuzzleGraphicsPiece::boundingRect() const QRectF VPGraphicsPiece::boundingRect() const
{ {
if(!m_cuttingLine.isEmpty()) if(!m_cuttingLine.isEmpty())
{ {
@ -125,7 +125,7 @@ QRectF VPuzzleGraphicsPiece::boundingRect() const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QPainterPath VPuzzleGraphicsPiece::shape() const QPainterPath VPGraphicsPiece::shape() const
{ {
if(!m_cuttingLine.isEmpty()) if(!m_cuttingLine.isEmpty())
{ {
@ -136,7 +136,7 @@ QPainterPath VPuzzleGraphicsPiece::shape() const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void VPGraphicsPiece::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{ {
Q_UNUSED(widget); Q_UNUSED(widget);
Q_UNUSED(option); Q_UNUSED(option);
@ -179,7 +179,7 @@ void VPuzzleGraphicsPiece::paint(QPainter *painter, const QStyleOptionGraphicsIt
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::mousePressEvent(QGraphicsSceneMouseEvent *event) void VPGraphicsPiece::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
bool selectionState = isSelected(); bool selectionState = isSelected();
//perform the default behaviour //perform the default behaviour
@ -214,7 +214,7 @@ void VPuzzleGraphicsPiece::mousePressEvent(QGraphicsSceneMouseEvent *event)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::mouseMoveEvent(QGraphicsSceneMouseEvent * event) void VPGraphicsPiece::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{ {
if((event->buttons() == Qt::LeftButton) && (event->modifiers() & Qt::AltModifier)) if((event->buttons() == Qt::LeftButton) && (event->modifiers() & Qt::AltModifier))
{ {
@ -245,7 +245,7 @@ void VPuzzleGraphicsPiece::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void VPGraphicsPiece::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
bool selectionState = isSelected(); bool selectionState = isSelected();
@ -272,7 +272,7 @@ void VPuzzleGraphicsPiece::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::hoverMoveEvent(QGraphicsSceneHoverEvent *event) void VPGraphicsPiece::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{ {
if(event->modifiers() & Qt::AltModifier) if(event->modifiers() & Qt::AltModifier)
@ -292,7 +292,7 @@ void VPuzzleGraphicsPiece::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) void VPGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{ {
QMenu contextMenu; QMenu contextMenu;
@ -312,7 +312,7 @@ void VPuzzleGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *even
QVariant data = QVariant::fromValue(layer); QVariant data = QVariant::fromValue(layer);
moveToLayer->setData(data); moveToLayer->setData(data);
connect(moveToLayer, &QAction::triggered, this, &VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer); connect(moveToLayer, &QAction::triggered, this, &VPGraphicsPiece::on_ActionPieceMovedToLayer);
} }
} }
@ -320,13 +320,13 @@ void VPuzzleGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *even
QAction *removeAction = contextMenu.addAction(tr("Remove from Layout")); QAction *removeAction = contextMenu.addAction(tr("Remove from Layout"));
QVariant data = QVariant::fromValue(m_piece->GetLayer()->GetLayout()->GetUnplacedPiecesLayer()); QVariant data = QVariant::fromValue(m_piece->GetLayer()->GetLayout()->GetUnplacedPiecesLayer());
removeAction->setData(data); removeAction->setData(data);
connect(removeAction, &QAction::triggered, this, &VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer); connect(removeAction, &QAction::triggered, this, &VPGraphicsPiece::on_ActionPieceMovedToLayer);
contextMenu.exec(event->screenPos()); contextMenu.exec(event->screenPos());
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer() void VPGraphicsPiece::on_ActionPieceMovedToLayer()
{ {
QAction *act = qobject_cast<QAction *>(sender()); QAction *act = qobject_cast<QAction *>(sender());
QVariant v = act->data(); QVariant v = act->data();
@ -339,26 +339,26 @@ void VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::on_PieceSelectionChanged() void VPGraphicsPiece::on_PieceSelectionChanged()
{ {
setSelected(m_piece->GetIsSelected()); setSelected(m_piece->GetIsSelected());
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::on_PiecePositionChanged() void VPGraphicsPiece::on_PiecePositionChanged()
{ {
setPos(m_piece->GetPosition()); setPos(m_piece->GetPosition());
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::on_PieceRotationChanged() void VPGraphicsPiece::on_PieceRotationChanged()
{ {
setTransformOriginPoint(boundingRect().center()); setTransformOriginPoint(boundingRect().center());
setRotation(-m_piece->GetRotation()); setRotation(-m_piece->GetRotation());
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QVariant VPuzzleGraphicsPiece::itemChange(GraphicsItemChange change, const QVariant &value) QVariant VPGraphicsPiece::itemChange(GraphicsItemChange change, const QVariant &value)
{ {
if (scene()) { if (scene()) {

View File

@ -1,6 +1,6 @@
/************************************************************************ /************************************************************************
** **
** @file vpuzzlegraphicspiece.h ** @file vpgraphicspiece.h
** @author Ronan Le Tiec ** @author Ronan Le Tiec
** @date 4 5, 2020 ** @date 4 5, 2020
** **
@ -26,19 +26,19 @@
** **
*************************************************************************/ *************************************************************************/
#ifndef VPUZZLEGRAPHICSPIECE_H #ifndef VPGRAPHICSPIECE_H
#define VPUZZLEGRAPHICSPIECE_H #define VPGRAPHICSPIECE_H
#include <QGraphicsItem> #include <QGraphicsItem>
class VPuzzlePiece; class VPuzzlePiece;
class VPuzzleGraphicsPiece : public QGraphicsObject class VPGraphicsPiece : public QGraphicsObject
{ {
Q_OBJECT Q_OBJECT
public: public:
VPuzzleGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent = nullptr); VPGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent = nullptr);
~VPuzzleGraphicsPiece(); ~VPGraphicsPiece();
void Init(); void Init();
/** /**
@ -86,7 +86,7 @@ private slots:
void on_ActionPieceMovedToLayer(); void on_ActionPieceMovedToLayer();
private: private:
Q_DISABLE_COPY(VPuzzleGraphicsPiece) Q_DISABLE_COPY(VPGraphicsPiece)
VPuzzlePiece *m_piece; VPuzzlePiece *m_piece;
QPainterPath m_cuttingLine; QPainterPath m_cuttingLine;
@ -97,4 +97,4 @@ private:
}; };
#endif // VPUZZLEGRAPHICSPIECE_H #endif // VPGRAPHICSPIECE_H

View File

@ -137,7 +137,7 @@ void VPuzzleMainGraphicsView::keyPressEvent(QKeyEvent *event)
{ {
if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete)
{ {
QList<VPuzzleGraphicsPiece*> tmpGraphicsPieces = m_graphicsPieces; QList<VPGraphicsPiece*> tmpGraphicsPieces = m_graphicsPieces;
for(auto graphicsPiece : tmpGraphicsPieces) for(auto graphicsPiece : tmpGraphicsPieces)
{ {
@ -157,7 +157,7 @@ void VPuzzleMainGraphicsView::on_PieceMovedToLayer(VPuzzlePiece *piece, VPuzzleL
{ {
Q_UNUSED(layerBefore) Q_UNUSED(layerBefore)
VPuzzleGraphicsPiece *_graphicsPiece = nullptr; VPGraphicsPiece *_graphicsPiece = nullptr;
for(auto graphicPiece : m_graphicsPieces) for(auto graphicPiece : m_graphicsPieces)
{ {
if(graphicPiece->GetPiece() == piece) if(graphicPiece->GetPiece() == piece)
@ -175,7 +175,7 @@ void VPuzzleMainGraphicsView::on_PieceMovedToLayer(VPuzzlePiece *piece, VPuzzleL
{ {
if(_graphicsPiece == nullptr) if(_graphicsPiece == nullptr)
{ {
_graphicsPiece = new VPuzzleGraphicsPiece(piece); _graphicsPiece = new VPGraphicsPiece(piece);
m_graphicsPieces.append(_graphicsPiece); m_graphicsPieces.append(_graphicsPiece);
} }

View File

@ -30,7 +30,7 @@
#define VPUZZLEMAINGRAPHICSVIEW_H #define VPUZZLEMAINGRAPHICSVIEW_H
#include "vpgraphicssheet.h" #include "vpgraphicssheet.h"
#include "vpuzzlegraphicspiece.h" #include "vpgraphicspiece.h"
#include "../vwidgets/vmaingraphicsview.h" #include "../vwidgets/vmaingraphicsview.h"
class VMainGraphicsScene; class VMainGraphicsScene;
@ -79,7 +79,7 @@ private:
VPGraphicsSheet *m_graphicsSheet{nullptr}; VPGraphicsSheet *m_graphicsSheet{nullptr};
VPuzzleLayout *m_layout{nullptr}; VPuzzleLayout *m_layout{nullptr};
QList<VPuzzleGraphicsPiece*> m_graphicsPieces{}; QList<VPGraphicsPiece*> m_graphicsPieces{};
}; };