-rw-r--r-- | libopie2/opieui/olistview.cpp | 89 | ||||
-rw-r--r-- | libopie2/opieui/olistview.h | 35 |
2 files changed, 115 insertions, 9 deletions
diff --git a/libopie2/opieui/olistview.cpp b/libopie2/opieui/olistview.cpp index 8f97cc6..8f290d3 100644 --- a/libopie2/opieui/olistview.cpp +++ b/libopie2/opieui/olistview.cpp @@ -467,4 +467,66 @@ void ONamedListView::addColumns( const QStringList& columns ) +int ONamedListView::findColumn( const QString& text ) const +{ + //FIXME: If used excessively, this will slow down performance of updates + //FIXME: because of the linear search over all column texts. + //FIXME: I will optimize later by using a hash map. + for ( int i = 0; i < columns(); ++i ) + if ( columnText( i ) == text ) + return i; + return -1; +} + + +ONamedListViewItem* ONamedListView::find( int column, const QString& text, int recurse ) const +{ + return find( (ONamedListViewItem*) firstChild(), column, text, recurse ); +} + + +ONamedListViewItem* ONamedListView::find( ONamedListViewItem* item, int column, const QString& text, int recurse ) const +{ + ONamedListViewItem* result; + while ( item && item->text( column ) != text ) + { + qDebug( "checked %s", (const char*) item->text( column ) ); + + if ( recurse < 0 || recurse > 0 ) + { + qDebug( "recursion is %d - recursing into...", recurse ); + result = find( (ONamedListViewItem*) item->firstChild(), column, text, recurse-1 ); + if ( result ) return result; + } + + + item = (ONamedListViewItem*) item->itemBelow(); + } + if ( item && item->text( column ) == text ) + return item; + else + return 0; +} + + +ONamedListViewItem* ONamedListView::find( const QString& column, const QString& text, int recurse ) const +{ + int col = findColumn( column ); + if ( col != -1 ) + return find( (ONamedListViewItem*) firstChild(), col, text, recurse ); + else + return 0; +} + + +ONamedListViewItem* ONamedListView::find( ONamedListViewItem* item, const QString& column, const QString& text, int recurse ) const +{ + int col = findColumn( column ); + if ( col != -1 ) + return find( item, col, text, recurse ); + else + return 0; +} + + /*====================================================================================== * ONamedListViewItem @@ -521,12 +583,25 @@ void ONamedListViewItem::setText( const QString& column, const QString& text ) //FIXME: because of the linear search over all column texts. //FIXME: I will optimize later by using a hash map. - for ( int i = 0; i < listView()->columns(); ++i ) - { - if ( listView()->columnText( i ) == column ) - { - OListViewItem::setText( i, text ); - return; + int col = ( (ONamedListView*) listView() )->findColumn( column ); + if ( col != -1 ) + OListViewItem::setText( col, text ); + else + qWarning( "ONamedListViewItem::setText(): Warning! Columntext '%s' not found.", (const char*) column ); } + + +ONamedListViewItem* ONamedListViewItem::find( int column, const QString& text, int recurse ) const +{ + return ( (ONamedListView*) listView() )->find( (ONamedListViewItem*) firstChild(), column, text, recurse ); } - qWarning( "ONamedListViewItem::setText(): Warning! Columntext '%s' not found.", (const char*) column ); + + +ONamedListViewItem* ONamedListViewItem::find( const QString& column, const QString& text, int recurse ) const +{ + int col = ( (ONamedListView*) listView() )->findColumn( column ); + if ( col != -1 ) + return ( (ONamedListView*) listView() )->find( (ONamedListViewItem*) firstChild(), col, text, recurse ); + else + return 0; } + diff --git a/libopie2/opieui/olistview.h b/libopie2/opieui/olistview.h index 99770bf..1bbdd5b 100644 --- a/libopie2/opieui/olistview.h +++ b/libopie2/opieui/olistview.h @@ -242,8 +242,10 @@ QDataStream& operator>>( QDataStream& stream, OListViewItem& item ); *======================================================================================*/ +class ONamedListViewItem; + /** * @brief An OListView variant with named columns. * - * This class provides a higher-level interface to the columns in an OListView. + * This class provides a higher-level interface to an OListView. * * @author Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de> @@ -267,4 +269,22 @@ class ONamedListView: public OListView */ virtual void addColumns( const QStringList& columns ); + /** + * @returns the column index matching to @a text or -1 if not found. + */ + virtual int findColumn( const QString& text ) const; + /** + * @returns the first item which has a @a text in column @a column. + * Set @a recurse to indicate how much subchild levels to search, e.g.<ul> + * <li>set it to 0 to search only among direct childs, + * <li>set it to 1 to search direct childs and all 1st order subchilds + * <li>set it to -1 for maximum recursion. + * </ul> + * @sa ONamedListViewItem::find() + */ + virtual ONamedListViewItem* find( ONamedListViewItem* start, int column, const QString& text, int recurse = -1 ) const; + virtual ONamedListViewItem* find( int column, const QString& text, int recurse = -1 ) const; + + virtual ONamedListViewItem* find( ONamedListViewItem* start, const QString& column, const QString& text, int recurse = -1 ) const; + virtual ONamedListViewItem* find( const QString& column, const QString& text, int recurse = -1 ) const; }; @@ -276,5 +296,5 @@ class ONamedListView: public OListView * @brief An OListView variant with named columns. * - * This class provides a higher-level interface to the columns in an OListViewItem. + * This class provides a higher-level interface to an OListViewItem. * * @author Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de> @@ -305,4 +325,15 @@ class ONamedListViewItem: public OListViewItem */ virtual void setText( const QStringList& texts ); + /** + * @returns the first child which has a @a text in column @a column. + * Set @a recurse to indicate how much subchild levels to search, e.g.<ul> + * <li>set it to 0 to search only among direct childs, + * <li>set it to 1 to search direct childs and all 1st order subchilds + * <li>set it to -1 for maximum recursion. + * </ul> + * @sa ONamedListView::find() + */ + virtual ONamedListViewItem* find( int column, const QString& text, int recurse = -1 ) const; + virtual ONamedListViewItem* find( const QString& column, const QString& text, int recurse = -1 ) const; }; |