bool SearchLine::checkItemParentsVisible( Q3ListViewItem *item ) { //BEGIN code from KSearchLine::checkItemParentsVisible bool visible = false; for( ; item; item = item->nextSibling() ) { if( ( item->firstChild() && checkItemParentsVisible( item->firstChild() ) ) || itemMatches( item, search ) ) { setItemVisible( item, true ); // OUCH! this operation just became exponential-time. // however, setting an item visible sets all its descendents // visible too, which we definitely don't want. // plus, in Kopete the nesting is never more than 2 deep, // so this really just doubles the runtime, if that. // this still can be done in O(n) time by a mark-set process, // but that's overkill in our case. checkItemParentsVisible( item->firstChild() ); visible = true; } else setItemVisible( item, false ); } return visible; //END code from KSearchLine::checkItemParentsVisible }
/** Check whether \p item, its siblings and their descendants should be shown. Show or hide the items as necessary. * * \p item The list view item to start showing / hiding items at. Typically, this is the first child of another item, or * the first child of the list view. * \return \c true if an item which should be visible is found, \c false if all items found should be hidden. If this function * returns true and \p highestHiddenParent was not 0, highestHiddenParent will have been shown. */ bool KTreeWidgetSearchLine::Private::checkItemParentsVisible(QTreeWidgetItem *item) { bool childMatch = false; for (int i = 0; i < item->childCount(); ++i) { childMatch |= checkItemParentsVisible(item->child(i)); } // Should this item be shown? It should if any children should be, or if it matches. bool newHidden = !childMatch && !q->itemMatches(item, search); if (item->isHidden() != newHidden) { item->setHidden(newHidden); emit q->hiddenChanged(item, newHidden); } return !newHidden; }
/** Check whether \p item, its siblings and their descendents should be shown. Show or hide the items as necessary. * * \p item The list view item to start showing / hiding items at. Typically, this is the first child of another item, or the * the first child of the list view. * \return \c true if an item which should be visible is found, \c false if all items found should be hidden. If this function * returns true and \p highestHiddenParent was not 0, highestHiddenParent will have been shown. */ bool KTreeViewSearchLine::Private::checkItemParentsVisible( QTreeView *treeView, const QModelIndex &index ) { bool childMatch = false; const int rowcount = treeView->model()->rowCount( index ); for ( int i = 0; i < rowcount; ++i ) childMatch |= checkItemParentsVisible( treeView, treeView->model()->index( i, 0, index ) ); // Should this item be shown? It should if any children should be, or if it matches. const QModelIndex parentindex = index.parent(); if ( childMatch || parent->itemMatches( parentindex, index.row(), search ) ) { treeView->setRowHidden( index.row(), parentindex, false ); return true; } treeView->setRowHidden( index.row(), parentindex, true ); return false; }
bool KTreeWidgetSearchLine::checkItemParentsVisible(QTreeWidgetItem* item) { QTreeWidget* treeWidget = item->treeWidget(); bool childMatch = false; for (int i = 0; i < item->childCount(); ++i) childMatch |= checkItemParentsVisible(item->child(i)); // Should this item be shown? It should if any children should be, or if it matches. if (childMatch || itemMatches(item, k->search)) { treeWidget->setItemHidden(item, false); return true; } treeWidget->setItemHidden(item, true); return false; }
void SearchLine::updateSearch( const QString &s ) { // we copy a huge chunk of code here simply in order to override // the way items are shown/hidden. KSearchLine rudely // calls setVisible() on items with no way to customize this behaviour. //BEGIN code from KSearchLine::updateSearch if( !listView() ) return; search = s.isNull() ? text() : s; searchEmpty = search.isEmpty(); // If there's a selected item that is visible, make sure that it's visible // when the search changes too (assuming that it still matches). Q3ListViewItem *currentItem = 0; switch( listView()->selectionMode() ) { case K3ListView::NoSelection: break; case K3ListView::Single: currentItem = listView()->selectedItem(); break; default: for( Q3ListViewItemIterator it(listView(), Q3ListViewItemIterator::Selected | Q3ListViewItemIterator::Visible); it.current() && !currentItem; ++it ) { if( listView()->itemRect( it.current() ).isValid() ) currentItem = it.current(); } } if( keepParentsVisible() ) checkItemParentsVisible( listView()->firstChild() ); else checkItemParentsNotVisible(); if( currentItem ) listView()->ensureItemVisible( currentItem ); //END code from KSearchLine::updateSearch }
void KTreeWidgetSearchLine::updateSearch(QTreeWidget *treeWidget) { if (!treeWidget || !treeWidget->topLevelItemCount()) return; // If there's a selected item that is visible, make sure that it's visible // when the search changes too (assuming that it still matches). QTreeWidgetItem *currentItem = treeWidget->currentItem(); if (k->keepParentsVisible) for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) checkItemParentsVisible(treeWidget->topLevelItem(i)); else checkItemParentsNotVisible(treeWidget); if(currentItem) treeWidget->scrollToItem(currentItem); }