Add Add/Remove sheets.

This commit is contained in:
Roman Telezhynskyi 2021-07-31 12:21:07 +03:00
parent 52c9257bd0
commit bc0b271f16
9 changed files with 156 additions and 28 deletions

View File

@ -62,10 +62,12 @@ VPCarrousel::VPCarrousel(VPLayout *layout, QWidget *parent) :
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPCarrousel::Refresh() void VPCarrousel::Refresh()
{ {
const int index = ui->comboBoxPieceList->currentIndex(); const QUuid sheetUuid = ui->comboBoxPieceList->currentData().toUuid();
// --- clears the content of the carrousel // --- clears the content of the carrousel
ui->comboBoxPieceList->blockSignals(true);
Clear(); Clear();
ui->comboBoxPieceList->blockSignals(false);
// --- add the content saved in the layout to the carrousel. // --- add the content saved in the layout to the carrousel.
// Do not rely on m_layout because we do not control it. // Do not rely on m_layout because we do not control it.
@ -86,26 +88,34 @@ void VPCarrousel::Refresh()
QList<VPSheet *> sheets = m_layout->GetSheets(); QList<VPSheet *> sheets = m_layout->GetSheets();
for (auto *sheet : sheets) for (auto *sheet : sheets)
{ {
VPCarrouselSheet carrouselSheet; if (sheet->IsVisible())
carrouselSheet.unplaced = false; {
carrouselSheet.active = (sheet == m_layout->GetFocusedSheet()); VPCarrouselSheet carrouselSheet;
carrouselSheet.name = sheet->GetName(); carrouselSheet.unplaced = false;
carrouselSheet.pieces = sheet->GetPieces(); 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); ui->comboBoxPieceList->blockSignals(true);
for (const auto& sheet: m_pieceLists) 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(false);
} }
ui->comboBoxPieceList->blockSignals(true);
ui->comboBoxPieceList->setCurrentIndex(-1); ui->comboBoxPieceList->setCurrentIndex(-1);
ui->comboBoxPieceList->blockSignals(false);
int index = ui->comboBoxPieceList->findData(sheetUuid);
ui->comboBoxPieceList->setCurrentIndex(index != -1 ? index : 0); ui->comboBoxPieceList->setCurrentIndex(index != -1 ? index : 0);
RefreshOrientation(); RefreshOrientation();
@ -114,18 +124,23 @@ void VPCarrousel::Refresh()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPCarrousel::RefreshSheetNames() void VPCarrousel::RefreshSheetNames()
{ {
// Here we assume that order and number of sheets are the same in layout and here for (int i=0; i < m_pieceLists.size(); ++i)
QList<VPSheet *> sheets = m_layout->GetSheets();
if (m_pieceLists.size() != sheets.size()+1)
{ {
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) ui->comboBoxPieceList->setItemText(i, GetSheetName(m_pieceLists.at(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)));
} }
} }
@ -149,11 +164,13 @@ void VPCarrousel::on_ActivePieceListChanged(int index)
if (index > 0) if (index > 0)
{ {
QList<VPSheet *> 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<VPPiece *>()); ui->listWidget->SetCurrentPieceList(QList<VPPiece *>());
m_layout->SetFocusedSheet(nullptr); m_layout->SetFocusedSheet(nullptr);
emit on_ActiveSheetChanged();
} }
RefreshSheetNames(); RefreshSheetNames();

View File

@ -46,6 +46,7 @@ struct VPCarrouselSheet
bool active{false}; bool active{false};
QString name{}; QString name{};
QList<VPPiece *> pieces{}; QList<VPPiece *> pieces{};
QUuid sheetUuid{};
}; };
class VPCarrousel : public QWidget class VPCarrousel : public QWidget
@ -68,10 +69,7 @@ public:
*/ */
void RefreshOrientation(); void RefreshOrientation();
/**
* @brief Refresh Refreshes the content of the carrousel
*/
void Refresh();
void RefreshSheetNames(); void RefreshSheetNames();
@ -80,6 +78,15 @@ public:
*/ */
void Clear(); void Clear();
signals:
void on_ActiveSheetChanged();
public slots:
/**
* @brief Refresh Refreshes the content of the carrousel
*/
void Refresh();
protected: protected:
virtual void changeEvent(QEvent* event) override; virtual void changeEvent(QEvent* event) override;

View File

