Optimization piece carrousel

This commit is contained in:
Ronan Le Tiec 2020-04-26 14:03:43 +02:00
parent bf09544d87
commit 25cb7f9e6e
6 changed files with 68 additions and 37 deletions

View File

@ -64,10 +64,6 @@ PuzzleMainWindow::PuzzleMainWindow(const VPuzzleCommandLinePtr &cmd, QWidget *pa
m_layout->SetPiecesGapConverted(1.27);
m_layout->SetUnit(Unit::Cm);
m_layout->SetWarningSuperpositionOfPieces(true);
VPuzzleLayer *unplacedLayer = m_layout->GetUnplacedPiecesLayer();
VPuzzlePiece *piece = new VPuzzlePiece();
piece->SetName("Hello");
unplacedLayer->AddPiece(piece);
// --------------------------------------------------------
ui->setupUi(this);
@ -289,7 +285,7 @@ void PuzzleMainWindow::InitPropertyTabLayers()
//---------------------------------------------------------------------------------------------------------------------
void PuzzleMainWindow::InitPieceCarrousel()
{
m_pieceCarrousel = new VPieceCarrousel(m_layout);
m_pieceCarrousel = new VPieceCarrousel(m_layout, ui->dockWidgetPieceCarrousel);
ui->dockWidgetPieceCarrousel->setWidget(m_pieceCarrousel);
connect(ui->dockWidgetPieceCarrousel, QOverload<Qt::DockWidgetArea>::of(&QDockWidget::dockLocationChanged), this,

View File

@ -94,8 +94,8 @@
</property>
<property name="minimumSize">
<size>
<width>160</width>
<height>208</height>
<width>24</width>
<height>37</height>
</size>
</property>
<property name="styleSheet">
@ -126,7 +126,11 @@
<height>0</height>
</size>
</property>
<layout class="QVBoxLayout" name="verticalLayout_10"/>
<layout class="QVBoxLayout" name="verticalLayout_10">
<property name="spacing">
<number>0</number>
</property>
</layout>
</widget>
</widget>
<widget class="QDockWidget" name="dockWidgetProperties">
@ -228,8 +232,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>170</width>
<height>452</height>
<width>356</width>
<height>760</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
@ -858,8 +862,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>98</width>
<height>41</height>
<width>356</width>
<height>760</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
@ -938,8 +942,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>98</width>
<height>41</height>
<width>356</width>
<height>760</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">

View File

@ -32,6 +32,7 @@
#include "../vmisc/backport/qoverload.h"
#include <QLoggingCategory>
#include <QScrollBar>
Q_LOGGING_CATEGORY(pCarrousel, "p.carrousel")
@ -40,6 +41,7 @@ VPieceCarrousel::VPieceCarrousel(VPuzzleLayout *layout, QWidget *parent) :
QWidget(parent),
m_layout(layout),
m_comboBoxLayer(new QComboBox(this)),
m_scrollArea(new QScrollArea(this)),
m_layersContainer(new QWidget(this)),
m_carrouselLayers(QList<VPieceCarrouselLayer *>())
{
@ -65,6 +67,7 @@ void VPieceCarrousel::Init()
// init the layers container and corresponding scroll area
QWidget *layersContainerWrapper = new QWidget();
QVBoxLayout *layersContainerWrapperLayout = new QVBoxLayout();
layersContainerWrapperLayout->setMargin(0);
layersContainerWrapper->setLayout(layersContainerWrapperLayout);
@ -77,20 +80,21 @@ void VPieceCarrousel::Init()
layersContainerWrapperLayout->addWidget(m_layersContainer);
layersContainerWrapperLayout->addSpacerItem(spacer);
QScrollArea *scrollArea = new QScrollArea();
scrollArea->setWidgetResizable( true );
scrollArea->setWidget(layersContainerWrapper);
m_scrollArea->setWidgetResizable( true );
m_scrollArea->setWidget(layersContainerWrapper);
// init the layout of the piece carrousel
QVBoxLayout *mainLayout = new QVBoxLayout();
setLayout(mainLayout);
setMinimumSize(140,140);
mainLayout->addWidget(m_comboBoxLayer);
mainLayout->addWidget(scrollArea);
mainLayout->addWidget(m_scrollArea);
// ------ then we fill the carrousel with the layout content
Refresh();
// ------ and make sure the calculation for the qlayout is right
SetOrientation(Qt::Vertical);
}
//---------------------------------------------------------------------------------------------------------------------
@ -111,8 +115,6 @@ void VPieceCarrousel::Refresh()
// add layer name to combo
m_comboBoxLayer->addItem(layer->GetName());
qCDebug(pCarrousel, "layer name : %s", layer->GetName().toStdString().c_str());
// add new carrousel layer
VPieceCarrouselLayer *carrouselLayer = new VPieceCarrouselLayer(layer, this);
m_carrouselLayers.append(carrouselLayer);
@ -142,7 +144,7 @@ void VPieceCarrousel::Clear()
}
}
// Removes and deletes the carrousel layer from the list
// Removes and deletes the carrousel layers from the list
while (!m_carrouselLayers.isEmpty())
{
VPieceCarrouselLayer *carrouselLayer = m_carrouselLayers.takeLast();
@ -168,29 +170,51 @@ void VPieceCarrousel::on_ActiveLayerChanged(int index)
//---------------------------------------------------------------------------------------------------------------------
void VPieceCarrousel::SetOrientation(Qt::Orientation orientation)
{
QBoxLayout::Direction direction = QBoxLayout::LeftToRight;
if(orientation == Qt::Horizontal)
{
m_comboBoxLayer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
}
else // Qt::Vertical
{
direction = QBoxLayout::TopToBottom;
m_comboBoxLayer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
}
// TODO: it's not updated anymore:
QBoxLayout::Direction direction = (orientation == Qt::Horizontal)?
QBoxLayout::LeftToRight
:
QBoxLayout::TopToBottom;
// Update the various qlayouts
QBoxLayout* mainScrollAreaLayout = qobject_cast<QBoxLayout*>(m_layersContainer->layout());
mainScrollAreaLayout->setDirection(direction);
QBoxLayout* layerContainerWrapper = qobject_cast<QBoxLayout*>(m_scrollArea->widget()->layout());
layerContainerWrapper->setDirection(direction);
for (VPieceCarrouselLayer *widget: m_carrouselLayers) {
QBoxLayout* layerLayout = qobject_cast<QBoxLayout*>(widget->layout());
layerLayout->setDirection(direction);
}
// then update the scrollarea min height / width and scrollbar behaviour
if(orientation == Qt::Horizontal)
{
m_comboBoxLayer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// scroll bar policy of scroll area
m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
// FIXME: find a nicer way than putting directly the 120 height of the piece
m_scrollArea->setMinimumHeight(120 + m_scrollArea->horizontalScrollBar()->sizeHint().height()+2);
m_scrollArea->setMinimumWidth(0);
}
else // Qt::Vertical
{
m_comboBoxLayer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
// scroll bar policy of scroll area
m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_scrollArea->setMinimumHeight(0);
m_scrollArea->setMinimumWidth(120 + m_scrollArea->verticalScrollBar()->sizeHint().width()+2);
// FIXME: find a nicer way than putting directly the 120 width of the piece
}
}

View File

@ -69,6 +69,7 @@ private:
VPuzzleLayout *m_layout;
QComboBox *m_comboBoxLayer;
QScrollArea *m_scrollArea;
QWidget *m_layersContainer;
QList<VPieceCarrouselLayer*> m_carrouselLayers;

View File

@ -67,9 +67,14 @@ void VPieceCarrouselLayer::Refresh()
// Updates the carrousel pieces from the pieces list
QList<VPuzzlePiece*> pieces = m_layer->GetPieces();
// sort the pieces in alphabetical order
std::sort(pieces.begin(), pieces.end(),
[](const VPuzzlePiece* a, const VPuzzlePiece* b) -> bool { return a->GetName() < b->GetName();});
// create the corresponding carrousel pieces
for (auto piece : pieces)
{
qCDebug(pCarrouselLayer, "piece name : %s", piece->GetName().toStdString().c_str());
// qCDebug(pCarrouselLayer, "piece name : %s", piece->GetName().toStdString().c_str());
VPieceCarrouselPiece *carrouselPiece = new VPieceCarrouselPiece(piece);
m_carrouselPieces.append(carrouselPiece);

View File

@ -64,6 +64,7 @@ void VPieceCarrouselPiece::Init()
m_label->setStyleSheet("background-color:cornflowerblue");
pieceLayout->addWidget(m_label);
setMinimumSize(120,120);
// then refresh the data
Refresh();