From e2b816f1928678ca0e047e93e0451f9e4ad5bbe7 Mon Sep 17 00:00:00 2001 From: Ronan Le Tiec Date: Tue, 5 May 2020 17:40:36 +0200 Subject: [PATCH] work on piece selection and position --- src/app/puzzle/puzzlemainwindow.cpp | 109 +++++++++++++-------- src/app/puzzle/puzzlemainwindow.h | 14 ++- src/app/puzzle/vpiececarrousel.cpp | 28 +----- src/app/puzzle/vpiececarrousel.h | 7 -- src/app/puzzle/vpiececarrousellayer.cpp | 9 -- src/app/puzzle/vpiececarrousellayer.h | 6 -- src/app/puzzle/vpiececarrouselpiece.cpp | 21 ++-- src/app/puzzle/vpiececarrouselpiece.h | 18 +--- src/app/puzzle/vpuzzlegraphicspiece.cpp | 55 ++++++++++- src/app/puzzle/vpuzzlegraphicspiece.h | 18 +++- src/app/puzzle/vpuzzlelayout.cpp | 23 +++++ src/app/puzzle/vpuzzlelayout.h | 27 ++++- src/app/puzzle/vpuzzlemaingraphicsview.cpp | 22 ++++- src/app/puzzle/vpuzzlemaingraphicsview.h | 7 +- src/app/puzzle/vpuzzlepiece.cpp | 50 ++++++++++ src/app/puzzle/vpuzzlepiece.h | 74 +++++++++++++- 16 files changed, 353 insertions(+), 135 deletions(-) diff --git a/src/app/puzzle/puzzlemainwindow.cpp b/src/app/puzzle/puzzlemainwindow.cpp index 21a26d23c..7c2571619 100644 --- a/src/app/puzzle/puzzlemainwindow.cpp +++ b/src/app/puzzle/puzzlemainwindow.cpp @@ -189,6 +189,11 @@ VPuzzlePiece* PuzzleMainWindow::CreatePiece(const VLayoutPiece &rawPiece) // TODO : set all the information we need for the piece! + // + connect(piece, &VPuzzlePiece::SelectionChanged, this, &PuzzleMainWindow::on_PieceSelectionChanged); + connect(piece, &VPuzzlePiece::PositionChanged, this, &PuzzleMainWindow::on_PiecePositionChanged); + + return piece; } @@ -228,9 +233,9 @@ void PuzzleMainWindow::InitPropertyTabCurrentPiece() // ------------------------------ placement ----------------------------------- connect(ui->doubleSpinBoxCurrentPieceBoxPositionX, QOverload::of(&QDoubleSpinBox::valueChanged), this, - &PuzzleMainWindow::on_CurrentPiecePositionChanged); + &PuzzleMainWindow::on_CurrentPiecePositionEdited); connect(ui->doubleSpinBoxCurrentPieceBoxPositionY, QOverload::of(&QDoubleSpinBox::valueChanged), this, - &PuzzleMainWindow::on_CurrentPiecePositionChanged); + &PuzzleMainWindow::on_CurrentPiecePositionEdited); } @@ -310,9 +315,6 @@ void PuzzleMainWindow::InitPieceCarrousel() connect(ui->dockWidgetPieceCarrousel, QOverload::of(&QDockWidget::dockLocationChanged), this, &PuzzleMainWindow::on_PieceCarrouselLocationChanged); - - connect(m_pieceCarrousel, QOverload::of(&VPieceCarrousel::pieceClicked), this, - &PuzzleMainWindow::on_PieceSelected); } @@ -335,31 +337,34 @@ void PuzzleMainWindow::SetPropertiesData() //--------------------------------------------------------------------------------------------------------------------- void PuzzleMainWindow::SetPropertyTabCurrentPieceData() { - if(m_selectedPiece == nullptr) + if(m_selectedPieces.count() == 0) { - if(false) // check for multiple piece selection - { - // TODO in the future - } - else - { - // TODO : update current piece data to show a "no current piece selected" - ui->containerCurrentPieceNoData->setVisible(true); - ui->containerCurrentPieceData->setVisible(false); - } + // TODO : update current piece data to show a "no current piece selected" + ui->containerCurrentPieceNoData->setVisible(true); + ui->containerCurrentPieceData->setVisible(false); } - else + else if(m_selectedPieces.count() == 1) { + VPuzzlePiece *selectedPiece = m_selectedPieces.first(); + ui->containerCurrentPieceNoData->setVisible(false); ui->containerCurrentPieceData->setVisible(true); // set the value to the current piece - ui->lineEditCurrentPieceName->setText(m_selectedPiece->GetName()); + ui->lineEditCurrentPieceName->setText(selectedPiece->GetName()); - ui->checkBoxCurrentPieceShowSeamline->setChecked(m_selectedPiece->GetShowSeamLine()); - ui->checkBoxCurrentPieceMirrorPiece->setChecked(m_selectedPiece->GetPieceMirrored()); + ui->checkBoxCurrentPieceShowSeamline->setChecked(selectedPiece->GetShowSeamLine()); + ui->checkBoxCurrentPieceMirrorPiece->setChecked(selectedPiece->GetPieceMirrored()); - // TODO:rotation and placement; + QPointF pos = selectedPiece->GetPosition(); + SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionX, UnitConvertor(pos.x(), Unit::Px, m_layout->GetUnit())); + SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionY, UnitConvertor(pos.y(), Unit::Px, m_layout->GetUnit())); + + // TODO: rotation + } + else + { + // TODO in the future } } @@ -790,18 +795,18 @@ void PuzzleMainWindow::on_pushButtonLayoutExport_clicked() //--------------------------------------------------------------------------------------------------------------------- void PuzzleMainWindow::on_checkBoxCurrentPieceShowSeamline_toggled(bool checked) { - if(m_selectedPiece != nullptr) + if(m_selectedPieces.count() == 1) { - m_selectedPiece->SetShowSeamLine(checked); + m_selectedPieces.first()->SetShowSeamLine(checked); } } //--------------------------------------------------------------------------------------------------------------------- void PuzzleMainWindow::on_checkBoxCurrentPieceMirrorPiece_toggled(bool checked) { - if(m_selectedPiece != nullptr) + if(m_selectedPieces.count() == 1) { - m_selectedPiece->SetPieceMirrored(checked); + m_selectedPieces.first()->SetPieceMirrored(checked); } } @@ -821,16 +826,22 @@ void PuzzleMainWindow::on_doubleSpinBoxCurrentPieceAngle_valueChanged(double val //--------------------------------------------------------------------------------------------------------------------- -void PuzzleMainWindow::on_CurrentPiecePositionChanged() +void PuzzleMainWindow::on_CurrentPiecePositionEdited() { - // just for test purpuses, to be removed: - QMessageBox msgBox; - msgBox.setText("TODO PuzzleMainWindow::CurrentPiecePositionChanged"); - int ret = msgBox.exec(); +// ui->doubleSpinBoxCurrentPieceBoxPositionX->blockSignals(true); +// ui->doubleSpinBoxCurrentPieceBoxPositionY->blockSignals(true); - Q_UNUSED(ret); + if(m_selectedPieces.count() == 1) + { + VPuzzlePiece *piece = m_selectedPieces.first(); + QPointF pos(UnitConvertor(ui->doubleSpinBoxCurrentPieceBoxPositionX->value(), m_layout->GetUnit(), Unit::Px), + UnitConvertor(ui->doubleSpinBoxCurrentPieceBoxPositionY->value(), m_layout->GetUnit(), Unit::Px)); + piece->SetPosition(pos); + } + +// ui->doubleSpinBoxCurrentPieceBoxPositionX->blockSignals(false); +// ui->doubleSpinBoxCurrentPieceBoxPositionY->blockSignals(false); - // TODO } //--------------------------------------------------------------------------------------------------------------------- @@ -851,17 +862,35 @@ void PuzzleMainWindow::on_PieceCarrouselLocationChanged(Qt::DockWidgetArea area) } //--------------------------------------------------------------------------------------------------------------------- -void PuzzleMainWindow::on_PieceSelected(VPuzzlePiece* piece) +void PuzzleMainWindow::on_PieceSelectionChanged() { - m_selectedPiece = piece; + // for now we have only single selection + // FIXME / TODO : To be updated when we support multiple selection. - // update the state of the piece carrousel - m_pieceCarrousel->SelectPiece(piece); - - // update the Layout - - // TODO + for (auto piece : m_selectedPieces) + { + if(piece->GetIsSelected()) + { + piece->SetIsSelected(false); + } + } + m_selectedPieces = m_layout->GetSelectedPieces(); // update the property of the piece currently selected SetPropertyTabCurrentPieceData(); } + + +//--------------------------------------------------------------------------------------------------------------------- +void PuzzleMainWindow::on_PiecePositionChanged() +{ + + if(m_selectedPieces.count() == 1) + { + VPuzzlePiece *piece = m_selectedPieces.first(); + QPointF pos = piece->GetPosition(); + + SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionX, UnitConvertor(pos.x(), Unit::Px, m_layout->GetUnit())); + SetDoubleSpinBoxValue(ui->doubleSpinBoxCurrentPieceBoxPositionY, UnitConvertor(pos.y(), Unit::Px, m_layout->GetUnit())); + } +} diff --git a/src/app/puzzle/puzzlemainwindow.h b/src/app/puzzle/puzzlemainwindow.h index a51515f21..8d5015a4e 100644 --- a/src/app/puzzle/puzzlemainwindow.h +++ b/src/app/puzzle/puzzlemainwindow.h @@ -94,7 +94,7 @@ private: VPuzzleCommandLinePtr m_cmd; VPuzzleLayout *m_layout{nullptr}; - VPuzzlePiece *m_selectedPiece{nullptr}; + QListm_selectedPieces{QList()}; /** * @brief CreatePiece creates a piece from the given VLayoutPiece data @@ -354,7 +354,7 @@ private slots: * @brief on_CurrentPiecePositionChanged When the positionX or the positionY * is changed in the current piece tab */ - void on_CurrentPiecePositionChanged(); + void on_CurrentPiecePositionEdited(); /** * @brief PieceCarrouselLocationChanged When the piece carrousel's location @@ -364,10 +364,14 @@ private slots: void on_PieceCarrouselLocationChanged(Qt::DockWidgetArea area); /** - * @brief on_PieceSelected When a piece has been selected - * @param piece the piece that was selected + * @brief on_PieceSelectionChanged When the piece selection has changed */ - void on_PieceSelected(VPuzzlePiece* piece); + void on_PieceSelectionChanged(); + + /** + * @brief on_PiecePositionChanged When the current piece position has changed + */ + void on_PiecePositionChanged(); }; diff --git a/src/app/puzzle/vpiececarrousel.cpp b/src/app/puzzle/vpiececarrousel.cpp index 03bd866bf..224bea4ec 100644 --- a/src/app/puzzle/vpiececarrousel.cpp +++ b/src/app/puzzle/vpiececarrousel.cpp @@ -28,11 +28,11 @@ #include "vpiececarrousel.h" #include #include +#include #include "../vmisc/backport/qoverload.h" #include -#include Q_LOGGING_CATEGORY(pCarrousel, "p.carrousel") @@ -118,10 +118,6 @@ void VPieceCarrousel::Refresh() VPieceCarrouselLayer *carrouselLayer = new VPieceCarrouselLayer(layer, this); m_carrouselLayers.append(carrouselLayer); m_layersContainer->layout()->addWidget(carrouselLayer); - - connect(carrouselLayer, QOverload::of(&VPieceCarrouselLayer::pieceClicked), this, - &VPieceCarrousel::on_PieceClicked); - } on_ActiveLayerChanged(0); @@ -160,20 +156,6 @@ void VPieceCarrousel::Clear() } } -//--------------------------------------------------------------------------------------------------------------------- -void VPieceCarrousel::SelectPiece(VPuzzlePiece* piece) -{ - for (auto layer : m_carrouselLayers) - { - QList carrouselPieces = layer->GetCarrouselPieces(); - for (auto carrouselPiece : carrouselPieces) - { - carrouselPiece->SetIsSelected(carrouselPiece->GetPiece() == piece); - } - } -} - - //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrousel::on_ActiveLayerChanged(int index) { @@ -239,11 +221,3 @@ void VPieceCarrousel::RefreshOrientation() // FIXME: find a nicer way than putting directly the 120 width of the piece } } - -//--------------------------------------------------------------------------------------------------------------------- -void VPieceCarrousel::on_PieceClicked(VPieceCarrouselPiece* carrouselPiece) -{ - emit pieceClicked(carrouselPiece->GetPiece()); -} - - diff --git a/src/app/puzzle/vpiececarrousel.h b/src/app/puzzle/vpiececarrousel.h index 54b9e786e..294db69ba 100644 --- a/src/app/puzzle/vpiececarrousel.h +++ b/src/app/puzzle/vpiececarrousel.h @@ -78,13 +78,6 @@ public: void SelectPiece(VPuzzlePiece* piece); -signals: - void pieceClicked(VPuzzlePiece* piece); - -public slots: - void on_PieceClicked(VPieceCarrouselPiece* carrouselPiece); - - private: Q_DISABLE_COPY(VPieceCarrousel) diff --git a/src/app/puzzle/vpiececarrousellayer.cpp b/src/app/puzzle/vpiececarrousellayer.cpp index d00ed6ecb..36b3b4e0d 100644 --- a/src/app/puzzle/vpiececarrousellayer.cpp +++ b/src/app/puzzle/vpiececarrousellayer.cpp @@ -89,10 +89,6 @@ void VPieceCarrouselLayer::Refresh() setVisible(true); carrouselPiece->CleanPreview(); setVisible(false); - - connect(carrouselPiece, QOverload::of(&VPieceCarrouselPiece::clicked), this, - &VPieceCarrouselLayer::on_PieceClicked); - } } @@ -102,8 +98,3 @@ QList VPieceCarrouselLayer::GetCarrouselPieces() return m_carrouselPieces; } -//--------------------------------------------------------------------------------------------------------------------- -void VPieceCarrouselLayer::on_PieceClicked(VPieceCarrouselPiece* carrouselPiece) -{ - emit pieceClicked(carrouselPiece); -} diff --git a/src/app/puzzle/vpiececarrousellayer.h b/src/app/puzzle/vpiececarrousellayer.h index aa3bae3a2..08d46a093 100644 --- a/src/app/puzzle/vpiececarrousellayer.h +++ b/src/app/puzzle/vpiececarrousellayer.h @@ -45,12 +45,6 @@ public: QList GetCarrouselPieces(); -signals: - void pieceClicked(VPieceCarrouselPiece* carrouselPiece); - -public slots: - void on_PieceClicked(VPieceCarrouselPiece* carrouselPiece); - private: Q_DISABLE_COPY(VPieceCarrouselLayer) diff --git a/src/app/puzzle/vpiececarrouselpiece.cpp b/src/app/puzzle/vpiececarrouselpiece.cpp index 3051e2c6c..27299cdd3 100644 --- a/src/app/puzzle/vpiececarrouselpiece.cpp +++ b/src/app/puzzle/vpiececarrouselpiece.cpp @@ -92,6 +92,11 @@ void VPieceCarrouselPiece::Init() pieceLayout->addWidget(m_piecePreview); pieceLayout->addWidget(m_label); + + // connect the signals + connect(m_piece, &VPuzzlePiece::SelectionChanged, this, &VPieceCarrouselPiece::on_PieceSelectionChanged); + + // then refresh the data Refresh(); } @@ -142,11 +147,9 @@ VPuzzlePiece * VPieceCarrouselPiece::GetPiece() } //--------------------------------------------------------------------------------------------------------------------- -void VPieceCarrouselPiece::SetIsSelected(bool value) +void VPieceCarrouselPiece::on_PieceSelectionChanged() { - m_isSelected = value; - - if(value) + if(m_piece->GetIsSelected()) { setStyleSheet("background-color:white; border: 2px solid red;"); } @@ -156,11 +159,7 @@ void VPieceCarrouselPiece::SetIsSelected(bool value) } } -//--------------------------------------------------------------------------------------------------------------------- -bool VPieceCarrouselPiece::GetIsSelected() -{ - return m_isSelected; -} + //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrouselPiece::mousePressEvent(QMouseEvent *event) @@ -170,9 +169,9 @@ void VPieceCarrouselPiece::mousePressEvent(QMouseEvent *event) if (event->button() == Qt::LeftButton) { - if(!m_isSelected) + if(!m_piece->GetIsSelected()) { - emit clicked(this); + m_piece->SetIsSelected(true); } m_dragStart = event->pos(); } diff --git a/src/app/puzzle/vpiececarrouselpiece.h b/src/app/puzzle/vpiececarrouselpiece.h index ad7d6a272..72d75e639 100644 --- a/src/app/puzzle/vpiececarrouselpiece.h +++ b/src/app/puzzle/vpiececarrouselpiece.h @@ -58,22 +58,8 @@ public: */ VPuzzlePiece * GetPiece(); - /** - * @brief SetSelected sets the selected state to the given value - * @param value the new selected state - */ - void SetIsSelected(bool value); - - /** - * @brief GetSelected Returns wether the piece is selected or not - * @return true if the piece is selected - */ - bool GetIsSelected(); - -signals: - void clicked(VPieceCarrouselPiece* m_piece); - public slots: + void on_PieceSelectionChanged(); protected: void mousePressEvent(QMouseEvent *event) override; @@ -87,8 +73,6 @@ private: QLabel *m_label{nullptr}; VPieceCarrouselPiecePreview *m_piecePreview{nullptr}; - bool m_isSelected = false; - QPoint m_dragStart; private slots: diff --git a/src/app/puzzle/vpuzzlegraphicspiece.cpp b/src/app/puzzle/vpuzzlegraphicspiece.cpp index b69332fe6..70b5d9156 100644 --- a/src/app/puzzle/vpuzzlegraphicspiece.cpp +++ b/src/app/puzzle/vpuzzlegraphicspiece.cpp @@ -33,11 +33,16 @@ #include #include #include +#include +#include "vpuzzlepiece.h" + +#include +Q_LOGGING_CATEGORY(pGraphicsPiece, "p.graphicsPiece") //--------------------------------------------------------------------------------------------------------------------- VPuzzleGraphicsPiece::VPuzzleGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent) : - QGraphicsItem(parent), + QGraphicsObject(parent), m_piece(piece), m_cuttingLine(QPainterPath()), m_seamLine(QPainterPath()) @@ -55,7 +60,7 @@ VPuzzleGraphicsPiece::~VPuzzleGraphicsPiece() void VPuzzleGraphicsPiece::Init() { // set some infos - setFlags(ItemIsSelectable | ItemIsMovable); + setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges); setCursor(QCursor(Qt::OpenHandCursor)); //setAcceptHoverEvents(true); // maybe we can do some stuff with this @@ -76,7 +81,9 @@ void VPuzzleGraphicsPiece::Init() // TODO : initialises the other elements like grain line, labels, passmarks etc. - + // Initialises the connectors + connect(m_piece, &VPuzzlePiece::SelectionChanged, this, &VPuzzleGraphicsPiece::on_PieceSelectionChanged); + connect(m_piece, &VPuzzlePiece::PositionChanged, this, &VPuzzleGraphicsPiece::on_PiecePositionChanged); } @@ -106,8 +113,14 @@ QPainterPath VPuzzleGraphicsPiece::shape() const void VPuzzleGraphicsPiece::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(widget); + Q_UNUSED(option); QPen pen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); + if(isSelected()) + { + pen.setColor(Qt::red); + } + QBrush noBrush(Qt::NoBrush); painter->setPen(pen); @@ -136,6 +149,8 @@ void VPuzzleGraphicsPiece::mousePressEvent(QGraphicsSceneMouseEvent *event) return; } + setSelected(true); + setCursor(Qt::ClosedHandCursor); } @@ -155,4 +170,38 @@ void VPuzzleGraphicsPiece::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) } +//--------------------------------------------------------------------------------------------------------------------- +void VPuzzleGraphicsPiece::on_PieceSelectionChanged() +{ + setSelected(m_piece->GetIsSelected()); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPuzzleGraphicsPiece::on_PiecePositionChanged() +{ + setPos(m_piece->GetPosition()); +} + +//--------------------------------------------------------------------------------------------------------------------- +QVariant VPuzzleGraphicsPiece::itemChange(GraphicsItemChange change, const QVariant &value) +{ + if (scene()) { + if(change == ItemPositionHasChanged) + { + blockSignals(true); + m_piece->SetPosition(pos()); + blockSignals(false); + } + + if(change == ItemSelectedHasChanged) + { + if(m_piece->GetIsSelected() != isSelected()) + { + m_piece->SetIsSelected(isSelected()); + } + } + } + + return QGraphicsObject::itemChange(change, value); +} diff --git a/src/app/puzzle/vpuzzlegraphicspiece.h b/src/app/puzzle/vpuzzlegraphicspiece.h index 89d55b9ef..ef585d9cb 100644 --- a/src/app/puzzle/vpuzzlegraphicspiece.h +++ b/src/app/puzzle/vpuzzlegraphicspiece.h @@ -31,15 +31,27 @@ #include -#include "vpuzzlepiece.h" +class VPuzzlePiece; -class VPuzzleGraphicsPiece : public QGraphicsItem +class VPuzzleGraphicsPiece : public QGraphicsObject { + Q_OBJECT public: VPuzzleGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent = nullptr); ~VPuzzleGraphicsPiece(); void Init(); +public slots: + /** + * @brief on_PieceSelectionChanged When the piece selection was changed + */ + void on_PieceSelectionChanged(); + + /** + * @brief on_PiecePositionChanged When the piece position was changed + */ + void on_PiecePositionChanged(); + protected: QRectF boundingRect() const override; QPainterPath shape() const override; @@ -48,6 +60,7 @@ protected: void mousePressEvent(QGraphicsSceneMouseEvent * event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; + QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; private: Q_DISABLE_COPY(VPuzzleGraphicsPiece) @@ -55,7 +68,6 @@ private: QPainterPath m_cuttingLine; QPainterPath m_seamLine; - }; #endif // VPUZZLEGRAPHICSPIECE_H diff --git a/src/app/puzzle/vpuzzlelayout.cpp b/src/app/puzzle/vpuzzlelayout.cpp index 61290add7..49d75a662 100644 --- a/src/app/puzzle/vpuzzlelayout.cpp +++ b/src/app/puzzle/vpuzzlelayout.cpp @@ -75,6 +75,29 @@ QList VPuzzleLayout::GetLayers() return m_layers; } +//--------------------------------------------------------------------------------------------------------------------- +QList VPuzzleLayout::GetSelectedPieces() +{ + QList result = QList(); + + QList layers = m_layers; + layers.prepend(m_unplacedPiecesLayer); + + for (auto layer : layers) + { + for (auto piece : layer->GetPieces()) + { + if(piece->GetIsSelected()) + { + result.append(piece); + } + } + } + + return result; +} + + //--------------------------------------------------------------------------------------------------------------------- void VPuzzleLayout::SetUnit(Unit unit) { diff --git a/src/app/puzzle/vpuzzlelayout.h b/src/app/puzzle/vpuzzlelayout.h index 436957475..e404f3025 100644 --- a/src/app/puzzle/vpuzzlelayout.h +++ b/src/app/puzzle/vpuzzlelayout.h @@ -35,6 +35,7 @@ #include "def.h" class VPuzzleLayer; +class VPuzzlePiece; // is this the right place for the definition? enum class FollowGrainline : qint8 { No = 0, Follow90 = 1, Follow180 = 2}; @@ -51,7 +52,22 @@ public: VPuzzleLayer* AddLayer(VPuzzleLayer *layer); QList GetLayers(); + /** + * @brief GetSelectedPieces Returns the list of the selected pieces + * @return the selected pieces + */ + QList GetSelectedPieces(); + + /** + * @brief SetUnit Sets the unit of the layout to the given unit + * @param unit the new unit + */ void SetUnit(Unit unit); + + /** + * @brief GetUnit Returns the current unit of the layout + * @return the unit + */ Unit GetUnit() const; /** @@ -133,7 +149,16 @@ public: */ QMarginsF GetLayoutMarginsConverted() const; - void SetFollowGrainline(FollowGrainline state); + /** + * @brief SetFollowGrainline Sets the type of grainline for the pieces to follow + * @param state the type of grainline + */ + void SetFollowGrainline(FollowGrainline state); + + /** + * @brief GetFollowGrainline Returns if the layout's pieces follow a grainline or not + * @return wether the pieces follow a grainline and if so, which grainline + */ FollowGrainline GetFollowGrainline() const; /** diff --git a/src/app/puzzle/vpuzzlemaingraphicsview.cpp b/src/app/puzzle/vpuzzlemaingraphicsview.cpp index bdadeb2c8..9d1a8238f 100644 --- a/src/app/puzzle/vpuzzlemaingraphicsview.cpp +++ b/src/app/puzzle/vpuzzlemaingraphicsview.cpp @@ -40,7 +40,8 @@ Q_LOGGING_CATEGORY(pMainGraphicsView, "p.mainGraphicsView") //--------------------------------------------------------------------------------------------------------------------- VPuzzleMainGraphicsView::VPuzzleMainGraphicsView(VPuzzleLayout *layout, QWidget *parent) : - QGraphicsView(parent) + QGraphicsView(parent), + m_graphicsPieces(QList()) { m_scene = new VPuzzleMainGraphicsScene(this); setScene(m_scene); @@ -123,9 +124,22 @@ void VPuzzleMainGraphicsView::dropEvent(QDropEvent *event) QPointF scenePos = mapToScene(point); // todo take the position into account - VPuzzleGraphicsPiece *item = new VPuzzleGraphicsPiece(piece); - item->setPos(scenePos); - m_scene->addItem(item); + AddPiece(piece, scenePos); + } } } + + +//--------------------------------------------------------------------------------------------------------------------- +void VPuzzleMainGraphicsView::AddPiece(VPuzzlePiece *piece, QPointF pos) +{ + VPuzzleGraphicsPiece *item = new VPuzzleGraphicsPiece(piece); + m_scene->addItem(item); + item->setSelected(true); + item->setPos(pos); + + item->blockSignals(true); + piece->SetPosition(pos); + item->blockSignals(false); +} diff --git a/src/app/puzzle/vpuzzlemaingraphicsview.h b/src/app/puzzle/vpuzzlemaingraphicsview.h index 9f6fd4aa9..4bf74cb15 100644 --- a/src/app/puzzle/vpuzzlemaingraphicsview.h +++ b/src/app/puzzle/vpuzzlemaingraphicsview.h @@ -50,6 +50,8 @@ public: */ void RefreshLayout(); + void AddPiece(VPuzzlePiece *piece, QPointF pos); + protected: void dragEnterEvent(QDragEnterEvent *event) override; void dragMoveEvent(QDragMoveEvent *event) override; @@ -60,8 +62,11 @@ protected: private: Q_DISABLE_COPY(VPuzzleMainGraphicsView) - VPuzzleGraphicsLayout *m_graphicsLayout{nullptr}; VPuzzleMainGraphicsScene *m_scene{nullptr}; + + VPuzzleGraphicsLayout *m_graphicsLayout{nullptr}; + QList m_graphicsPieces; + }; #endif // VPUZZLEMAINGRAPHICVIEW_H diff --git a/src/app/puzzle/vpuzzlepiece.cpp b/src/app/puzzle/vpuzzlepiece.cpp index b01f9b59b..66b3d377a 100644 --- a/src/app/puzzle/vpuzzlepiece.cpp +++ b/src/app/puzzle/vpuzzlepiece.cpp @@ -101,6 +101,8 @@ bool VPuzzlePiece::GetShowSeamLine() void VPuzzlePiece::SetShowSeamLine(bool value) { m_showSeamline = value; + + emit PropertiesChanged(); } //--------------------------------------------------------------------------------------------------------------------- @@ -113,5 +115,53 @@ bool VPuzzlePiece::GetPieceMirrored() void VPuzzlePiece::SetPieceMirrored(bool value) { m_mirrorPiece = value; + + emit PropertiesChanged(); } +//--------------------------------------------------------------------------------------------------------------------- +void VPuzzlePiece::SetPosition(QPointF point) +{ + m_transform.translate(point.x() - m_transform.dx(), point.y() - m_transform.dy()); + + emit PositionChanged(); +} + +//--------------------------------------------------------------------------------------------------------------------- +QPointF VPuzzlePiece::GetPosition() +{ + return QPointF(m_transform.dx(), m_transform.dy()); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPuzzlePiece::SetRotation(qreal angle) +{ + Q_UNUSED(angle); + //TODO + + emit RotationChanged(); +} + +//--------------------------------------------------------------------------------------------------------------------- +qreal VPuzzlePiece::GetRotation() +{ + // TODO + return 0; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPuzzlePiece::SetIsSelected(bool value) +{ + m_isSelected = value; + + emit SelectionChanged(); +} + +//--------------------------------------------------------------------------------------------------------------------- +bool VPuzzlePiece::GetIsSelected() +{ + return m_isSelected; +} + + + diff --git a/src/app/puzzle/vpuzzlepiece.h b/src/app/puzzle/vpuzzlepiece.h index 735707e1c..4f5b37b11 100644 --- a/src/app/puzzle/vpuzzlepiece.h +++ b/src/app/puzzle/vpuzzlepiece.h @@ -31,9 +31,11 @@ #include #include #include +#include -class VPuzzlePiece +class VPuzzlePiece : public QObject { + Q_OBJECT public: VPuzzlePiece(); ~VPuzzlePiece(); @@ -109,14 +111,84 @@ public: */ void SetPieceMirrored(bool value); + /** + * @brief SetPosition Sets the position of the piece, in relation to the origin of the scene + * @param point the point where to set the piece + */ + void SetPosition(QPointF point); + + /** + * @brief GetPosition Returns the position of the piece + * @return the position of the piece + */ + QPointF GetPosition(); + + /** + * @brief SetRotation Sets the rotation of the piece to the given angle. + * @param angle the angle of the rotation + */ + void SetRotation(qreal angle); + + /** + * @brief GetRotation Returns the angle of rotation + * @return the angle of rotation + */ + qreal GetRotation(); + + /** + * @brief SetIsSelected Sets wether the piece is selected + * @param value true if the piece is selected + */ + void SetIsSelected(bool value); + + /** + * @brief GetIsSelected Returns wether the piece is selected + * @return true if the piece is selected + */ + bool GetIsSelected(); + + +signals: + /** + * @brief SelectionChanged emited when the selection of the piece was + * changed through the SetIsSelected function + */ + void SelectionChanged(); + + /** + * @brief PositionChanged emited when the position of the piece was + * changed through the SetPosition function + */ + void PositionChanged(); + + /** + * @brief RotationChanged emited when the position of the piece was + * changed through the function SetRotation + */ + void RotationChanged(); + + /** + * @brief PropertiesChanged emited when of the properties showSemaline + * or mirrorpiece where changed. + */ + void PropertiesChanged(); + + /** + * @brief LayerChanged emited when the piece's layer was changed. + */ + void LayerChanged(); private: QUuid m_uuid{QUuid()}; QString m_name{QString()}; QVector m_cuttingLine{QVector()}; QVector m_seamLine{QVector()}; + + QTransform m_transform{QTransform()}; + bool m_showSeamline{true}; bool m_mirrorPiece{false}; + bool m_isSelected{false}; }; #endif // VPUZZLEPIECE_H