work on piece selection and position

This commit is contained in:
Ronan Le Tiec 2020-05-05 17:40:36 +02:00
parent c4dbea1d56
commit e2b816f192
16 changed files with 353 additions and 135 deletions

View File

@ -189,6 +189,11 @@ VPuzzlePiece* PuzzleMainWindow::CreatePiece(const VLayoutPiece &rawPiece)
// TODO : set all the information we need for the piece! // 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; return piece;
} }
@ -228,9 +233,9 @@ void PuzzleMainWindow::InitPropertyTabCurrentPiece()
// ------------------------------ placement ----------------------------------- // ------------------------------ placement -----------------------------------
connect(ui->doubleSpinBoxCurrentPieceBoxPositionX, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, connect(ui->doubleSpinBoxCurrentPieceBoxPositionX, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&PuzzleMainWindow::on_CurrentPiecePositionChanged); &PuzzleMainWindow::on_CurrentPiecePositionEdited);
connect(ui->doubleSpinBoxCurrentPieceBoxPositionY, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, connect(ui->doubleSpinBoxCurrentPieceBoxPositionY, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&PuzzleMainWindow::on_CurrentPiecePositionChanged); &PuzzleMainWindow::on_CurrentPiecePositionEdited);
} }
@ -310,9 +315,6 @@ void PuzzleMainWindow::InitPieceCarrousel()
connect(ui->dockWidgetPieceCarrousel, QOverload<Qt::DockWidgetArea>::of(&QDockWidget::dockLocationChanged), this, connect(ui->dockWidgetPieceCarrousel, QOverload<Qt::DockWidgetArea>::of(&QDockWidget::dockLocationChanged), this,
&PuzzleMainWindow::on_PieceCarrouselLocationChanged); &PuzzleMainWindow::on_PieceCarrouselLocationChanged);
connect(m_pieceCarrousel, QOverload<VPuzzlePiece*>::of(&VPieceCarrousel::pieceClicked), this,
&PuzzleMainWindow::on_PieceSelected);
} }
@ -335,31 +337,34 @@ void PuzzleMainWindow::SetPropertiesData()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::SetPropertyTabCurrentPieceData() 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" // TODO : update current piece data to show a "no current piece selected"
ui->containerCurrentPieceNoData->setVisible(true); ui->containerCurrentPieceNoData->setVisible(true);
ui->containerCurrentPieceData->setVisible(false); ui->containerCurrentPieceData->setVisible(false);
} }
} else if(m_selectedPieces.count() == 1)
else
{ {
VPuzzlePiece *selectedPiece = m_selectedPieces.first();
ui->containerCurrentPieceNoData->setVisible(false); ui->containerCurrentPieceNoData->setVisible(false);
ui->containerCurrentPieceData->setVisible(true); ui->containerCurrentPieceData->setVisible(true);
// set the value to the current piece // 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->checkBoxCurrentPieceShowSeamline->setChecked(selectedPiece->GetShowSeamLine());
ui->checkBoxCurrentPieceMirrorPiece->setChecked(m_selectedPiece->GetPieceMirrored()); 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) 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) 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: // ui->doubleSpinBoxCurrentPieceBoxPositionX->blockSignals(true);
QMessageBox msgBox; // ui->doubleSpinBoxCurrentPieceBoxPositionY->blockSignals(true);
msgBox.setText("TODO PuzzleMainWindow::CurrentPiecePositionChanged");
int ret = msgBox.exec();
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 for (auto piece : m_selectedPieces)
m_pieceCarrousel->SelectPiece(piece); {
if(piece->GetIsSelected())
// update the Layout {
piece->SetIsSelected(false);
// TODO }
}
m_selectedPieces = m_layout->GetSelectedPieces();
// update the property of the piece currently selected // update the property of the piece currently selected
SetPropertyTabCurrentPieceData(); 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()));
}
}

View File

