[Kstars-devel] KDE/kdeedu/kstars/kstars

Jason Harris kstars at 30doradus.org
Fri Sep 7 16:30:21 CEST 2007


SVN commit 709451 by harris:

Find Dialog:

Make name filter case-insensitive

When a filter string is entered, auto-select the first item in the
list that *begins* with the filter string

CCMAIL: kstars-devel at kde.org



 M  +54 -35    finddialog.cpp  


--- trunk/KDE/kdeedu/kstars/kstars/finddialog.cpp #709450:709451
@@ -58,6 +58,7 @@
 
 	fModel = new QStringListModel( this );
 	sortModel = new QSortFilterProxyModel( ui->SearchList );
+	sortModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
 	ui->SearchList->setModel( sortModel );
 	sortModel->setSourceModel( fModel );
 	ui->SearchList->setModel( sortModel );
@@ -148,46 +149,48 @@
 		return;
 	}
 
-	//Pre-select the first item
-	QModelIndex selectItem = sortModel->index( 0, sortModel->filterKeyColumn(), QModelIndex() );
-	switch ( ui->FilterType->currentIndex() ) {
-		case 0: //All objects, choose Andromeda galaxy
-		{
-			QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Andromeda Galaxy") ) );
-			selectItem = sortModel->mapFromSource( qmi );
-			break;
+	if ( ui->SearchBox->text().isEmpty() ) {
+		//Pre-select the first item
+		QModelIndex selectItem = sortModel->index( 0, sortModel->filterKeyColumn(), QModelIndex() );
+		switch ( ui->FilterType->currentIndex() ) {
+			case 0: //All objects, choose Andromeda galaxy
+			{
+				QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Andromeda Galaxy") ) );
+				selectItem = sortModel->mapFromSource( qmi );
+				break;
+			}
+			case 1: //Stars, choose Aldebaran
+			{
+				QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Aldebaran") ) );
+				selectItem = sortModel->mapFromSource( qmi );
+				break;
+			}
+			case 2: //Solar system or Asteroids, choose Aaltje
+			case 9:
+			{
+				QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Aaltje") ) );
+				selectItem = sortModel->mapFromSource( qmi );
+				break;
+			}
+			case 8: //Comets, choose 'Aarseth-Brewington (1989 W1)'
+			{
+				QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Aarseth-Brewington (1989 W1)") ) );
+				selectItem = sortModel->mapFromSource( qmi );
+				break;
+			}
+		
 		}
-		case 1: //Stars, choose Aldebaran
-		{
-			QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Aldebaran") ) );
-			selectItem = sortModel->mapFromSource( qmi );
-			break;
+	
+		if ( selectItem.isValid() ) {
+			ui->SearchList->selectionModel()->select( selectItem, QItemSelectionModel::ClearAndSelect );
+			ui->SearchList->scrollTo( selectItem );
+			ui->SearchList->setCurrentIndex( selectItem );
+			button( Ok )->setEnabled( true );
 		}
-		case 2: //Solar system or Asteroids, choose Aaltje
-		case 9:
-		{
-			QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Aaltje") ) );
-			selectItem = sortModel->mapFromSource( qmi );
-			break;
-		}
-		case 8: //Comets, choose 'Aarseth-Brewington (1989 W1)'
-		{
-			QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Aarseth-Brewington (1989 W1)") ) );
-			selectItem = sortModel->mapFromSource( qmi );
-			break;
-		}
-	
 	}
-
-	if ( selectItem.isValid() ) {
-		ui->SearchList->selectionModel()->select( selectItem, QItemSelectionModel::ClearAndSelect );
-		ui->SearchList->scrollTo( selectItem );
-		ui->SearchList->setCurrentIndex( selectItem );
-		button( Ok )->setEnabled( true );
-	}
 }
 
-void FindDialog::filterByType( int f ) {
+void FindDialog::filterByType( int /*f*/ ) {
 	if ( timer ) {
 		timer->stop();
 	}
@@ -198,6 +201,22 @@
 void FindDialog::filterByName() {  //Filter the list of names with the string in the SearchBox
 	sortModel->setFilterFixedString( ui->SearchBox->text() );
 	initSelection();
+
+	//Select the first item in the list that begins with the filter string
+	QStringList mItems = fModel->stringList().filter( QRegExp( "^"+ui->SearchBox->text(), Qt::CaseInsensitive ) );
+	mItems.sort();
+
+	if ( mItems.size() ) {
+		QModelIndex qmi = fModel->index( fModel->stringList().indexOf( mItems[0] ) );
+		QModelIndex selectItem = sortModel->mapFromSource( qmi );
+	
+		if ( selectItem.isValid() ) {
+			ui->SearchList->selectionModel()->select( selectItem, QItemSelectionModel::ClearAndSelect );
+			ui->SearchList->scrollTo( selectItem );
+			ui->SearchList->setCurrentIndex( selectItem );
+			button( Ok )->setEnabled( true );
+		}
+	}
 }
 
 SkyObject* FindDialog::selectedObject() const {


More information about the Kstars-devel mailing list