From f5e325a23591e2cfa7ab3b4cbce16460a27d35b8 Mon Sep 17 00:00:00 2001 From: Ronan Le Tiec Date: Fri, 1 May 2020 18:26:02 +0200 Subject: [PATCH] Piece carrousel design and piece selection --- src/app/puzzle/puzzlemainwindow.cpp | 23 +++++++- src/app/puzzle/puzzlemainwindow.h | 6 ++ src/app/puzzle/vpiececarrousel.cpp | 28 ++++++++- src/app/puzzle/vpiececarrousel.h | 11 ++++ src/app/puzzle/vpiececarrousellayer.cpp | 16 +++++ src/app/puzzle/vpiececarrousellayer.h | 4 ++ src/app/puzzle/vpiececarrouselpiece.cpp | 58 +++++++++++++++++-- src/app/puzzle/vpiececarrouselpiece.h | 33 +++++++++-- src/app/puzzle/vpuzzlepiece.h | 1 - .../puzzle/xml/vpuzzlelayoutfilereader.cpp | 12 ++-- 10 files changed, 172 insertions(+), 20 deletions(-) diff --git a/src/app/puzzle/puzzlemainwindow.cpp b/src/app/puzzle/puzzlemainwindow.cpp index 1fa45c60e..de5220dcb 100644 --- a/src/app/puzzle/puzzlemainwindow.cpp +++ b/src/app/puzzle/puzzlemainwindow.cpp @@ -294,6 +294,9 @@ void PuzzleMainWindow::InitPieceCarrousel() connect(ui->dockWidgetPieceCarrousel, QOverload::of(&QDockWidget::dockLocationChanged), this, &PuzzleMainWindow::on_PieceCarrouselLocationChanged); + + connect(m_pieceCarrousel, QOverload::of(&VPieceCarrousel::pieceClicked), this, + &PuzzleMainWindow::on_PieceSelected); } @@ -329,7 +332,10 @@ void PuzzleMainWindow::SetPropertyTabCurrentPieceData() } else { - // TODO set the values of the piece currently selected + // set the value to the current piece + ui->lineEditCurrentPieceName->setText(m_selectedPiece->GetName()); + + // TODO: checkbox show seamline, mirror piece, rotation and placement; } } @@ -816,3 +822,18 @@ void PuzzleMainWindow::on_PieceCarrouselLocationChanged(Qt::DockWidgetArea area) } } +//--------------------------------------------------------------------------------------------------------------------- +void PuzzleMainWindow::on_PieceSelected(VPuzzlePiece* piece) +{ + m_selectedPiece = piece; + + // update the state of the piece carrousel + m_pieceCarrousel->SelectPiece(piece); + + // update the Layout + + // TODO + + // update the property of the piece currently selected + SetPropertyTabCurrentPieceData(); +} diff --git a/src/app/puzzle/puzzlemainwindow.h b/src/app/puzzle/puzzlemainwindow.h index 3432d50e5..011021d9c 100644 --- a/src/app/puzzle/puzzlemainwindow.h +++ b/src/app/puzzle/puzzlemainwindow.h @@ -356,6 +356,12 @@ private slots: */ void on_PieceCarrouselLocationChanged(Qt::DockWidgetArea area); + /** + * @brief on_PieceSelected When a been has been selected + * @param piece the piece that was selected + */ + void on_PieceSelected(VPuzzlePiece* piece); + }; #endif // PUZZLEMAINWINDOW_H diff --git a/src/app/puzzle/vpiececarrousel.cpp b/src/app/puzzle/vpiececarrousel.cpp index 1ff7db0e2..06741ffb7 100644 --- a/src/app/puzzle/vpiececarrousel.cpp +++ b/src/app/puzzle/vpiececarrousel.cpp @@ -119,6 +119,10 @@ void VPieceCarrousel::Refresh() 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); @@ -155,6 +159,20 @@ void VPieceCarrousel::Clear() } } +//--------------------------------------------------------------------------------------------------------------------- +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) { @@ -198,7 +216,7 @@ void VPieceCarrousel::SetOrientation(Qt::Orientation orientation) m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); // FIXME: find a nicer way than putting directly the 120 height of the piece - m_scrollArea->setMinimumHeight(120 + m_scrollArea->horizontalScrollBar()->sizeHint().height()+2); + m_scrollArea->setMinimumHeight(128 + m_scrollArea->horizontalScrollBar()->sizeHint().height()+2); m_scrollArea->setMinimumWidth(0); } else // Qt::Vertical @@ -211,10 +229,16 @@ void VPieceCarrousel::SetOrientation(Qt::Orientation orientation) m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); m_scrollArea->setMinimumHeight(0); - m_scrollArea->setMinimumWidth(120 + m_scrollArea->verticalScrollBar()->sizeHint().width()+2); + 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()); +} + diff --git a/src/app/puzzle/vpiececarrousel.h b/src/app/puzzle/vpiececarrousel.h index e8dd9c9ed..9e90c56d7 100644 --- a/src/app/puzzle/vpiececarrousel.h +++ b/src/app/puzzle/vpiececarrousel.h @@ -33,6 +33,7 @@ #include #include #include "vpuzzlelayout.h" +#include "vpuzzlepiece.h" #include "vpiececarrousellayer.h" class VPieceCarrousel : public QWidget @@ -59,9 +60,19 @@ public: */ void Clear(); + /** + * @brief SelectPiece Updates the carrousel so that the given piece is selected + * @param piece the piece to select + */ + void SelectPiece(VPuzzlePiece* piece); + + signals: + void pieceClicked(VPuzzlePiece* piece); public slots: + void on_PieceClicked(VPieceCarrouselPiece* carrouselPiece); + private: Q_DISABLE_COPY(VPieceCarrousel) diff --git a/src/app/puzzle/vpiececarrousellayer.cpp b/src/app/puzzle/vpiececarrousellayer.cpp index 1c7c0e1f3..20cecd43d 100644 --- a/src/app/puzzle/vpiececarrousellayer.cpp +++ b/src/app/puzzle/vpiececarrousellayer.cpp @@ -86,5 +86,21 @@ void VPieceCarrouselLayer::Refresh() setVisible(true); carrouselPiece->CleanPreview(); setVisible(false); + + connect(carrouselPiece, QOverload::of(&VPieceCarrouselPiece::clicked), this, + &VPieceCarrouselLayer::on_PieceClicked); + } } + +//--------------------------------------------------------------------------------------------------------------------- +QList VPieceCarrouselLayer::GetCarrouselPieces() +{ + return m_carrouselPieces; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPieceCarrouselLayer::on_PieceClicked(VPieceCarrouselPiece* carrouselPiece) +{ + emit pieceClicked(carrouselPiece); +} diff --git a/src/app/puzzle/vpiececarrousellayer.h b/src/app/puzzle/vpiececarrousellayer.h index 82dcad530..aa3bae3a2 100644 --- a/src/app/puzzle/vpiececarrousellayer.h +++ b/src/app/puzzle/vpiececarrousellayer.h @@ -43,9 +43,13 @@ public: void Init(); void Refresh(); + QList GetCarrouselPieces(); + signals: + void pieceClicked(VPieceCarrouselPiece* carrouselPiece); public slots: + void on_PieceClicked(VPieceCarrouselPiece* carrouselPiece); private: Q_DISABLE_COPY(VPieceCarrouselLayer) diff --git a/src/app/puzzle/vpiececarrouselpiece.cpp b/src/app/puzzle/vpiececarrouselpiece.cpp index fea3c1f57..ae690b0ae 100644 --- a/src/app/puzzle/vpiececarrouselpiece.cpp +++ b/src/app/puzzle/vpiececarrouselpiece.cpp @@ -37,7 +37,7 @@ Q_LOGGING_CATEGORY(pCarrouselPiece, "p.carrouselPiece") //--------------------------------------------------------------------------------------------------------------------- -VPieceCarrouselPiece::VPieceCarrouselPiece(VPuzzlePiece *piece, QWidget *parent) : QWidget(parent), m_piece(piece) +VPieceCarrouselPiece::VPieceCarrouselPiece(VPuzzlePiece *piece, QWidget *parent) : QFrame(parent), m_piece(piece) { Init(); } @@ -52,25 +52,31 @@ VPieceCarrouselPiece::~VPieceCarrouselPiece() //--------------------------------------------------------------------------------------------------------------------- void VPieceCarrouselPiece::Init() { - //m_label->setStyleSheet("background-color:cornflowerblue"); - // Define the structure - setFixedSize(120,120); + setFixedSize(124,128); QVBoxLayout *pieceLayout = new QVBoxLayout(); pieceLayout->setMargin(0); + pieceLayout->setSpacing(0); setLayout(pieceLayout); + setStyleSheet("background-color:white; border: 2px solid transparent;"); + // define the preview of the piece m_graphicsView = new QGraphicsView(this); + + // m_graphicsView = new VMainGraphicsView(this); + // --> undefined reference to 'VMainGraphicsView::VMainGraphicView(QWidget*)' QGraphicsScene *graphicsScene = new QGraphicsScene(this); m_graphicsView->setScene(graphicsScene); m_graphicsView->setFixedSize(120,100); + m_graphicsView->setStyleSheet("border: 4px solid transparent;"); // define the label m_label = new QLabel(); m_label->sizePolicy(); m_label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter); - m_label->setFixedSize(120,20); + m_label->setFixedSize(120,24); + m_label->setStyleSheet("border: 0px;"); pieceLayout->addWidget(m_graphicsView); pieceLayout->addWidget(m_label); @@ -111,6 +117,46 @@ void VPieceCarrouselPiece::Refresh() QString clippedText = metrix.elidedText(m_piece->GetName(), Qt::ElideRight, width); m_label->setText(clippedText); - m_label->setToolTip(m_piece->GetName()); + setToolTip(m_piece->GetName()); } + +//--------------------------------------------------------------------------------------------------------------------- +VPuzzlePiece * VPieceCarrouselPiece::GetPiece() +{ + return m_piece; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPieceCarrouselPiece::SetIsSelected(bool value) +{ + m_isSelected = value; + + if(value) + { + setStyleSheet("background-color:white; border: 2px solid red;"); + } + else + { + setStyleSheet("background-color:white; border: 2px solid transparent;"); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +bool VPieceCarrouselPiece::GetIsSelected() +{ + return m_isSelected; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPieceCarrouselPiece::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) + { + if(!m_isSelected) + { + emit clicked(this); + } + } +} + diff --git a/src/app/puzzle/vpiececarrouselpiece.h b/src/app/puzzle/vpiececarrouselpiece.h index 7b9cc8e90..6eb7df138 100644 --- a/src/app/puzzle/vpiececarrouselpiece.h +++ b/src/app/puzzle/vpiececarrouselpiece.h @@ -28,14 +28,15 @@ #ifndef VPIECECARROUSELPIECE_H #define VPIECECARROUSELPIECE_H -#include +#include #include #include +#include #include "vpuzzlepiece.h" -class VPieceCarrouselPiece : public QWidget +class VPieceCarrouselPiece : public QFrame { Q_OBJECT public: @@ -50,16 +51,40 @@ public: */ void CleanPreview(); + /** + * @brief GetLayoutPiece Returns the corresponding layout piece + * @return the corresponding layout piece + */ + VPuzzlePiece * GetPiece(); + + /** + * @brief SetSelected sets the selected state to the given value + * @param value the new selected state + */ + void SetIsSelected(bool value); + + /** + * @brief GetSelected Returns wether the piece is selected or not + * @return true if the piece is selected + */ + bool GetIsSelected(); + signals: + void clicked(VPieceCarrouselPiece* m_piece); public slots: +protected: + void mousePressEvent(QMouseEvent *event) override; + private: Q_DISABLE_COPY(VPieceCarrouselPiece) VPuzzlePiece *m_piece; - QLabel *m_label; - QGraphicsView *m_graphicsView; + QLabel *m_label{nullptr}; + QGraphicsView *m_graphicsView{nullptr}; + + bool m_isSelected = false; private slots: diff --git a/src/app/puzzle/vpuzzlepiece.h b/src/app/puzzle/vpuzzlepiece.h index 6504e6d66..42331232f 100644 --- a/src/app/puzzle/vpuzzlepiece.h +++ b/src/app/puzzle/vpuzzlepiece.h @@ -58,7 +58,6 @@ public: /** * @brief SetUuid Sets the uuid of the piece to the given value - * @return the uuid of the piece */ void SetUuid(const QUuid &uuid); diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp b/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp index be52922c3..f622fd733 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp +++ b/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp @@ -62,7 +62,7 @@ bool VPuzzleLayoutFileReader::ReadFile(VPuzzleLayout *layout, QFile *file) //--------------------------------------------------------------------------------------------------------------------- void VPuzzleLayoutFileReader::ReadLayout(VPuzzleLayout *layout) { - Q_ASSERT(isStartElement() && name() == ML::TagLayout); + SCASSERT(isStartElement() && name() == ML::TagLayout); while (readNextStartElement()) { @@ -84,7 +84,7 @@ void VPuzzleLayoutFileReader::ReadLayout(VPuzzleLayout *layout) //--------------------------------------------------------------------------------------------------------------------- void VPuzzleLayoutFileReader::ReadProperties(VPuzzleLayout *layout) { - Q_ASSERT(isStartElement() && name() == ML::TagProperties); + SCASSERT(isStartElement() && name() == ML::TagProperties); while (readNextStartElement()) { @@ -162,7 +162,7 @@ void VPuzzleLayoutFileReader::ReadTiles(VPuzzleLayout *layout) { Q_UNUSED(layout); // to be removed when used - Q_ASSERT(isStartElement() && name() == ML::TagTiles); + SCASSERT(isStartElement() && name() == ML::TagTiles); // QXmlStreamAttributes attribs = attributes(); // attribs.value(ML::AttrVisible); // TODO @@ -195,7 +195,7 @@ void VPuzzleLayoutFileReader::ReadTiles(VPuzzleLayout *layout) //--------------------------------------------------------------------------------------------------------------------- void VPuzzleLayoutFileReader::ReadLayers(VPuzzleLayout *layout) { - Q_ASSERT(isStartElement() && name() == ML::TagLayers); + SCASSERT(isStartElement() && name() == ML::TagLayers); while (readNextStartElement()) { @@ -219,7 +219,7 @@ void VPuzzleLayoutFileReader::ReadLayers(VPuzzleLayout *layout) //--------------------------------------------------------------------------------------------------------------------- void VPuzzleLayoutFileReader::ReadLayer(VPuzzleLayer *layer) { - Q_ASSERT(isStartElement() && (name() == ML::TagLayer || name() == ML::TagUnplacedPiecesLayer)); + SCASSERT(isStartElement() && (name() == ML::TagLayer || name() == ML::TagUnplacedPiecesLayer)); QXmlStreamAttributes attribs = attributes(); layer->SetName(ReadAttributeString(attribs, ML::AttrName, tr("Layer"))); @@ -245,7 +245,7 @@ void VPuzzleLayoutFileReader::ReadLayer(VPuzzleLayer *layer) void VPuzzleLayoutFileReader::ReadPiece(VPuzzlePiece *piece) { Q_UNUSED(piece); - Q_ASSERT(isStartElement() && name() == ML::TagPiece); + SCASSERT(isStartElement() && name() == ML::TagPiece); QXmlStreamAttributes attribs = attributes(); piece->SetName(ReadAttributeString(attribs, ML::AttrName, tr("Piece")));