valentina/src/app/puzzle/vpiececarrousel.cpp

197 lines
6.7 KiB
C++
Raw Normal View History

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>
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)),
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();
QVBoxLayout *layersContainerWrapperLayout = new QVBoxLayout();
layersContainerWrapperLayout->setMargin(0);
layersContainerWrapper->setLayout(layersContainerWrapperLayout);
QVBoxLayout *layersContainerLayout = new QVBoxLayout();
layersContainerLayout->setMargin(0);
m_layersContainer->setLayout(layersContainerLayout);
QSpacerItem *spacer = new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding);
layersContainerWrapperLayout->addWidget(m_layersContainer);
layersContainerWrapperLayout->addSpacerItem(spacer);
QScrollArea *scrollArea = new QScrollArea();
scrollArea->setWidgetResizable( true );
scrollArea->setWidget(layersContainerWrapper);
// init the layout of the piece carrousel
2020-04-13 12:24:26 +02:00
QVBoxLayout *mainLayout = new QVBoxLayout();
setLayout(mainLayout);
setMinimumSize(140,140);
2020-04-26 12:09:28 +02:00
mainLayout->addWidget(m_comboBoxLayer);
mainLayout->addWidget(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
qCDebug(pCarrousel, "layer name : %s", layer->GetName().toStdString().c_str());
// add new carrousel layer
VPieceCarrouselLayer *carrouselLayer = new VPieceCarrouselLayer(layer, this);
m_carrouselLayers.append(carrouselLayer);
m_layersContainer->layout()->addWidget(carrouselLayer);
}
2020-04-13 12:24:26 +02:00
2020-04-26 12:09:28 +02:00
m_comboBoxLayer->setCurrentIndex(0);
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 12:09:28 +02:00
// Removes and deletes the carrousel layer from the list
while (!m_carrouselLayers.isEmpty())
{
VPieceCarrouselLayer *carrouselLayer = m_carrouselLayers.takeLast();
if(carrouselLayer != nullptr)
{
delete carrouselLayer;
}
}
}
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
{
QBoxLayout::Direction direction = QBoxLayout::LeftToRight;
if(orientation == Qt::Horizontal)
{
2020-04-26 12:09:28 +02:00
m_comboBoxLayer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
2020-04-13 12:24:26 +02:00
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
}
else // Qt::Vertical
{
direction = QBoxLayout::TopToBottom;
2020-04-26 12:09:28 +02:00
m_comboBoxLayer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
2020-04-13 12:24:26 +02:00
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
}
2020-04-26 12:09:28 +02:00
// TODO: it's not updated anymore:
QBoxLayout* mainScrollAreaLayout = qobject_cast<QBoxLayout*>(m_layersContainer->layout());
2020-04-13 12:24:26 +02:00
mainScrollAreaLayout->setDirection(direction);
2020-04-26 12:09:28 +02:00
for (VPieceCarrouselLayer *widget: m_carrouselLayers) {
2020-04-13 12:24:26 +02:00
QBoxLayout* layerLayout = qobject_cast<QBoxLayout*>(widget->layout());
layerLayout->setDirection(direction);
}
}
2020-04-26 12:09:28 +02:00