Improve Piece carrousel icon and drag icon

This commit is contained in:
Ronan Le Tiec 2020-11-21 15:38:33 +01:00
parent 728b2c83c3
commit dd5970f8ba
3 changed files with 60 additions and 25 deletions

View File

@ -74,7 +74,7 @@ void VPCarrouselPiece::RefreshSelection()
}
//---------------------------------------------------------------------------------------------------------------------
QIcon VPCarrouselPiece::CreatePieceIcon(const QSize &size) const
QIcon VPCarrouselPiece::CreatePieceIcon(const QSize &size, bool isDragIcon) const
{
QVector<QPointF> points = m_piece->GetMappedContourPoints(); // seamline
if(points.isEmpty())
@ -92,31 +92,66 @@ QIcon VPCarrouselPiece::CreatePieceIcon(const QSize &size) const
qreal dx = canvas.center().x() - boundingRect.center().x();
qreal dy = canvas.center().y() - boundingRect.center().y();
QPixmap pixmap(size);
pixmap.fill(QColor("white"));
QVector<QIcon::Mode> iconModes;
iconModes.append(QIcon::Normal);
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)));
painter.translate(dx, dy);
painter.drawPolygon(shape);
painter.end();
if(not isDragIcon)
{
iconModes.append(QIcon::Selected);
}
QIcon icon;
icon.addPixmap(pixmap,QIcon::Normal);
icon.addPixmap(pixmap,QIcon::Selected);
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);
}
return icon;
}

View File

@ -56,7 +56,7 @@ public:
* @param size of the icon
* @return the created icon
*/
QIcon CreatePieceIcon(const QSize &size) const;
QIcon CreatePieceIcon(const QSize &size, bool isDragIcon = false) const;
private:

View File

@ -47,7 +47,7 @@ VPCarrouselPieceList::VPCarrouselPieceList(QWidget* parent) :
QListWidget(parent),
m_dragStart(QPoint())
{
setStyleSheet("QListWidget::item{border: 2px solid transparent; color: black;} QListWidget::item:selected {border: 2px solid red;}");
setStyleSheet("QListWidget::item{border: 2px solid transparent; color: black;} QListWidget::item:selected {border: 2px solid rgb(255,160,160);}");
setContextMenuPolicy(Qt::DefaultContextMenu);
setSelectionMode(QAbstractItemView::MultiSelection);
setViewMode(QListView::IconMode);
@ -163,7 +163,7 @@ void VPCarrouselPieceList::startDrag(Qt::DropActions supportedActions)
mimeData->SetPiecePtr(piece);
mimeData->setObjectName("piecePointer");
QPixmap pixmap = pieceItem->CreatePieceIcon(QSize(120,120)).pixmap(QSize(120,120));
QPixmap pixmap = pieceItem->CreatePieceIcon(QSize(120,120), true).pixmap(QSize(120,120));
drag->setPixmap(pixmap);
drag->setMimeData(mimeData);