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

View file

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

View file

@ -32,6 +32,7 @@
#include "../vmisc/backport/qoverload.h" #include "../vmisc/backport/qoverload.h"
#include <QLoggingCategory> #include <QLoggingCategory>
#include <QScrollBar>
Q_LOGGING_CATEGORY(pCarrousel, "p.carrousel") Q_LOGGING_CATEGORY(pCarrousel, "p.carrousel")
@ -40,6 +41,7 @@ VPieceCarrousel::VPieceCarrousel(VPuzzleLayout *layout, QWidget *parent) :
QWidget(parent), QWidget(parent),
m_layout(layout), m_layout(layout),
m_comboBoxLayer(new QComboBox(this)), m_comboBoxLayer(new QComboBox(this)),
m_scrollArea(new QScrollArea(this)),
m_layersContainer(new QWidget(this)), m_layersContainer(new QWidget(this)),
m_carrouselLayers(QList<VPieceCarrouselLayer *>()) m_carrouselLayers(QList<VPieceCarrouselLayer *>())
{ {
@ -65,6 +67,7 @@ void VPieceCarrousel::Init()
// init the layers container and corresponding scroll area // init the layers container and corresponding scroll area
QWidget *layersContainerWrapper = new QWidget(); QWidget *layersContainerWrapper = new QWidget();
QVBoxLayout *layersContainerWrapperLayout = new QVBoxLayout(); QVBoxLayout *layersContainerWrapperLayout = new QVBoxLayout();
layersContainerWrapperLayout->setMargin(0); layersContainerWrapperLayout->setMargin(0);
layersContainerWrapper->setLayout(layersContainerWrapperLayout); layersContainerWrapper->setLayout(layersContainerWrapperLayout);
@ -77,20 +80,21 @@ void VPieceCarrousel::Init()
layersContainerWrapperLayout->addWidget(m_layersContainer); layersContainerWrapperLayout->addWidget(m_layersContainer);
layersContainerWrapperLayout->addSpacerItem(spacer); layersContainerWrapperLayout->addSpacerItem(spacer);
QScrollArea *scrollArea = new QScrollArea(); m_scrollArea->setWidgetResizable( true );
scrollArea->setWidgetResizable( true ); m_scrollArea->setWidget(layersContainerWrapper);
scrollArea->setWidget(layersContainerWrapper);
// init the layout of the piece carrousel // init the layout of the piece carrousel
QVBoxLayout *mainLayout = new QVBoxLayout(); QVBoxLayout *mainLayout = new QVBoxLayout();
setLayout(mainLayout); setLayout(mainLayout);
setMinimumSize(140,140);
mainLayout->addWidget(m_comboBoxLayer); mainLayout->addWidget(m_comboBoxLayer);
mainLayout->addWidget(scrollArea); mainLayout->addWidget(m_scrollArea);
// ------ then we fill the carrousel with the layout content // ------ then we fill the carrousel with the layout content
Refresh(); 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 // add layer name to combo
m_comboBoxLayer->addItem(layer->GetName()); m_comboBoxLayer->addItem(layer->GetName());
qCDebug(pCarrousel, "layer name : %s", layer->GetName().toStdString().c_str());
// add new carrousel layer // add new carrousel layer
VPieceCarrouselLayer *carrouselLayer = new VPieceCarrouselLayer(layer, this); VPieceCarrouselLayer *carrouselLayer = new VPieceCarrouselLayer(layer, this);
m_carrouselLayers.append(carrouselLayer); 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()) while (!m_carrouselLayers.isEmpty())
{ {
VPieceCarrouselLayer *carrouselLayer = m_carrouselLayers.takeLast(); VPieceCarrouselLayer *carrouselLayer = m_carrouselLayers.takeLast();
@ -168,29 +170,51 @@ void VPieceCarrousel::on_ActiveLayerChanged(int index)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPieceCarrousel::SetOrientation(Qt::Orientation orientation) void VPieceCarrousel::SetOrientation(Qt::Orientation orientation)
{ {
QBoxLayout::Direction direction = QBoxLayout::LeftToRight;
if(orientation == Qt::Horizontal) QBoxLayout::Direction direction = (orientation == Qt::Horizontal)?
{ QBoxLayout::LeftToRight
m_comboBoxLayer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); :
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); QBoxLayout::TopToBottom;
}
else // Qt::Vertical
{
direction = QBoxLayout::TopToBottom;
m_comboBoxLayer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
}
// TODO: it's not updated anymore:
// Update the various qlayouts
QBoxLayout* mainScrollAreaLayout = qobject_cast<QBoxLayout*>(m_layersContainer->layout()); QBoxLayout* mainScrollAreaLayout = qobject_cast<QBoxLayout*>(m_layersContainer->layout());
mainScrollAreaLayout->setDirection(direction); mainScrollAreaLayout->setDirection(direction);
QBoxLayout* layerContainerWrapper = qobject_cast<QBoxLayout*>(m_scrollArea->widget()->layout());
layerContainerWrapper->setDirection(direction);
for (VPieceCarrouselLayer *widget: m_carrouselLayers) { for (VPieceCarrouselLayer *widget: m_carrouselLayers) {
QBoxLayout* layerLayout = qobject_cast<QBoxLayout*>(widget->layout()); QBoxLayout* layerLayout = qobject_cast<QBoxLayout*>(widget->layout());
layerLayout->setDirection(direction); 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; VPuzzleLayout *m_layout;
QComboBox *m_comboBoxLayer; QComboBox *m_comboBoxLayer;
QScrollArea *m_scrollArea;
QWidget *m_layersContainer; QWidget *m_layersContainer;
QList<VPieceCarrouselLayer*> m_carrouselLayers; QList<VPieceCarrouselLayer*> m_carrouselLayers;

View file

@ -67,9 +67,14 @@ void VPieceCarrouselLayer::Refresh()
// Updates the carrousel pieces from the pieces list // Updates the carrousel pieces from the pieces list
QList<VPuzzlePiece*> pieces = m_layer->GetPieces(); 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) 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); VPieceCarrouselPiece *carrouselPiece = new VPieceCarrouselPiece(piece);
m_carrouselPieces.append(carrouselPiece); m_carrouselPieces.append(carrouselPiece);

View file

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