From dd5970f8ba75a8c93addffa80edf88c92b019d61 Mon Sep 17 00:00:00 2001 From: Ronan Le Tiec Date: Sat, 21 Nov 2020 15:38:33 +0100 Subject: [PATCH] Improve Piece carrousel icon and drag icon --- src/app/puzzle/vpcarrouselpiece.cpp | 79 ++++++++++++++++++------- src/app/puzzle/vpcarrouselpiece.h | 2 +- src/app/puzzle/vpcarrouselpiecelist.cpp | 4 +- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/app/puzzle/vpcarrouselpiece.cpp b/src/app/puzzle/vpcarrouselpiece.cpp index c5e651546..7fc443003 100644 --- a/src/app/puzzle/vpcarrouselpiece.cpp +++ b/src/app/puzzle/vpcarrouselpiece.cpp @@ -74,7 +74,7 @@ void VPCarrouselPiece::RefreshSelection() } //--------------------------------------------------------------------------------------------------------------------- -QIcon VPCarrouselPiece::CreatePieceIcon(const QSize &size) const +QIcon VPCarrouselPiece::CreatePieceIcon(const QSize &size, bool isDragIcon) const { QVector 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 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; } diff --git a/src/app/puzzle/vpcarrouselpiece.h b/src/app/puzzle/vpcarrouselpiece.h index 9be77a355..d9d0a83f4 100644 --- a/src/app/puzzle/vpcarrouselpiece.h +++ b/src/app/puzzle/vpcarrouselpiece.h @@ -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: diff --git a/src/app/puzzle/vpcarrouselpiecelist.cpp b/src/app/puzzle/vpcarrouselpiecelist.cpp index 6ee1d6528..00ca57d9d 100644 --- a/src/app/puzzle/vpcarrouselpiecelist.cpp +++ b/src/app/puzzle/vpcarrouselpiecelist.cpp @@ -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);