+static bool matches(QString const & input, QString const & search)
+{
+ QString text = input;
+
+ // Check if the input contains the search string
+ return text.remove('&').contains(search, Qt::CaseInsensitive);
+}
+
+static void setTreeItemStatus(QTreeWidgetItem * tree_item, bool enabled)
+{
+ // Enable/disable the item
+ tree_item->setDisabled(!enabled);
+
+ // Change the color from black to gray or viceversa
+ QPalette::ColorGroup new_color = enabled ? QPalette::Active : QPalette::Disabled;
+ tree_item->setTextColor(0, QApplication::palette().color(new_color, QPalette::Text));
+}
+
+void PanelStack::hideEvent(QHideEvent * event)
+{
+ QWidget::hideEvent(event);
+
+ // Programatically hidden (not simply minimized by the user)
+ if (!event->spontaneous()) {
+ resetSearch();
+ }
+}
+
+void PanelStack::resetSearch()
+{
+ search_->setText(QString());
+ search();
+}
+
+void PanelStack::filterChanged(QString const & /*search*/)
+{
+ // The text in the search box is changed, reset the timer
+ // and then search in the widgets
+ delay_search_->start(300);
+}
+
+void PanelStack::search()
+{
+ QString search = search_->text();
+ bool enable_all = search.isEmpty();
+
+ // If the search string is empty we enable all the items
+ // otherwise we disable everything and then selectively
+ // re-enable matching items
+ foreach (QTreeWidgetItem * tree_item, panel_map_) {
+ setTreeItemStatus(tree_item, enable_all);
+ }
+
+ foreach (QTreeWidgetItem * tree_item, panel_map_) {
+ // Current widget
+ QWidget * pane_widget = widget_map_[tree_item];
+
+ // First of all we look in the pane name
+ bool pane_matches = tree_item->text(0).contains(search, Qt::CaseInsensitive);
+
+ // If the tree item has an associated pane
+ if (pane_widget) {
+ // Loops on the list of children widgets (recursive)
+ QWidgetList children = pane_widget->findChildren<QWidget *>();
+ foreach (QWidget * child_widget, children) {
+ bool widget_matches = false;
+
+ // Try to cast to the most common widgets and looks in it's content
+ // It's bad OOP, it would be nice to have a QWidget::toString() overloaded by
+ // each widget, but this would require to change Qt or subclass each widget.
+ // Note that we have to ignore the amperstand symbol
+ if (QAbstractButton * button = qobject_cast<QAbstractButton *>(child_widget)) {
+ widget_matches = matches(button->text(), search);
+
+ } else if (QGroupBox * group_box = qobject_cast<QGroupBox *>(child_widget)) {
+ widget_matches = matches(group_box->title(), search);
+
+ } else if (QLabel * label = qobject_cast<QLabel *>(child_widget)) {
+ widget_matches = matches(label->text(), search);
+
+ } else if (QLineEdit * line_edit = qobject_cast<QLineEdit *>(child_widget)) {
+ widget_matches = matches(line_edit->text(), search);
+
+ } else if (QListWidget * list_widget = qobject_cast<QListWidget *>(child_widget)) {
+ widget_matches = (list_widget->findItems(search, Qt::MatchContains)).count() > 0;
+
+ } else if (QTreeWidget * tree_view = qobject_cast<QTreeWidget *>(child_widget)) {
+ widget_matches = (tree_view->findItems(search, Qt::MatchContains)).count() > 0;
+
+ } else if (QComboBox * combo_box = qobject_cast<QComboBox *>(child_widget)) {
+ widget_matches = (combo_box->findText(search, Qt::MatchContains)) != -1;
+
+ } else {
+ continue;
+ }
+
+ // If this widget meets the search criteria
+ if (widget_matches && !enable_all) {
+ // The pane too meets the search criteria
+ pane_matches = true;
+
+ // Highlight the widget
+ QPalette widget_palette = child_widget->palette();
+ widget_palette.setColor(child_widget->foregroundRole(), Qt::red);
+ child_widget->setPalette(widget_palette);
+ } else {
+ // Reset the color of the widget
+ child_widget->setPalette(QApplication::palette(child_widget));
+ }
+ }
+
+ // If the pane meets the search criteria
+ if (pane_matches && !enable_all) {
+ // Expand and enable the pane and his ancestors (typically just the parent)
+ QTreeWidgetItem * item = tree_item;
+ do {
+ item->setExpanded(true);
+ setTreeItemStatus(item, true);
+ item = item->parent();
+ } while (item);
+ }
+ }
+
+ }
+}