context menu for carrousel piece and graphics piece

This commit is contained in:
Ronan Le Tiec 2020-05-09 09:54:56 +02:00
parent f516d80513
commit e72a664c8a
4 changed files with 122 additions and 5 deletions

View File

@ -35,6 +35,7 @@
#include <QDrag> #include <QDrag>
#include <QPainter> #include <QPainter>
#include <QApplication> #include <QApplication>
#include <QMenu>
#include "vpuzzlemimedatapiece.h" #include "vpuzzlemimedatapiece.h"
#include "vpiececarrousellayer.h" #include "vpiececarrousellayer.h"
@ -229,3 +230,56 @@ void VPieceCarrouselPiece::mouseMoveEvent(QMouseEvent *event)
drag->setMimeData(mimeData); drag->setMimeData(mimeData);
drag->exec(); drag->exec();
} }
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::contextMenuEvent(QContextMenuEvent *event)
{
QMenu contextMenu;
VPuzzleLayer* unplacedLayer = m_piece->GetLayer()->GetLayout()->GetUnplacedPiecesLayer();
QList<VPuzzleLayer*> layers = m_piece->GetLayer()->GetLayout()->GetLayers();
// move to layer actions -- TODO : To be tested properly when we have several layers
layers.removeAll(m_piece->GetLayer());
if(layers.count() > 0)
{
QMenu *moveMenu = contextMenu.addMenu(tr("Move to"));
// TODO order in alphabetical order
for (auto layer : layers)
{
QAction* moveToLayer = moveMenu->addAction(layer->GetName());
QVariant data = QVariant::fromValue(layer);
moveToLayer->setData(data);
connect(moveToLayer, &QAction::triggered, this, &VPieceCarrouselPiece::on_ActionPieceMovedToLayer);
}
}
// remove from layout action
if(m_piece->GetLayer() != unplacedLayer)
{
QAction *removeAction = contextMenu.addAction(tr("Remove from Layout"));
QVariant data = QVariant::fromValue(m_piece->GetLayer()->GetLayout()->GetUnplacedPiecesLayer());
removeAction->setData(data);
connect(removeAction, &QAction::triggered, this, &VPieceCarrouselPiece::on_ActionPieceMovedToLayer);
}
contextMenu.exec(event->globalPos());
}
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::on_ActionPieceMovedToLayer()
{
QAction *act = qobject_cast<QAction *>(sender());
QVariant v = act->data();
VPuzzleLayer *layer = (VPuzzleLayer *) v.value<VPuzzleLayer *>();
if(layer != nullptr)
{
layer->GetLayout()->MovePieceToLayer(m_piece, layer);
}
}

View File

@ -68,6 +68,15 @@ protected:
void mouseMoveEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override;
private slots:
/**
* @brief on_ActionPieceMovedToLayer Slot called when the piece is moved via the
* context menu to anoter layer
*/
void on_ActionPieceMovedToLayer();
private: private:
Q_DISABLE_COPY(VPieceCarrouselPiece) Q_DISABLE_COPY(VPieceCarrouselPiece)

View File

@ -34,8 +34,12 @@
#include <QCursor> #include <QCursor>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QStyleOptionGraphicsItem> #include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneContextMenuEvent>
#include <QMenu>
#include "vpuzzlepiece.h" #include "vpuzzlepiece.h"
#include "vpuzzlelayer.h"
#include "vpuzzlelayout.h"
#include <QLoggingCategory> #include <QLoggingCategory>
Q_LOGGING_CATEGORY(pGraphicsPiece, "p.graphicsPiece") Q_LOGGING_CATEGORY(pGraphicsPiece, "p.graphicsPiece")
@ -193,20 +197,61 @@ void VPuzzleGraphicsPiece::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
//perform the default behaviour //perform the default behaviour
QGraphicsItem::mouseReleaseEvent(event); QGraphicsItem::mouseReleaseEvent(event);
qCDebug(pGraphicsPiece, "piiiiieeece --- mouse release");
// change the cursor when clicking left button // change the cursor when clicking left button
if (event->button() == Qt::LeftButton) if (event->button() == Qt::LeftButton)
{ {
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
qCDebug(pGraphicsPiece, "piiiiieeece --- left button");
setSelected(selectionState); setSelected(selectionState);
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
QMenu contextMenu;
// move to layer actions -- TODO : To be tested properly when we have several layers
QList<VPuzzleLayer*> layers = m_piece->GetLayer()->GetLayout()->GetLayers();
layers.removeAll(m_piece->GetLayer());
if(layers.count() > 0)
{
QMenu *moveMenu = contextMenu.addMenu(tr("Move to"));
// TODO order in alphabetical order
for (auto layer : layers)
{
QAction* moveToLayer = moveMenu->addAction(layer->GetName());
QVariant data = QVariant::fromValue(layer);
moveToLayer->setData(data);
connect(moveToLayer, &QAction::triggered, this, &VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer);
}
}
// remove from layout action
QAction *removeAction = contextMenu.addAction(tr("Remove from Layout"));
QVariant data = QVariant::fromValue(m_piece->GetLayer()->GetLayout()->GetUnplacedPiecesLayer());
removeAction->setData(data);
connect(removeAction, &QAction::triggered, this, &VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer);
contextMenu.exec(event->screenPos());
}
//---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::on_ActionPieceMovedToLayer()
{
QAction *act = qobject_cast<QAction *>(sender());
QVariant v = act->data();
VPuzzleLayer *layer = (VPuzzleLayer *) v.value<VPuzzleLayer *>();
if(layer != nullptr)
{
layer->GetLayout()->MovePieceToLayer(m_piece, layer);
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleGraphicsPiece::on_PieceSelectionChanged() void VPuzzleGraphicsPiece::on_PieceSelectionChanged()

View File

@ -68,6 +68,15 @@ protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
private slots:
/**
* @brief on_ActionPieceMovedToLayer Slot called when the piece is moved via the
* context menu to anoter layer
*/
void on_ActionPieceMovedToLayer();
private: private:
Q_DISABLE_COPY(VPuzzleGraphicsPiece) Q_DISABLE_COPY(VPuzzleGraphicsPiece)
VPuzzlePiece *m_piece; VPuzzlePiece *m_piece;