valentina/src/app/puzzle/vpiececarrouselpiece.cpp

163 lines
5.4 KiB
C++
Raw Normal View History

2020-04-26 12:09:28 +02:00
/************************************************************************
**
** @file vpiececarrouselpiece.cpp
** @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/>.
**
*************************************************************************/
#include "vpiececarrouselpiece.h"
#include <QLabel>
#include <QVBoxLayout>
2020-04-26 15:36:20 +02:00
#include <QGraphicsScene>
#include <QPainter>
2020-04-26 12:09:28 +02:00
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(pCarrouselPiece, "p.carrouselPiece")
//---------------------------------------------------------------------------------------------------------------------
VPieceCarrouselPiece::VPieceCarrouselPiece(VPuzzlePiece *piece, QWidget *parent) : QFrame(parent), m_piece(piece)
2020-04-26 12:09:28 +02:00
{
Init();
}
//---------------------------------------------------------------------------------------------------------------------
VPieceCarrouselPiece::~VPieceCarrouselPiece()
{
2020-04-26 15:36:20 +02:00
delete m_graphicsView;
2020-04-26 12:09:28 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::Init()
{
2020-04-26 15:36:20 +02:00
// Define the structure
setFixedSize(124,128);
2020-04-26 12:09:28 +02:00
QVBoxLayout *pieceLayout = new QVBoxLayout();
pieceLayout->setMargin(0);
pieceLayout->setSpacing(0);
2020-04-26 12:09:28 +02:00
setLayout(pieceLayout);
setStyleSheet("background-color:white; border: 2px solid transparent;");
2020-04-26 15:36:20 +02:00
// define the preview of the piece
m_graphicsView = new QGraphicsView(this);
// m_graphicsView = new VMainGraphicsView(this);
// --> undefined reference to 'VMainGraphicsView::VMainGraphicView(QWidget*)'
2020-04-26 15:36:20 +02:00
QGraphicsScene *graphicsScene = new QGraphicsScene(this);
m_graphicsView->setScene(graphicsScene);
m_graphicsView->setFixedSize(120,100);
m_graphicsView->setStyleSheet("border: 4px solid transparent;");
2020-04-26 15:36:20 +02:00
// define the label
2020-04-26 12:09:28 +02:00
m_label = new QLabel();
m_label->sizePolicy();
2020-04-26 15:36:20 +02:00
m_label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
m_label->setFixedSize(120,24);
m_label->setStyleSheet("border: 0px;");
2020-04-26 12:09:28 +02:00
2020-04-26 15:36:20 +02:00
pieceLayout->addWidget(m_graphicsView);
2020-04-26 12:09:28 +02:00
pieceLayout->addWidget(m_label);
// then refresh the data
Refresh();
}
//---------------------------------------------------------------------------------------------------------------------
2020-04-26 22:32:08 +02:00
void VPieceCarrouselPiece::CleanPreview()
{
m_graphicsView->fitInView(m_graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
}
//---------------------------------------------------------------------------------------------------------------------
2020-04-26 12:09:28 +02:00
void VPieceCarrouselPiece::Refresh()
{
2020-04-26 15:36:20 +02:00
// update the graphic view / the scene
// TODO / FIXME : not perfect and maybe not the right way, still need to work on this
QVector<QPointF> points = m_piece->GetCuttingLine();
2020-04-26 12:09:28 +02:00
2020-04-26 15:36:20 +02:00
QPen pen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
pen.setCosmetic(true);
QBrush noBrush(Qt::NoBrush);
QPainterPath path;
path.moveTo(points.first());
for (int i = 1; i < points.size(); ++i)
path.lineTo(points.at(i));
m_graphicsView->scene()->addPath(path, pen, noBrush);
m_graphicsView->fitInView(m_graphicsView->scene()->sceneRect(), Qt::KeepAspectRatio);
// update the label of the piece
2020-04-26 12:09:28 +02:00
QFontMetrics metrix(m_label->font());
int width = m_label->width() - 8;
QString clippedText = metrix.elidedText(m_piece->GetName(), Qt::ElideRight, width);
m_label->setText(clippedText);
setToolTip(m_piece->GetName());
}
//---------------------------------------------------------------------------------------------------------------------
VPuzzlePiece * VPieceCarrouselPiece::GetPiece()
{
return m_piece;
}
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::SetIsSelected(bool value)
{
m_isSelected = value;
if(value)
{
setStyleSheet("background-color:white; border: 2px solid red;");
}
else
{
setStyleSheet("background-color:white; border: 2px solid transparent;");
}
}
//---------------------------------------------------------------------------------------------------------------------
bool VPieceCarrouselPiece::GetIsSelected()
{
return m_isSelected;
}
2020-04-26 12:09:28 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrouselPiece::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
if(!m_isSelected)
{
emit clicked(this);
}
}
2020-04-26 12:09:28 +02:00
}