Refactoring Piece carrousel part 2

This commit is contained in:
Ronan Le Tiec 2020-05-24 19:53:51 +02:00
parent f00168e59b
commit af40b52988
11 changed files with 302 additions and 153 deletions

View file

@ -49,6 +49,7 @@ VPCarrousel::VPCarrousel(VPLayout *layout, QWidget *parent) :
m_layout(layout)
{
ui->setupUi(this);
ui->listWidget->SetCarrousel(this);
// init the combo box
connect(ui->comboBoxPieceList, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
@ -61,8 +62,6 @@ VPCarrousel::VPCarrousel(VPLayout *layout, QWidget *parent) :
//---------------------------------------------------------------------------------------------------------------------
void VPCarrousel::Refresh()
{
// NOTE: alternative to clearing the carrousel and adding things again, we could make comparision
// --- clears the content of the carrousel
Clear();
@ -70,18 +69,14 @@ void VPCarrousel::Refresh()
// Do not rely on m_layout because we do not control it.
m_pieceLists = QList<VPPieceList*>();
m_pieceLists.append(m_layout->GetUnplacedPieceList());
for(auto sheet : m_layout->GetSheets())
{
m_pieceLists.append(sheet->GetPieceList());
}
m_pieceLists.append(m_layout->GetFocusedSheet()->GetPieceList());
for (auto pieceList : m_pieceLists)
{
// add piece list name to combo
ui->comboBoxPieceList->blockSignals(true);
ui->comboBoxPieceList->addItem(pieceList->GetName());
ui->comboBoxPieceList->blockSignals(false);
}
ui->comboBoxPieceList->blockSignals(true);
ui->comboBoxPieceList->addItem(m_layout->GetUnplacedPieceList()->GetName());
ui->comboBoxPieceList->addItem(tr("Pieces of ") + m_layout->GetFocusedSheet()->GetName());
ui->comboBoxPieceList->blockSignals(false);
on_ActivePieceListChanged(0);
@ -148,3 +143,13 @@ void VPCarrousel::ClearSelection()
{
m_layout->ClearSelection();
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrousel::ClearSelectionExceptForCurrentPieceList()
{
if (m_layout != nullptr)
{
m_layout->ClearSelectionExceptForGivenPieceList(ui->listWidget->GetCurrentPieceList());
}
}

View file

@ -75,11 +75,18 @@ public:
*/
void ClearSelection();
/**
* @brief ClearSelectionExceptForCurrentPieceList Clears the selection of all pieces of
* the layout except for the one in the current piece list
*/
void ClearSelectionExceptForCurrentPieceList();
private:
Q_DISABLE_COPY(VPCarrousel)
Ui::VPCarrousel *ui;
VPLayout *m_layout;
VPLayout *m_layout{nullptr};
QList<VPPieceList*> m_pieceLists{};
Qt::Orientation m_orientation{Qt::Vertical};

View file

@ -30,6 +30,7 @@
#include <QApplication>
#include <QMenu>
#include <QPainter>
#include "vpmimedatapiece.h"
#include "vpcarrouselpiecelist.h"
@ -49,7 +50,7 @@ VPCarrouselPiece::VPCarrouselPiece(VPPiece *piece, QListWidget* parent) :
int width = 120 - 8;
QFontMetrics metrix = QFontMetrics(QFont());
QString clippedText = metrix.elidedText(piece->GetName(), Qt::ElideRight, width);
setIcon(m_piece->PieceIcon(QSize(120, 120)));
setIcon(CreatePieceIcon(QSize(120, 120)));
setText(clippedText);
}
@ -72,57 +73,50 @@ void VPCarrouselPiece::RefreshSelection()
setSelected(m_piece->GetIsSelected());
}
////---------------------------------------------------------------------------------------------------------------------
//void VPCarrouselPiece::contextMenuEvent(QContextMenuEvent *event)
//{
// QMenu contextMenu;
//---------------------------------------------------------------------------------------------------------------------
QIcon VPCarrouselPiece::CreatePieceIcon(const QSize &size) const
{
QVector<QPointF> points = m_piece->GetSeamLine();
if(points.isEmpty())
{
points = m_piece->GetCuttingLine();
}
// VPPieceList* unplacedPieces = m_piece->GetPieceList()->GetLayout()->GetUnplacedPieceList();
// QList<VPSheet*> sheets = m_piece->GetPieceList()->GetLayout()->GetSheets();
// QList<VPPieceList*> pieceLists = QList<VPPieceList*>();
// for (auto sheet : sheets)
// {
// pieceLists.append(sheet->GetPieceList());
// }
QPolygonF shape(points);
shape << shape.first();
// // move to piece list actions -- TODO : To be tested properly when we have several piece lists
// pieceLists.removeAll(m_piece->GetPieceList());
// if(pieceLists.count() > 0)
// {
// QMenu *moveMenu = contextMenu.addMenu(tr("Move to"));
QRectF boundingRect = shape.boundingRect();
qreal canvasSize = qMax(boundingRect.height(), boundingRect.width());
QRectF canvas = QRectF(0, 0, canvasSize, canvasSize);
// // TODO order in alphabetical order
qreal dx = canvas.center().x() - boundingRect.center().x();
qreal dy = canvas.center().y() - boundingRect.center().y();
// for (auto pieceList : pieceLists)
// {
// QAction* moveToPieceList = moveMenu->addAction(pieceList->GetName());
// QVariant data = QVariant::fromValue(pieceList);
// moveToPieceList->setData(data);
QPixmap pixmap(size);
pixmap.fill(QColor("white"));
// connect(moveToPieceList, &QAction::triggered, this, &VPCarrouselPiece::on_ActionPieceMovedToPieceList);
// }
// }
QPainter painter;
painter.begin(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
// // remove from piece list action
// if(m_piece->GetPieceList() != unplacedPieces)
// {
// QAction *removeAction = contextMenu.addAction(tr("Remove from Sheet"));
// QVariant data = QVariant::fromValue(m_piece->GetPieceList()->GetLayout()->GetUnplacedPieceList());
// removeAction->setData(data);
// connect(removeAction, &QAction::triggered, this, &VPCarrouselPiece::on_ActionPieceMovedToPieceList);
// }
int spacing = 2;
painter.translate(spacing, spacing);
// contextMenu.exec(event->globalPos());
//}
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)));
////---------------------------------------------------------------------------------------------------------------------
//void VPCarrouselPiece::on_ActionPieceMovedToPieceList()
//{
// QAction *act = qobject_cast<QAction *>(sender());
// QVariant v = act->data();
// VPPieceList *pieceList = v.value<VPPieceList *>();
// if(pieceList != nullptr)
// {
// pieceList->GetLayout()->MovePieceToPieceList(m_piece, pieceList);
// }
//}
painter.translate(dx, dy);
painter.drawPolygon(shape);
painter.end();
QIcon icon;
icon.addPixmap(pixmap,QIcon::Normal);
icon.addPixmap(pixmap,QIcon::Selected);
return icon;
}

