• 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
addcatdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  addcatdialog.cpp - description
3  -------------------
4  begin : Sun Mar 3 2002
5  copyright : (C) 2002 by Jason Harris
6  email : kstars@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 "addcatdialog.h"
19 
20 #include <QFrame>
21 #include <QTextStream>
22 
23 #include <kcolorbutton.h>
24 #include <kdebug.h>
25 #include <kmessagebox.h>
26 #include <knuminput.h>
27 #include <ktemporaryfile.h>
28 #include <kurl.h>
29 
30 #include "kstars.h"
31 #include "kstarsdata.h"
32 #include "Options.h"
33 #include "skycomponents/catalogcomponent.h"
34 #include "skycomponents/skymapcomposite.h"
35 
36 AddCatDialogUI::AddCatDialogUI( QWidget *parent ) : QFrame( parent ) {
37  setupUi(this);
38 }
39 
40 AddCatDialog::AddCatDialog( KStars *_ks )
41  : KDialog( ( QWidget* )_ks ), ks( _ks )
42 {
43  QDir::setCurrent( QDir::homePath() );
44  acd = new AddCatDialogUI(this);
45  setMainWidget(acd);
46  setCaption( i18n( "Import Catalog" ) );
47  setButtons( KDialog::Help|KDialog::Ok|KDialog::Cancel );
48 
49  connect( acd->DataURL->lineEdit(), SIGNAL( lostFocus() ), this, SLOT( slotShowDataFile() ) );
50  connect( acd->DataURL, SIGNAL( urlSelected( const KUrl & ) ),
51  this, SLOT( slotShowDataFile() ) );
52  connect( acd->PreviewButton, SIGNAL( clicked() ), this, SLOT( slotPreviewCatalog() ) );
53  connect( this, SIGNAL( okClicked() ), this, SLOT( slotCreateCatalog() ) );
54 // connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
55  connect(this,SIGNAL(cancelClicked()),this,SLOT(slotCancel()));
56  connect(this,SIGNAL(helpClicked()),this,SLOT(slotHelp()));
57 
58  acd->FieldList->addItem( i18n( "ID Number" ) );
59  acd->FieldList->addItem( i18n( "Right Ascension" ) );
60  acd->FieldList->addItem( i18n( "Declination" ) );
61  acd->FieldList->addItem( i18n( "Object Type" ) );
62 
63  acd->FieldPool->addItem( i18n( "Common Name" ) );
64  acd->FieldPool->addItem( i18n( "Magnitude" ) );
65  acd->FieldPool->addItem( i18n( "Flux" ) );
66  acd->FieldPool->addItem( i18n( "Major Axis" ) );
67  acd->FieldPool->addItem( i18n( "Minor Axis" ) );
68  acd->FieldPool->addItem( i18n( "Position Angle" ) );
69  acd->FieldPool->addItem( i18n( "Ignore" ) );
70 }
71 
72 AddCatDialog::~AddCatDialog(){
73 }
74 
75 void AddCatDialog::slotOk() {
76  //Overriding slotOk() so that custom data file can be validated before
77  //KDialog::accept() is emitted and the window is closed.
78 
79  //the validation code needs to be aware of AddCatDialog members, so I will just
80  //emit the okClicked() signal, which is connected to AddCatDialog::validateFile()
81  emit okClicked();
82 }
83 
84 void AddCatDialog::slotHelp() {
85  QString message =
86  i18n( "A valid custom catalog file has one line per object, "
87  "with the following fields in each line:") + "\n\t" +
88  i18n( "1. Type identifier. Must be one of: 0 (star), 3 (open cluster), 4 (globular cluster), "
89  "5 (gaseous nebula), 6 (planetary nebula), 7 (supernova remnant), or 8 (galaxy)" ) + "\n\t" +
90  i18n( "2. Right Ascension (floating-point value)" ) + "\n\t" +
91  i18n( "3. Declination (floating-point value)" ) + "\n\t" +
92  i18n( "4. Magnitude (floating-point value)" ) + "\n\t" +
93  i18n( "5. Integrated Flux (floating-point value); frequency and units are set separately in the catalog file." ) + "\n\t" +
94  i18n( "6. Spectral type (if type=0); otherwise object's catalog name" ) + "\n\t" +
95  i18n( "7. Star name (if type=0); otherwise object's common name. [field 7 is optional]" ) + "\n\n" +
96 
97  i18n( "The fields should be separated by whitespace. In addition, the catalog "
98  "may contain comment lines beginning with \'#\'." );
99 
100  KMessageBox::information( 0, message, i18n( "Help on custom catalog file format" ) );
101 }
102 
103 /* Attempt to parse the catalog data file specified in the DataURL box.
104  * We assume the data file has space-separated fields, and that each line has
105  * the data fields listed in the Catalog fields list, in the same order.
106  * Each data field is parsed as follows:
107  *
108  * ID number: integer value
109  * Right Ascension: colon-delimited hh:mm:ss.s or floating-point value
110  * Declination: colon-delimited dd:mm:ss.s or floating-point value
111  * Object type: integer value, one of [ 0,1,2,3,4,5,6,7,8 ]
112  * Common name: string value (if it contains a space, it *must* be enclosed in quotes!)
113  * Magnitude: floating-point value
114  * Major axis: floating-point value (length of major axis in arcmin)
115  * Minor axis: floating-point value (length of minor axis in arcmin)
116  * Position angle: floating-point value (position angle, in degrees)
117  */
118 bool AddCatDialog::validateDataFile() {
119  //Create the catalog file contents: first the header
120  CatalogContents = writeCatalogHeader();
121 
122  // Check if the URL is empty
123  if (acd->DataURL->url().isEmpty())
124  return false;
125 
126  //Next, the data lines (fill from user-specified file)
127  QFile dataFile( acd->DataURL->url().toLocalFile() );
128  if (dataFile.open( QIODevice::ReadOnly ) ) {
129  QTextStream dataStream( &dataFile );
130  CatalogContents += dataStream.readAll();
131 
132  dataFile.close();
133  return true;
134  }
135 
136  return false;
137 
138 }
139 
140 QString AddCatDialog::writeCatalogHeader() {
141  QString name = ( acd->CatalogName->text().isEmpty() ? i18n("Custom") : acd->CatalogName->text() );
142  QString pre = ( acd->Prefix->text().isEmpty() ? "CC" : acd->Prefix->text() );
143  char delimiter = ( acd->CSVButton->isChecked() ? ',' : ' ' );
144 
145  QString h = QString("# Delimiter: %1\n").arg( delimiter );
146  h += QString("# Name: %1\n").arg( name );
147  h += QString("# Prefix: %1\n").arg( pre );
148  h += QString("# Color: %1\n").arg( acd->ColorButton->color().name() );
149  h += QString("# Epoch: %1\n").arg( acd->Epoch->value() );
150  h += QString("# ");
151 
152  for ( int i=0; i < acd->FieldList->count(); ++i ) {
153  QString f = acd->FieldList->item( i )->text();
154 
155  if ( f == i18n( "ID Number" ) ) {
156  h += "ID ";
157  } else if ( f == i18n( "Right Ascension" ) ) {
158  h += "RA ";
159  } else if ( f == i18n( "Declination" ) ) {
160  h += "Dc ";
161  } else if ( f == i18n( "Object Type" ) ) {
162  h += "Tp ";
163  } else if ( f == i18n( "Common Name" ) ) {
164  h += "Nm ";
165  } else if ( f == i18n( "Magnitude" ) ) {
166  h += "Mg ";
167  } else if ( f == i18n( "Flux" ) ) {
168  h += "Flux ";
169  } else if ( f == i18n( "Major Axis" ) ) {
170  h += "Mj ";
171  } else if ( f == i18n( "Minor Axis" ) ) {
172  h += "Mn ";
173  } else if ( f == i18n( "Position Angle" ) ) {
174  h += "PA ";
175  } else if ( f == i18n( "Ignore" ) ) {
176  h += "Ig ";
177  }
178  }
179 
180  h += '\n';
181 
182  return h;
183 }
184 
185 void AddCatDialog::slotShowDataFile() {
186  QFile dataFile( acd->DataURL->url().toLocalFile() );
187  if ( ! acd->DataURL->url().isEmpty() && dataFile.open( QIODevice::ReadOnly ) ) {
188  acd->DataFileBox->clear();
189  QTextStream dataStream( &dataFile );
190  acd->DataFileBox->addItems( dataStream.readAll().split( '\n' ) );
191  dataFile.close();
192  }
193 }
194 
195 void AddCatDialog::slotPreviewCatalog() {
196  if ( validateDataFile() ) {
197  KMessageBox::informationList( 0, i18n( "Preview of %1", acd->CatalogName->text() ),
198  CatalogContents.split( '\n' ), i18n( "Catalog Preview" ) );
199  }
200 }
201 
202 void AddCatDialog::slotCreateCatalog() {
203 
204  if ( validateDataFile() ) {
205  //CatalogContents now contains the text for the catalog file,
206  //and objList contains the parsed objects
207 
208  //Warn user if file exists!
209  if ( QFile::exists( acd->CatalogURL->url().toLocalFile() ) )
210  {
211  KUrl u( acd->CatalogURL->url() );
212  int r=KMessageBox::warningContinueCancel( 0,
213  i18n( "A file named \"%1\" already exists. "
214  "Overwrite it?", u.fileName() ),
215  i18n( "Overwrite File?" ),
216  KStandardGuiItem::overwrite() );
217 
218  if(r==KMessageBox::Cancel) return;
219  }
220 
221  QFile OutFile( acd->CatalogURL->url().toLocalFile() );
222  if ( ! OutFile.open( QIODevice::WriteOnly ) ) {
223  KMessageBox::sorry( 0,
224  i18n( "Could not open the file %1 for writing.", acd->CatalogURL->url().toLocalFile() ),
225  i18n( "Error Opening Output File" ) );
226  } else {
227  QTextStream outStream( &OutFile );
228  outStream << CatalogContents;
229  OutFile.close();
230 
231  KStarsData::Instance()->skyComposite()->addCustomCatalog(OutFile.fileName(), 0);
232 
233  emit KDialog::accept();
234  close();
235  }
236  }
237 }
238 
239 #include "addcatdialog.moc"
QWidget
KStarsData::Instance
static KStarsData * Instance()
Definition: kstarsdata.h:92
addcatdialog.h
KDialog
KStars
This is the main window for KStars.
Definition: kstars.h:94
SkyMapComposite::addCustomCatalog
void addCustomCatalog(const QString &filename, int index)
Definition: skymapcomposite.cpp:468
NaN::f
const float f
Definition: nan.h:36
skymapcomposite.h
AddCatDialogUI::AddCatDialogUI
AddCatDialogUI(QWidget *parent=0)
Definition: addcatdialog.cpp:36
AddCatDialogUI
Definition: addcatdialog.h:30
KStarsData::skyComposite
SkyMapComposite * skyComposite()
Definition: kstarsdata.h:146
AddCatDialog::~AddCatDialog
~AddCatDialog()
Destructor (empty)
Definition: addcatdialog.cpp:72
AddCatDialog::AddCatDialog
AddCatDialog(KStars *_ks)
Default constructor.
Definition: addcatdialog.cpp:40
Options.h
QTextStream
kstarsdata.h
QFrame
kstars.h
catalogcomponent.h
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