From bc0b271f1627d101e4828403efad301d8329501d Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 31 Jul 2021 12:21:07 +0300 Subject: [PATCH] Add Add/Remove sheets. --- src/app/puzzle/vpcarrousel.cpp | 60 +++++++++++++++++---------- src/app/puzzle/vpcarrousel.h | 15 +++++-- src/app/puzzle/vplayout.cpp | 14 +++++++ src/app/puzzle/vplayout.h | 1 + src/app/puzzle/vpmaingraphicsview.cpp | 55 +++++++++++++++++++++++- src/app/puzzle/vpmaingraphicsview.h | 9 +++- src/app/puzzle/vpmainwindow.cpp | 2 + src/app/puzzle/vpsheet.cpp | 18 ++++++++ src/app/puzzle/vpsheet.h | 10 +++++ 9 files changed, 156 insertions(+), 28 deletions(-) diff --git a/src/app/puzzle/vpcarrousel.cpp b/src/app/puzzle/vpcarrousel.cpp index bb64d2591..08a13944a 100644 --- a/src/app/puzzle/vpcarrousel.cpp +++ b/src/app/puzzle/vpcarrousel.cpp @@ -62,10 +62,12 @@ VPCarrousel::VPCarrousel(VPLayout *layout, QWidget *parent) : //--------------------------------------------------------------------------------------------------------------------- void VPCarrousel::Refresh() { - const int index = ui->comboBoxPieceList->currentIndex(); + const QUuid sheetUuid = ui->comboBoxPieceList->currentData().toUuid(); // --- clears the content of the carrousel + ui->comboBoxPieceList->blockSignals(true); Clear(); + ui->comboBoxPieceList->blockSignals(false); // --- add the content saved in the layout to the carrousel. // Do not rely on m_layout because we do not control it. @@ -86,26 +88,34 @@ void VPCarrousel::Refresh() QList sheets = m_layout->GetSheets(); for (auto *sheet : sheets) { - VPCarrouselSheet carrouselSheet; - carrouselSheet.unplaced = false; - carrouselSheet.active = (sheet == m_layout->GetFocusedSheet()); - carrouselSheet.name = sheet->GetName(); - carrouselSheet.pieces = sheet->GetPieces(); + if (sheet->IsVisible()) + { + VPCarrouselSheet carrouselSheet; + carrouselSheet.unplaced = false; + carrouselSheet.active = (sheet == m_layout->GetFocusedSheet()); + carrouselSheet.name = sheet->GetName(); + carrouselSheet.pieces = sheet->GetPieces(); + carrouselSheet.sheetUuid = sheet->Uuid(); - m_pieceLists.append(carrouselSheet); + m_pieceLists.append(carrouselSheet); + } } ui->comboBoxPieceList->blockSignals(true); for (const auto& sheet: m_pieceLists) { - ui->comboBoxPieceList->addItem(GetSheetName(sheet)); + ui->comboBoxPieceList->addItem(GetSheetName(sheet), sheet.sheetUuid); } ui->comboBoxPieceList->blockSignals(false); } + ui->comboBoxPieceList->blockSignals(true); ui->comboBoxPieceList->setCurrentIndex(-1); + ui->comboBoxPieceList->blockSignals(false); + + int index = ui->comboBoxPieceList->findData(sheetUuid); ui->comboBoxPieceList->setCurrentIndex(index != -1 ? index : 0); RefreshOrientation(); @@ -114,18 +124,23 @@ void VPCarrousel::Refresh() //--------------------------------------------------------------------------------------------------------------------- void VPCarrousel::RefreshSheetNames() { - // Here we assume that order and number of sheets are the same in layout and here - QList sheets = m_layout->GetSheets(); - if (m_pieceLists.size() != sheets.size()+1) + for (int i=0; i < m_pieceLists.size(); ++i) { - return; - } + if (not m_pieceLists.at(i).unplaced) + { + VPSheet *sheet = m_layout->GetSheet(m_pieceLists.at(i).sheetUuid); + if (sheet != nullptr) + { + m_pieceLists[i].name = sheet->GetName(); + m_pieceLists[i].active = (sheet == m_layout->GetFocusedSheet()); + } + } + else + { + m_pieceLists[i].name = tr("Unplaced pieces"); + } - for (int i=0; i < sheets.size(); ++i) - { - m_pieceLists[i+1].name = sheets.at(i)->GetName(); - m_pieceLists[i+1].active = (sheets.at(i) == m_layout->GetFocusedSheet()); - ui->comboBoxPieceList->setItemText(i+1, GetSheetName(m_pieceLists.at(i+1))); + ui->comboBoxPieceList->setItemText(i, GetSheetName(m_pieceLists.at(i))); } } @@ -149,11 +164,13 @@ void VPCarrousel::on_ActivePieceListChanged(int index) if (index > 0) { - QList sheets = m_layout->GetSheets(); + QUuid sheetUuid = ui->comboBoxPieceList->currentData().toUuid(); + VPSheet *sheet = m_layout->GetSheet(sheetUuid); - if (index <= sheets.size()) + if (sheet != nullptr) { - m_layout->SetFocusedSheet(sheets.at(index - 1)); + m_layout->SetFocusedSheet(sheet); + emit on_ActiveSheetChanged(); } } } @@ -161,6 +178,7 @@ void VPCarrousel::on_ActivePieceListChanged(int index) { ui->listWidget->SetCurrentPieceList(QList()); m_layout->SetFocusedSheet(nullptr); + emit on_ActiveSheetChanged(); } RefreshSheetNames(); diff --git a/src/app/puzzle/vpcarrousel.h b/src/app/puzzle/vpcarrousel.h index 74e8984a1..304641119 100644 --- a/src/app/puzzle/vpcarrousel.h +++ b/src/app/puzzle/vpcarrousel.h @@ -46,6 +46,7 @@ struct VPCarrouselSheet bool active{false}; QString name{}; QList pieces{}; + QUuid sheetUuid{}; }; class VPCarrousel : public QWidget @@ -68,10 +69,7 @@ public: */ void RefreshOrientation(); - /** - * @brief Refresh Refreshes the content of the carrousel - */ - void Refresh(); + void RefreshSheetNames(); @@ -80,6 +78,15 @@ public: */ void Clear(); +signals: + void on_ActiveSheetChanged(); + +public slots: + /** + * @brief Refresh Refreshes the content of the carrousel + */ + void Refresh(); + protected: virtual void changeEvent(QEvent* event) override; diff --git a/src/app/puzzle/vplayout.cpp b/src/app/puzzle/vplayout.cpp index e6add2fc3..9273cc26f 100644 --- a/src/app/puzzle/vplayout.cpp +++ b/src/app/puzzle/vplayout.cpp @@ -100,6 +100,20 @@ auto VPLayout::GetSheets() -> QList return m_sheets; } +//--------------------------------------------------------------------------------------------------------------------- +auto VPLayout::GetSheet(const QUuid &uuid) -> VPSheet * +{ + for (auto *sheet : m_sheets) + { + if (sheet->Uuid() == uuid) + { + return sheet; + } + } + + return nullptr; +} + //--------------------------------------------------------------------------------------------------------------------- void VPLayout::SetFocusedSheet(VPSheet *focusedSheet) { diff --git a/src/app/puzzle/vplayout.h b/src/app/puzzle/vplayout.h index ddc0102ee..9a901566d 100644 --- a/src/app/puzzle/vplayout.h +++ b/src/app/puzzle/vplayout.h @@ -51,6 +51,7 @@ public: auto AddSheet() -> VPSheet*; auto AddSheet(VPSheet *sheet) -> VPSheet*; auto GetSheets() -> QList; + auto GetSheet(const QUuid &uuid) -> VPSheet *; /** * @brief SetFocusedSheet Sets the focused sheet, to which pieces are added from the carrousel via drag diff --git a/src/app/puzzle/vpmaingraphicsview.cpp b/src/app/puzzle/vpmaingraphicsview.cpp index 57cb7cbb2..ac97786c9 100644 --- a/src/app/puzzle/vpmaingraphicsview.cpp +++ b/src/app/puzzle/vpmaingraphicsview.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include "vpmimedatapiece.h" #include "vplayout.h" @@ -46,11 +47,10 @@ Q_LOGGING_CATEGORY(pMainGraphicsView, "p.mainGraphicsView") //--------------------------------------------------------------------------------------------------------------------- VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, VPTileFactory *tileFactory, QWidget *parent) : VMainGraphicsView(parent), + m_scene(new VMainGraphicsScene(this)), m_layout(layout) { SCASSERT(m_layout != nullptr) - // TODO : list of scenes - m_scene = new VMainGraphicsScene(this); setScene(m_scene); m_graphicsSheet = new VPGraphicsSheet(layout->GetFocusedSheet()); @@ -78,6 +78,28 @@ void VPMainGraphicsView::RefreshLayout() m_scene->update(); } +//--------------------------------------------------------------------------------------------------------------------- +void VPMainGraphicsView::RefreshPieces() +{ + qDeleteAll(m_graphicsPieces); + m_graphicsPieces.clear(); + + VPSheet *sheet = m_layout->GetFocusedSheet(); + if (sheet != nullptr) + { + QList pieces = sheet->GetPieces(); + m_graphicsPieces.reserve(pieces.size()); + + for (auto *piece : pieces) + { + auto *graphicsPiece = new VPGraphicsPiece(piece); + m_graphicsPieces.append(graphicsPiece); + + scene()->addItem(graphicsPiece); + } + } +} + //--------------------------------------------------------------------------------------------------------------------- VMainGraphicsScene* VPMainGraphicsView::GetScene() { @@ -195,6 +217,35 @@ void VPMainGraphicsView::keyPressEvent(QKeyEvent *event) } } +//--------------------------------------------------------------------------------------------------------------------- +void VPMainGraphicsView::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu menu; + + VPSheet *sheet = m_layout->GetFocusedSheet(); + QAction *removeSheetAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), tr("Remove sheet")); + removeSheetAction->setEnabled(sheet != nullptr && m_layout->GetSheets().size() > 1); + + QAction *selectedAction = menu.exec(event->globalPos()); + if (selectedAction == removeSheetAction) + { + if (sheet != nullptr) + { + sheet->SetVisible(false); + + QList pieces = sheet->GetPieces(); + for (auto *piece : pieces) + { + piece->SetSheet(nullptr); + } + } + + m_layout->SetFocusedSheet(nullptr); + emit on_SheetRemoved(); + RefreshPieces(); + } +} + //--------------------------------------------------------------------------------------------------------------------- void VPMainGraphicsView::on_PieceSheetChanged(VPPiece *piece) { diff --git a/src/app/puzzle/vpmaingraphicsview.h b/src/app/puzzle/vpmaingraphicsview.h index 6b2023daf..bd5e505ad 100644 --- a/src/app/puzzle/vpmaingraphicsview.h +++ b/src/app/puzzle/vpmaingraphicsview.h @@ -67,6 +67,9 @@ public: */ void CleanAfterExport(); +signals: + void on_SheetRemoved(); + public slots: /** * @brief on_PieceSheetChanged The slot is called when the given piece was moved from the given piece list to @@ -75,6 +78,8 @@ public slots: */ void on_PieceSheetChanged(VPPiece *piece); + void RefreshPieces(); + protected: void dragEnterEvent(QDragEnterEvent *event) override; void dragMoveEvent(QDragMoveEvent *event) override; @@ -83,12 +88,14 @@ protected: void keyPressEvent(QKeyEvent *event) override; + void contextMenuEvent(QContextMenuEvent *event) override; + void drawTilesLine(); private: Q_DISABLE_COPY(VPMainGraphicsView) - VMainGraphicsScene *m_scene{nullptr}; + VMainGraphicsScene *m_scene; VPGraphicsSheet *m_graphicsSheet{nullptr}; diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index c86ba19e8..b87b03930 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -734,6 +734,8 @@ void VPMainWindow::InitMainGraphics() connect(m_graphicsView, &VPMainGraphicsView::ScaleChanged, this, &VPMainWindow::on_ScaleChanged); connect(m_graphicsView->GetScene(), &VMainGraphicsScene::mouseMove, this, &VPMainWindow::on_MouseMoved); + connect(m_carrousel, &VPCarrousel::on_ActiveSheetChanged, m_graphicsView, &VPMainGraphicsView::RefreshPieces); + connect(m_graphicsView, &VPMainGraphicsView::on_SheetRemoved, m_carrousel, &VPCarrousel::Refresh); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/vpsheet.cpp b/src/app/puzzle/vpsheet.cpp index e7ed966a8..71f6732c6 100644 --- a/src/app/puzzle/vpsheet.cpp +++ b/src/app/puzzle/vpsheet.cpp @@ -67,3 +67,21 @@ void VPSheet::SetName(const QString &name) { m_name = name; } + +//--------------------------------------------------------------------------------------------------------------------- +auto VPSheet::Uuid() const -> const QUuid & +{ + return m_uuid; +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VPSheet::IsVisible() const -> bool +{ + return m_visible; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPSheet::SetVisible(bool visible) +{ + m_visible = visible; +} diff --git a/src/app/puzzle/vpsheet.h b/src/app/puzzle/vpsheet.h index effe3d0c2..37a4292e3 100644 --- a/src/app/puzzle/vpsheet.h +++ b/src/app/puzzle/vpsheet.h @@ -33,6 +33,7 @@ #include #include #include +#include #include "def.h" @@ -67,12 +68,21 @@ public: */ void SetName(const QString &name); + auto Uuid() const -> const QUuid &; + + bool IsVisible() const; + void SetVisible(bool visible); + private: Q_DISABLE_COPY(VPSheet) VPLayout *m_layout; QString m_name{}; + + QUuid m_uuid{QUuid::createUuid()}; + + bool m_visible{true}; }; #endif // VPSHEET_H