valentina/src/app/puzzle/vpcarrouselpiece.cpp

158 lines
4.7 KiB
C++
Raw Normal View History

2020-04-26 12:09:28 +02:00
/************************************************************************
**
2020-05-23 14:36:35 +02:00
** @file vpcarrouselpiece.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:36:35 +02:00
#include "vpcarrouselpiece.h"
2020-05-05 07:44:20 +02:00
#include <QApplication>
#include <QMenu>
2020-05-24 19:53:51 +02:00
#include <QPainter>
2020-05-05 07:44:20 +02:00
2020-05-23 15:38:59 +02:00
#include "vpmimedatapiece.h"
2020-05-23 14:33:02 +02:00
#include "vpcarrouselpiecelist.h"
2020-05-23 14:29:18 +02:00
#include "vpcarrousel.h"
#include "vpsheet.h"
2020-04-26 12:09:28 +02:00
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(pCarrouselPiece, "p.carrouselPiece")
2020-05-05 07:44:20 +02:00
2020-04-26 12:09:28 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-24 14:55:03 +02:00
VPCarrouselPiece::VPCarrouselPiece(VPPiece *piece, QListWidget* parent) :
QListWidgetItem(parent,1001),
m_piece(piece)
2020-04-26 12:09:28 +02:00
{
2020-05-24 14:55:03 +02:00
int width = 120 - 8;
QFontMetrics metrix = QFontMetrics(QFont());
QString clippedText = metrix.elidedText(piece->GetName(), Qt::ElideRight, width);
2020-05-24 19:53:51 +02:00
setIcon(CreatePieceIcon(QSize(120, 120)));
2020-05-24 14:55:03 +02:00
setText(clippedText);
2020-04-26 12:09:28 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:36:35 +02:00
VPCarrouselPiece::~VPCarrouselPiece()
2020-04-26 12:09:28 +02:00
{
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:42:51 +02:00
VPPiece * VPCarrouselPiece::GetPiece()
{
return m_piece;
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-24 14:55:03 +02:00
void VPCarrouselPiece::RefreshSelection()
2020-05-05 07:44:20 +02:00
{
2020-05-24 14:55:03 +02:00
setSelected(m_piece->GetIsSelected());
2020-05-05 07:44:20 +02:00
}
2020-05-24 19:53:51 +02:00
//---------------------------------------------------------------------------------------------------------------------
QIcon VPCarrouselPiece::CreatePieceIcon(const QSize &size, bool isDragIcon) const
2020-05-24 19:53:51 +02:00
{
2020-06-25 14:17:31 +02:00
QVector<QPointF> points = m_piece->GetMappedContourPoints(); // seamline
2020-05-24 19:53:51 +02:00
if(points.isEmpty())
{
2020-06-25 14:17:31 +02:00
points = m_piece->GetMappedSeamAllowancePoints(); // cutting line
2020-05-24 19:53:51 +02:00
}
QPolygonF shape(points);
shape << shape.first();
QRectF boundingRect = shape.boundingRect();
qreal canvasSize = qMax(boundingRect.height(), boundingRect.width());
QRectF canvas = QRectF(0, 0, canvasSize, canvasSize);
qreal dx = canvas.center().x() - boundingRect.center().x();
qreal dy = canvas.center().y() - boundingRect.center().y();
QVector<QIcon::Mode> iconModes;
iconModes.append(QIcon::Normal);
2020-05-24 19:53:51 +02:00
if(not isDragIcon)
{
iconModes.append(QIcon::Selected);
}
2020-05-24 19:53:51 +02:00
QIcon icon;
for(auto iconMode : iconModes)
{
QPixmap pixmap(size);
if(not isDragIcon)
{
pixmap.fill(QColor(Qt::white));
}
else
{
pixmap.fill(QColor(Qt::transparent));
}
QPainter painter;
painter.begin(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
int spacing = 2;
painter.translate(spacing, spacing);
qreal scaleFactorX = canvasSize * 100 / (size.width() - spacing*2) / 100;
qreal scaleFactorY = canvasSize * 100 / (size.height() - spacing*2) / 100;
painter.scale(1./scaleFactorX, 1./scaleFactorY);
painter.setPen(QPen(Qt::black, 0.8*qMax(scaleFactorX, scaleFactorY)));
if(not isDragIcon)
{
painter.translate(dx, dy);
}
else
{
painter.translate(-boundingRect.topLeft().x()+spacing, -boundingRect.topLeft().y()+spacing);
}
if(iconMode == QIcon::Selected)
{
painter.setBrush(QBrush(QColor(255,160,160,60)));
}
else
{
painter.setBrush(QBrush(Qt::white));
}
painter.drawPolygon(shape);
painter.end();
icon.addPixmap(pixmap,iconMode);
}
2020-05-24 19:53:51 +02:00
return icon;
}