diff --git a/common/lc_mainwindow.cpp b/common/lc_mainwindow.cpp index 9fb3b7e9..8bf2bb75 100644 --- a/common/lc_mainwindow.cpp +++ b/common/lc_mainwindow.cpp @@ -706,6 +706,10 @@ void lcMainWindow::CreateToolBars() mPartsToolBar->setWidget(mPartSelectionWidget); addDockWidget(Qt::RightDockWidgetArea, mPartsToolBar); + connect(mPartsToolBar, &QDockWidget::dockLocationChanged, mPartSelectionWidget, &lcPartSelectionWidget::DockLocationChanged); + connect(mPartSelectionWidget, &lcPartSelectionWidget::PartDoubleClicked, this, &lcMainWindow::PartListDoubleClicked); + connect(mPartSelectionWidget, &lcPartSelectionWidget::PartChanged, this, &lcMainWindow::SetCurrentPieceInfo); + mColorsToolBar = new QDockWidget(tr("Colors"), this); mColorsToolBar->setObjectName("ColorsToolbar"); mColorsToolBar->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); @@ -1104,6 +1108,16 @@ void lcMainWindow::ColorChanged(int ColorIndex) SetColorIndex(ColorIndex); } +void lcMainWindow::PartListDoubleClicked(PieceInfo* Info) +{ + if (!Info) + return; + + quint32 ColorCode = lcGetColorCode(mPartSelectionWidget->GetColorIndex()); + + gMainWindow->PreviewPiece(Info->mFileName, ColorCode, true); +} + void lcMainWindow::ColorButtonClicked() { lcModel* ActiveModel = GetActiveModel(); diff --git a/common/lc_mainwindow.h b/common/lc_mainwindow.h index c3f78e89..bf8e99ec 100644 --- a/common/lc_mainwindow.h +++ b/common/lc_mainwindow.h @@ -230,7 +230,6 @@ public: void SetAngleSnapIndex(int Index); void SetRelativeTransform(bool RelativeTransform); void SetSeparateTransform(bool SelectionTransform); - void SetCurrentPieceInfo(PieceInfo* Info); void SetShadingMode(lcShadingMode ShadingMode); void SetSelectionMode(lcSelectionMode SelectionMode); void ToggleViewSphere(); @@ -289,6 +288,7 @@ public slots: void ProjectFileChanged(const QString& Path); void PreviewPiece(const QString& PartId, int ColorCode, bool ShowPreview); void TogglePreviewWidget(bool Visible); + void SetCurrentPieceInfo(PieceInfo* Info); protected slots: void CameraMenuAboutToShow(); @@ -303,6 +303,7 @@ protected slots: void ClipboardChanged(); void ActionTriggered(); void ColorChanged(int ColorIndex); + void PartListDoubleClicked(PieceInfo* Info); void ColorButtonClicked(); void Print(QPrinter* Printer); void EnableWindowFlags(bool); diff --git a/common/lc_model.cpp b/common/lc_model.cpp index 1f9b8986..2bd431ce 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -4739,7 +4739,9 @@ void lcModel::SetPreviewPieceInfo(PieceInfo* Info, int ColorIndex) AddPiece(Piece); Piece->UpdatePosition(1); - SetCurrentStep(LC_STEP_MAX); + mCurrentStep = LC_STEP_MAX; + CalculateStep(LC_STEP_MAX); + SaveCheckpoint(QString()); } diff --git a/common/lc_partselectionwidget.cpp b/common/lc_partselectionwidget.cpp index 3fe31817..6830e1df 100644 --- a/common/lc_partselectionwidget.cpp +++ b/common/lc_partselectionwidget.cpp @@ -327,6 +327,18 @@ Qt::ItemFlags lcPartSelectionListModel::flags(const QModelIndex& Index) const return DefaultFlags; } +QModelIndex lcPartSelectionListModel::GetPieceInfoIndex(PieceInfo* Info) const +{ + if (Info) + { + for (int PartIndex = 0; PartIndex < static_cast(mParts.size()); PartIndex++) + if (mParts[PartIndex].Info == Info) + return index(PartIndex, 0); + } + + return QModelIndex(); +} + void lcPartSelectionListModel::ReleaseThumbnails() { lcThumbnailManager* ThumbnailManager = lcGetPiecesLibrary()->GetThumbnailManager(); @@ -502,6 +514,17 @@ void lcPartSelectionListView::SetCategory(lcPartCategoryType Type, int Index) setCurrentIndex(mListModel->index(0, 0)); } +void lcPartSelectionListView::SetCurrentPart(PieceInfo* Info) +{ + QModelIndex Index = mListModel->GetPieceInfoIndex(Info); + + if (Index.isValid()) + { + setCurrentIndex(Index); + scrollTo(Index, QAbstractItemView::EnsureVisible); + } +} + void lcPartSelectionListView::SetNoIcons() { SetIconSize(0); @@ -603,26 +626,6 @@ void lcPartSelectionListView::startDrag(Qt::DropActions SupportedActions) Drag->exec(Qt::CopyAction); } -void lcPartSelectionListView::mouseDoubleClickEvent(QMouseEvent* MouseEvent) -{ - if (MouseEvent->button() == Qt::LeftButton ) - PreviewSelection(currentIndex().row()); - - QListView::mouseDoubleClickEvent(MouseEvent); -} - -void lcPartSelectionListView::PreviewSelection(int InfoIndex) -{ - PieceInfo* Info = mListModel->GetPieceInfo(InfoIndex); - - if (!Info) - return; - - quint32 ColorCode = lcGetColorCode(mListModel->GetColorIndex()); - - gMainWindow->PreviewPiece(Info->mFileName, ColorCode, true); -} - lcPartSelectionWidget::lcPartSelectionWidget(QWidget* Parent) : QWidget(Parent), mFilterAction(nullptr) { @@ -698,18 +701,17 @@ lcPartSelectionWidget::lcPartSelectionWidget(QWidget* Parent) Layout->addWidget(mSplitter); setLayout(Layout); - connect(mPartsWidget->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(PartChanged(const QModelIndex&, const QModelIndex&))); - connect(mFilterWidget, SIGNAL(textChanged(const QString&)), this, SLOT(FilterChanged(const QString&))); - connect(mCategoriesWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(CategoryChanged(QTreeWidgetItem*, QTreeWidgetItem*))); - connect(mFilterCategoriesWidget, SIGNAL(textChanged(const QString&)), this, SLOT(FilterCategoriesChanged(const QString&))); + connect(mPartsWidget, &QListView::doubleClicked, this, &lcPartSelectionWidget::PartViewDoubleClicked); + connect(mPartsWidget->selectionModel(), &QItemSelectionModel::currentChanged, this, &lcPartSelectionWidget::PartViewSelectionChanged); + connect(mFilterWidget, &QLineEdit::textChanged, this, &lcPartSelectionWidget::FilterChanged); + connect(mCategoriesWidget, &QTreeWidget::currentItemChanged, this, &lcPartSelectionWidget::CategoryChanged); + connect(mFilterCategoriesWidget, &QLineEdit::textChanged, this, &lcPartSelectionWidget::FilterCategoriesChanged); LoadPartPalettes(); UpdateCategories(); mSplitter->setStretchFactor(0, 0); mSplitter->setStretchFactor(1, 1); - - connect(Parent, SIGNAL(dockLocationChanged(Qt::DockWidgetArea)), this, SLOT(DockLocationChanged(Qt::DockWidgetArea))); } bool lcPartSelectionWidget::event(QEvent* Event) @@ -750,7 +752,7 @@ void lcPartSelectionWidget::LoadState(QSettings& Settings) if (Sizes.size() != 2) { int Length = mSplitter->orientation() == Qt::Horizontal ? mSplitter->width() : mSplitter->height(); - Sizes << Length / 3 << 2 * Length / 3; + Sizes = { Length / 3, 2 * Length / 3 }; } mSplitter->setSizes(Sizes); @@ -767,6 +769,22 @@ void lcPartSelectionWidget::DisableIconMode() mPartsWidget->SetNoIcons(); } +void lcPartSelectionWidget::SetCurrentPart(PieceInfo* Info) +{ + mCategoriesWidget->setCurrentItem(mAllPartsCategoryItem); + mPartsWidget->SetCurrentPart(Info); +} + +void lcPartSelectionWidget::SetOrientation(Qt::Orientation Orientation) +{ + mSplitter->setOrientation(Orientation); + + int Length = mSplitter->orientation() == Qt::Horizontal ? mSplitter->width() : mSplitter->height(); + QList Sizes = { Length / 3, 2 * Length / 3 }; + + mSplitter->setSizes(Sizes); +} + void lcPartSelectionWidget::DockLocationChanged(Qt::DockWidgetArea Area) { if (Area == Qt::LeftDockWidgetArea || Area == Qt::RightDockWidgetArea) @@ -777,7 +795,9 @@ void lcPartSelectionWidget::DockLocationChanged(Qt::DockWidgetArea Area) void lcPartSelectionWidget::resizeEvent(QResizeEvent* Event) { - if (((QDockWidget*)parent())->isFloating()) + QDockWidget* DockWidget = qobject_cast(parent()); + + if (DockWidget && DockWidget->isFloating()) { if (Event->size().width() > Event->size().height()) mSplitter->setOrientation(Qt::Horizontal); @@ -855,12 +875,20 @@ void lcPartSelectionWidget::CategoryChanged(QTreeWidgetItem* Current, QTreeWidge mPartsWidget->SetCategory(static_cast(Type), Index); } -void lcPartSelectionWidget::PartChanged(const QModelIndex& Current, const QModelIndex& Previous) +void lcPartSelectionWidget::PartViewSelectionChanged(const QModelIndex& Current, const QModelIndex& Previous) { Q_UNUSED(Current); Q_UNUSED(Previous); - gMainWindow->SetCurrentPieceInfo(mPartsWidget->GetCurrentPart()); + emit PartChanged(mPartsWidget->GetCurrentPart()); +} + +void lcPartSelectionWidget::PartViewDoubleClicked(const QModelIndex& Index) +{ + PieceInfo* Info = mPartsWidget->GetListModel()->GetPieceInfo(Index.row()); + + if (Info) + emit PartDoubleClicked(Info); } void lcPartSelectionWidget::OptionsMenuAboutToShow() @@ -1084,11 +1112,11 @@ void lcPartSelectionWidget::UpdateCategories() mCategoriesWidget->clear(); - QTreeWidgetItem* AllPartsCategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(tr("All Parts"))); - AllPartsCategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::AllParts)); + mAllPartsCategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(tr("All Parts"))); + mAllPartsCategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::AllParts)); if (CurrentType == lcPartCategoryType::AllParts && CurrentIndex == 0) - CurrentItem = AllPartsCategoryItem; + CurrentItem = mAllPartsCategoryItem; QTreeWidgetItem* CurrentModelCategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(tr("In Use"))); CurrentModelCategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::PartsInUse)); diff --git a/common/lc_partselectionwidget.h b/common/lc_partselectionwidget.h index f1f1ba3e..be049247 100644 --- a/common/lc_partselectionwidget.h +++ b/common/lc_partselectionwidget.h @@ -65,6 +65,8 @@ public: QVariant headerData(int Section, Qt::Orientation Orientation, int Role = Qt::DisplayRole) const override; Qt::ItemFlags flags(const QModelIndex& Index) const override; + QModelIndex GetPieceInfoIndex(PieceInfo* Info) const; + PieceInfo* GetPieceInfo(const QModelIndex& Index) const { return Index.isValid() ? mParts[Index.row()].Info : nullptr; @@ -153,6 +155,7 @@ public: void startDrag(Qt::DropActions SupportedActions) override; void SetCategory(lcPartCategoryType Type, int Index); + void SetCurrentPart(PieceInfo* Info); PieceInfo* GetCurrentPart() const { @@ -191,8 +194,6 @@ public slots: protected: void SetIconSize(int Size); - void PreviewSelection(int InfoIndex); - void mouseDoubleClickEvent(QMouseEvent* MouseEvent) override; lcPartSelectionListModel* mListModel; lcPartSelectionWidget* mPartSelectionWidget; @@ -215,6 +216,13 @@ public: void LoadState(QSettings& Settings); void SaveState(QSettings& Settings); void DisableIconMode(); + void SetOrientation(Qt::Orientation Orientation); + void SetCurrentPart(PieceInfo* Info); + + int GetColorIndex() const + { + return mPartsWidget->GetListModel()->GetColorIndex(); + } void SetColorIndex(int ColorIndex) { @@ -226,19 +234,29 @@ public: return mPartPalettes; } + PieceInfo* GetCurrentPart() const + { + return mPartsWidget->GetCurrentPart(); + } + +signals: + void PartDoubleClicked(PieceInfo* Info); + void PartChanged(PieceInfo* Info); + public slots: void AddToPalette(); void RemoveFromPalette(); + void DockLocationChanged(Qt::DockWidgetArea Area); protected slots: - void DockLocationChanged(Qt::DockWidgetArea Area); void FilterChanged(const QString& Text); void FilterCategoriesChanged(const QString& Text); void FilterTriggered(); void FilterCaseTriggered(); void FilterCategoriesTriggered(); void CategoryChanged(QTreeWidgetItem* Current, QTreeWidgetItem* Previous); - void PartChanged(const QModelIndex& Current, const QModelIndex& Previous); + void PartViewSelectionChanged(const QModelIndex& Current, const QModelIndex& Previous); + void PartViewDoubleClicked(const QModelIndex& Index); void OptionsMenuAboutToShow(); void EditPartPalettes(); @@ -257,5 +275,6 @@ protected: QAction* mFilterAction; lcPartSelectionListView* mPartsWidget; QSplitter* mSplitter; + QTreeWidgetItem* mAllPartsCategoryItem; std::vector mPartPalettes; }; diff --git a/common/lc_propertieswidget.cpp b/common/lc_propertieswidget.cpp index 85e482d4..d57cb854 100644 --- a/common/lc_propertieswidget.cpp +++ b/common/lc_propertieswidget.cpp @@ -898,7 +898,7 @@ void lcPropertiesWidget::PieceIdButtonClicked() QMenu* Menu = new QMenu(PieceIdButton); QWidgetAction* Action = new QWidgetAction(Menu); - lcPieceIdPickerPopup* Popup = new lcPieceIdPickerPopup(gMainWindow->GetActiveModel(), Partial ? nullptr : Info, Menu); + lcPieceIdPickerPopup* Popup = new lcPieceIdPickerPopup(Partial ? nullptr : Info, Menu); Action->setDefaultWidget(Popup); Menu->addAction(Action); diff --git a/qt/lc_qutils.cpp b/qt/lc_qutils.cpp index 77e6dae3..e4e4b30a 100644 --- a/qt/lc_qutils.cpp +++ b/qt/lc_qutils.cpp @@ -4,6 +4,7 @@ #include "lc_library.h" #include "lc_model.h" #include "pieceinf.h" +#include "lc_partselectionwidget.h" QString lcFormatValue(float Value, int Precision) { @@ -170,82 +171,58 @@ QVariant lcPieceIdStringModel::data(const QModelIndex& Index, int Role) const return QVariant(); } -lcPieceIdPickerPopup::lcPieceIdPickerPopup(lcModel* Model, PieceInfo* Current, QWidget* Parent) - : QWidget(Parent) +lcPieceIdPickerPopup::lcPieceIdPickerPopup(PieceInfo* Current, QWidget* Parent) + : QWidget(Parent), mInitialPart(Current) { QVBoxLayout* Layout = new QVBoxLayout(this); - mFilterEdit = new QLineEdit(this); - mFilterEdit->setPlaceholderText(tr("Filter")); - Layout->addWidget(mFilterEdit); + mPartSelectionWidget = new lcPartSelectionWidget(this); + Layout->addWidget(mPartSelectionWidget); - connect(mFilterEdit, &QLineEdit::textEdited, this, &lcPieceIdPickerPopup::FilterEdited); + mPartSelectionWidget->setMinimumWidth(450); - mListView = new QListView(this); - mListView->setEditTriggers(QAbstractItemView::NoEditTriggers); - mListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - mListView->setSelectionBehavior(QAbstractItemView::SelectRows); - mListView->setSelectionMode(QAbstractItemView::SingleSelection); - mListView->setUniformItemSizes(true); + connect(mPartSelectionWidget, &lcPartSelectionWidget::PartDoubleClicked, this, &lcPieceIdPickerPopup::PartDoubleClicked); - mListView->installEventFilter(this); + QDialogButtonBox* ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, this); + Layout->addWidget(ButtonBox); - lcPieceIdStringModel* StringModel = new lcPieceIdStringModel(Model, mListView); - mListView->setModel(StringModel); - - mListView->setMinimumWidth(450); - mListView->setTextElideMode(Qt::ElideMiddle); - - if (Current) - mListView->setCurrentIndex(StringModel->Index(Current)); - - Layout->addWidget(mListView); - - connect(mListView, &QListView::doubleClicked, this, &lcPieceIdPickerPopup::ListViewDoubleClicked); + QObject::connect(ButtonBox, &QDialogButtonBox::accepted, this, &lcPieceIdPickerPopup::Accept); + QObject::connect(ButtonBox, &QDialogButtonBox::rejected, this, &lcPieceIdPickerPopup::Reject); } -bool lcPieceIdPickerPopup::eventFilter(QObject* Object, QEvent* Event) +void lcPieceIdPickerPopup::showEvent(QShowEvent* ShowEvent) { - if (Object == mListView && Event->type() == QEvent::KeyPress) - { - EmitSelectedEvent(mListView->currentIndex()); - return true; - } + QWidget::showEvent(ShowEvent); - return QWidget::eventFilter(Object, Event); + mPartSelectionWidget->SetOrientation(Qt::Horizontal); + mPartSelectionWidget->SetCurrentPart(mInitialPart); } -void lcPieceIdPickerPopup::EmitSelectedEvent(const QModelIndex& Index) +void lcPieceIdPickerPopup::Accept() { - if (!Index.isValid()) - return; + PieceInfo* Info = mPartSelectionWidget->GetCurrentPart(); - lcPieceIdStringModel* StringModel = qobject_cast(mListView->model()); - QVariant Variant = StringModel->data(Index, Qt::UserRole); + emit PieceIdSelected(Info); - if (Variant.isValid()) - { - emit PieceIdSelected(static_cast(Variant.value())); - } + Close(); +} +void lcPieceIdPickerPopup::Reject() +{ + Close(); +} + +void lcPieceIdPickerPopup::PartDoubleClicked(PieceInfo* Info) +{ + emit PieceIdSelected(Info); + + Close(); +} + +void lcPieceIdPickerPopup::Close() +{ QMenu* Menu = qobject_cast(parent()); - Menu->close(); -} - -void lcPieceIdPickerPopup::ListViewDoubleClicked(const QModelIndex& Index) -{ - EmitSelectedEvent(Index); -} - -void lcPieceIdPickerPopup::FilterEdited(const QString& Text) -{ - lcPieceIdStringModel* StringModel = qobject_cast(mListView->model()); - std::vector FilteredRows = StringModel->GetFilteredRows(Text); - - mListView->setUpdatesEnabled(false); - - for (int Row = 0; Row < static_cast(FilteredRows.size()); Row++) - mListView->setRowHidden(Row, !FilteredRows[Row]); - - mListView->setUpdatesEnabled(true); + + if (Menu) + Menu->close(); } diff --git a/qt/lc_qutils.h b/qt/lc_qutils.h index c8ae9985..0b8afd0c 100644 --- a/qt/lc_qutils.h +++ b/qt/lc_qutils.h @@ -2,6 +2,8 @@ #include +class lcPartSelectionWidget; + QString lcFormatValue(float Value, int Precision); QString lcFormatValueLocalized(float Value); float lcParseValueLocalized(const QString& Value); @@ -170,20 +172,20 @@ class lcPieceIdPickerPopup : public QWidget Q_OBJECT public: - lcPieceIdPickerPopup(lcModel* Model, PieceInfo* Current, QWidget* Parent); - - bool eventFilter(QObject* Object, QEvent* Event) override; + lcPieceIdPickerPopup(PieceInfo* Current, QWidget* Parent); signals: void PieceIdSelected(PieceInfo* Info); protected slots: - void ListViewDoubleClicked(const QModelIndex& Index); - void FilterEdited(const QString& Text); + void Accept(); + void Reject(); + void PartDoubleClicked(PieceInfo* Info); protected: - void EmitSelectedEvent(const QModelIndex& Index); + void showEvent(QShowEvent* ShowEvent) override; + void Close(); - QListView* mListView = nullptr; - QLineEdit* mFilterEdit = nullptr; + lcPartSelectionWidget* mPartSelectionWidget = nullptr; + PieceInfo* mInitialPart = nullptr; };