@ -100,6 +100,20 @@ auto VPLayout::GetSheets() -> QList<VPSheet *>
return m_sheets; 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) void VPLayout::SetFocusedSheet(VPSheet *focusedSheet)
{ {

View File

@ -51,6 +51,7 @@ public:
auto AddSheet() -> VPSheet*; auto AddSheet() -> VPSheet*;
auto AddSheet(VPSheet *sheet) -> VPSheet*; auto AddSheet(VPSheet *sheet) -> VPSheet*;
auto GetSheets() -> QList<VPSheet *>; auto GetSheets() -> QList<VPSheet *>;
auto GetSheet(const QUuid &uuid) -> VPSheet *;
/** /**
* @brief SetFocusedSheet Sets the focused sheet, to which pieces are added from the carrousel via drag * @brief SetFocusedSheet Sets the focused sheet, to which pieces are added from the carrousel via drag

View File

@ -31,6 +31,7 @@
#include <QDragEnterEvent> #include <QDragEnterEvent>
#include <QMimeData> #include <QMimeData>
#include <QKeyEvent> #include <QKeyEvent>
#include <QMenu>
#include "vpmimedatapiece.h" #include "vpmimedatapiece.h"
#include "vplayout.h" #include "vplayout.h"
@ -46,11 +47,10 @@ Q_LOGGING_CATEGORY(pMainGraphicsView, "p.mainGraphicsView")
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, VPTileFactory *tileFactory, QWidget *parent) : VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, VPTileFactory *tileFactory, QWidget *parent) :
VMainGraphicsView(parent), VMainGraphicsView(parent),
m_scene(new VMainGraphicsScene(this)),
m_layout(layout) m_layout(layout)
{ {
SCASSERT(m_layout != nullptr) SCASSERT(m_layout != nullptr)
// TODO : list of scenes
m_scene = new VMainGraphicsScene(this);
setScene(m_scene); setScene(m_scene);
m_graphicsSheet = new VPGraphicsSheet(layout->GetFocusedSheet()); m_graphicsSheet = new VPGraphicsSheet(layout->GetFocusedSheet());
@ -78,6 +78,28 @@ void VPMainGraphicsView::RefreshLayout()
m_scene->update(); m_scene->update();
} }
//---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::RefreshPieces()
{
qDeleteAll(m_graphicsPieces);
m_graphicsPieces.clear();
VPSheet *sheet = m_layout->GetFocusedSheet();
if (sheet != nullptr)
{
QList<VPPiece *> 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() 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<VPPiece *> pieces = sheet->GetPieces();
for (auto *piece : pieces)
{
piece->SetSheet(nullptr);
}
}
m_layout->SetFocusedSheet(nullptr);
emit on_SheetRemoved();
RefreshPieces();
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPMainGraphicsView::on_PieceSheetChanged(VPPiece *piece) void VPMainGraphicsView::on_PieceSheetChanged(VPPiece *piece)
{ {

View File

@ -67,6 +67,9 @@ public:
*/ */
void CleanAfterExport(); void CleanAfterExport();
signals:
void on_SheetRemoved();
public slots: public slots:
/** /**
* @brief on_PieceSheetChanged The slot is called when the given piece was moved from the given piece list to * @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 on_PieceSheetChanged(VPPiece *piece);
void RefreshPieces();
protected: protected:
void dragEnterEvent(QDragEnterEvent *event) override; void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override; void dragMoveEvent(QDragMoveEvent *event) override;
@ -83,12 +88,14 @@ protected:
void keyPressEvent(QKeyEvent *event) override; void keyPressEvent(QKeyEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override;
void drawTilesLine(); void drawTilesLine();
private: private:
Q_DISABLE_COPY(VPMainGraphicsView) Q_DISABLE_COPY(VPMainGraphicsView)
VMainGraphicsScene *m_scene{nullptr}; VMainGraphicsScene *m_scene;
VPGraphicsSheet *m_graphicsSheet{nullptr}; VPGraphicsSheet *m_graphicsSheet{nullptr};

View File

@ -734,6 +734,8 @@ void VPMainWindow::InitMainGraphics()
connect(m_graphicsView, &VPMainGraphicsView::ScaleChanged, this, &VPMainWindow::on_ScaleChanged); connect(m_graphicsView, &VPMainGraphicsView::ScaleChanged, this, &VPMainWindow::on_ScaleChanged);
connect(m_graphicsView->GetScene(), &VMainGraphicsScene::mouseMove, this, &VPMainWindow::on_MouseMoved); 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);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -67,3 +67,21 @@ void VPSheet::SetName(const QString &name)
{ {
m_name = 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;
}

View File

@ -33,6 +33,7 @@
#include <QMarginsF> #include <QMarginsF>
#include <QList> #include <QList>
#include <QComboBox> #include <QComboBox>
#include <QUuid>
#include "def.h" #include "def.h"
@ -67,12 +68,21 @@ public:
*/ */
void SetName(const QString &name); void SetName(const QString &name);
auto Uuid() const -> const QUuid &;
bool IsVisible() const;
void SetVisible(bool visible);
private: private:
Q_DISABLE_COPY(VPSheet) Q_DISABLE_COPY(VPSheet)
VPLayout *m_layout; VPLayout *m_layout;
QString m_name{}; QString m_name{};
QUuid m_uuid{QUuid::createUuid()};
bool m_visible{true};
}; };
#endif // VPSHEET_H #endif // VPSHEET_H