Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search bar to OSCollapsibleItemList #618

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/openstudio_lib/ApplyMeasureNowDialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

#include "../shared_gui_components/HeaderViews.hpp"
#include "../shared_gui_components/OSDialog.hpp"
#include "../shared_gui_components/OSListView.hpp"

#include <openstudio/model/Model.hpp>

Expand Down
86 changes: 69 additions & 17 deletions src/openstudio_lib/OSCollapsibleItemList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <openstudio/utilities/core/Assert.hpp>

#include <QHBoxLayout>
#include <QLineEdit>
#include <QPainter>
#include <QPushButton>
#include <QResizeEvent>
Expand All @@ -50,6 +51,7 @@ OSCollapsibleItemList::OSCollapsibleItemList(bool addScrollArea, QWidget* parent
m_vLayout(nullptr),
m_contentLayout(nullptr),
m_selectedCollapsibleItem(nullptr),
m_searchActive(false),
m_itemsDraggable(false),
m_itemsRemoveable(false),
m_itemsType(OSItemType::ListItem) {
Expand All @@ -59,6 +61,11 @@ OSCollapsibleItemList::OSCollapsibleItemList(bool addScrollArea, QWidget* parent
outerVLayout->setContentsMargins(0, 0, 0, 0);
this->setLayout(outerVLayout);

m_searchBox = new QLineEdit();
m_searchBox->setClearButtonEnabled(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, that icon is a broom?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe, weird on Windows it is a nice x button.

outerVLayout->addWidget(m_searchBox);
connect(m_searchBox, &QLineEdit::textEdited, this, &OSCollapsibleItemList::onSearchTextEdited);

auto* outerWidget = new QWidget();

if (addScrollArea) {
Expand Down Expand Up @@ -158,18 +165,11 @@ void OSCollapsibleItemList::addCollapsibleItem(OSCollapsibleItem* collapsibleIte
connect(collapsibleItem, &OSCollapsibleItem::selectionCleared, this, &OSCollapsibleItemList::selectionCleared);

connect(collapsibleItem, &OSCollapsibleItem::openLibDlgClicked, this, &OSCollapsibleItemList::openLibDlgClicked);

if (!selectedItem()) {
collapsibleItem->itemList()->selectItem(collapsibleItem->itemList()->firstItem());
}

collapsibleItem->setSelected(true);
}

void OSCollapsibleItemList::onCollapsableItemSelected(OSCollapsibleItem* selectedItem) {
QLayoutItem* layoutItem = nullptr;
OSCollapsibleItem* collapsibleItem = nullptr;
OSItem* newSelectedItem = nullptr;

for (int i = 0; i < m_vLayout->count(); ++i) {

Expand All @@ -181,18 +181,21 @@ void OSCollapsibleItemList::onCollapsableItemSelected(OSCollapsibleItem* selecte
if (collapsibleItem == selectedItem) {

if (m_selectedCollapsibleItem != collapsibleItem) {
// select new item
// no need to select collapsibleItem since it is already selected
// expand collapsable item and select first item inside
m_selectedCollapsibleItem = collapsibleItem;
m_selectedCollapsibleItem->setExpanded(true);
newSelectedItem = m_selectedCollapsibleItem->itemList()->firstItem();
OSItem* newSelectedItem = m_selectedCollapsibleItem->itemList()->firstItem();
m_selectedCollapsibleItem->itemList()->selectItem(newSelectedItem);
}

} else {
// deselect
// deselect other collapsable items
collapsibleItem->setSelected(false);
collapsibleItem->setExpanded(false);
collapsibleItem->itemList()->clearSelection();
if (!m_searchActive) {
collapsibleItem->setExpanded(false);
}
//collapsibleItem->itemList()->clearSelection();
}
}
}
Expand All @@ -212,14 +215,63 @@ void OSCollapsibleItemList::onItemSelected(OSItem* item) {
std::vector<OSItem*> items = collapsibleItem->itemList()->items();
if (std::find(items.begin(), items.end(), item) != items.end()) {

// select item
m_selectedCollapsibleItem = collapsibleItem;
m_selectedCollapsibleItem->setSelected(true);
if (m_selectedCollapsibleItem != collapsibleItem) {
// no need to select item since it is already selected
// expand collapsable item and select first item inside
m_selectedCollapsibleItem = collapsibleItem;
m_selectedCollapsibleItem->setSelected(true);
m_selectedCollapsibleItem->setExpanded(true);
}

} else {
// deselect
// deselect other collapsable items
collapsibleItem->setSelected(false);
collapsibleItem->itemList()->clearSelection();
if (!m_searchActive) {
collapsibleItem->setExpanded(false);
}
//collapsibleItem->itemList()->clearSelection();
}
}
}
}

void OSCollapsibleItemList::onSearchTextEdited(const QString& text) {
m_searchActive = !text.isEmpty();

OSItem* newSelectedItem = nullptr;
for (int i = 0; i < m_vLayout->count(); ++i) {

QLayoutItem* layoutItem = m_vLayout->itemAt(i);
QWidget* widget = layoutItem->widget();

OSCollapsibleItem* collapsibleItem = qobject_cast<OSCollapsibleItem*>(widget);
if (collapsibleItem) {
std::vector<OSItem*> items = collapsibleItem->itemList()->items();
unsigned numVisible = 0;
for (const auto& item : items) {
if (m_searchActive) {
if (item->text().contains(text, Qt::CaseInsensitive)) {
item->setVisible(true);
if (!newSelectedItem) {
newSelectedItem = item;
collapsibleItem->itemList()->selectItem(newSelectedItem);
}
++numVisible;
} else {
item->setVisible(false);
}
} else {
item->setVisible(true);
++numVisible;
}
}

if (!m_searchActive) {
collapsibleItem->setExpanded(collapsibleItem->isSelected());
collapsibleItem->setVisible(true);
} else {
collapsibleItem->setExpanded(numVisible > 0);
collapsibleItem->setVisible(numVisible > 0);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/openstudio_lib/OSCollapsibleItemList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

class QVBoxLayout;
class QHBoxLayout;
class QLineEdit;

namespace openstudio {

Expand Down Expand Up @@ -77,14 +78,18 @@ class OSCollapsibleItemList : public OSItemSelector

void onItemSelected(OSItem* item);

void onSearchTextEdited(const QString& text);

protected:
void paintEvent(QPaintEvent* event) override;

private:
QVBoxLayout* m_vLayout;
QLineEdit* m_searchBox;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to see if this shouldn't be on the base class "OSItemSelector" and realize we have an unused class ModelObjectTreeWidget.

The other class that inherits it is OSItemList. Should that one had a search bar? Not sure what it does to be honest.

QHBoxLayout* m_contentLayout;
OSCollapsibleItem* m_selectedCollapsibleItem;
std::vector<OSCollapsibleItem*> m_collapsibleItems;
bool m_searchActive;
bool m_itemsDraggable;
bool m_itemsRemoveable;
OSItemType m_itemsType;
Expand Down
1 change: 0 additions & 1 deletion src/openstudio_lib/RefrigerationGraphicsItems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "OSItem.hpp"
#include "OSDropZone.hpp"
#include "../shared_gui_components/OSListController.hpp"
#include "../shared_gui_components/OSListView.hpp"
#include "../shared_gui_components/GraphicsItems.hpp"
#include <openstudio/utilities/idf/Handle.hpp>

Expand Down
1 change: 0 additions & 1 deletion src/openstudio_lib/VRFGraphicsItems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "OSItem.hpp"
#include "OSDropZone.hpp"
#include "../shared_gui_components/OSListController.hpp"
#include "../shared_gui_components/OSListView.hpp"
#include "../shared_gui_components/GraphicsItems.hpp"

class QGraphicsView;
Expand Down