/************************************************************************ ** ** @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 ** 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 . ** *************************************************************************/ #include "vpiececarrousel.h" #include #include #include "../vmisc/backport/qoverload.h" #include #include Q_LOGGING_CATEGORY(pCarrousel, "p.carrousel") //--------------------------------------------------------------------------------------------------------------------- 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()) { Init(); } //--------------------------------------------------------------------------------------------------------------------- VPieceCarrousel::~VPieceCarrousel() { 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::of(&QComboBox::currentIndexChanged), this, &VPieceCarrousel::on_ActiveLayerChanged); // 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); m_layersContainer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); QSpacerItem *spacer = new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding); layersContainerWrapperLayout->addWidget(m_layersContainer); layersContainerWrapperLayout->addSpacerItem(spacer); m_scrollArea->setWidgetResizable( true ); m_scrollArea->setWidget(layersContainerWrapper); // init the layout of the piece carrousel QVBoxLayout *mainLayout = new QVBoxLayout(); setLayout(mainLayout); mainLayout->addWidget(m_comboBoxLayer); mainLayout->addWidget(m_scrollArea); // ------ then we fill the carrousel with the layout content Refresh(); } //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrousel::Refresh() { // NOTE: alternative to clearing the carrousel and adding things again, we could make comparision // --- clears the content of the carrousel Clear(); // --- add the content saved in the layout to the carrousel. QList layers = m_layout->GetLayers(); layers.prepend(m_layout->GetUnplacedPiecesLayer()); for (auto layer : layers) { // add layer name to combo m_comboBoxLayer->addItem(layer->GetName()); // add new carrousel layer VPieceCarrouselLayer *carrouselLayer = new VPieceCarrouselLayer(layer, this); m_carrouselLayers.append(carrouselLayer); m_layersContainer->layout()->addWidget(carrouselLayer); connect(carrouselLayer, QOverload::of(&VPieceCarrouselLayer::pieceClicked), this, &VPieceCarrousel::on_PieceClicked); } on_ActiveLayerChanged(0); RefreshOrientation(); } //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrousel::Clear() { // remove the combobox entries int layerCount = m_comboBoxLayer->count(); for(int i=0;iremoveItem(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; } } // Removes and deletes the carrousel layers from the list while (!m_carrouselLayers.isEmpty()) { VPieceCarrouselLayer *carrouselLayer = m_carrouselLayers.takeLast(); if(carrouselLayer != nullptr) { delete carrouselLayer; } } } //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrousel::SelectPiece(VPuzzlePiece* piece) { for (auto layer : m_carrouselLayers) { QList carrouselPieces = layer->GetCarrouselPieces(); for (auto carrouselPiece : carrouselPieces) { carrouselPiece->SetIsSelected(carrouselPiece->GetPiece() == piece); } } } //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrousel::on_ActiveLayerChanged(int index) { qCDebug(pCarrousel, "index changed %i", index); int j=0; for (VPieceCarrouselLayer *carrouselLayer: m_carrouselLayers) { carrouselLayer->setVisible(j == index); j++; } } //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrousel::SetOrientation(Qt::Orientation orientation) { m_orientation = orientation; RefreshOrientation(); } //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrousel::RefreshOrientation() { QBoxLayout::Direction direction = (m_orientation == Qt::Horizontal)? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom; // Update the various qlayouts QBoxLayout* mainScrollAreaLayout = qobject_cast(m_layersContainer->layout()); mainScrollAreaLayout->setDirection(direction); QBoxLayout* layerContainerWrapper = qobject_cast(m_scrollArea->widget()->layout()); layerContainerWrapper->setDirection(direction); for (VPieceCarrouselLayer *widget: m_carrouselLayers) { QBoxLayout* layerLayout = qobject_cast(widget->layout()); layerLayout->setDirection(direction); } // then update the scrollarea min height / width and scrollbar behaviour if(m_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(128 + 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(124 + m_scrollArea->verticalScrollBar()->sizeHint().width()+2); // FIXME: find a nicer way than putting directly the 120 width of the piece } } //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrousel::on_PieceClicked(VPieceCarrouselPiece* carrouselPiece) { emit pieceClicked(carrouselPiece->GetPiece()); }