View file

@ -51,12 +51,13 @@ public:
*/
void RefreshSelection();
private slots:
/**
* @brief on_ActionPieceMovedToPieceList Slot called when the piece is moved via the
* context menu to anoter piece list
* @brief CreatePieceIcon Creates an icon of the piece of given size
* @param size of the icon
* @return the created icon
*/
void on_ActionPieceMovedToPieceList();
QIcon CreatePieceIcon(const QSize &size) const;
private:
VPPiece *m_piece;

View file

@ -31,6 +31,7 @@
#include <QDragMoveEvent>
#include <QPainter>
#include <QApplication>
#include <QMenu>
#include "vpcarrousel.h"
#include "vpcarrouselpiece.h"
@ -43,12 +44,11 @@ Q_LOGGING_CATEGORY(pCarrouselPieceList, "p.carrouselPieceList")
//---------------------------------------------------------------------------------------------------------------------
VPCarrouselPieceList::VPCarrouselPieceList(QWidget* parent) :
QListWidget(parent)
QListWidget(parent),
m_dragStart(QPoint())
{
// Init();
setStyleSheet("QListWidget::item{background-color:transparent; border: 2px solid transparent; color: black;} QListWidget::item:selected {background-color:transparent; border: 2px solid red; color: black; selection-background-color: white;}");
setContextMenuPolicy(Qt::CustomContextMenu);
setStyleSheet("QListWidget::item{border: 2px solid transparent; color: black;} QListWidget::item:selected {border: 2px solid red;}");
setContextMenuPolicy(Qt::DefaultContextMenu);
setSelectionMode(QAbstractItemView::MultiSelection);
setViewMode(QListView::IconMode);
@ -62,11 +62,9 @@ VPCarrouselPieceList::~VPCarrouselPieceList()
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::Init()
void VPCarrouselPieceList::SetCarrousel(VPCarrousel *carrousel)
{
// // add the connections
// connect(m_pieceList, &VPPieceList::PieceAdded, this, &VPCarrouselPieceList::on_PieceAdded);
// connect(m_pieceList, &VPPieceList::PieceRemoved, this, &VPCarrouselPieceList::on_PieceRemoved);
m_carrousel = carrousel;
}
//---------------------------------------------------------------------------------------------------------------------
@ -76,13 +74,11 @@ void VPCarrouselPieceList::Refresh()
if(m_pieceList != nullptr)
{
m_pieceList->disconnect(this);
// Updates the carrousel pieces from the pieces list
QList<VPPiece*> pieces = m_pieceList->GetPieces();
// sort the pieces in alphabetical order
std::sort(pieces.begin(), pieces.end(),
[](const VPPiece* a, const VPPiece* b) -> bool { return a->GetName() < b->GetName();});
// create the corresponding carrousel pieces
for (auto piece : pieces)
{
@ -91,6 +87,10 @@ void VPCarrouselPieceList::Refresh()
carrouselpiece->setSelected(piece->GetIsSelected());
connect(piece, &VPPiece::SelectionChanged, this, &VPCarrouselPieceList::on_SelectionChangedExternal);
}
sortItems();
connect(m_pieceList, &VPPieceList::PieceAdded, this, &VPCarrouselPieceList::on_PieceAdded);
connect(m_pieceList, &VPPieceList::PieceRemoved, this, &VPCarrouselPieceList::on_PieceRemoved);
}
}
@ -112,8 +112,6 @@ void VPCarrouselPieceList::SetCurrentPieceList(VPPieceList* pieceList)
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::mousePressEvent(QMouseEvent *event)
{
qCDebug(pCarrouselPieceList, "mouse pressed");
if (event->button() == Qt::LeftButton)
{
m_dragStart = event->pos();
@ -134,10 +132,9 @@ void VPCarrouselPieceList::mousePressEvent(QMouseEvent *event)
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::mouseMoveEvent(QMouseEvent *event)
{
qCDebug(pCarrouselPieceList, "mouse moved");
if ((event->buttons() & Qt::LeftButton) &&
((event->pos() - m_dragStart).manhattanLength() >= QApplication::startDragDistance()) &&
(selectedItems().count() > 0) &&
(m_pieceList->GetSheet() == nullptr)) // only if it's from unplaced pieces
{
startDrag(Qt::MoveAction);
@ -152,28 +149,29 @@ void VPCarrouselPieceList::mouseMoveEvent(QMouseEvent *event)
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::startDrag(Qt::DropActions supportedActions)
{
Q_UNUSED(supportedActions)
qCDebug(pCarrouselPieceList, "start drag");
QListWidgetItem* item = currentItem();
if(item->type() == 1001)
QListWidgetItem* _item = currentItem();
if(_item->type() == 1001)
{
VPCarrouselPiece *pieceItem = static_cast<VPCarrouselPiece *> (item);
VPCarrouselPiece *pieceItem = static_cast<VPCarrouselPiece *> (_item);
// starts the dragging
QDrag *drag = new QDrag(this);
VPMimeDataPiece *mimeData = new VPMimeDataPiece();
mimeData->SetPiecePtr(pieceItem->GetPiece()); //TODO
VPPiece* piece = pieceItem->GetPiece();
mimeData->SetPiecePtr(piece);
mimeData->setObjectName("piecePointer");
QPixmap pixmap = pieceItem->GetPiece()->PieceIcon(QSize(120,120)).pixmap(QSize(120,120));
QPixmap pixmap = pieceItem->CreatePieceIcon(QSize(120,120)).pixmap(QSize(120,120));
drag->setPixmap(pixmap);
drag->setMimeData(mimeData);
if(drag->exec() == Qt::MoveAction)
{
delete takeItem(row(item));
delete takeItem(row(_item));
clearSelection();
piece->SetIsSelected(true);
}
}
}
@ -182,56 +180,128 @@ void VPCarrouselPieceList::startDrag(Qt::DropActions supportedActions)
void VPCarrouselPieceList::dragMoveEvent(QDragMoveEvent* e)
{
qCDebug(pCarrouselPieceList, "drag move");
e->acceptProposedAction();
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::contextMenuEvent(QContextMenuEvent *event)
{
QListWidgetItem* _item = currentItem();
if(_item->type() == 1001)
{
VPCarrouselPiece *pieceItem = static_cast<VPCarrouselPiece *> (_item);
QMenu contextMenu;
if(m_pieceList->GetSheet() == nullptr)
{
VPPieceList* sheetPieces = pieceItem->GetPiece()->GetPieceList()->GetLayout()->GetFocusedSheet()->GetPieceList();
QAction *moveAction = contextMenu.addAction(tr("Move to Sheet"));
QVariant data = QVariant::fromValue(sheetPieces);
moveAction->setData(data);
connect(moveAction, &QAction::triggered, this, &VPCarrouselPieceList::on_ActionPieceMovedToPieceList);
}
// remove from piece list action
if(m_pieceList->GetSheet() != nullptr)
{
VPPieceList* unplacedPieces = pieceItem->GetPiece()->GetPieceList()->GetLayout()->GetUnplacedPieceList();
QAction *removeAction = contextMenu.addAction(tr("Remove from Sheet"));
QVariant data = QVariant::fromValue(unplacedPieces);
removeAction->setData(data);
connect(removeAction, &QAction::triggered, this, &VPCarrouselPieceList::on_ActionPieceMovedToPieceList);
}
contextMenu.exec(event->globalPos());
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_ActionPieceMovedToPieceList()
{
QListWidgetItem* _item = currentItem();
if(_item->type() == 1001)
{
VPCarrouselPiece *pieceItem = static_cast<VPCarrouselPiece *> (_item);
QAction *act = qobject_cast<QAction *>(sender());
QVariant v = act->data();
VPPieceList *pieceList = v.value<VPPieceList *>();
if(pieceList != nullptr)
{
pieceList->GetLayout()->MovePieceToPieceList(pieceItem->GetPiece(), pieceList);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_PieceAdded(VPPiece* piece)
{
Q_UNUSED(piece)
// TODO/ FIXME: see if we find a solution more efficient refreshing the complete layout everytime.
Refresh();
if(piece->GetPieceList() == m_pieceList)
{
// update the label of the piece
VPCarrouselPiece* carrouselpiece = new VPCarrouselPiece(piece,this);
carrouselpiece->setSelected(piece->GetIsSelected());
connect(piece, &VPPiece::SelectionChanged, this, &VPCarrouselPieceList::on_SelectionChangedExternal);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_PieceRemoved(VPPiece* piece)
{
// TODO
Q_UNUSED(piece)
for(int i = 0; i < count(); ++i)
{
QListWidgetItem* _item = item(i);
if(_item->type() == 1001)
{
VPCarrouselPiece *itemPiece = static_cast<VPCarrouselPiece *> (_item);
if(piece == itemPiece->GetPiece())
{
delete takeItem(row(_item));
return;
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_SelectionChangedInternal()
{
blockSignals(true);
for(int i = 0; i < count(); ++i)
{
QListWidgetItem* _item = item(i);
if(_item->type() == 1001)
{
VPCarrouselPiece *itemPiece = static_cast<VPCarrouselPiece *> (_item);
blockSignals(true);
itemPiece->GetPiece()->SetIsSelected(itemPiece->isSelected());
blockSignals(false);
}
}
m_carrousel->ClearSelectionExceptForCurrentPieceList();
// TODO FIXME: when selecting pieces on the sheet, and then selecting a unplaced piece in the piece carrousel
// the selection is cleared in the sheet (good !) but the cliked item in unplaced pieces in not selected (bad!)
blockSignals(false);
}
//---------------------------------------------------------------------------------------------------------------------
void VPCarrouselPieceList::on_SelectionChangedExternal()
{
blockSignals(true);
for(int i = 0; i < count(); ++i)
{
QListWidgetItem* _item = item(i);
if(_item->type() == 1001)
{
VPCarrouselPiece *itemPiece = static_cast<VPCarrouselPiece *> (_item);
blockSignals(true);
itemPiece->RefreshSelection();
blockSignals(false);
}
}
blockSignals(false);
}

View file

@ -31,6 +31,7 @@
#include <QListWidget>
#include "vppiecelist.h"
#include "vpcarrousel.h"
class VPCarrouselPieceList : public QListWidget
@ -40,7 +41,9 @@ public:
VPCarrouselPieceList(QWidget* parent);
~VPCarrouselPieceList();
void Init();
/**
* @brief Refresh refreshes the items of the carrousel piece list
*/
void Refresh();
/**
@ -53,9 +56,19 @@ public:
* @brief SetCurrentPieceList Sets the current piece list to the given piece list and redraw
* the carrousel.
*/
void SetCurrentPieceList(VPPieceList* pieceList);
void SetCurrentPieceList(VPPieceList *pieceList);
/**
* @brief SetCarrousel Sets the carrousel corresponding to the list
* @param carrousel
*/
void SetCarrousel(VPCarrousel *carrousel);
public slots:
/**
* @brief on_SelectionChangedExternal when the selection was changed outside of the carrousel
*/
void on_SelectionChangedExternal();
protected:
@ -65,11 +78,14 @@ protected:
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override;
private:
Q_DISABLE_COPY(VPCarrouselPieceList)
VPPieceList *m_pieceList{nullptr};
QPoint m_dragStart;
VPCarrousel *m_carrousel{nullptr};
private slots:
@ -83,8 +99,16 @@ private slots:
*/
void on_PieceRemoved(VPPiece* piece);
/**
* @brief on_SelectionChangedInternal when the selection was changed inside of the carrousel
*/
void on_SelectionChangedInternal();
/**
* @brief on_ActionPieceMovedToPieceList when a piece is moved to another piece list via a context menu
*/
void on_ActionPieceMovedToPieceList();
};
#endif // VPCARROUSELPIECELIST_H

View file

@ -30,6 +30,11 @@
#include "vppiece.h"
#include "vpsheet.h"
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(pLayout, "p.layout")
//---------------------------------------------------------------------------------------------------------------------
VPLayout::VPLayout() :
m_unplacedPieceList(new VPPieceList(this)),
@ -147,6 +152,25 @@ void VPLayout::ClearSelection()
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayout::ClearSelectionExceptForGivenPieceList(VPPieceList* pieceList)
{
if(m_unplacedPieceList != pieceList)
{
m_unplacedPieceList->ClearSelection();
}
for (auto sheet : m_sheets)
{
if(sheet->GetPieceList() != pieceList)
{
sheet->ClearSelection();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPLayout::MovePieceToPieceList(VPPiece* piece, VPPieceList* pieceList)
{

View file

@ -84,6 +84,14 @@ public:
*/
void ClearSelection();
/**
* @brief ClearSelectionExceptForPieceList same as clearSelection but it leaves the selection
* for the given piece list like it ist.
*
* @param pieceList the piece list to let be the way it is.
*/
void ClearSelectionExceptForGivenPieceList(VPPieceList* pieceList);
/**
* @brief MovePieceToPieceList Moves the given piece to the given piece list
* @param piece the piece to move

View file

@ -395,6 +395,9 @@ void VPMainWindow::SetPropertyTabCurrentPieceData()
//---------------------------------------------------------------------------------------------------------------------
void VPMainWindow::SetPropertyTabSheetData()
{
// set name // TODO FIXME make it better
ui->lineEditSheetName->setText(m_layout->GetFocusedSheet()->GetName());
// set Width / Length
QSizeF size = m_layout->GetFocusedSheet()->GetSheetSizeConverted();
SetDoubleSpinBoxValue(ui->doubleSpinBoxSheetWidth, size.width());

View file

@ -172,7 +172,7 @@
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<property name="iconSize">
<size>
@ -230,7 +230,7 @@
<x>0</x>
<y>0</y>
<width>342</width>
<height>1318</height>
<height>1264</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
@ -253,6 +253,18 @@
<item>
<widget class="QWidget" name="containerCurrentPieceData" native="true">
<layout class="QVBoxLayout" name="containerCurrentPieceDataLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QGroupBox" name="groupBoxCurrentPieceInfo">
<property name="title">
@ -398,6 +410,18 @@
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="labelCurrentPieceNoPieceSelected">
<property name="minimumSize">
@ -420,6 +444,18 @@
<item>
<widget class="QWidget" name="containerCurrentPieceMultipleData" native="true">
<layout class="QVBoxLayout" name="verticalLayout_11">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="labelCurrentPieceMultiplePieceSelected">
<property name="minimumSize">
@ -519,6 +555,29 @@
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBoxSheetInfos">
<property name="title">
<string>Infos</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_17">
<item>
<layout class="QFormLayout" name="formLayout_8">
<item row="0" column="0">
<widget class="QLabel" name="labelSheetName">
<property name="text">
<string>Name </string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEditSheetName"/>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBoxSheetFormat">
<property name="title">

View file

@ -256,50 +256,4 @@ void VPPiece::SetPieceList(VPPieceList* pieceList)
}
}
//---------------------------------------------------------------------------------------------------------------------
QIcon VPPiece::PieceIcon(const QSize &size) const
{
QVector<QPointF> points = GetSeamLine();
if(points.isEmpty())
{
points = GetCuttingLine();
}
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();
QPixmap pixmap(size);
pixmap.fill(QColor("white"));
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();
QIcon icon;
icon.addPixmap(pixmap,QIcon::Normal);
icon.addPixmap(pixmap,QIcon::Selected);
return icon;
}