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>
|
2020-05-09 09:54:56 +02:00
|
|
|
#include <QMenu>
|
2020-05-24 19:53:51 +02:00
|
|
|
#include <QPainter>
|
2020-05-05 07:44:20 +02:00
|
|
|
|
2021-08-09 14:09:10 +02:00
|
|
|
#include "../layout/vppiece.h"
|
2023-08-05 16:51:23 +02:00
|
|
|
#include "../vmisc/theme/vscenestylesheet.h"
|
2020-04-26 12:09:28 +02:00
|
|
|
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
2022-08-12 17:50:13 +02:00
|
|
|
QT_WARNING_PUSH
|
|
|
|
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
|
|
|
|
QT_WARNING_DISABLE_INTEL(1418)
|
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(pCarrouselPiece, "p.carrouselPiece") // NOLINT
|
|
|
|
|
|
|
|
QT_WARNING_POP
|
2020-04-26 12:09:28 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-08-05 16:51:23 +02:00
|
|
|
VPCarrouselPiece::VPCarrouselPiece(const VPPiecePtr &piece, QListWidget *parent)
|
|
|
|
: QListWidgetItem(parent, Type),
|
2020-05-24 14:55:03 +02:00
|
|
|
m_piece(piece)
|
2020-04-26 12:09:28 +02:00
|
|
|
{
|
2021-07-29 17:05:25 +02:00
|
|
|
SCASSERT(m_piece != nullptr)
|
2023-08-05 16:51:23 +02:00
|
|
|
const int width = 120 - 8;
|
|
|
|
QString clippedText = QFontMetrics(font()).elidedText(piece->GetName(), Qt::ElideRight, width);
|
|
|
|
RefreshPieceIcon();
|
2020-05-24 14:55:03 +02:00
|
|
|
setText(clippedText);
|
2023-08-05 16:51:23 +02:00
|
|
|
setToolTip(piece->GetName());
|
2020-04-26 12:09:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:02 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-08-18 19:33:47 +02:00
|
|
|
auto VPCarrouselPiece::GetPiece() const -> VPPiecePtr
|
2020-05-01 18:26:02 +02:00
|
|
|
{
|
|
|
|
return m_piece;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-24 14:55:03 +02:00
|
|
|
void VPCarrouselPiece::RefreshSelection()
|
2020-05-05 07:44:20 +02:00
|
|
|
{
|
2021-08-18 19:33:47 +02:00
|
|
|
VPPiecePtr piece = GetPiece();
|
|
|
|
if (not piece.isNull())
|
|
|
|
{
|
|
|
|
setSelected(piece->IsSelected());
|
|
|
|
}
|
2020-05-05 07:44:20 +02:00
|
|
|
}
|
2020-05-09 09:54:56 +02:00
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPCarrouselPiece::RefreshPieceIcon()
|
|
|
|
{
|
|
|
|
setIcon(CreatePieceIcon(QSize(120, 120)));
|
|
|
|
}
|
|
|
|
|
2020-05-24 19:53:51 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-07-29 17:05:25 +02:00
|
|
|
auto VPCarrouselPiece::CreatePieceIcon(const QSize &size, bool isDragIcon) const -> QIcon
|
2020-05-24 19:53:51 +02:00
|
|
|
{
|
2021-08-18 19:33:47 +02:00
|
|
|
VPPiecePtr piece = GetPiece();
|
|
|
|
if (piece.isNull())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF boundingRect = piece->DetailBoundingRect();
|
2020-05-24 19:53:51 +02:00
|
|
|
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();
|
|
|
|
|
2020-11-21 15:38:33 +01:00
|
|
|
QVector<QIcon::Mode> iconModes;
|
|
|
|
iconModes.append(QIcon::Normal);
|
2020-05-24 19:53:51 +02:00
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
if (not isDragIcon)
|
2020-11-21 15:38:33 +01:00
|
|
|
{
|
|
|
|
iconModes.append(QIcon::Selected);
|
|
|
|
}
|
2020-05-24 19:53:51 +02:00
|
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
const VManualLayoutStyle &style = VSceneStylesheet::ManualLayoutStyle();
|
|
|
|
|
|
|
|
for (auto iconMode : iconModes)
|
2020-11-21 15:38:33 +01:00
|
|
|
{
|
|
|
|
QPixmap pixmap(size);
|
2023-08-05 16:51:23 +02:00
|
|
|
pixmap.fill(not isDragIcon ? style.CarrouselPieceBackgroundColor() : QColor(Qt::transparent));
|
2020-11-21 15:38:33 +01:00
|
|
|
|
|
|
|
QPainter painter;
|
|
|
|
painter.begin(&pixmap);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
|
|
|
|
int spacing = 2;
|
|
|
|
|
|
|
|
painter.translate(spacing, spacing);
|
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
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(style.CarrouselPieceColor(), 0.8 * qMax(scaleFactorX, scaleFactorY)));
|
2020-11-21 15:38:33 +01:00
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
if (not isDragIcon)
|
2020-11-21 15:38:33 +01:00
|
|
|
{
|
|
|
|
painter.translate(dx, dy);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-05 16:51:23 +02:00
|
|
|
painter.translate(-boundingRect.topLeft().x() + spacing, -boundingRect.topLeft().y() + spacing);
|
2020-11-21 15:38:33 +01:00
|
|
|
}
|
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
painter.setBrush(QBrush(iconMode == QIcon::Selected ? style.CarrouselPieceSelectedColor()
|
|
|
|
: style.CarrouselPieceForegroundColor()));
|
2020-11-21 15:38:33 +01:00
|
|
|
|
2021-08-18 19:33:47 +02:00
|
|
|
piece->DrawMiniature(painter);
|
2021-05-27 18:52:37 +02:00
|
|
|
|
2020-11-21 15:38:33 +01:00
|
|
|
painter.end();
|
|
|
|
|
2023-08-05 16:51:23 +02:00
|
|
|
icon.addPixmap(pixmap, iconMode);
|
2020-11-21 15:38:33 +01:00
|
|
|
}
|
2020-05-24 19:53:51 +02:00
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|