valentina/src/app/puzzle/vpcarrousel.cpp

157 lines
5.2 KiB
C++
Raw Normal View History

2020-04-13 12:43:27 +02:00
/************************************************************************
**
2020-05-23 14:29:18 +02:00
** @file vpcarrousel.cpp
2020-04-13 12:43:27 +02:00
** @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-05-23 14:29:18 +02:00
#include "vpcarrousel.h"
#include "ui_vpcarrousel.h"
2020-04-13 12:24:26 +02:00
#include <QVBoxLayout>
#include <QMessageBox>
2020-05-05 17:40:36 +02:00
#include <QScrollBar>
2020-04-13 12:24:26 +02:00
2020-04-13 14:27:52 +02:00
#include "../vmisc/backport/qoverload.h"
2020-05-23 15:29:57 +02:00
#include "vppiecelist.h"
2020-04-13 14:27:52 +02:00
2020-04-26 12:09:28 +02:00
#include <QLoggingCategory>
#include <QMenu>
#include <QPainter>
2020-04-26 12:09:28 +02:00
Q_LOGGING_CATEGORY(pCarrousel, "p.carrousel")
2020-04-13 12:24:26 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:34:11 +02:00
VPCarrousel::VPCarrousel(VPLayout *layout, QWidget *parent) :
2020-04-13 12:24:26 +02:00
QWidget(parent),
2020-05-23 14:29:18 +02:00
ui(new Ui::VPCarrousel),
m_layout(layout)
2020-04-26 12:09:28 +02:00
{
ui->setupUi(this);
2020-04-26 12:09:28 +02:00
// init the combo box
2020-05-23 15:29:57 +02:00
connect(ui->comboBoxPieceList, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
&VPCarrousel::on_ActivePieceListChanged);
2020-04-26 12:09:28 +02:00
ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
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
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:29:18 +02:00
void VPCarrousel::Refresh()
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.
// Do not rely on m_layout because we do not control it.
2020-05-23 15:29:57 +02:00
m_pieceLists = m_layout->GetPiecesLists();
m_pieceLists.prepend(m_layout->GetUnplacedPieceList());
2020-04-26 12:09:28 +02:00
2020-05-23 15:29:57 +02:00
for (auto pieceList : m_pieceLists)
2020-04-13 12:24:26 +02:00
{
2020-05-23 15:29:57 +02:00
// add piece list name to combo
ui->comboBoxPieceList->blockSignals(true);
ui->comboBoxPieceList->addItem(pieceList->GetName());
ui->comboBoxPieceList->blockSignals(false);
2020-04-26 12:09:28 +02:00
}
2020-04-13 12:24:26 +02:00
2020-05-23 15:29:57 +02:00
on_ActivePieceListChanged(0);
2020-05-02 12:17:06 +02:00
RefreshOrientation();
2020-04-13 12:24:26 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:29:18 +02:00
void VPCarrousel::Clear()
2020-04-13 12:24:26 +02:00
{
2020-04-26 12:09:28 +02:00
// remove the combobox entries
2020-05-23 15:29:57 +02:00
ui->comboBoxPieceList->clear();
2020-04-26 12:09:28 +02:00
ui->listWidget->clear();
2020-04-26 12:09:28 +02:00
}
2020-04-13 12:24:26 +02:00
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 15:29:57 +02:00
void VPCarrousel::on_ActivePieceListChanged(int index)
2020-04-13 12:24:26 +02:00
{
2020-04-26 12:09:28 +02:00
qCDebug(pCarrousel, "index changed %i", index);
ui->listWidget->clear();
2020-05-23 15:29:57 +02:00
if (index >= 0 && index < m_pieceLists.size())
{
2020-05-23 15:29:57 +02:00
VPPieceList *pieceList = m_pieceLists.at(index);
2020-05-23 15:29:57 +02:00
if (pieceList)
{
2020-05-23 15:29:57 +02:00
QList<VPuzzlePiece*> pieces = pieceList->GetPieces();
for (auto piece : pieces)
{
new QListWidgetItem(piece->PieceIcon(QSize(120, 120)) , piece->GetName(), ui->listWidget);
}
}
2020-04-13 12:24:26 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:29:18 +02:00
void VPCarrousel::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
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:29:18 +02:00
void VPCarrousel::RefreshOrientation()
2020-05-02 12:17:06 +02:00
{
2020-04-26 14:03:43 +02:00
// 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-05-23 15:29:57 +02:00
ui->comboBoxPieceList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
2020-04-26 14:03:43 +02:00
// scroll bar policy of scroll area
ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
2020-04-26 14:03:43 +02:00
ui->listWidget->setFlow(QListView::TopToBottom);
2020-04-13 12:24:26 +02:00
}
else // Qt::Vertical
{
2020-05-23 15:29:57 +02:00
ui->comboBoxPieceList->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
ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
2020-04-13 12:24:26 +02:00
ui->listWidget->setFlow(QListView::LeftToRight);
2020-04-13 12:24:26 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
2020-05-23 14:29:18 +02:00
void VPCarrousel::ClearSelection()
{
m_layout->ClearSelection();
}