From 1cc6716cff3e459a3c5033dca0e77333d61ae2f8 Mon Sep 17 00:00:00 2001 From: pojienie Date: Apr 19 2020 11:42:21 +0000 Subject: add search bar in fx browser --- diff --git a/toonz/sources/toonz/insertfxpopup.cpp b/toonz/sources/toonz/insertfxpopup.cpp index aea4782..bc5ea15 100644 --- a/toonz/sources/toonz/insertfxpopup.cpp +++ b/toonz/sources/toonz/insertfxpopup.cpp @@ -51,6 +51,8 @@ #include #include #include +#include +#include #include @@ -138,6 +140,55 @@ TFx *createMacroFxByPath(TFilePath path) { //----------------------------------------------------------------------------- //============================================================================= +// FxTree +//============================================================================= + +void FxTree::searchItems(const QString &searchWord) { + // if search word is empty, show all items + if (searchWord.isEmpty()) { + int itemCount = topLevelItemCount(); + for (int i = 0; i < itemCount; ++i) { + QTreeWidgetItem *item = topLevelItem(i); + int childCount = item->childCount(); + for (int j = 0; j < childCount; ++j) item->child(j)->setHidden(false); + item->setHidden(false); + item->setExpanded(false); + } + update(); + return; + } + + // hide all items first + int itemCount = topLevelItemCount(); + for (int i = 0; i < itemCount; ++i) { + QTreeWidgetItem *item = topLevelItem(i); + int childCount = item->childCount(); + for (int j = 0; j < childCount; ++j) item->child(j)->setHidden(true); + item->setHidden(true); + item->setExpanded(false); + } + + QList foundItems = + findItems(searchWord, Qt::MatchContains | Qt::MatchRecursive, 0); + if (foundItems.isEmpty()) { // if nothing is found, do nothing but update + update(); + return; + } + + // for each item found, show it and show its parent + for (auto item : foundItems) { + item->setHidden(false); + QTreeWidgetItem *parent = item->parent(); + if (parent) { + parent->setHidden(false); + parent->setExpanded(true); + } + } + + update(); +} + +//============================================================================= /*! \class InsertFxPopup \brief The InsertFxPopup class provides a dialog to browse fx and add it to @@ -157,7 +208,18 @@ InsertFxPopup::InsertFxPopup() setTopMargin(0); setTopSpacing(0); - m_fxTree = new QTreeWidget(); + QHBoxLayout *searchLay = new QHBoxLayout(); + QLineEdit *searchEdit = new QLineEdit(this); + + searchLay->setMargin(0); + searchLay->setSpacing(5); + searchLay->addWidget(new QLabel(tr("Search:"), this), 0); + searchLay->addWidget(searchEdit); + addLayout(searchLay); + connect(searchEdit, SIGNAL(textChanged(const QString &)), this, + SLOT(onSearchTextChanged(const QString &))); + + m_fxTree = new FxTree(); m_fxTree->setIconSize(QSize(21, 17)); m_fxTree->setColumnCount(1); m_fxTree->header()->close(); @@ -229,6 +291,16 @@ InsertFxPopup::InsertFxPopup() //------------------------------------------------------------------- +void InsertFxPopup::onSearchTextChanged(const QString &text) { + static bool busy = false; + if (busy) return; + busy = true; + m_fxTree->searchItems(text); + busy = false; +} + +//------------------------------------------------------------------- + void InsertFxPopup::makeItem(QTreeWidgetItem *parent, std::string fxId) { QTreeWidgetItem *fxItem = new QTreeWidgetItem( (QTreeWidget *)0, diff --git a/toonz/sources/toonz/insertfxpopup.h b/toonz/sources/toonz/insertfxpopup.h index 3fdc1bb..ab32599 100644 --- a/toonz/sources/toonz/insertfxpopup.h +++ b/toonz/sources/toonz/insertfxpopup.h @@ -3,6 +3,7 @@ #ifndef INSERTFXPOPUP_H #define INSERTFXPOPUP_H +#include #include "toonzqt/dvdialog.h" #include "tfilepath.h" #include "tstream.h" @@ -15,13 +16,24 @@ class TFx; #include //============================================================================= +// FxTree +//----------------------------------------------------------------------------- + +class FxTree final : public QTreeWidget { + Q_OBJECT + +public: + void searchItems(const QString &searchWord = QString()); +}; + +//============================================================================= // InsertFxPopup //----------------------------------------------------------------------------- class InsertFxPopup final : public DVGui::Dialog { Q_OBJECT - QTreeWidget *m_fxTree; + FxTree *m_fxTree; TIStream *m_is; TFilePath m_presetFolder; @@ -59,6 +71,7 @@ protected: protected slots: void updatePresets(); void removePreset(); + void onSearchTextChanged(const QString &text); }; #endif // INSERTFXPOPUP_H