Add search command list for customize popups (#101)

This commit is contained in:
manongjohn 2020-07-26 01:05:39 -04:00 committed by GitHub
parent 4871443ae5
commit 1d09e20c2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 94 additions and 4 deletions

View file

@ -393,6 +393,66 @@ void CommandBarListTree::mousePressEvent(QMouseEvent* event) {
QTreeWidget::mousePressEvent(event);
}
//-----------------------------------------------------------------------------
void CommandBarListTree::displayAll(QTreeWidgetItem* item) {
int childCount = item->childCount();
for (int i = 0; i < childCount; ++i) {
displayAll(item->child(i));
}
item->setHidden(false);
item->setExpanded(false);
}
//-------------------------------------------------------------------
void CommandBarListTree::hideAll(QTreeWidgetItem* item) {
int childCount = item->childCount();
for (int i = 0; i < childCount; ++i) {
hideAll(item->child(i));
}
item->setHidden(true);
item->setExpanded(false);
}
//-----------------------------------------------------------------------------
void CommandBarListTree::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) {
displayAll(topLevelItem(i));
}
update();
return;
}
// hide all items first
int itemCount = topLevelItemCount();
for (int i = 0; i < itemCount; ++i) {
hideAll(topLevelItem(i));
}
QList<QTreeWidgetItem*> 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) {
while (item) {
item->setHidden(false);
item->setExpanded(true);
item = item->parent();
}
}
update();
}
//=============================================================================
// CommandBarPopup
//-----------------------------------------------------------------------------
@ -434,6 +494,8 @@ CommandBarPopup::CommandBarPopup(bool isXsheetToolbar)
nf.setItalic(true);
noticeLabel->setFont(nf);
QLineEdit* searchEdit = new QLineEdit(this);
//--- layout
QVBoxLayout* mainLay = new QVBoxLayout();
m_topLayout->setMargin(0);
@ -446,10 +508,20 @@ CommandBarPopup::CommandBarPopup(bool isXsheetToolbar)
{
mainUILay->addWidget(commandBarLabel, 0, 0);
mainUILay->addWidget(commandItemListLabel, 0, 1);
mainUILay->addWidget(m_menuBarTree, 1, 0);
mainUILay->addWidget(m_commandListTree, 1, 1);
mainUILay->addWidget(noticeLabel, 2, 0, 1, 2);
mainUILay->addWidget(m_menuBarTree, 1, 0, 2, 1);
QHBoxLayout* searchLay = new QHBoxLayout();
searchLay->setMargin(0);
searchLay->setSpacing(5);
{
searchLay->addWidget(new QLabel(tr("Search:"), this), 0);
searchLay->addWidget(searchEdit);
}
mainUILay->addLayout(searchLay, 1, 1);
mainUILay->addWidget(m_commandListTree, 2, 1);
mainUILay->addWidget(noticeLabel, 3, 0, 1, 2);
}
mainUILay->setRowStretch(0, 0);
mainUILay->setRowStretch(1, 1);
@ -473,6 +545,9 @@ CommandBarPopup::CommandBarPopup(bool isXsheetToolbar)
bool ret = connect(okBtn, SIGNAL(clicked()), this, SLOT(onOkPressed()));
ret = ret && connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
ret = ret && connect(searchEdit, SIGNAL(textChanged(const QString&)), this,
SLOT(onSearchTextChanged(const QString&)));
assert(ret);
}
@ -482,4 +557,12 @@ void CommandBarPopup::onOkPressed() {
m_menuBarTree->saveMenuTree(m_path);
accept();
}
}
void CommandBarPopup::onSearchTextChanged(const QString& text) {
static bool busy = false;
if (busy) return;
busy = true;
m_commandListTree->searchItems(text);
busy = false;
}

View file

@ -50,6 +50,12 @@ class CommandBarListTree final : public QTreeWidget {
public:
CommandBarListTree(QWidget* parent = 0);
void searchItems(const QString& searchWord = QString());
private:
void displayAll(QTreeWidgetItem* item);
void hideAll(QTreeWidgetItem* item);
protected:
void mousePressEvent(QMouseEvent*) override;
};
@ -68,6 +74,7 @@ public:
CommandBarPopup(bool isXsheetToolbar = false);
protected slots:
void onOkPressed();
void onSearchTextChanged(const QString& text);
};
#endif