diff --git a/common/lc_findreplacewidget.cpp b/common/lc_findreplacewidget.cpp index cf60bc3c..dbc7935a 100644 --- a/common/lc_findreplacewidget.cpp +++ b/common/lc_findreplacewidget.cpp @@ -18,17 +18,18 @@ lcFindReplaceWidget::lcFindReplaceWidget(QWidget* Parent, lcModel* Model, bool R QGridLayout* Layout = new QGridLayout(this); Layout->setContentsMargins(5, 5, 5, 5); - QCheckBox* FindColorCheckBox = new QCheckBox(tr("Find Color"), this); + Layout->addWidget(new QLabel(tr("Find:")), 0, 0); + + QCheckBox* FindColorCheckBox = new QCheckBox(this); Layout->addWidget(FindColorCheckBox, 0, 1); lcQColorPicker* FindColorPicker = new lcQColorPicker(this); Layout->addWidget(FindColorPicker, 0, 2); - QCheckBox* FindPartCheckBox = new QCheckBox(tr("Find Part"), this); - Layout->addWidget(FindPartCheckBox, 0, 3); - - QComboBox* FindPartComboBox = new QComboBox(this); - Layout->addWidget(FindPartComboBox, 0, 4); + mFindPartComboBox = new QComboBox(this); + mFindPartComboBox->setEditable(true); + mFindPartComboBox->setInsertPolicy(QComboBox::NoInsert); + Layout->addWidget(mFindPartComboBox, 0, 4); QToolButton* FindNextButton = new QToolButton(this); FindNextButton->setAutoRaise(true); @@ -40,25 +41,19 @@ lcFindReplaceWidget::lcFindReplaceWidget(QWidget* Parent, lcModel* Model, bool R FindAllButton ->setDefaultAction(gMainWindow->mActions[LC_EDIT_FIND_ALL]); Layout->addWidget(FindAllButton, 0, 6); - connect(FindColorCheckBox, &QCheckBox::toggled, [](bool Checked) + connect(FindColorCheckBox, &QCheckBox::toggled, [this](bool Checked) { - gMainWindow->mSearchOptions.MatchColor = Checked; + mFindReplaceParams.MatchColor = Checked; }); - connect(FindColorPicker, &lcQColorPicker::colorChanged, [](int ColorIndex) + connect(FindColorPicker, &lcQColorPicker::colorChanged, [this](int ColorIndex) { - gMainWindow->mSearchOptions.ColorIndex = ColorIndex; + mFindReplaceParams.ColorIndex = ColorIndex; }); - connect(FindPartCheckBox, &QCheckBox::toggled, [](bool Checked) - { - gMainWindow->mSearchOptions.MatchInfo = Checked; - }); - - connect(FindPartComboBox, qOverload(&QComboBox::currentIndexChanged), [FindPartComboBox](int Index) - { - gMainWindow->mSearchOptions.Info = (PieceInfo*)FindPartComboBox->itemData(Index).value(); - }); + connect(mFindPartComboBox->lineEdit(), &QLineEdit::returnPressed, gMainWindow->mActions[LC_EDIT_FIND_NEXT], &QAction::trigger); + connect(mFindPartComboBox->lineEdit(), &QLineEdit::textEdited, this, &lcFindReplaceWidget::FindTextEdited); + connect(mFindPartComboBox, qOverload(&QComboBox::currentIndexChanged), this, &lcFindReplaceWidget::FindIndexChanged); QCheckBox* ReplaceColorCheckBox = nullptr; lcQColorPicker* ReplaceColorPicker = nullptr; @@ -89,24 +84,24 @@ lcFindReplaceWidget::lcFindReplaceWidget(QWidget* Parent, lcModel* Model, bool R ReplaceAllButton->setDefaultAction(gMainWindow->mActions[LC_EDIT_REPLACE_ALL]); Layout->addWidget(ReplaceAllButton, 1, 6); - connect(ReplaceColorCheckBox, &QCheckBox::toggled, [](bool Checked) + connect(ReplaceColorCheckBox, &QCheckBox::toggled, [this](bool Checked) { - gMainWindow->mSearchOptions.ReplaceColor = Checked; + mFindReplaceParams.ReplaceColor = Checked; }); - connect(ReplaceColorPicker, &lcQColorPicker::colorChanged, [](int ColorIndex) + connect(ReplaceColorPicker, &lcQColorPicker::colorChanged, [this](int ColorIndex) { - gMainWindow->mSearchOptions.ReplaceColorIndex = ColorIndex; + mFindReplaceParams.ReplaceColorIndex = ColorIndex; }); - connect(ReplacePartCheckBox, &QCheckBox::toggled, [](bool Checked) + connect(ReplacePartCheckBox, &QCheckBox::toggled, [this](bool Checked) { - gMainWindow->mSearchOptions.ReplaceInfo = Checked; + mFindReplaceParams.ReplaceInfo = Checked; }); - connect(ReplacePartComboBox, qOverload(&QComboBox::currentIndexChanged), [ReplacePartComboBox](int Index) + connect(ReplacePartComboBox, qOverload(&QComboBox::currentIndexChanged), [this, ReplacePartComboBox](int Index) { - gMainWindow->mSearchOptions.ReplacePieceInfo = (PieceInfo*)ReplacePartComboBox->itemData(Index).value(); + mFindReplaceParams.ReplacePieceInfo = (PieceInfo*)ReplacePartComboBox->itemData(Index).value(); }); ReplacePartComboBox->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon); @@ -134,41 +129,46 @@ lcFindReplaceWidget::lcFindReplaceWidget(QWidget* Parent, lcModel* Model, bool R Model->GetPartsList(gDefaultColor, false, true, PartsList); for (const auto& PartIt : PartsList) - FindPartComboBox->addItem(PartIt.first->m_strDescription, QVariant::fromValue((void*)PartIt.first)); - FindPartComboBox->model()->sort(0); + mFindPartComboBox->addItem(QString::fromLatin1(PartIt.first->m_strDescription), QVariant::fromValue((void*)PartIt.first)); + mFindPartComboBox->model()->sort(0); lcPiece* Focus = dynamic_cast(Model->GetFocusObject()); - lcSearchOptions& SearchOptions = gMainWindow->mSearchOptions; - - SearchOptions.SearchValid = true; - - if (!Replace) - { - SearchOptions.ReplaceColor = false; - SearchOptions.ReplaceInfo = false; - } - if (Focus) { - SearchOptions.Info = Focus->mPieceInfo; - SearchOptions.ColorIndex = Focus->GetColorIndex(); + mFindReplaceParams.FindInfo = Focus->mPieceInfo; + mFindReplaceParams.ColorIndex = Focus->GetColorIndex(); FindColorCheckBox->setChecked(true); - FindColorPicker->setCurrentColor(SearchOptions.ColorIndex); - FindPartCheckBox->setChecked(true); - FindPartComboBox->setCurrentIndex(FindPartComboBox->findData(QVariant::fromValue((void*)SearchOptions.Info))); + FindColorPicker->setCurrentColor(mFindReplaceParams.ColorIndex); + mFindPartComboBox->setCurrentIndex(mFindPartComboBox->findData(QVariant::fromValue((void*)mFindReplaceParams.FindInfo))); if (Replace) { ReplaceColorCheckBox->setChecked(true); - ReplaceColorPicker->setCurrentColor(SearchOptions.ColorIndex); + ReplaceColorPicker->setCurrentColor(mFindReplaceParams.ColorIndex); ReplacePartCheckBox->setChecked(true); - ReplacePartComboBox->setCurrentIndex(ReplacePartComboBox->findData(QVariant::fromValue((void*)SearchOptions.Info))); + ReplacePartComboBox->setCurrentIndex(ReplacePartComboBox->findData(QVariant::fromValue((void*)mFindReplaceParams.FindInfo))); } } + else + { + mFindPartComboBox->setEditText(QString()); + } adjustSize(); move(1, 1); show(); } + +void lcFindReplaceWidget::FindTextEdited(const QString& Text) +{ + mFindReplaceParams.FindString = Text; + mFindReplaceParams.FindInfo = nullptr; +} + +void lcFindReplaceWidget::FindIndexChanged(int Index) +{ + mFindReplaceParams.FindString.clear(); + mFindReplaceParams.FindInfo = (PieceInfo*)mFindPartComboBox->itemData(Index).value(); +} diff --git a/common/lc_findreplacewidget.h b/common/lc_findreplacewidget.h index 013a2341..4d4f5960 100644 --- a/common/lc_findreplacewidget.h +++ b/common/lc_findreplacewidget.h @@ -1,9 +1,36 @@ #pragma once +struct lcFindReplaceParams +{ + PieceInfo* FindInfo = nullptr; + QString FindString; + + bool MatchColor = false; + bool ReplaceInfo = false; + bool ReplaceColor = false; + int ColorIndex = 0; + PieceInfo* ReplacePieceInfo = nullptr; + int ReplaceColorIndex = 0; +}; + class lcFindReplaceWidget : public QWidget { Q_OBJECT public: lcFindReplaceWidget(QWidget* Parent, lcModel* Model, bool Replace); + + lcFindReplaceParams* GetFindReplaceParams() + { + return &mFindReplaceParams; + } + +protected slots: + void FindTextEdited(const QString& Text); + void FindIndexChanged(int Index); + +protected: + QComboBox* mFindPartComboBox = nullptr; + + lcFindReplaceParams mFindReplaceParams; }; diff --git a/common/lc_global.h b/common/lc_global.h index 3572fc6b..709513d3 100644 --- a/common/lc_global.h +++ b/common/lc_global.h @@ -85,6 +85,8 @@ class lcVector4; class lcMatrix33; class lcMatrix44; +class lcFindReplaceWidget; +struct lcFindReplaceParams; class lcCollapsibleWidget; class lcViewWidget; class lcView; diff --git a/common/lc_mainwindow.cpp b/common/lc_mainwindow.cpp index df4594da..65dd068e 100644 --- a/common/lc_mainwindow.cpp +++ b/common/lc_mainwindow.cpp @@ -97,8 +97,6 @@ lcMainWindow::lcMainWindow() mPreviewToolBar = nullptr; mPreviewWidget = nullptr; - memset(&mSearchOptions, 0, sizeof(mSearchOptions)); - for (int FileIdx = 0; FileIdx < LC_MAX_RECENT_FILES; FileIdx++) mRecentFiles[FileIdx] = lcGetProfileString((LC_PROFILE_KEY)(LC_PROFILE_RECENT_FILE1 + FileIdx)); diff --git a/common/lc_mainwindow.h b/common/lc_mainwindow.h index b59fe0be..34cbe282 100644 --- a/common/lc_mainwindow.h +++ b/common/lc_mainwindow.h @@ -20,20 +20,6 @@ class QPrinter; #define LC_MAX_RECENT_FILES 4 -struct lcSearchOptions -{ - bool SearchValid; - bool MatchInfo; - bool MatchColor; - bool ReplaceInfo; - bool ReplaceColor; - - PieceInfo* Info; - int ColorIndex; - PieceInfo* ReplacePieceInfo; - int ReplaceColorIndex; -}; - class lcTabBar : public QTabBar { public: @@ -340,7 +326,6 @@ public: QString mRecentFiles[LC_MAX_RECENT_FILES]; int mColorIndex; - lcSearchOptions mSearchOptions; QAction* mActions[LC_NUM_COMMANDS]; public slots: diff --git a/common/lc_model.cpp b/common/lc_model.cpp index d7fd29da..30e50bd3 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -25,6 +25,7 @@ #include "lc_qutils.h" #include "lc_lxf.h" #include "lc_previewwidget.h" +#include "lc_findreplacewidget.h" void lcModelProperties::LoadDefaults() { @@ -3784,15 +3785,20 @@ void lcModel::FindReplacePiece(bool SearchForward, bool FindAll) if (mPieces.IsEmpty()) return; - const lcSearchOptions& SearchOptions = gMainWindow->mSearchOptions; + const lcFindReplaceParams* Params = lcView::GetFindReplaceParams(); - if (!SearchOptions.SearchValid) + if (!Params) return; - auto PieceMatches = [](const lcPiece* Piece) + auto PieceMatches = [Params](const lcPiece* Piece) { - const lcSearchOptions& SearchOptions = gMainWindow->mSearchOptions; - return (!SearchOptions.MatchInfo || Piece->mPieceInfo == SearchOptions.Info) && (!SearchOptions.MatchColor || Piece->GetColorIndex() == SearchOptions.ColorIndex); + if (Params->FindInfo && Params->FindInfo != Piece->mPieceInfo) + return false; + + if (!Params->FindString.isEmpty() && !strcasestr(Piece->mPieceInfo->m_strDescription, Params->FindString.toLatin1())) + return false; + + return !Params->MatchColor || Piece->GetColorIndex() == Params->ColorIndex; }; int StartIdx = mPieces.GetSize() - 1; @@ -3805,11 +3811,11 @@ void lcModel::FindReplacePiece(bool SearchForward, bool FindAll) { if (PieceMatches(Piece)) { - if (SearchOptions.ReplaceColor) - Piece->SetColorIndex(SearchOptions.ReplaceColorIndex); + if (Params->ReplaceColor) + Piece->SetColorIndex(Params->ReplaceColorIndex); - if (SearchOptions.ReplaceInfo) - Piece->SetPieceInfo(SearchOptions.ReplacePieceInfo, QString(), true); + if (Params->ReplaceInfo) + Piece->SetPieceInfo(Params->ReplacePieceInfo, QString(), true); SaveCheckpoint(tr("Replacing Part")); gMainWindow->UpdateSelectedObjects(false); diff --git a/common/lc_view.cpp b/common/lc_view.cpp index 02bb2649..26283297 100644 --- a/common/lc_view.cpp +++ b/common/lc_view.cpp @@ -14,7 +14,7 @@ #include "lc_viewsphere.h" #include "lc_findreplacewidget.h" -QWidget* lcView::mFindWidget; +lcFindReplaceWidget* lcView::mFindWidget; lcView* lcView::mLastFocusedView; std::vector lcView::mViews; @@ -60,6 +60,11 @@ lcView::~lcView() delete mContext; } +lcFindReplaceParams* lcView::GetFindReplaceParams() +{ + return mFindWidget ? mFindWidget->GetFindReplaceParams() : nullptr; +} + void lcView::UpdateProjectViews(const Project* Project) { for (lcView* View : mViews) diff --git a/common/lc_view.h b/common/lc_view.h index 667f256f..458618d9 100644 --- a/common/lc_view.h +++ b/common/lc_view.h @@ -101,6 +101,8 @@ public: lcView(const lcView&) = delete; lcView& operator=(const lcView&) = delete; + static lcFindReplaceParams* GetFindReplaceParams(); + void Clear() { mModel = nullptr; @@ -326,7 +328,7 @@ protected: lcVertexBuffer mGridBuffer; int mGridSettings[7]; - static QWidget* mFindWidget; + static lcFindReplaceWidget* mFindWidget; static lcView* mLastFocusedView; static std::vector mViews;