moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / finddialog.cpp
blobcf203e340fcef085e09c1fc703a7a6f4766d4c51
1 /***************************************************************************
2 finddialog.cpp - K Desktop Planetarium
3 -------------------
4 begin : Wed Jul 4 2001
5 copyright : (C) 2001 by Jason Harris
6 email : jharris@30doradus.org
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <qlayout.h>
19 #include <qlineedit.h>
20 #include <qlabel.h>
21 #include <qcombobox.h>
22 #include <qlistbox.h>
23 #include <qtimer.h>
25 #include <kmessagebox.h>
27 #include "finddialog.h"
28 #include "kstars.h"
29 #include "kstarsdata.h"
30 #include "Options.h"
31 #include "skyobject.h"
32 #include "skyobjectname.h"
33 #include "objectnamelist.h"
36 FindDialog::FindDialog( QWidget* parent ) :
37 KDialogBase( KDialogBase::Plain, i18n( "Find Object" ), Ok|Cancel, Ok, parent ),
38 vlay(0), hlay(0), SearchList(0), SearchBox(0), filterTypeLabel(0), filterType(0),
39 currentitem(0)
41 QFrame *page = plainPage();
43 //Create Layout managers
44 vlay = new QVBoxLayout( page, 2, 2 );
45 hlay = new QHBoxLayout( 2 ); //this mgr will be added to vlay
47 //Create Widgets
48 SearchBox = new QLineEdit( page, "SearchBox" );
50 filterTypeLabel = new QLabel( page, "filterTypeLabel" );
51 filterTypeLabel->setAlignment( AlignRight );
52 filterTypeLabel->setText( i18n( "Filter by type: " ) );
54 filterType = new QComboBox( page, "filterType" );
55 filterType->setEditable( false );
56 filterType->insertItem( i18n ("Any") );
57 filterType->insertItem( i18n ("Stars") );
58 // filterType->insertItem( i18n ("Double Stars") );
59 filterType->insertItem( i18n ("Solar System") );
60 filterType->insertItem( i18n ("Open Clusters") );
61 filterType->insertItem( i18n ("Glob. Clusters") );
62 filterType->insertItem( i18n ("Gas. Nebulae") );
63 filterType->insertItem( i18n ("Plan. Nebulae") );
64 // filterType->insertItem( i18n ("SN Remnants") );
65 filterType->insertItem( i18n ("Galaxies") );
66 filterType->insertItem( i18n ("Comets") );
67 filterType->insertItem( i18n ("Asteroids") );
68 filterType->insertItem( i18n ("Constellations") );
70 SearchList = new QListBox( page, "SearchList" );
71 SearchList->setMinimumWidth( 256 );
72 SearchList->setMinimumHeight( 320 );
73 SearchList->setVScrollBarMode( QListBox::AlwaysOn );
74 SearchList->setHScrollBarMode( QListBox::AlwaysOff );
76 //Pack Widgets into layout manager
77 hlay->addWidget( filterTypeLabel, 0, 0 );
78 hlay->addWidget( filterType, 0, 0 );
80 vlay->addWidget( SearchBox, 0, 0 );
81 vlay->addSpacing( 12 );
82 vlay->addWidget( SearchList, 0, 0 );
83 vlay->addLayout( hlay, 0 );
85 vlay->activate();
87 // no item currently set
88 currentitem = 0;
90 // no filters set
91 Filter = 0;
93 //Connect signals to slots
94 // connect( this, SIGNAL( okClicked() ), this, SLOT( accept() ) ) ;
95 connect( this, SIGNAL( cancelClicked() ), this, SLOT( reject() ) );
96 connect( SearchBox, SIGNAL( textChanged( const QString & ) ), SLOT( filter() ) );
97 connect( SearchBox, SIGNAL( returnPressed() ), SLOT( slotOk() ) );
98 connect( filterType, SIGNAL( activated( int ) ), this, SLOT( setFilter( int ) ) );
99 connect( SearchList, SIGNAL (selectionChanged (QListBoxItem *)), SLOT (updateSelection (QListBoxItem *)));
100 connect( SearchList, SIGNAL( doubleClicked ( QListBoxItem * ) ), SLOT( slotOk() ) );
102 // first create and paint dialog and then load list
103 QTimer::singleShot(0, this, SLOT( init() ));
106 FindDialog::~FindDialog() {
107 delete SearchList;
110 void FindDialog::init() {
111 SearchBox->clear(); // QLineEdit
112 filterType->setCurrentItem(0); // show all types of objects
113 filter();
116 void FindDialog::filter() { //Filter the list of names with the string in the SearchBox
117 KStars *p = (KStars *)parent();
119 SearchList->clear();
120 ObjectNameList &ObjNames = p->data()->ObjNames;
121 // check if latin names are used
122 ObjNames.setLanguage( Options::useLatinConstellNames() );
124 QString searchFor = SearchBox->text().lower();
125 for ( SkyObjectName *name = ObjNames.first( searchFor ); name; name = ObjNames.next() ) {
126 if ( name->text().lower().startsWith( searchFor ) ) {
127 new SkyObjectNameListItem ( SearchList, name );
128 /* if ( i++ >= 5000 ) { //Every 5000 name insertions,
129 kapp->processEvents ( 50 ); //spend 50 msec processing KApplication events
130 i = 0;
134 setListItemEnabled(); // Automatically highlight first item
135 SearchBox->setFocus(); // set cursor to QLineEdit
138 void FindDialog::filterByType() {
139 KStars *p = (KStars *)parent();
141 SearchList->clear(); // QListBox
142 QString searchFor = SearchBox->text().lower(); // search string
144 ObjectNameList &ObjNames = p->data()->ObjNames;
145 // check if latin names are used
146 ObjNames.setLanguage( Options::useLatinConstellNames() );
148 for ( SkyObjectName *name = ObjNames.first( searchFor ); name; name = ObjNames.next() ) {
149 //Special case: match SkyObject Type 0 with Filter==1 (stars)
150 if ( name->skyObject()->type() == Filter || (name->skyObject()->type() == 0 && Filter == 1 ) ) {
151 if ( name->text().lower().startsWith( searchFor ) ) {
152 // for stars, don't show the ones below the faint limit
153 if (Filter!=1 || name->skyObject()->mag() <= Options::magLimitDrawStar() ) {
154 new SkyObjectNameListItem ( SearchList, name );
160 setListItemEnabled(); // Automatically highlight first item
161 SearchBox->setFocus(); // set cursor to QLineEdit
164 void FindDialog::setListItemEnabled() {
165 SearchList->setSelected (0, true);
166 if (!SearchList->isSelected (0))
167 updateSelection (0);
170 void FindDialog::updateSelection (QListBoxItem *it) {
171 currentitem = (SkyObjectNameListItem *) it;
172 SearchBox->setFocus(); // set cursor to QLineEdit
175 void FindDialog::setFilter( int f ) {
176 // Translate the Listbox index to the correct SkyObject Type ID
177 int f2( f ); // in most cases, they are the same number
178 if ( f >= 7 ) f2 = f + 1; //need to skip unused "Supernova Remnant" Type at position 7
180 // check if filter was changed or if filter is still the same
181 if ( Filter != f2 ) {
182 Filter = f2;
183 if ( Filter == 0 ) { // any type will shown
184 // delete old connections and create new connections
185 disconnect( SearchBox, SIGNAL( textChanged( const QString & ) ), this, SLOT( filterByType() ) );
186 connect( SearchBox, SIGNAL( textChanged( const QString & ) ), SLOT( filter() ) );
187 filter();
189 else {
190 // delete old connections and create new connections
191 disconnect( SearchBox, SIGNAL( textChanged( const QString & ) ), this, SLOT( filter() ) );
192 connect( SearchBox, SIGNAL( textChanged( const QString & ) ), SLOT( filterByType() ) );
193 filterByType();
198 void FindDialog::slotOk() {
199 //If no valid object selected, show a sorry-box. Otherwise, emit accept()
200 if ( currentItem() == 0 ) {
201 QString message = i18n( "No object named %1 found." ).arg( SearchBox->text() );
202 KMessageBox::sorry( 0, message, i18n( "Bad object name" ) );
203 } else {
204 accept();
208 void FindDialog::keyPressEvent( QKeyEvent *e ) {
209 switch( e->key() ) {
210 case Key_Down :
211 if ( SearchList->currentItem() < ((int) SearchList->count()) - 1 )
212 SearchList->setCurrentItem( SearchList->currentItem() + 1 );
213 break;
215 case Key_Up :
216 if ( SearchList->currentItem() )
217 SearchList->setCurrentItem( SearchList->currentItem() - 1 );
218 break;
220 case Key_Escape :
221 reject();
222 break;
227 #include "finddialog.moc"