00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00044 vlay = new QVBoxLayout( page, 2, 2 );
00045 hlay = new QHBoxLayout( 2 );
00046
00047
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
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
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
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
00088 currentitem = 0;
00089
00090
00091 Filter = 0;
00092
00093
00094
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
00103 QTimer::singleShot(0, this, SLOT( init() ));
00104 }
00105
00106 FindDialog::~FindDialog() {
00107 delete SearchList;
00108 }
00109
00110 void FindDialog::init() {
00111 SearchBox->clear();
00112 filterType->setCurrentItem(0);
00113 filter();
00114 }
00115
00116 void FindDialog::filter() {
00117 KStars *p = (KStars *)parent();
00118
00119 SearchList->clear();
00120 ObjectNameList &ObjNames = p->data()->ObjNames;
00121
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
00129
00130
00131
00132 }
00133 }
00134 setListItemEnabled();
00135 SearchBox->setFocus();
00136 }
00137
00138 void FindDialog::filterByType() {
00139 KStars *p = (KStars *)parent();
00140
00141 SearchList->clear();
00142 QString searchFor = SearchBox->text().lower();
00143
00144 ObjectNameList &ObjNames = p->data()->ObjNames;
00145
00146 ObjNames.setLanguage( Options::useLatinConstellNames() );
00147
00148 for ( SkyObjectName *name = ObjNames.first( searchFor ); name; name = ObjNames.next() ) {
00149
00150 if ( name->skyObject()->type() == Filter || (name->skyObject()->type() == 0 && Filter == 1 ) ) {
00151 if ( name->text().lower().startsWith( searchFor ) ) {
00152
00153 if (Filter!=1 || name->skyObject()->mag() <= Options::magLimitDrawStar() ) {
00154 new SkyObjectNameListItem ( SearchList, name );
00155 }
00156 }
00157 }
00158 }
00159
00160 setListItemEnabled();
00161 SearchBox->setFocus();
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();
00173 }
00174
00175 void FindDialog::setFilter( int f ) {
00176
00177 int f2( f );
00178 if ( f >= 7 ) f2 = f + 1;
00179
00180
00181 if ( Filter != f2 ) {
00182 Filter = f2;
00183 if ( Filter == 0 ) {
00184
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
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
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"