@ -94,7 +94,7 @@ private:
VPuzzleCommandLinePtr m_cmd; VPuzzleCommandLinePtr m_cmd;
VPuzzleLayout *m_layout{nullptr}; VPuzzleLayout *m_layout{nullptr};
VPuzzlePiece *m_selectedPiece{nullptr}; QList<VPuzzlePiece *>m_selectedPieces{QList<VPuzzlePiece *>()};
/** /**
* @brief CreatePiece creates a piece from the given VLayoutPiece data * @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 * @brief on_CurrentPiecePositionChanged When the positionX or the positionY
* is changed in the current piece tab * is changed in the current piece tab
*/ */
void on_CurrentPiecePositionChanged(); void on_CurrentPiecePositionEdited();
/** /**
* @brief PieceCarrouselLocationChanged When the piece carrousel's location * @brief PieceCarrouselLocationChanged When the piece carrousel's location
@ -364,10 +364,14 @@ private slots:
void on_PieceCarrouselLocationChanged(Qt::DockWidgetArea area); void on_PieceCarrouselLocationChanged(Qt::DockWidgetArea area);
/** /**
* @brief on_PieceSelected When a piece has been selected * @brief on_PieceSelectionChanged When the piece selection has changed
* @param piece the piece that was selected
*/ */
void on_PieceSelected(VPuzzlePiece* piece); void on_PieceSelectionChanged();
/**
* @brief on_PiecePositionChanged When the current piece position has changed
*/
void on_PiecePositionChanged();
}; };

View File

@ -28,11 +28,11 @@
#include "vpiececarrousel.h" #include "vpiececarrousel.h"
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QMessageBox> #include <QMessageBox>
#include <QScrollBar>
#include "../vmisc/backport/qoverload.h" #include "../vmisc/backport/qoverload.h"
#include <QLoggingCategory> #include <QLoggingCategory>
#include <QScrollBar>
Q_LOGGING_CATEGORY(pCarrousel, "p.carrousel") Q_LOGGING_CATEGORY(pCarrousel, "p.carrousel")
@ -118,10 +118,6 @@ void VPieceCarrousel::Refresh()
VPieceCarrouselLayer *carrouselLayer = new VPieceCarrouselLayer(layer, this); VPieceCarrouselLayer *carrouselLayer = new VPieceCarrouselLayer(layer, this);
m_carrouselLayers.append(carrouselLayer); m_carrouselLayers.append(carrouselLayer);
m_layersContainer->layout()->addWidget(carrouselLayer); m_layersContainer->layout()->addWidget(carrouselLayer);
connect(carrouselLayer, QOverload<VPieceCarrouselPiece*>::of(&VPieceCarrouselLayer::pieceClicked), this,
&VPieceCarrousel::on_PieceClicked);
} }
on_ActiveLayerChanged(0); on_ActiveLayerChanged(0);
@ -160,20 +156,6 @@ void VPieceCarrousel::Clear()
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrousel::SelectPiece(VPuzzlePiece* piece)
{
for (auto layer : m_carrouselLayers)
{
QList<VPieceCarrouselPiece*> carrouselPieces = layer->GetCarrouselPieces();
for (auto carrouselPiece : carrouselPieces)
{
carrouselPiece->SetIsSelected(carrouselPiece->GetPiece() == piece);
}
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPieceCarrousel::on_ActiveLayerChanged(int index) 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 // FIXME: find a nicer way than putting directly the 120 width of the piece
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrousel::on_PieceClicked(VPieceCarrouselPiece* carrouselPiece)
{
emit pieceClicked(carrouselPiece->GetPiece());
}

View File

@ -78,13 +78,6 @@ public:
void SelectPiece(VPuzzlePiece* piece); void SelectPiece(VPuzzlePiece* piece);
signals:
void pieceClicked(VPuzzlePiece* piece);
public slots:
void on_PieceClicked(VPieceCarrouselPiece* carrouselPiece);
private: private:
Q_DISABLE_COPY(VPieceCarrousel) Q_DISABLE_COPY(VPieceCarrousel)

View File

@ -89,10 +89,6 @@ void VPieceCarrouselLayer::Refresh()
setVisible(true); setVisible(true);
carrouselPiece->CleanPreview(); carrouselPiece->CleanPreview();
setVisible(false); setVisible(false);
connect(carrouselPiece, QOverload<VPieceCarrouselPiece*>::of(&VPieceCarrouselPiece::clicked), this,
&VPieceCarrouselLayer::on_PieceClicked);
} }
} }
@ -102,8 +98,3 @@ QList<VPieceCarrouselPiece*> VPieceCarrouselLayer::GetCarrouselPieces()
return m_carrouselPieces; return m_carrouselPieces;
} }
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselLayer::on_PieceClicked(VPieceCarrouselPiece* carrouselPiece)
{
emit pieceClicked(carrouselPiece);
}

View File

@ -45,12 +45,6 @@ public:
QList<VPieceCarrouselPiece*> GetCarrouselPieces(); QList<VPieceCarrouselPiece*> GetCarrouselPieces();
signals:
void pieceClicked(VPieceCarrouselPiece* carrouselPiece);
public slots:
void on_PieceClicked(VPieceCarrouselPiece* carrouselPiece);
private: private:
Q_DISABLE_COPY(VPieceCarrouselLayer) Q_DISABLE_COPY(VPieceCarrouselLayer)

View File

@ -92,6 +92,11 @@ void VPieceCarrouselPiece::Init()
pieceLayout->addWidget(m_piecePreview); pieceLayout->addWidget(m_piecePreview);
pieceLayout->addWidget(m_label); pieceLayout->addWidget(m_label);
// connect the signals
connect(m_piece, &VPuzzlePiece::SelectionChanged, this, &VPieceCarrouselPiece::on_PieceSelectionChanged);
// then refresh the data // then refresh the data
Refresh(); Refresh();
} }
@ -142,11 +147,9 @@ VPuzzlePiece * VPieceCarrouselPiece::GetPiece()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::SetIsSelected(bool value) void VPieceCarrouselPiece::on_PieceSelectionChanged()
{ {
m_isSelected = value; if(m_piece->GetIsSelected())
if(value)
{ {
setStyleSheet("background-color:white; border: 2px solid red;"); 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) void VPieceCarrouselPiece::mousePressEvent(QMouseEvent *event)
@ -170,9 +169,9 @@ void VPieceCarrouselPiece::mousePressEvent(QMouseEvent *event)
if (event->button() == Qt::LeftButton) if (event->button() == Qt::LeftButton)
{ {
if(!m_isSelected) if(!m_piece->GetIsSelected())
{ {
emit clicked(this); m_piece->SetIsSelected(true);
} }
m_dragStart = event->pos(); m_dragStart = event->pos();
} }

View File

@ -58,22 +58,8 @@ public:
*/ */
VPuzzlePiece * GetPiece(); 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: public slots:
void on_PieceSelectionChanged();
protected: protected:
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
@ -87,8 +73,6 @@ private:
QLabel *m_label{nullptr}; QLabel *m_label{nullptr};
VPieceCarrouselPiecePreview *m_piecePreview{nullptr}; VPieceCarrouselPiecePreview *m_piecePreview{nullptr};
bool m_isSelected = false;
QPoint m_dragStart; QPoint m_dragStart;
private slots: private slots:

View File

@ -33,11 +33,16 @@
#include <QPainter> #include <QPainter>
#include <QCursor> #include <QCursor>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QStyleOptionGraphicsItem>
#include "vpuzzlepiece.h"
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(pGraphicsPiece, "p.graphicsPiece")
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPuzzleGraphicsPiece::VPuzzleGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent) : VPuzzleGraphicsPiece::VPuzzleGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent) :
QGraphicsItem(parent), QGraphicsObject(parent),
m_piece(piece), m_piece(piece),
m_cuttingLine(QPainterPath()), m_cuttingLine(QPainterPath()),
m_seamLine(QPainterPath()) m_seamLine(QPainterPath())
@ -55,7 +60,7 @@ VPuzzleGraphicsPiece::~VPuzzleGraphicsPiece()
void VPuzzleGraphicsPiece::Init() void VPuzzleGraphicsPiece::Init()
{ {
// set some infos // set some infos
setFlags(ItemIsSelectable | ItemIsMovable); setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
setCursor(QCursor(Qt::OpenHandCursor)); setCursor(QCursor(Qt::OpenHandCursor));
//setAcceptHoverEvents(true); // maybe we can do some stuff with this //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. // 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) void VPuzzleGraphicsPiece::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{ {
Q_UNUSED(widget); Q_UNUSED(widget);
Q_UNUSED(option);
QPen pen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); QPen pen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
if(isSelected())
{
pen.setColor(Qt::red);
}
QBrush noBrush(Qt::NoBrush); QBrush noBrush(Qt::NoBrush);
painter->setPen(pen); painter->setPen(pen);
@ -136,6 +149,8 @@ void VPuzzleGraphicsPiece::mousePressEvent(QGraphicsSceneMouseEvent *event)
return; return;
} }
setSelected(true);
setCursor(Qt::ClosedHandCursor); 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);
}

View File

@ -31,15 +31,27 @@
#include <QGraphicsItem> #include <QGraphicsItem>
#include "vpuzzlepiece.h" class VPuzzlePiece;
class VPuzzleGraphicsPiece : public QGraphicsItem class VPuzzleGraphicsPiece : public QGraphicsObject
{ {
Q_OBJECT
public: public:
VPuzzleGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent = nullptr); VPuzzleGraphicsPiece(VPuzzlePiece *piece, QGraphicsItem *parent = nullptr);
~VPuzzleGraphicsPiece(); ~VPuzzleGraphicsPiece();
void Init(); 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: protected:
QRectF boundingRect() const override; QRectF boundingRect() const override;
QPainterPath shape() const override; QPainterPath shape() const override;
@ -48,6 +60,7 @@ protected:
void mousePressEvent(QGraphicsSceneMouseEvent * event) override; void mousePressEvent(QGraphicsSceneMouseEvent * event) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
private: private:
Q_DISABLE_COPY(VPuzzleGraphicsPiece) Q_DISABLE_COPY(VPuzzleGraphicsPiece)
@ -55,7 +68,6 @@ private:
QPainterPath m_cuttingLine; QPainterPath m_cuttingLine;
QPainterPath m_seamLine; QPainterPath m_seamLine;
}; };
#endif // VPUZZLEGRAPHICSPIECE_H #endif // VPUZZLEGRAPHICSPIECE_H

View File

@ -75,6 +75,29 @@ QList<VPuzzleLayer *> VPuzzleLayout::GetLayers()
return m_layers; return m_layers;
} }
//---------------------------------------------------------------------------------------------------------------------
QList<VPuzzlePiece *> VPuzzleLayout::GetSelectedPieces()
{
QList<VPuzzlePiece *> result = QList<VPuzzlePiece *>();
QList<VPuzzleLayer *> 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) void VPuzzleLayout::SetUnit(Unit unit)
{ {

View File

@ -35,6 +35,7 @@
#include "def.h" #include "def.h"
class VPuzzleLayer; class VPuzzleLayer;
class VPuzzlePiece;
// is this the right place for the definition? // is this the right place for the definition?
enum class FollowGrainline : qint8 { No = 0, Follow90 = 1, Follow180 = 2}; enum class FollowGrainline : qint8 { No = 0, Follow90 = 1, Follow180 = 2};
@ -51,7 +52,22 @@ public:
VPuzzleLayer* AddLayer(VPuzzleLayer *layer); VPuzzleLayer* AddLayer(VPuzzleLayer *layer);
QList<VPuzzleLayer *> GetLayers(); QList<VPuzzleLayer *> GetLayers();
/**
* @brief GetSelectedPieces Returns the list of the selected pieces
* @return the selected pieces
*/
QList<VPuzzlePiece *> GetSelectedPieces();
/**
* @brief SetUnit Sets the unit of the layout to the given unit
* @param unit the new unit
*/
void SetUnit(Unit unit); void SetUnit(Unit unit);
/**
* @brief GetUnit Returns the current unit of the layout
* @return the unit
*/
Unit GetUnit() const; Unit GetUnit() const;
/** /**
@ -133,7 +149,16 @@ public:
*/ */
QMarginsF GetLayoutMarginsConverted() const; QMarginsF GetLayoutMarginsConverted() const;
/**
* @brief SetFollowGrainline Sets the type of grainline for the pieces to follow
* @param state the type of grainline
*/
void SetFollowGrainline(FollowGrainline state); 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; FollowGrainline GetFollowGrainline() const;
/** /**

View File

@ -40,7 +40,8 @@ Q_LOGGING_CATEGORY(pMainGraphicsView, "p.mainGraphicsView")
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPuzzleMainGraphicsView::VPuzzleMainGraphicsView(VPuzzleLayout *layout, QWidget *parent) : VPuzzleMainGraphicsView::VPuzzleMainGraphicsView(VPuzzleLayout *layout, QWidget *parent) :
QGraphicsView(parent) QGraphicsView(parent),
m_graphicsPieces(QList<VPuzzleGraphicsPiece*>())
{ {
m_scene = new VPuzzleMainGraphicsScene(this); m_scene = new VPuzzleMainGraphicsScene(this);
setScene(m_scene); setScene(m_scene);
@ -123,9 +124,22 @@ void VPuzzleMainGraphicsView::dropEvent(QDropEvent *event)
QPointF scenePos = mapToScene(point); QPointF scenePos = mapToScene(point);
// todo take the position into account // todo take the position into account
AddPiece(piece, scenePos);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleMainGraphicsView::AddPiece(VPuzzlePiece *piece, QPointF pos)
{
VPuzzleGraphicsPiece *item = new VPuzzleGraphicsPiece(piece); VPuzzleGraphicsPiece *item = new VPuzzleGraphicsPiece(piece);
item->setPos(scenePos);
m_scene->addItem(item); m_scene->addItem(item);
} item->setSelected(true);
} item->setPos(pos);
item->blockSignals(true);
piece->SetPosition(pos);
item->blockSignals(false);
} }

View File

@ -50,6 +50,8 @@ public:
*/ */
void RefreshLayout(); void RefreshLayout();
void AddPiece(VPuzzlePiece *piece, QPointF pos);
protected: protected:
void dragEnterEvent(QDragEnterEvent *event) override; void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override; void dragMoveEvent(QDragMoveEvent *event) override;
@ -60,8 +62,11 @@ protected:
private: private:
Q_DISABLE_COPY(VPuzzleMainGraphicsView) Q_DISABLE_COPY(VPuzzleMainGraphicsView)
VPuzzleGraphicsLayout *m_graphicsLayout{nullptr};
VPuzzleMainGraphicsScene *m_scene{nullptr}; VPuzzleMainGraphicsScene *m_scene{nullptr};
VPuzzleGraphicsLayout *m_graphicsLayout{nullptr};
QList<VPuzzleGraphicsPiece*> m_graphicsPieces;
}; };
#endif // VPUZZLEMAINGRAPHICVIEW_H #endif // VPUZZLEMAINGRAPHICVIEW_H

View File

@ -101,6 +101,8 @@ bool VPuzzlePiece::GetShowSeamLine()
void VPuzzlePiece::SetShowSeamLine(bool value) void VPuzzlePiece::SetShowSeamLine(bool value)
{ {
m_showSeamline = value; m_showSeamline = value;
emit PropertiesChanged();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -113,5 +115,53 @@ bool VPuzzlePiece::GetPieceMirrored()
void VPuzzlePiece::SetPieceMirrored(bool value) void VPuzzlePiece::SetPieceMirrored(bool value)
{ {
m_mirrorPiece = 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;
}

View File

@ -31,9 +31,11 @@
#include <QUuid> #include <QUuid>
#include <QVector> #include <QVector>
#include <QPoint> #include <QPoint>
#include <QTransform>
class VPuzzlePiece class VPuzzlePiece : public QObject
{ {
Q_OBJECT
public: public:
VPuzzlePiece(); VPuzzlePiece();
~VPuzzlePiece(); ~VPuzzlePiece();
@ -109,14 +111,84 @@ public:
*/ */
void SetPieceMirrored(bool value); 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: private:
QUuid m_uuid{QUuid()}; QUuid m_uuid{QUuid()};
QString m_name{QString()}; QString m_name{QString()};
QVector<QPointF> m_cuttingLine{QVector<QPointF>()}; QVector<QPointF> m_cuttingLine{QVector<QPointF>()};
QVector<QPointF> m_seamLine{QVector<QPointF>()}; QVector<QPointF> m_seamLine{QVector<QPointF>()};
QTransform m_transform{QTransform()};
bool m_showSeamline{true}; bool m_showSeamline{true};
bool m_mirrorPiece{false}; bool m_mirrorPiece{false};
bool m_isSelected{false};
}; };
#endif // VPUZZLEPIECE_H #endif // VPUZZLEPIECE_H