26 #include <QVBoxLayout>
30 #include <QItemEditorFactory>
38 TreeBox::TreeBox(QStandardItemModel* model,
bool hideChildren, QWidget* parent) : QComboBox(parent), skipNextHide(false)
42 treeView->setHeaderHidden(
true);
43 treeView->setModel(model);
44 treeView->setMinimumHeight(300);
45 treeView->setMinimumWidth(250);
46 treeView->setAnimated(
true);
47 treeView->setSortingEnabled(
true);
49 setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
50 setMinimumContentsLength(10);
54 view()->viewport()->installEventFilter(
this);
59 if (event->type() == QEvent::MouseButtonPress &&
object == view()->viewport())
61 QMouseEvent* mouseEvent =
static_cast<QMouseEvent*
>(event);
62 QModelIndex
index = view()->indexAt(mouseEvent->pos());
75 else if ((event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
76 &&
object == view()->viewport())
79 QKeyEvent* keyEvent =
static_cast<QKeyEvent*
>(event);
81 if (keyEvent->key() != Qt::Key_Return && keyEvent->key() != Qt::Key_Enter)
94 QComboBox::showPopup();
101 skipNextHide =
false;
105 if (treeView->isVisible())
107 QTimer::singleShot(20,
this, SLOT(delayedIndexChanging()));
110 QComboBox::hidePopup();
121 StateItemModel* stateModel = qobject_cast<StateItemModel*>(model());
128 QStandardItem* root = stateModel->invisibleRootItem();
136 treeView->setRowHidden(0, root->index(),
false);
138 if (searchString.length() > 0)
140 treeView->expandAll();
146 void TreeBox::delayedIndexChanging()
159 for (
int row = 0; row < parent->rowCount(); ++row)
161 QStandardItem* child = parent->child(row);
168 bool showThisRow =
applyFilter(child, searchString);
169 QRegExp rex(searchString, Qt::CaseInsensitive, QRegExp::Wildcard);
171 if (child->data(Qt::DisplayRole).toString().contains(rex))
182 treeView->setRowHidden(row, parent->index(), !showThisRow);