• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

finddialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           finddialog.cpp  -  K Desktop Planetarium
00003                              -------------------
00004     begin                : Wed Jul 4 2001
00005     copyright            : (C) 2001 by Jason Harris
00006     email                : jharris@30doradus.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <qlayout.h>
00019 #include <qlineedit.h>
00020 #include <qlabel.h>
00021 #include <qcombobox.h>
00022 #include <qlistbox.h>
00023 #include <qtimer.h>
00024 
00025 #include <kmessagebox.h>
00026 
00027 #include "finddialog.h"
00028 #include "kstars.h"
00029 #include "kstarsdata.h"
00030 #include "Options.h"
00031 #include "skyobject.h"
00032 #include "skyobjectname.h"
00033 #include "objectnamelist.h"
00034 
00035 
00036 FindDialog::FindDialog( QWidget* parent ) :
00037         KDialogBase( KDialogBase::Plain, i18n( "Find Object" ), Ok|Cancel, Ok, parent ),
00038         vlay(0), hlay(0), SearchList(0), SearchBox(0), filterTypeLabel(0), filterType(0),
00039         currentitem(0)
00040 {
00041     QFrame *page = plainPage();
00042 
00043 //Create Layout managers
00044     vlay = new QVBoxLayout( page, 2, 2 );
00045     hlay = new QHBoxLayout( 2 ); //this mgr will be added to vlay
00046 
00047 //Create Widgets
00048     SearchBox = new QLineEdit( page, "SearchBox" );
00049 
00050     filterTypeLabel = new QLabel( page, "filterTypeLabel" );
00051     filterTypeLabel->setAlignment( AlignRight );
00052     filterTypeLabel->setText( i18n( "Filter by type: " ) );
00053 
00054     filterType = new QComboBox( page, "filterType" );
00055     filterType->setEditable( false );
00056     filterType->insertItem( i18n ("Any") );
00057     filterType->insertItem( i18n ("Stars") );
00058     //  filterType->insertItem( i18n ("Double Stars") );
00059     filterType->insertItem( i18n ("Solar System") );
00060     filterType->insertItem( i18n ("Open Clusters") );
00061     filterType->insertItem( i18n ("Glob. Clusters") );
00062     filterType->insertItem( i18n ("Gas. Nebulae") );
00063     filterType->insertItem( i18n ("Plan. Nebulae") );
00064     //  filterType->insertItem( i18n ("SN Remnants") );
00065     filterType->insertItem( i18n ("Galaxies") );
00066     filterType->insertItem( i18n ("Comets") );
00067     filterType->insertItem( i18n ("Asteroids") );
00068     filterType->insertItem( i18n ("Constellations") );
00069 
00070     SearchList = new QListBox( page, "SearchList" );
00071     SearchList->setMinimumWidth( 256 );
00072     SearchList->setMinimumHeight( 320 );
00073     SearchList->setVScrollBarMode( QListBox::AlwaysOn );
00074     SearchList->setHScrollBarMode( QListBox::AlwaysOff );
00075 
00076 //Pack Widgets into layout manager
00077     hlay->addWidget( filterTypeLabel, 0, 0 );
00078     hlay->addWidget( filterType, 0, 0 );
00079 
00080     vlay->addWidget( SearchBox, 0, 0 );
00081     vlay->addSpacing( 12 );
00082     vlay->addWidget( SearchList, 0, 0 );
00083     vlay->addLayout( hlay, 0 );
00084 
00085     vlay->activate();
00086 
00087 // no item currently set
00088     currentitem = 0;
00089 
00090 // no filters set
00091     Filter = 0;
00092 
00093 //Connect signals to slots
00094 //  connect( this, SIGNAL( okClicked() ), this, SLOT( accept() ) ) ;
00095     connect( this, SIGNAL( cancelClicked() ), this, SLOT( reject() ) );
00096     connect( SearchBox, SIGNAL( textChanged( const QString & ) ), SLOT( filter() ) );
00097     connect( SearchBox, SIGNAL( returnPressed() ), SLOT( slotOk() ) );
00098     connect( filterType, SIGNAL( activated( int ) ), this, SLOT( setFilter( int ) ) );
00099     connect( SearchList, SIGNAL (selectionChanged  (QListBoxItem *)), SLOT (updateSelection (QListBoxItem *)));
00100     connect( SearchList, SIGNAL( doubleClicked ( QListBoxItem *  ) ), SLOT( slotOk() ) );
00101 
00102     // first create and paint dialog and then load list
00103     QTimer::singleShot(0, this, SLOT( init() ));
00104 }
00105 
00106 FindDialog::~FindDialog() {
00107     delete SearchList;
00108 }
00109 
00110 void FindDialog::init() {
00111     SearchBox->clear();  // QLineEdit
00112     filterType->setCurrentItem(0);  // show all types of objects
00113     filter();
00114 }
00115 
00116 void FindDialog::filter() {  //Filter the list of names with the string in the SearchBox
00117     KStars *p = (KStars *)parent();
00118 
00119     SearchList->clear();
00120     ObjectNameList &ObjNames = p->data()->ObjNames;
00121     // check if latin names are used
00122     ObjNames.setLanguage( Options::useLatinConstellNames() );
00123 
00124     QString searchFor = SearchBox->text().lower();
00125         for ( SkyObjectName *name = ObjNames.first( searchFor ); name; name = ObjNames.next() ) {
00126             if ( name->text().lower().startsWith( searchFor ) ) {
00127                 new SkyObjectNameListItem ( SearchList, name );
00128 /*              if ( i++ >= 5000 ) {              //Every 5000 name insertions,
00129                     kapp->processEvents ( 50 );     //spend 50 msec processing KApplication events
00130                     i = 0;
00131                 }*/
00132             }
00133         }
00134     setListItemEnabled(); // Automatically highlight first item
00135     SearchBox->setFocus();  // set cursor to QLineEdit
00136 }
00137 
00138 void FindDialog::filterByType() {
00139     KStars *p = (KStars *)parent();
00140 
00141     SearchList->clear();    // QListBox
00142     QString searchFor = SearchBox->text().lower();  // search string
00143 
00144     ObjectNameList &ObjNames = p->data()->ObjNames;
00145     // check if latin names are used
00146     ObjNames.setLanguage( Options::useLatinConstellNames() );
00147 
00148     for ( SkyObjectName *name = ObjNames.first( searchFor ); name; name = ObjNames.next() ) {
00149             //Special case: match SkyObject Type 0 with Filter==1 (stars)
00150         if ( name->skyObject()->type() == Filter || (name->skyObject()->type() == 0 && Filter == 1 ) ) {
00151             if ( name->text().lower().startsWith( searchFor ) ) {
00152                 // for stars, don't show the ones below the faint limit
00153                 if (Filter!=1 || name->skyObject()->mag() <= Options::magLimitDrawStar() ) {
00154                     new SkyObjectNameListItem ( SearchList, name );
00155                 }
00156             }
00157         }
00158     }
00159 
00160     setListItemEnabled();    // Automatically highlight first item
00161     SearchBox->setFocus();  // set cursor to QLineEdit
00162 }
00163 
00164 void FindDialog::setListItemEnabled() {
00165     SearchList->setSelected (0, true);
00166     if (!SearchList->isSelected (0))
00167         updateSelection (0);
00168 }
00169 
00170 void FindDialog::updateSelection (QListBoxItem *it) {
00171     currentitem = (SkyObjectNameListItem *) it;
00172     SearchBox->setFocus();  // set cursor to QLineEdit
00173 }
00174 
00175 void FindDialog::setFilter( int f ) {
00176         // Translate the Listbox index to the correct SkyObject Type ID 
00177         int f2( f ); // in most cases, they are the same number
00178     if ( f >= 7 ) f2 = f + 1; //need to skip unused "Supernova Remnant" Type at position 7
00179     
00180         // check if filter was changed or if filter is still the same
00181     if ( Filter != f2 ) {
00182         Filter = f2;
00183         if ( Filter == 0 ) {  // any type will shown
00184         // delete old connections and create new connections
00185             disconnect( SearchBox, SIGNAL( textChanged( const QString & ) ), this, SLOT( filterByType() ) );
00186             connect( SearchBox, SIGNAL( textChanged( const QString & ) ), SLOT( filter() ) );
00187             filter();
00188         }
00189         else {
00190         // delete old connections and create new connections
00191             disconnect( SearchBox, SIGNAL( textChanged( const QString & ) ), this, SLOT( filter() ) );
00192             connect( SearchBox, SIGNAL( textChanged( const QString & ) ), SLOT( filterByType() ) );
00193             filterByType();
00194         }
00195     }
00196 }
00197 
00198 void FindDialog::slotOk() {
00199     //If no valid object selected, show a sorry-box.  Otherwise, emit accept()
00200     if ( currentItem() == 0 ) {
00201         QString message = i18n( "No object named %1 found." ).arg( SearchBox->text() );
00202         KMessageBox::sorry( 0, message, i18n( "Bad object name" ) );
00203     } else {
00204         accept();
00205     }
00206 }
00207 
00208 void FindDialog::keyPressEvent( QKeyEvent *e ) {
00209     switch( e->key() ) {
00210         case Key_Down :
00211             if ( SearchList->currentItem() < ((int) SearchList->count()) - 1 )
00212                 SearchList->setCurrentItem( SearchList->currentItem() + 1 );
00213             break;
00214             
00215         case Key_Up :
00216             if ( SearchList->currentItem() )
00217                 SearchList->setCurrentItem( SearchList->currentItem() - 1 );
00218             break;
00219             
00220         case Key_Escape :
00221             reject();
00222             break;
00223             
00224     }
00225 }
00226 
00227 #include "finddialog.moc"

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal