• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • dialogs
finddialog.cpp
Go to the documentation of this file.
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  ***************************************************************************/
8 
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  ***************************************************************************/
17 
18 #include "finddialog.h"
19 
20 #include "kstarsdata.h"
21 #include "Options.h"
22 #include "detaildialog.h"
23 #include "skyobjects/skyobject.h"
24 #include "skycomponents/starcomponent.h"
25 #include "skycomponents/skymapcomposite.h"
26 
27 #include <kmessagebox.h>
28 
29 #include <QSortFilterProxyModel>
30 #include <QStringListModel>
31 #include <QTimer>
32 
33 FindDialogUI::FindDialogUI( QWidget *parent ) : QFrame( parent ) {
34  setupUi( this );
35 
36  FilterType->addItem( i18n ("Any") );
37  FilterType->addItem( i18n ("Stars") );
38  FilterType->addItem( i18n ("Solar System") );
39  FilterType->addItem( i18n ("Open Clusters") );
40  FilterType->addItem( i18n ("Globular Clusters") );
41  FilterType->addItem( i18n ("Gaseous Nebulae") );
42  FilterType->addItem( i18n ("Planetary Nebulae") );
43  FilterType->addItem( i18n ("Galaxies") );
44  FilterType->addItem( i18n ("Comets") );
45  FilterType->addItem( i18n ("Asteroids") );
46  FilterType->addItem( i18n ("Constellations") );
47  FilterType->addItem( i18n ("Supernovae") );
48 
49  SearchList->setMinimumWidth( 256 );
50  SearchList->setMinimumHeight( 320 );
51 }
52 
53 FindDialog::FindDialog( QWidget* parent ) :
54  KDialog( parent ),
55  timer(0)
56 {
57  ui = new FindDialogUI( this );
58  setMainWidget( ui );
59  setCaption( i18n( "Find Object" ) );
60  setButtons( KDialog::Ok|KDialog::User1|KDialog::Cancel );
61  ui->FilterType->setCurrentIndex(0); // show all types of objects
62 
63  fModel = new QStringListModel( this );
64  sortModel = new QSortFilterProxyModel( ui->SearchList );
65  sortModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
66  ui->SearchList->setModel( sortModel );
67  sortModel->setSourceModel( fModel );
68  ui->SearchList->setModel( sortModel );
69  setButtonText(KDialog::User1, i18n("Details..."));
70 
71  // Connect signals to slots
72  connect( this, SIGNAL( okClicked() ), this, SLOT( slotOk() ) );
73  connect( this, SIGNAL( cancelClicked() ), this, SLOT( reject() ) );
74  connect(this, SIGNAL(user1Clicked()), this, SLOT(slotDetails()));
75  connect( ui->SearchBox, SIGNAL( textChanged( const QString & ) ), SLOT( enqueueSearch() ) );
76  connect( ui->SearchBox, SIGNAL( returnPressed() ), SLOT( slotOk() ) );
77  connect( ui->FilterType, SIGNAL( activated( int ) ), this, SLOT( enqueueSearch() ) );
78  connect( ui->SearchList, SIGNAL( doubleClicked( const QModelIndex & ) ), SLOT( slotOk() ) );
79 
80  // Set focus to object name edit
81  ui->SearchBox->setFocus();
82 
83  // First create and paint dialog and then load list
84  QTimer::singleShot(0, this, SLOT( init() ));
85 
86  listFiltered = false;
87 }
88 
89 FindDialog::~FindDialog() { }
90 
91 void FindDialog::init() {
92  ui->SearchBox->clear();
93  filterByType();
94  sortModel->sort( 0 );
95  initSelection();
96 }
97 
98 void FindDialog::initSelection() {
99  if ( sortModel->rowCount() <= 0 ) {
100  button( Ok )->setEnabled( false );
101  return;
102  }
103 
104  if ( ui->SearchBox->text().isEmpty() ) {
105  //Pre-select the first item
106  QModelIndex selectItem = sortModel->index( 0, sortModel->filterKeyColumn(), QModelIndex() );
107  switch ( ui->FilterType->currentIndex() ) {
108  case 0: //All objects, choose Andromeda galaxy
109  {
110  QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Andromeda Galaxy") ) );
111  selectItem = sortModel->mapFromSource( qmi );
112  break;
113  }
114  case 1: //Stars, choose Aldebaran
115  {
116  QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Aldebaran") ) );
117  selectItem = sortModel->mapFromSource( qmi );
118  break;
119  }
120  case 2: //Solar system or Asteroids, choose Aaltje
121  case 9:
122  {
123  QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Aaltje") ) );
124  selectItem = sortModel->mapFromSource( qmi );
125  break;
126  }
127  case 8: //Comets, choose 'Aarseth-Brewington (1989 W1)'
128  {
129  QModelIndex qmi = fModel->index( fModel->stringList().indexOf( i18n("Aarseth-Brewington (1989 W1)") ) );
130  selectItem = sortModel->mapFromSource( qmi );
131  break;
132  }
133 
134  }
135 
136  if ( selectItem.isValid() ) {
137  ui->SearchList->selectionModel()->select( selectItem, QItemSelectionModel::ClearAndSelect );
138  ui->SearchList->scrollTo( selectItem );
139  ui->SearchList->setCurrentIndex( selectItem );
140  button( Ok )->setEnabled( true );
141  }
142  }
143 
144  listFiltered = true;
145 }
146 
147 void FindDialog::filterByType() {
148  KStarsData *data = KStarsData::Instance();
149 
150  switch ( ui->FilterType->currentIndex() ) {
151  case 0: // All object types
152  {
153  QStringList allObjects;
154  foreach( int type, data->skyComposite()->objectNames().keys() )
155  allObjects += data->skyComposite()->objectNames( type );
156  fModel->setStringList( allObjects );
157  break;
158  }
159  case 1: //Stars
160  {
161  QStringList starObjects;
162  starObjects += data->skyComposite()->objectNames( SkyObject::STAR );
163  starObjects += data->skyComposite()->objectNames( SkyObject::CATALOG_STAR );
164  fModel->setStringList( starObjects );
165  break;
166  }
167  case 2: //Solar system
168  {
169  QStringList ssObjects;
170  ssObjects += data->skyComposite()->objectNames( SkyObject::PLANET );
171  ssObjects += data->skyComposite()->objectNames( SkyObject::COMET );
172  ssObjects += data->skyComposite()->objectNames( SkyObject::ASTEROID );
173  ssObjects += data->skyComposite()->objectNames( SkyObject::MOON );
174  ssObjects += i18n("Sun");
175  fModel->setStringList( ssObjects );
176  break;
177  }
178  case 3: //Open Clusters
179  fModel->setStringList( data->skyComposite()->objectNames( SkyObject::OPEN_CLUSTER ) );
180  break;
181  case 4: //Open Clusters
182  fModel->setStringList( data->skyComposite()->objectNames( SkyObject::GLOBULAR_CLUSTER ) );
183  break;
184  case 5: //Gaseous nebulae
185  fModel->setStringList( data->skyComposite()->objectNames( SkyObject::GASEOUS_NEBULA ) );
186  break;
187  case 6: //Planetary nebula
188  fModel->setStringList( data->skyComposite()->objectNames( SkyObject::PLANETARY_NEBULA ) );
189  break;
190  case 7: //Galaxies
191  fModel->setStringList( data->skyComposite()->objectNames( SkyObject::GALAXY ) );
192  break;
193  case 8: //Comets
194  fModel->setStringList( data->skyComposite()->objectNames( SkyObject::COMET ) );
195  break;
196  case 9: //Asteroids
197  fModel->setStringList( data->skyComposite()->objectNames( SkyObject::ASTEROID ) );
198  break;
199  case 10: //Constellations
200  fModel->setStringList( data->skyComposite()->objectNames( SkyObject::CONSTELLATION ) );
201  break;
202  case 11: //Supernovae
203  fModel->setStringList( data->skyComposite()->objectNames( SkyObject::SUPERNOVA ) );
204  break;
205  }
206 }
207 
208 void FindDialog::filterList() {
209  QString SearchText;
210  SearchText = processSearchText();
211  sortModel->setFilterFixedString( SearchText );
212  filterByType();
213  initSelection();
214 
215  //Select the first item in the list that begins with the filter string
216  if ( !SearchText.isEmpty() ) {
217  QStringList mItems = fModel->stringList().filter( QRegExp( '^'+SearchText, Qt::CaseInsensitive ) );
218  mItems.sort();
219 
220  if ( mItems.size() ) {
221  QModelIndex qmi = fModel->index( fModel->stringList().indexOf( mItems[0] ) );
222  QModelIndex selectItem = sortModel->mapFromSource( qmi );
223 
224  if ( selectItem.isValid() ) {
225  ui->SearchList->selectionModel()->select( selectItem, QItemSelectionModel::ClearAndSelect );
226  ui->SearchList->scrollTo( selectItem );
227  ui->SearchList->setCurrentIndex( selectItem );
228  button( Ok )->setEnabled( true );
229  }
230  }
231  }
232 
233  listFiltered = true;
234 }
235 
236 SkyObject* FindDialog::selectedObject() const {
237  QModelIndex i = ui->SearchList->currentIndex();
238  SkyObject *obj = 0;
239  if ( i.isValid() ) {
240  QString ObjName = i.data().toString();
241  obj = KStarsData::Instance()->skyComposite()->findByName( ObjName );
242  }
243  if( !obj ) {
244  QString stext = ui->SearchBox->text();
245  if( stext.startsWith( QLatin1String( "HD" ) ) ) {
246  stext.remove( "HD" );
247  bool ok;
248  int HD = stext.toInt( &ok );
249  // Looks like the user is looking for a HD star
250  if( ok ) {
251  obj = StarComponent::Instance()->findByHDIndex( HD );
252  }
253  }
254  }
255  return obj;
256 }
257 
258 void FindDialog::enqueueSearch() {
259  listFiltered = false;
260  if ( timer ) {
261  timer->stop();
262  } else {
263  timer = new QTimer( this );
264  timer->setSingleShot( true );
265  connect( timer, SIGNAL( timeout() ), this, SLOT( filterList() ) );
266  }
267  timer->start( 500 );
268 }
269 
270 // Process the search box text to replace equivalent names like "m93" with "m 93"
271 QString FindDialog::processSearchText() {
272  QRegExp re;
273  QString searchtext = ui->SearchBox->text();
274 
275  re.setCaseSensitivity( Qt::CaseInsensitive );
276 
277  // If it is an NGC/IC/M catalog number, as in "M 76" or "NGC 5139", check for absence of the space
278  re.setPattern("^(m|ngc|ic)\\s*\\d*$");
279  if(ui->SearchBox->text().contains(re)) {
280  re.setPattern("\\s*(\\d+)");
281  searchtext.replace( re, " \\1" );
282  re.setPattern("\\s*$");
283  searchtext.remove( re );
284  re.setPattern("^\\s*");
285  searchtext.remove( re );
286  }
287 
288  // TODO after KDE 4.1 release:
289  // If it is a IAU standard three letter abbreviation for a constellation, then go to that constellation
290  // Check for genetive names of stars. Example: alp CMa must go to alpha Canis Majoris
291 
292  return searchtext;
293 }
294 
295 void FindDialog::slotOk() {
296  //If no valid object selected, show a sorry-box. Otherwise, emit accept()
297  SkyObject *selObj;
298  if(!listFiltered) {
299  filterList();
300  }
301  selObj = selectedObject();
302  if ( selObj == 0 ) {
303  QString message = i18n( "No object named %1 found.", ui->SearchBox->text() );
304  KMessageBox::sorry( 0, message, i18n( "Bad object name" ) );
305  } else {
306  accept();
307  }
308 }
309 
310 void FindDialog::keyPressEvent( QKeyEvent *e ) {
311  switch( e->key() ) {
312  case Qt::Key_Escape :
313  reject();
314  break;
315  case Qt::Key_Up :
316  {
317  int currentRow = ui->SearchList->currentIndex().row();
318  if ( currentRow > 0 ) {
319  QModelIndex selectItem = sortModel->index( currentRow-1, sortModel->filterKeyColumn(), QModelIndex() );
320  ui->SearchList->selectionModel()->setCurrentIndex( selectItem, QItemSelectionModel::SelectCurrent );
321  }
322  break;
323  }
324  case Qt::Key_Down :
325  {
326  int currentRow = ui->SearchList->currentIndex().row();
327  if ( currentRow < sortModel->rowCount()-1 ) {
328  QModelIndex selectItem = sortModel->index( currentRow+1, sortModel->filterKeyColumn(), QModelIndex() );
329  ui->SearchList->selectionModel()->setCurrentIndex( selectItem, QItemSelectionModel::SelectCurrent );
330  }
331  break;
332  }
333  }
334 }
335 
336 void FindDialog::slotDetails()
337 {
338  if ( selectedObject() ) {
339  QPointer<DetailDialog> dd = new DetailDialog( selectedObject(), KStarsData::Instance()->lt(), KStarsData::Instance()->geo(), KStars::Instance());
340  dd->exec();
341  delete dd;
342  }
343 
344 }
345 
346 #include "finddialog.moc"
DetailDialog
window showing detailed information for a selected object.
Definition: detaildialog.h:88
KStarsData
KStarsData is the backbone of KStars.
Definition: kstarsdata.h:66
detaildialog.h
skyobject.h
FindDialog::FindDialog
FindDialog(QWidget *parent=0)
Constructor.
Definition: finddialog.cpp:53
SkyObject::PLANET
Definition: skyobject.h:108
QWidget
KStarsData::Instance
static KStarsData * Instance()
Definition: kstarsdata.h:92
starcomponent.h
KStars::Instance
static KStars * Instance()
Definition: kstars.h:125
SkyObject::GALAXY
Definition: skyobject.h:109
KDialog
SkyObject::COMET
Definition: skyobject.h:110
StarComponent::Instance
static StarComponent * Instance()
Definition: starcomponent.h:74
FindDialog::~FindDialog
virtual ~FindDialog()
Destructor.
Definition: finddialog.cpp:89
skymapcomposite.h
FindDialog::selectedObject
SkyObject * selectedObject() const
Definition: finddialog.cpp:236
SkyObject::MOON
Definition: skyobject.h:110
KStarsData::skyComposite
SkyMapComposite * skyComposite()
Definition: kstarsdata.h:146
QSortFilterProxyModel
Options.h
FindDialog::filterList
void filterList()
When Text is entered in the QLineEdit, filter the List of objects so that only objects which start wi...
Definition: finddialog.cpp:208
StarComponent::findByHDIndex
SkyObject * findByHDIndex(int HDnum)
Find stars by HD catalog index.
Definition: starcomponent.cpp:526
finddialog.h
SkyObject::CONSTELLATION
Definition: skyobject.h:110
SkyObject::PLANETARY_NEBULA
Definition: skyobject.h:109
SkyMapComposite::findByName
virtual SkyObject * findByName(const QString &name)
Search the children of this SkyMapComposite for a SkyObject whose name matches the argument...
Definition: skymapcomposite.cpp:426
SkyObject::ASTEROID
Definition: skyobject.h:110
SkyObject::SUPERNOVA
Definition: skyobject.h:112
FindDialogUI
Definition: finddialog.h:32
kstarsdata.h
SkyObject::GASEOUS_NEBULA
Definition: skyobject.h:109
FindDialog::slotOk
void slotOk()
Overloading the Standard KDialogBase slotOk() to show a "sorry" message box if no object is selected ...
Definition: finddialog.cpp:295
SkyObject::GLOBULAR_CLUSTER
Definition: skyobject.h:108
SkyComponent::objectNames
QHash< int, QStringList > & objectNames()
Definition: skycomponent.h:127
SkyObject::STAR
Definition: skyobject.h:108
SkyObject
Provides all necessary information about an object in the sky: its coordinates, name(s), type, magnitude, and QStringLists of URLs for images and webpages regarding the object.
Definition: skyobject.h:46
QFrame
SkyObject::CATALOG_STAR
Definition: skyobject.h:108
SkyObject::OPEN_CLUSTER
Definition: skyobject.h:108
FindDialog::keyPressEvent
void keyPressEvent(QKeyEvent *e)
Process Keystrokes.
Definition: finddialog.cpp:310
FindDialogUI::FindDialogUI
FindDialogUI(QWidget *parent=0)
Definition: finddialog.cpp:33
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal