Restore adding pieces on sheet.

This commit is contained in:
Roman Telezhynskyi 2021-07-30 14:49:38 +03:00
parent ec6c1cad23
commit 5242b4ed54
7 changed files with 93 additions and 32 deletions

View File

@ -110,7 +110,7 @@ void VPCarrouselPieceList::mousePressEvent(QMouseEvent *event)
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::mouseMoveEvent(QMouseEvent *event)
{
if (((event->buttons() & Qt::LeftButton) != 0u) &&
if (((event->buttons() & Qt::LeftButton) != 0U) &&
((event->pos() - m_dragStart).manhattanLength() >= QApplication::startDragDistance()) &&
(selectedItems().count() > 0) &&
(not m_pieceList.isEmpty() && m_pieceList.first()->Sheet() == nullptr)) // only if it's from unplaced pieces
@ -123,7 +123,6 @@ void VPCarrouselPieceList::mouseMoveEvent(QMouseEvent *event)
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::startDrag(Qt::DropActions supportedActions)
{
@ -139,7 +138,6 @@ void VPCarrouselPieceList::startDrag(Qt::DropActions supportedActions)
auto *mimeData = new VPMimeDataPiece();
VPPiece* piece = pieceItem->GetPiece();
mimeData->SetPiecePtr(piece);
mimeData->setObjectName("piecePointer");
QPixmap pixmap = pieceItem->CreatePieceIcon(QSize(120,120), true).pixmap(QSize(120,120));
@ -147,8 +145,7 @@ void VPCarrouselPieceList::startDrag(Qt::DropActions supportedActions)
drag->setMimeData(mimeData);
if(drag->exec() == Qt::MoveAction)
{
delete takeItem(row(_item));
clearSelection();
m_carrousel->Refresh();
piece->SetIsSelected(true);
}
}
@ -193,19 +190,28 @@ void VPCarrouselPieceList::contextMenuEvent(QContextMenuEvent *event)
QAction *selectedAction = menu.exec(event->globalPos());
VPPiece *piece = pieceItem->GetPiece();
VPLayout *layout = piece->Layout();
if (selectedAction == moveAction)
{
VPSheet *sheet = pieceItem->GetPiece()->Layout()->GetFocusedSheet();
pieceItem->GetPiece()->SetSheet(sheet);
VPSheet *sheet = layout->GetFocusedSheet();
piece->SetSheet(sheet);
emit layout->PieceSheetChanged(piece);
m_carrousel->Refresh();
}
else if (selectedAction == deleteAction)
{
VPSheet *sheet = pieceItem->GetPiece()->Layout()->GetTrashSheet();
pieceItem->GetPiece()->SetSheet(sheet);
VPSheet *sheet = layout->GetTrashSheet();
piece->SetSheet(sheet);
emit layout->PieceSheetChanged(piece);
m_carrousel->Refresh();
}
else if (selectedAction == removeAction)
{
pieceItem->GetPiece()->SetSheet(nullptr);
piece->SetSheet(nullptr);
emit layout->PieceSheetChanged(piece);
m_carrousel->Refresh();
}
}
}

View File

@ -146,6 +146,10 @@ void VPGraphicsPiece::Init()
// TODO : initialises the text labels
// Init position
on_PiecePositionChanged();
on_PieceRotationChanged();
on_PieceSelectionChanged();
// Initialises the connectors
connect(m_piece, &VPPiece::SelectionChanged, this, &VPGraphicsPiece::on_PieceSelectionChanged);

View File

@ -72,6 +72,9 @@ public:
auto PiecesForSheet(const VPSheet* sheet) const -> QList<VPPiece *>;
signals:
void PieceSheetChanged(VPPiece *piece);
private:
Q_DISABLE_COPY(VPLayout)

View File

@ -48,6 +48,7 @@ VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, VPTileFactory *tileFact
VMainGraphicsView(parent),
m_layout(layout)
{
SCASSERT(m_layout != nullptr)
// TODO : list of scenes
m_scene = new VMainGraphicsScene(this);
setScene(m_scene);
@ -60,6 +61,9 @@ VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, VPTileFactory *tileFact
m_graphicsTileGrid = new VPGraphicsTileGrid(layout, tileFactory);
m_scene->addItem(m_graphicsTileGrid);
// add the connections
connect(m_layout, &VPLayout::PieceSheetChanged, this, &VPMainGraphicsView::on_PieceSheetChanged);
}
//---------------------------------------------------------------------------------------------------------------------
@ -80,7 +84,6 @@ VMainGraphicsScene* VPMainGraphicsView::GetScene()
return m_scene;
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::PrepareForExport()
{
@ -114,7 +117,7 @@ void VPMainGraphicsView::dragEnterEvent(QDragEnterEvent *event)
{
const QMimeData *mime = event->mimeData();
if(mime->objectName() == "piecePointer")
if(mime->hasFormat(VPMimeDataPiece::mineFormatPiecePtr))
{
qCDebug(pMainGraphicsView(), "drag enter");
event->acceptProposedAction();
@ -126,7 +129,7 @@ void VPMainGraphicsView::dragMoveEvent(QDragMoveEvent *event)
{
const QMimeData *mime = event->mimeData();
if(mime->objectName() == "piecePointer")
if(mime->hasFormat(VPMimeDataPiece::mineFormatPiecePtr))
{
event->acceptProposedAction();
}
@ -145,9 +148,9 @@ void VPMainGraphicsView::dropEvent(QDropEvent *event)
qCDebug(pMainGraphicsView(), "drop enter , %s", qUtf8Printable(mime->objectName()));
if(mime->objectName() == "piecePointer")
if(mime->hasFormat(VPMimeDataPiece::mineFormatPiecePtr))
{
const VPMimeDataPiece *mimePiece = qobject_cast<const VPMimeDataPiece *> (mime);
const auto *mimePiece = qobject_cast<const VPMimeDataPiece *> (mime);
VPPiece *piece = mimePiece->GetPiecePtr();
if(piece != nullptr)
@ -157,9 +160,17 @@ void VPMainGraphicsView::dropEvent(QDropEvent *event)
QPoint point = event->pos();
piece->SetPosition(mapToScene(point));
piece->SetRotation(0);
// change the piecelist of the piece
piece->SetSheet(m_layout->GetFocusedSheet());
auto *graphicsPiece = new VPGraphicsPiece(piece);
m_graphicsPieces.append(graphicsPiece);
scene()->addItem(graphicsPiece);
event->acceptProposedAction();
}
}
}
@ -171,7 +182,7 @@ void VPMainGraphicsView::keyPressEvent(QKeyEvent *event)
{
QList<VPGraphicsPiece*> tmpGraphicsPieces = m_graphicsPieces;
for(auto graphicsPiece : tmpGraphicsPieces)
for(auto *graphicsPiece : tmpGraphicsPieces)
{
VPPiece *piece = graphicsPiece->GetPiece();
@ -183,3 +194,34 @@ void VPMainGraphicsView::keyPressEvent(QKeyEvent *event)
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::on_PieceSheetChanged(VPPiece *piece)
{
VPGraphicsPiece *_graphicsPiece = nullptr;
for(auto *graphicPiece : m_graphicsPieces)
{
if(graphicPiece->GetPiece() == piece)
{
_graphicsPiece = graphicPiece;
}
}
if (piece->Sheet() == nullptr || piece->Sheet() == m_layout->GetTrashSheet()) // remove
{
if (_graphicsPiece != nullptr)
{
scene()->removeItem(_graphicsPiece);
m_graphicsPieces.removeAll(_graphicsPiece);
}
}
else // add
{
if(_graphicsPiece == nullptr)
{
_graphicsPiece = new VPGraphicsPiece(piece);
m_graphicsPieces.append(_graphicsPiece);
}
scene()->addItem(_graphicsPiece);
}
}

View File

@ -68,6 +68,14 @@ public:
*/
void CleanAfterExport();
public slots:
/**
* @brief on_PieceSheetChanged The slot is called when the given piece was moved from the given piece list to
* the other given piece list
* @param piece the piece that was moved
*/
void on_PieceSheetChanged(VPPiece *piece);
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
@ -81,13 +89,13 @@ protected:
private:
Q_DISABLE_COPY(VPMainGraphicsView)
VMainGraphicsScene *m_scene{nullptr};
VMainGraphicsScene *m_scene;
VPGraphicsSheet *m_graphicsSheet{nullptr};
VPGraphicsTileGrid *m_graphicsTileGrid{nullptr};
VPLayout *m_layout{nullptr};
VPLayout *m_layout;
QList<VPGraphicsPiece*> m_graphicsPieces{};

View File

@ -28,20 +28,16 @@
#include "vpmimedatapiece.h"
//---------------------------------------------------------------------------------------------------------------------
VPMimeDataPiece::VPMimeDataPiece()
{
const QString VPMimeDataPiece::mineFormatPiecePtr = QStringLiteral("application/vnd.puzzle.piece.ptr");
//---------------------------------------------------------------------------------------------------------------------
auto VPMimeDataPiece::formats() const -> QStringList
{
return {mineFormatPiecePtr};
}
//---------------------------------------------------------------------------------------------------------------------
VPMimeDataPiece::~VPMimeDataPiece()
{
}
//---------------------------------------------------------------------------------------------------------------------
VPPiece* VPMimeDataPiece::GetPiecePtr() const
auto VPMimeDataPiece::GetPiecePtr() const -> VPPiece*
{
return m_piece;
}

View File

@ -38,15 +38,16 @@ class VPMimeDataPiece : public QMimeData
Q_OBJECT
public:
VPMimeDataPiece() = default;
virtual ~VPMimeDataPiece() = default;
VPMimeDataPiece();
~VPMimeDataPiece();
virtual auto formats() const -> QStringList override;
/**
* @brief GetPiecePtr Returns the piece pointer of the mime data
* @return piece pointer
*/
VPPiece* GetPiecePtr() const;
auto GetPiecePtr() const -> VPPiece*;
/**
* @brief SetPiecePtr sets the piece pointer to the given value
@ -54,11 +55,12 @@ public:
*/
void SetPiecePtr(VPPiece* piece);
static const QString mineFormatPiecePtr;
private:
Q_DISABLE_COPY(VPMimeDataPiece)
VPPiece *m_piece{nullptr};
};