valentina/src/app/puzzle/vpcarrouselpiecelist.cpp

238 lines
8.1 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-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) :
QListWidget(parent)
2020-04-26 12:09:28 +02:00
{
2020-05-24 14:55:03 +02:00
// Init();
setStyleSheet("QListWidget::item{background-color:transparent; border: 2px solid transparent; color: black;} QListWidget::item:selected {background-color:transparent; border: 2px solid red; color: black; selection-background-color: white;}");
setContextMenuPolicy(Qt::CustomContextMenu);
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-23 14:33:02 +02:00
void VPCarrouselPieceList::Init()
2020-04-26 12:09:28 +02:00
{
2020-05-24 14:55:03 +02:00
// // add the connections
// connect(m_pieceList, &VPPieceList::PieceAdded, this, &VPCarrouselPieceList::on_PieceAdded);
// connect(m_pieceList, &VPPieceList::PieceRemoved, this, &VPCarrouselPieceList::on_PieceRemoved);
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)
{
// 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
// sort the pieces in alphabetical order
std::sort(pieces.begin(), pieces.end(),
[](const VPPiece* a, const VPPiece* b) -> bool { return a->GetName() < b->GetName();});
2020-04-26 22:32:08 +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 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)
{
qCDebug(pCarrouselPieceList, "mouse pressed");
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
qCDebug(pCarrouselPieceList, "mouse moved");
if ((event->buttons() & Qt::LeftButton) &&
((event->pos() - m_dragStart).manhattanLength() >= QApplication::startDragDistance()) &&
(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 14:55:03 +02:00
qCDebug(pCarrouselPieceList, "start drag");
QListWidgetItem* item = currentItem();
if(item->type() == 1001)
{
VPCarrouselPiece *pieceItem = static_cast<VPCarrouselPiece *> (item);
// starts the dragging
QDrag *drag = new QDrag(this);
VPMimeDataPiece *mimeData = new VPMimeDataPiece();
mimeData->SetPiecePtr(pieceItem->GetPiece()); //TODO
mimeData->setObjectName("piecePointer");
QPixmap pixmap = pieceItem->GetPiece()->PieceIcon(QSize(120,120)).pixmap(QSize(120,120));
drag->setPixmap(pixmap);
drag->setMimeData(mimeData);
if(drag->exec() == Qt::MoveAction)
{
delete takeItem(row(item));
clearSelection();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
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");
e->acceptProposedAction();
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:42:51 +02:00
void VPCarrouselPieceList::on_PieceAdded(VPPiece* piece)
{
Q_UNUSED(piece)
// TODO/ FIXME: see if we find a solution more efficient refreshing the complete layout everytime.
Refresh();
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:42:51 +02:00
void VPCarrouselPieceList::on_PieceRemoved(VPPiece* piece)
{
2020-05-24 14:55:03 +02:00
// TODO
Q_UNUSED(piece)
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_SelectionChangedInternal()
{
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);
blockSignals(true);
itemPiece->GetPiece()->SetIsSelected(itemPiece->isSelected());
blockSignals(false);
}
}
}
2020-05-24 14:55:03 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_SelectionChangedExternal()
{
for(int i = 0; i < count(); ++i)
{
QListWidgetItem* _item = item(i);
if(_item->type() == 1001)
{
VPCarrouselPiece *itemPiece = static_cast<VPCarrouselPiece *> (_item);
blockSignals(true);
itemPiece->RefreshSelection();
blockSignals(false);
}
}
}