valentina/src/app/puzzle/carousel/vpcarrousel.cpp

299 lines
9.6 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-05-24 14:55:03 +02:00
#include <QFontMetrics>
2020-04-13 12:24:26 +02:00
2020-04-13 14:27:52 +02:00
#include "../vmisc/backport/qoverload.h"
2021-08-09 14:09:10 +02:00
#include "../layout/vpsheet.h"
2021-08-18 19:33:47 +02:00
#include "../layout/vplayout.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
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
VPCarrousel::VPCarrousel(const VPLayoutPtr &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
{
2021-08-18 19:33:47 +02:00
SCASSERT(not layout.isNull())
ui->setupUi(this);
2020-05-24 19:53:51 +02:00
ui->listWidget->SetCarrousel(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
2021-09-13 17:19:39 +02:00
connect(layout.data(), &VPLayout::ActiveSheetChanged, this, &VPCarrousel::on_ActiveSheetChanged);
connect(layout.data(), &VPLayout::SheetListChanged, this, &VPCarrousel::Refresh);
2021-08-18 19:33:47 +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
{
2021-07-31 11:21:07 +02:00
const QUuid sheetUuid = ui->comboBoxPieceList->currentData().toUuid();
2021-05-27 14:55:25 +02:00
2020-04-26 12:09:28 +02:00
// --- clears the content of the carrousel
2021-07-31 11:21:07 +02:00
ui->comboBoxPieceList->blockSignals(true);
2020-04-26 12:09:28 +02:00
Clear();
2021-07-31 11:21:07 +02:00
ui->comboBoxPieceList->blockSignals(false);
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.
2021-05-27 14:55:25 +02:00
m_pieceLists = QList<VPCarrouselSheet>();
2020-05-24 19:53:51 +02:00
2021-08-18 19:33:47 +02:00
VPLayoutPtr layout = m_layout.toStrongRef();
if (not layout.isNull())
2021-05-27 14:55:25 +02:00
{
{
VPCarrouselSheet carrouselSheet;
carrouselSheet.unplaced = true;
2021-07-31 08:24:38 +02:00
carrouselSheet.active = false;
2021-05-27 14:55:25 +02:00
carrouselSheet.name = tr("Unplaced pieces");
2021-08-18 19:33:47 +02:00
carrouselSheet.pieces = layout->GetUnplacedPieces();
2021-05-27 14:55:25 +02:00
m_pieceLists.append(carrouselSheet);
}
2021-08-18 19:33:47 +02:00
QList<VPSheetPtr> sheets = layout->GetSheets();
for (const auto &sheet : sheets)
2021-05-27 14:55:25 +02:00
{
2021-09-13 16:27:46 +02:00
if (not sheet.isNull())
2021-07-31 11:21:07 +02:00
{
VPCarrouselSheet carrouselSheet;
carrouselSheet.unplaced = false;
2021-08-18 19:33:47 +02:00
carrouselSheet.active = (sheet == layout->GetFocusedSheet());
2021-07-31 11:21:07 +02:00
carrouselSheet.name = sheet->GetName();
carrouselSheet.pieces = sheet->GetPieces();
carrouselSheet.sheetUuid = sheet->Uuid();
m_pieceLists.append(carrouselSheet);
}
2021-05-27 14:55:25 +02:00
}
ui->comboBoxPieceList->blockSignals(true);
for (const auto& sheet: m_pieceLists)
{
2021-07-31 11:21:07 +02:00
ui->comboBoxPieceList->addItem(GetSheetName(sheet), sheet.sheetUuid);
2021-05-27 14:55:25 +02:00
}
ui->comboBoxPieceList->blockSignals(false);
}
2020-04-13 12:24:26 +02:00
2021-07-31 11:21:07 +02:00
ui->comboBoxPieceList->blockSignals(true);
2021-05-27 20:11:32 +02:00
ui->comboBoxPieceList->setCurrentIndex(-1);
2021-07-31 11:21:07 +02:00
ui->comboBoxPieceList->blockSignals(false);
int index = ui->comboBoxPieceList->findData(sheetUuid);
2021-05-27 20:11:32 +02:00
ui->comboBoxPieceList->setCurrentIndex(index != -1 ? index : 0);
2020-05-02 12:17:06 +02:00
RefreshOrientation();
2020-04-13 12:24:26 +02:00
}
2021-08-18 19:33:47 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPCarrousel::on_ActiveSheetChanged(const VPSheetPtr &sheet)
{
if (not sheet.isNull())
{
int index = ui->comboBoxPieceList->findData(sheet->Uuid());
if (index != -1)
{
ui->comboBoxPieceList->setCurrentIndex(index);
}
}
else
{
ui->comboBoxPieceList->setCurrentIndex(0);
}
}
2020-11-21 13:45:26 +01:00
//---------------------------------------------------------------------------------------------------------------------
2021-05-27 14:55:25 +02:00
void VPCarrousel::RefreshSheetNames()
2020-11-21 13:45:26 +01:00
{
2021-08-18 19:33:47 +02:00
VPLayoutPtr layout = m_layout.toStrongRef();
if (layout.isNull())
{
return;
}
2021-07-31 11:21:07 +02:00
for (int i=0; i < m_pieceLists.size(); ++i)
2021-05-27 14:55:25 +02:00
{
2021-07-31 11:21:07 +02:00
if (not m_pieceLists.at(i).unplaced)
{
2021-08-18 19:33:47 +02:00
VPSheetPtr sheet = layout->GetSheet(m_pieceLists.at(i).sheetUuid);
if (not sheet.isNull())
2021-07-31 11:21:07 +02:00
{
m_pieceLists[i].name = sheet->GetName();
2021-08-18 19:33:47 +02:00
m_pieceLists[i].active = (sheet == layout->GetFocusedSheet());
2021-07-31 11:21:07 +02:00
}
}
else
{
m_pieceLists[i].name = tr("Unplaced pieces");
}
2020-11-21 13:45:26 +01:00
2021-07-31 11:21:07 +02:00
ui->comboBoxPieceList->setItemText(i, GetSheetName(m_pieceLists.at(i)));
2021-05-27 14:55:25 +02:00
}
2020-11-21 13:45:26 +01:00
}
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);
2021-08-18 19:33:47 +02:00
VPLayoutPtr layout = m_layout.toStrongRef();
if (layout.isNull())
{
return;
}
2021-05-27 14:55:25 +02:00
if (not m_pieceLists.isEmpty() && index >= 0 && index < m_pieceLists.size())
{
2021-05-27 14:55:25 +02:00
ui->listWidget->SetCurrentPieceList(m_pieceLists.at(index).pieces);
2021-07-31 08:24:38 +02:00
if (index > 0)
{
2021-07-31 11:21:07 +02:00
QUuid sheetUuid = ui->comboBoxPieceList->currentData().toUuid();
2021-08-18 19:33:47 +02:00
VPSheetPtr sheet = layout->GetSheet(sheetUuid);
2021-07-31 08:24:38 +02:00
2021-08-18 19:33:47 +02:00
if (not sheet.isNull())
2021-07-31 08:24:38 +02:00
{
VPSheetPtr activeSheet = layout->GetFocusedSheet();
if (not activeSheet.isNull())
{
activeSheet->ClearSelection();
}
2021-08-18 19:33:47 +02:00
m_ignoreActiveSheetChange = true;
layout->SetFocusedSheet(sheet);
m_ignoreActiveSheetChange = false;
2021-07-31 08:24:38 +02:00
}
}
2021-05-27 14:55:25 +02:00
}
else
{
2021-08-18 19:33:47 +02:00
ui->listWidget->SetCurrentPieceList(QList<VPPiecePtr>());
m_ignoreActiveSheetChange = true;
layout->SetFocusedSheet(VPSheetPtr());
m_ignoreActiveSheetChange = false;
2020-04-13 12:24:26 +02:00
}
2021-07-31 08:24:38 +02:00
RefreshSheetNames();
2020-04-13 12:24:26 +02:00
}
2021-09-28 16:12:55 +02:00
//---------------------------------------------------------------------------------------------------------------------
VPLayoutWeakPtr VPCarrousel::Layout() const
{
return m_layout;
}
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
}
}
2021-05-27 14:55:25 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPCarrousel::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange)
{
// retranslate designer form (single inheritance approach)
ui->retranslateUi(this);
RefreshSheetNames();
on_ActivePieceListChanged(ui->comboBoxPieceList->currentIndex());
}
// remember to call base class implementation
QWidget::changeEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
auto VPCarrousel::GetSheetName(const VPCarrouselSheet &sheet) -> QString
{
2021-07-31 08:24:38 +02:00
if (sheet.unplaced)
{
return sheet.name;
}
if (sheet.active)
{
return QStringLiteral("--> %1 %2 <--").arg(tr("Pieces of"), sheet.name);
}
2021-10-01 20:10:16 +02:00
return tr("Pieces of") + ' ' + sheet.name;
2021-05-27 14:55:25 +02:00
}