valentina/src/app/puzzle/vpcarrouselpiecelist.cpp

315 lines
11 KiB
C++
Raw Normal View History

2020-04-26 12:09:28 +02:00
/************************************************************************
**
2020-05-23 14:33:02 +02:00
** @file vpcarrouselpiecelist.cpp
2020-04-26 12:09:28 +02:00
** @author Ronan Le Tiec
** @date 25 4, 2020
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2020 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
2020-05-23 14:33:02 +02:00
#include "vpcarrouselpiecelist.h"
2020-05-24 14:55:03 +02:00
#include <QDrag>
#include <QDragMoveEvent>
#include <QPainter>
#include <QApplication>
2020-05-24 19:53:51 +02:00
#include <QMenu>
2020-05-24 14:55:03 +02:00
2020-05-23 14:29:18 +02:00
#include "vpcarrousel.h"
2020-05-24 14:55:03 +02:00
#include "vpcarrouselpiece.h"
2020-05-09 12:57:42 +02:00
#include "../vmisc/backport/qoverload.h"
2020-05-24 14:55:03 +02:00
#include "vpmimedatapiece.h"
2020-04-26 12:09:28 +02:00
#include <QLoggingCategory>
2020-05-23 15:29:57 +02:00
Q_LOGGING_CATEGORY(pCarrouselPieceList, "p.carrouselPieceList")
2020-04-26 12:09:28 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-24 14:55:03 +02:00
VPCarrouselPieceList::VPCarrouselPieceList(QWidget* parent) :
2020-05-24 19:53:51 +02:00
QListWidget(parent),
m_dragStart(QPoint())
2020-04-26 12:09:28 +02:00
{
setStyleSheet("QListWidget::item{border: 2px solid transparent; color: black;} QListWidget::item:selected {border: 2px solid rgb(255,160,160);}");
2020-05-24 19:53:51 +02:00
setContextMenuPolicy(Qt::DefaultContextMenu);
2020-05-24 14:55:03 +02:00
setSelectionMode(QAbstractItemView::MultiSelection);
setViewMode(QListView::IconMode);
connect(this, &VPCarrouselPieceList::itemSelectionChanged, this, &VPCarrouselPieceList::on_SelectionChangedInternal);
2020-04-26 12:09:28 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:33:02 +02:00
VPCarrouselPieceList::~VPCarrouselPieceList()
2020-04-26 12:09:28 +02:00
{
2020-05-24 14:55:03 +02:00
2020-04-26 12:09:28 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-24 19:53:51 +02:00
void VPCarrouselPieceList::SetCarrousel(VPCarrousel *carrousel)
2020-04-26 12:09:28 +02:00
{
2020-05-24 19:53:51 +02:00
m_carrousel = carrousel;
2020-04-26 12:09:28 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:33:02 +02:00
void VPCarrouselPieceList::Refresh()
2020-04-26 12:09:28 +02:00
{
2020-05-24 14:55:03 +02:00
clear();
2020-04-26 12:09:28 +02:00
2020-05-24 14:55:03 +02:00
if(m_pieceList != nullptr)
{
2020-05-24 19:53:51 +02:00
m_pieceList->disconnect(this);
2020-05-24 14:55:03 +02:00
// Updates the carrousel pieces from the pieces list
QList<VPPiece*> pieces = m_pieceList->GetPieces();
2020-04-26 14:03:43 +02:00
2020-05-24 14:55:03 +02:00
// create the corresponding carrousel pieces
for (auto piece : pieces)
{
// update the label of the piece
VPCarrouselPiece* carrouselpiece = new VPCarrouselPiece(piece,this);
carrouselpiece->setSelected(piece->GetIsSelected());
connect(piece, &VPPiece::SelectionChanged, this, &VPCarrouselPieceList::on_SelectionChangedExternal);
}
2020-05-24 19:53:51 +02:00
sortItems();
connect(m_pieceList, &VPPieceList::PieceAdded, this, &VPCarrouselPieceList::on_PieceAdded);
connect(m_pieceList, &VPPieceList::PieceRemoved, this, &VPCarrouselPieceList::on_PieceRemoved);
}
2020-05-24 14:55:03 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
VPPieceList* VPCarrouselPieceList::GetCurrentPieceList()
{
return m_pieceList;
}
2020-04-26 22:32:08 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-24 14:55:03 +02:00
void VPCarrouselPieceList::SetCurrentPieceList(VPPieceList* pieceList)
{
2020-05-24 14:55:03 +02:00
m_pieceList = pieceList;
Refresh();
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
2020-05-24 14:55:03 +02:00
m_dragStart = event->pos();
}
2020-05-24 14:55:03 +02:00
if (!(event->modifiers() & Qt::ControlModifier))
{
// clearSelection doesn't work properly here so we go through the elements.
for(auto item: selectedItems())
{
2020-05-24 14:55:03 +02:00
item->setSelected(false);
}
2020-04-26 12:09:28 +02:00
}
2020-05-24 14:55:03 +02:00
QListWidget::mousePressEvent(event);
2020-04-26 12:09:28 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-24 14:55:03 +02:00
void VPCarrouselPieceList::mouseMoveEvent(QMouseEvent *event)
{
2020-05-24 14:55:03 +02:00
if ((event->buttons() & Qt::LeftButton) &&
((event->pos() - m_dragStart).manhattanLength() >= QApplication::startDragDistance()) &&
2020-05-24 19:53:51 +02:00
(selectedItems().count() > 0) &&
2020-05-24 14:55:03 +02:00
(m_pieceList->GetSheet() == nullptr)) // only if it's from unplaced pieces
{
startDrag(Qt::MoveAction);
}
else
{
QListWidget::mouseMoveEvent(event);
}
}
2020-05-24 14:55:03 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-24 14:55:03 +02:00
void VPCarrouselPieceList::startDrag(Qt::DropActions supportedActions)
{
2020-05-24 19:53:51 +02:00
Q_UNUSED(supportedActions)
2020-05-24 14:55:03 +02:00
2020-05-24 19:53:51 +02:00
QListWidgetItem* _item = currentItem();
if(_item->type() == 1001)
2020-05-24 14:55:03 +02:00
{
2020-05-24 19:53:51 +02:00
VPCarrouselPiece *pieceItem = static_cast<VPCarrouselPiece *> (_item);
2020-05-24 14:55:03 +02:00
// starts the dragging
QDrag *drag = new QDrag(this);
VPMimeDataPiece *mimeData = new VPMimeDataPiece();
2020-05-24 19:53:51 +02:00
VPPiece* piece = pieceItem->GetPiece();
mimeData->SetPiecePtr(piece);
2020-05-24 14:55:03 +02:00
mimeData->setObjectName("piecePointer");
QPixmap pixmap = pieceItem->CreatePieceIcon(QSize(120,120), true).pixmap(QSize(120,120));
2020-05-24 14:55:03 +02:00
drag->setPixmap(pixmap);
drag->setMimeData(mimeData);
if(drag->exec() == Qt::MoveAction)
{
2020-05-24 19:53:51 +02:00
delete takeItem(row(_item));
2020-05-24 14:55:03 +02:00
clearSelection();
2020-05-24 19:53:51 +02:00
piece->SetIsSelected(true);
2020-05-24 14:55:03 +02:00
}
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-24 14:55:03 +02:00
void VPCarrouselPieceList::dragMoveEvent(QDragMoveEvent* e)
{
2020-05-24 14:55:03 +02:00
qCDebug(pCarrouselPieceList, "drag move");
2020-05-24 19:53:51 +02:00
e->acceptProposedAction();
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::contextMenuEvent(QContextMenuEvent *event)
{
QListWidgetItem* _item = currentItem();
2020-11-21 14:28:28 +01:00
if(_item != nullptr)
2020-05-24 19:53:51 +02:00
{
2020-11-21 14:28:28 +01:00
if(_item->type() == 1001)
2020-05-24 19:53:51 +02:00
{
2020-11-21 14:28:28 +01:00
VPCarrouselPiece *pieceItem = static_cast<VPCarrouselPiece *> (_item);
QMenu contextMenu;
if(m_pieceList->GetSheet() == nullptr)
{
VPPieceList* sheetPieces = pieceItem->GetPiece()->GetPieceList()->GetLayout()->GetFocusedSheet()->GetPieceList();
QAction *moveAction = contextMenu.addAction(tr("Move to Sheet"));
QVariant moveData = QVariant::fromValue(sheetPieces);
moveAction->setData(moveData);
VPPieceList* trashPieceList = pieceItem->GetPiece()->GetPieceList()->GetLayout()->GetTrashPieceList();
QAction *deleteAction = contextMenu.addAction(tr("Delete"));
QVariant deleteData = QVariant::fromValue(trashPieceList);
deleteAction->setData(deleteData);
connect(moveAction, &QAction::triggered, this, &VPCarrouselPieceList::on_ActionPieceMovedToPieceList);
connect(deleteAction, &QAction::triggered, this, &VPCarrouselPieceList::on_ActionPieceMovedToPieceList);
}
// remove from piece list action
if(m_pieceList->GetSheet() != nullptr)
{
VPPieceList* unplacedPieces = pieceItem->GetPiece()->GetPieceList()->GetLayout()->GetUnplacedPieceList();
QAction *removeAction = contextMenu.addAction(tr("Remove from Sheet"));
QVariant data = QVariant::fromValue(unplacedPieces);
removeAction->setData(data);
connect(removeAction, &QAction::triggered, this, &VPCarrouselPieceList::on_ActionPieceMovedToPieceList);
}
contextMenu.exec(event->globalPos());
2020-05-24 19:53:51 +02:00
}
}
}
2020-05-24 19:53:51 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-24 19:53:51 +02:00
void VPCarrouselPieceList::on_ActionPieceMovedToPieceList()
{
2020-05-24 19:53:51 +02:00
QListWidgetItem* _item = currentItem();
if(_item->type() == 1001)
{
VPCarrouselPiece *pieceItem = static_cast<VPCarrouselPiece *> (_item);
QAction *act = qobject_cast<QAction *>(sender());
QVariant v = act->data();
VPPieceList *pieceList = v.value<VPPieceList *>();
if(pieceList != nullptr)
{
pieceList->GetLayout()->MovePieceToPieceList(pieceItem->GetPiece(), pieceList);
}
}
}
2020-05-24 19:53:51 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_PieceAdded(VPPiece* piece)
{
if(piece->GetPieceList() == m_pieceList)
{
// update the label of the piece
VPCarrouselPiece* carrouselpiece = new VPCarrouselPiece(piece,this);
carrouselpiece->setSelected(piece->GetIsSelected());
connect(piece, &VPPiece::SelectionChanged, this, &VPCarrouselPieceList::on_SelectionChangedExternal);
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:42:51 +02:00
void VPCarrouselPieceList::on_PieceRemoved(VPPiece* piece)
{
2020-05-24 19:53:51 +02:00
for(int i = 0; i < count(); ++i)
{
QListWidgetItem* _item = item(i);
if(_item->type() == 1001)
{
VPCarrouselPiece *itemPiece = static_cast<VPCarrouselPiece *> (_item);
if(piece == itemPiece->GetPiece())
{
delete takeItem(row(_item));
return;
}
}
}
2020-05-24 14:55:03 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_SelectionChangedInternal()
{
2020-05-24 19:53:51 +02:00
blockSignals(true);
2020-05-24 14:55:03 +02:00
for(int i = 0; i < count(); ++i)
{
2020-05-24 14:55:03 +02:00
QListWidgetItem* _item = item(i);
if(_item->type() == 1001)
{
2020-05-24 14:55:03 +02:00
VPCarrouselPiece *itemPiece = static_cast<VPCarrouselPiece *> (_item);
itemPiece->GetPiece()->SetIsSelected(itemPiece->isSelected());
}
}
2020-05-24 19:53:51 +02:00
m_carrousel->ClearSelectionExceptForCurrentPieceList();
// TODO FIXME: when selecting pieces on the sheet, and then selecting a unplaced piece in the piece carrousel
// the selection is cleared in the sheet (good !) but the cliked item in unplaced pieces in not selected (bad!)
blockSignals(false);
2020-05-24 14:55:03 +02:00
}
2020-05-24 14:55:03 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_SelectionChangedExternal()
{
2020-05-24 19:53:51 +02:00
blockSignals(true);
2020-05-24 14:55:03 +02:00
for(int i = 0; i < count(); ++i)
{
QListWidgetItem* _item = item(i);
if(_item->type() == 1001)
{
VPCarrouselPiece *itemPiece = static_cast<VPCarrouselPiece *> (_item);
itemPiece->RefreshSelection();
}
}
2020-05-24 19:53:51 +02:00
blockSignals(false);
}