2020-04-13 12:43:27 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vpiececarrousel.cpp
|
|
|
|
** @author Ronan Le Tiec
|
|
|
|
** @date 13 4, 2020
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentina project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2020 Valentina project
|
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
2020-04-13 12:24:26 +02:00
|
|
|
#include "vpiececarrousel.h"
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
2020-04-13 14:27:52 +02:00
|
|
|
#include "../vmisc/backport/qoverload.h"
|
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
#include <QLoggingCategory>
|
2020-04-26 14:03:43 +02:00
|
|
|
#include <QScrollBar>
|
2020-04-26 12:09:28 +02:00
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(pCarrousel, "p.carrousel")
|
|
|
|
|
2020-04-13 12:24:26 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-04-26 12:09:28 +02:00
|
|
|
VPieceCarrousel::VPieceCarrousel(VPuzzleLayout *layout, QWidget *parent) :
|
2020-04-13 12:24:26 +02:00
|
|
|
QWidget(parent),
|
2020-04-26 12:09:28 +02:00
|
|
|
m_layout(layout),
|
|
|
|
m_comboBoxLayer(new QComboBox(this)),
|
2020-04-26 14:03:43 +02:00
|
|
|
m_scrollArea(new QScrollArea(this)),
|
2020-04-26 12:09:28 +02:00
|
|
|
m_layersContainer(new QWidget(this)),
|
|
|
|
m_carrouselLayers(QList<VPieceCarrouselLayer *>())
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VPieceCarrousel::~VPieceCarrousel()
|
2020-04-13 12:24:26 +02:00
|
|
|
{
|
2020-04-26 12:09:28 +02:00
|
|
|
delete m_comboBoxLayer;
|
|
|
|
delete m_layersContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPieceCarrousel::Init()
|
|
|
|
{
|
|
|
|
// ------ first we initialize the structure of the carrousel
|
|
|
|
|
|
|
|
// init the combo box
|
|
|
|
connect(m_comboBoxLayer, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
|
|
|
|
&VPieceCarrousel::on_ActiveLayerChanged);
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
// init the layers container and corresponding scroll area
|
|
|
|
QWidget *layersContainerWrapper = new QWidget();
|
2020-04-26 14:03:43 +02:00
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
QVBoxLayout *layersContainerWrapperLayout = new QVBoxLayout();
|
|
|
|
layersContainerWrapperLayout->setMargin(0);
|
|
|
|
layersContainerWrapper->setLayout(layersContainerWrapperLayout);
|
|
|
|
|
|
|
|
QVBoxLayout *layersContainerLayout = new QVBoxLayout();
|
|
|
|
layersContainerLayout->setMargin(0);
|
|
|
|
m_layersContainer->setLayout(layersContainerLayout);
|
2020-05-02 12:17:06 +02:00
|
|
|
m_layersContainer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
2020-04-26 12:09:28 +02:00
|
|
|
QSpacerItem *spacer = new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
|
|
|
2020-05-02 12:17:06 +02:00
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
layersContainerWrapperLayout->addWidget(m_layersContainer);
|
|
|
|
layersContainerWrapperLayout->addSpacerItem(spacer);
|
|
|
|
|
2020-04-26 14:03:43 +02:00
|
|
|
m_scrollArea->setWidgetResizable( true );
|
|
|
|
m_scrollArea->setWidget(layersContainerWrapper);
|
2020-04-26 12:09:28 +02:00
|
|
|
|
|
|
|
// init the layout of the piece carrousel
|
2020-04-13 12:24:26 +02:00
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout();
|
|
|
|
setLayout(mainLayout);
|
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
mainLayout->addWidget(m_comboBoxLayer);
|
2020-04-26 14:03:43 +02:00
|
|
|
mainLayout->addWidget(m_scrollArea);
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
// ------ then we fill the carrousel with the layout content
|
|
|
|
Refresh();
|
|
|
|
}
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPieceCarrousel::Refresh()
|
|
|
|
{
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
// NOTE: alternative to clearing the carrousel and adding things again, we could make comparision
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
// --- clears the content of the carrousel
|
|
|
|
Clear();
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
// --- add the content saved in the layout to the carrousel.
|
|
|
|
QList<VPuzzleLayer*> layers = m_layout->GetLayers();
|
|
|
|
layers.prepend(m_layout->GetUnplacedPiecesLayer());
|
|
|
|
|
|
|
|
for (auto layer : layers)
|
2020-04-13 12:24:26 +02:00
|
|
|
{
|
2020-04-26 12:09:28 +02:00
|
|
|
// add layer name to combo
|
|
|
|
m_comboBoxLayer->addItem(layer->GetName());
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
// add new carrousel layer
|
|
|
|
VPieceCarrouselLayer *carrouselLayer = new VPieceCarrouselLayer(layer, this);
|
|
|
|
m_carrouselLayers.append(carrouselLayer);
|
|
|
|
m_layersContainer->layout()->addWidget(carrouselLayer);
|
2020-05-01 18:26:02 +02:00
|
|
|
|
|
|
|
connect(carrouselLayer, QOverload<VPieceCarrouselPiece*>::of(&VPieceCarrouselLayer::pieceClicked), this,
|
|
|
|
&VPieceCarrousel::on_PieceClicked);
|
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
}
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 22:32:08 +02:00
|
|
|
on_ActiveLayerChanged(0);
|
2020-05-02 12:17:06 +02:00
|
|
|
|
|
|
|
RefreshOrientation();
|
2020-04-13 12:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-04-26 12:09:28 +02:00
|
|
|
void VPieceCarrousel::Clear()
|
2020-04-13 12:24:26 +02:00
|
|
|
{
|
2020-04-26 12:09:28 +02:00
|
|
|
// remove the combobox entries
|
|
|
|
int layerCount = m_comboBoxLayer->count();
|
|
|
|
for(int i=0;i<layerCount;i++)
|
|
|
|
{
|
|
|
|
m_comboBoxLayer->removeItem(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the carrousel layers from the qlayout
|
|
|
|
while(!m_layersContainer->layout()->isEmpty())
|
|
|
|
{
|
|
|
|
QLayoutItem* item = m_layersContainer->layout()->takeAt(0);
|
|
|
|
if(item != nullptr)
|
|
|
|
{
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 14:03:43 +02:00
|
|
|
// Removes and deletes the carrousel layers from the list
|
2020-04-26 12:09:28 +02:00
|
|
|
while (!m_carrouselLayers.isEmpty())
|
|
|
|
{
|
|
|
|
VPieceCarrouselLayer *carrouselLayer = m_carrouselLayers.takeLast();
|
|
|
|
if(carrouselLayer != nullptr)
|
|
|
|
{
|
|
|
|
delete carrouselLayer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-05-01 18:26:02 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPieceCarrousel::SelectPiece(VPuzzlePiece* piece)
|
|
|
|
{
|
|
|
|
for (auto layer : m_carrouselLayers)
|
|
|
|
{
|
|
|
|
QList<VPieceCarrouselPiece*> carrouselPieces = layer->GetCarrouselPieces();
|
|
|
|
for (auto carrouselPiece : carrouselPieces)
|
|
|
|
{
|
|
|
|
carrouselPiece->SetIsSelected(carrouselPiece->GetPiece() == piece);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-13 12:24:26 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-04-26 12:09:28 +02:00
|
|
|
void VPieceCarrousel::on_ActiveLayerChanged(int index)
|
2020-04-13 12:24:26 +02:00
|
|
|
{
|
2020-04-26 12:09:28 +02:00
|
|
|
qCDebug(pCarrousel, "index changed %i", index);
|
|
|
|
|
2020-04-13 12:24:26 +02:00
|
|
|
int j=0;
|
2020-04-26 12:09:28 +02:00
|
|
|
for (VPieceCarrouselLayer *carrouselLayer: m_carrouselLayers) {
|
|
|
|
carrouselLayer->setVisible(j == index);
|
2020-04-13 12:24:26 +02:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-04-26 12:09:28 +02:00
|
|
|
void VPieceCarrousel::SetOrientation(Qt::Orientation orientation)
|
2020-04-13 12:24:26 +02:00
|
|
|
{
|
2020-05-02 12:17:06 +02:00
|
|
|
m_orientation = orientation;
|
|
|
|
RefreshOrientation();
|
|
|
|
}
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-05-02 12:17:06 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPieceCarrousel::RefreshOrientation()
|
|
|
|
{
|
|
|
|
QBoxLayout::Direction direction = (m_orientation == Qt::Horizontal)?
|
2020-04-26 14:03:43 +02:00
|
|
|
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
|
2020-05-02 12:17:06 +02:00
|
|
|
if(m_orientation == Qt::Horizontal)
|
2020-04-13 12:24:26 +02:00
|
|
|
{
|
2020-04-26 12:09:28 +02:00
|
|
|
m_comboBoxLayer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
2020-04-26 14:03:43 +02:00
|
|
|
|
|
|
|
// 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
|
2020-05-01 18:26:02 +02:00
|
|
|
m_scrollArea->setMinimumHeight(128 + m_scrollArea->horizontalScrollBar()->sizeHint().height()+2);
|
2020-04-26 14:03:43 +02:00
|
|
|
m_scrollArea->setMinimumWidth(0);
|
2020-04-13 12:24:26 +02:00
|
|
|
}
|
|
|
|
else // Qt::Vertical
|
|
|
|
{
|
2020-04-26 12:09:28 +02:00
|
|
|
m_comboBoxLayer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 14:03:43 +02:00
|
|
|
// scroll bar policy of scroll area
|
|
|
|
m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
|
|
|
m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
2020-04-13 12:24:26 +02:00
|
|
|
|
2020-04-26 14:03:43 +02:00
|
|
|
m_scrollArea->setMinimumHeight(0);
|
2020-05-01 18:26:02 +02:00
|
|
|
m_scrollArea->setMinimumWidth(124 + m_scrollArea->verticalScrollBar()->sizeHint().width()+2);
|
2020-04-26 14:03:43 +02:00
|
|
|
// FIXME: find a nicer way than putting directly the 120 width of the piece
|
2020-04-13 12:24:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:26:02 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VPieceCarrousel::on_PieceClicked(VPieceCarrouselPiece* carrouselPiece)
|
|
|
|
{
|
|
|
|
emit pieceClicked(carrouselPiece->GetPiece());
|
|
|
|
}
|
|
|
|
|
2020-04-26 12:09:28 +02:00
|
|
|
|