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

kstars

addcatdialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           addcatdialog.cpp  -  description
00003                              -------------------
00004     begin                : Sun Mar 3 2002
00005     copyright            : (C) 2002 by Jason Harris
00006     email                : kstars@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 "addcatdialog.h"
00019 
00020 #include <QFrame>
00021 #include <QTextStream>
00022 
00023 #include <kcolorbutton.h>
00024 #include <kdebug.h>
00025 #include <kmessagebox.h>
00026 #include <knuminput.h>
00027 #include <ktemporaryfile.h>
00028 #include <kurl.h>
00029 
00030 #include "kstars.h"
00031 #include "kstarsdata.h"
00032 #include "Options.h"
00033 #include "skycomponents/customcatalogcomponent.h"
00034 
00035 AddCatDialogUI::AddCatDialogUI( QWidget *parent ) : QFrame( parent ) {
00036     setupUi(this);
00037 }
00038 
00039 AddCatDialog::AddCatDialog( KStars *_ks )
00040         : KDialog( ( QWidget* )_ks ),  ks( _ks )
00041 {
00042     QDir::setCurrent( QDir::homePath() );
00043     acd = new AddCatDialogUI(this);
00044     setMainWidget(acd);
00045     setCaption( i18n( "Import Catalog" ) );
00046     setButtons( KDialog::Help|KDialog::Ok|KDialog::Cancel );
00047 
00048     connect( acd->DataURL->lineEdit(), SIGNAL( lostFocus() ), this, SLOT( slotShowDataFile() ) );
00049     connect( acd->DataURL, SIGNAL( urlSelected( const KUrl & ) ),
00050              this, SLOT( slotShowDataFile() ) );
00051     connect( acd->PreviewButton, SIGNAL( clicked() ), this, SLOT( slotPreviewCatalog() ) );
00052     connect( this, SIGNAL( okClicked() ), this, SLOT( slotCreateCatalog() ) );
00053 
00054     acd->FieldList->addItem( i18n( "ID Number" ) );
00055     acd->FieldList->addItem( i18n( "Right Ascension" ) );
00056     acd->FieldList->addItem( i18n( "Declination" ) );
00057     acd->FieldList->addItem( i18n( "Object Type" ) );
00058 
00059     acd->FieldPool->addItem( i18n( "Common Name" ) );
00060     acd->FieldPool->addItem( i18n( "Magnitude" ) );
00061     acd->FieldPool->addItem( i18n( "Major Axis" ) );
00062     acd->FieldPool->addItem( i18n( "Minor Axis" ) );
00063     acd->FieldPool->addItem( i18n( "Position Angle" ) );
00064     acd->FieldPool->addItem( i18n( "Ignore" ) );
00065     connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
00066     connect(this,SIGNAL(cancelClicked()),this,SLOT(slotCancel()));
00067     connect(this,SIGNAL(helpClicked()),this,SLOT(slotHelp()));
00068 }
00069 
00070 AddCatDialog::~AddCatDialog(){
00071 }
00072 
00073 void AddCatDialog::slotOk() {
00074     //Overriding slotOk() so that custom data file can be validated before
00075     //KDialog::accept() is emitted and the window is closed.
00076 
00077     //the validation code needs to be aware of AddCatDialog members, so I will just
00078     //emit the okClicked() signal, which is connected to AddCatDialog::validateFile()
00079     emit okClicked();
00080 }
00081 
00082 void AddCatDialog::slotHelp() {
00083     QString message =
00084         i18n( "A valid custom catalog file has one line per object, "
00085               "with the following fields in each line:") + "\n\t" +
00086         i18n( "1. Type identifier.  Must be one of: 0 (star), 3 (open cluster), 4 (globular cluster), "
00087               "5 (gaseous nebula), 6 (planetary nebula), 7 (supernova remnant), or 8 (galaxy)" ) + "\n\t" +
00088         i18n( "2. Right Ascension (floating-point value)" ) + "\n\t" +
00089         i18n( "3. Declination (floating-point value)" ) + "\n\t" +
00090         i18n( "4. Magnitude (floating-point value)" ) + "\n\t" +
00091         i18n( "5. Spectral type (if type=0); otherwise object's catalog name" ) + "\n\t" +
00092         i18n( "6. Star name (if type=0); otherwise object's common name. [field 6 is optional]" ) + "\n\n" +
00093 
00094         i18n( "The fields should be separated by whitespace.  In addition, the catalog "
00095               "may contain comment lines beginning with \'#\'." );
00096 
00097     KMessageBox::information( 0, message, i18n( "Help on custom catalog file format" ) );
00098 }
00099 
00100 /* Attempt to parse the catalog data file specified in the DataURL box.
00101  * We assume the data file has space-separated fields, and that each line has
00102  * the data fields listed in the Catalog fields list, in the same order.
00103  * Each data field is parsed as follows:
00104  *
00105  * ID number: integer value
00106  * Right Ascension: colon-delimited hh:mm:ss.s or floating-point value
00107  * Declination: colon-delimited dd:mm:ss.s or floating-point value
00108  * Object type: integer value, one of [ 0,1,2,3,4,5,6,7,8 ]
00109  * Common name: string value (if it contains a space, it *must* be enclosed in quotes!)
00110  * Magnitude: floating-point value
00111  * Major axis: floating-point value (length of major axis in arcmin)
00112  * Minor axis: floating-point value (length of minor axis in arcmin)
00113  * Position angle: floating-point value (position angle, in degrees)
00114  */
00115 bool AddCatDialog::validateDataFile() {
00116     //Create the catalog file contents: first the header
00117     CatalogContents = writeCatalogHeader();
00118 
00119     //Next, the data lines (fill from user-specified file)
00120     QFile dataFile( acd->DataURL->url().path() );
00121     if ( ! acd->DataURL->url().isEmpty() && dataFile.open( QIODevice::ReadOnly ) ) {
00122         QTextStream dataStream( &dataFile );
00123         CatalogContents += dataStream.readAll();
00124 
00125         dataFile.close();
00126     }
00127 
00128     //Now create a temporary file for the Catalog, and attempt to parse it
00129     //into a temporary CustomCatalogComponent
00130     KTemporaryFile tmpFile;
00131     if ( tmpFile.open() ) {
00132         QTextStream ostream( &tmpFile );
00133         ostream << CatalogContents;
00134         ostream.flush();
00135         CustomCatalogComponent newCat( 0, tmpFile.fileName(), true, Options::showOther );
00136         newCat.init( ks->data() );
00137 
00138         int nObjects = newCat.objectList().size();
00139         if ( nObjects ) return true;
00140     }
00141 
00142     return false;
00143 }
00144 
00145 QString AddCatDialog::writeCatalogHeader() {
00146     QString name = ( acd->CatalogName->text().isEmpty() ? i18n("Custom") : acd->CatalogName->text() );
00147     QString pre = ( acd->Prefix->text().isEmpty() ? "CC" : acd->Prefix->text() );
00148 
00149     QString h = QString("# Name: %1\n").arg( name );
00150     h += QString("# Prefix: %1\n").arg( pre );
00151     h += QString("# Color: %1\n").arg( acd->ColorButton->color().name() );
00152     h += QString("# Epoch: %1\n").arg( acd->Epoch->value() );
00153     h += QString("# ");
00154 
00155     for ( int i=0; i < acd->FieldList->count(); ++i ) {
00156         QString f = acd->FieldList->item( i )->text();
00157 
00158         if ( f == i18n( "ID Number" ) ) {
00159             h += "ID  ";
00160         } else if ( f == i18n( "Right Ascension" ) ) {
00161             h += "RA  ";
00162         } else if ( f == i18n( "Declination" ) ) {
00163             h += "Dc  ";
00164         } else if ( f == i18n( "Object Type" ) ) {
00165             h += "Tp  ";
00166         } else if ( f == i18n( "Common Name" ) ) {
00167             h += "Nm  ";
00168         } else if ( f == i18n( "Magnitude" ) ) {
00169             h += "Mg  ";
00170         } else if ( f == i18n( "Major Axis" ) ) {
00171             h += "Mj  ";
00172         } else if ( f == i18n( "Minor Axis" ) ) {
00173             h += "Mn  ";
00174         } else if ( f == i18n( "Position Angle" ) ) {
00175             h += "PA  ";
00176         } else if ( f == i18n( "Ignore" ) ) {
00177             h += "Ig  ";
00178         }
00179     }
00180 
00181     h += '\n';
00182 
00183     return h;
00184 }
00185 
00186 void AddCatDialog::slotShowDataFile() {
00187     QFile dataFile( acd->DataURL->url().path() );
00188     if ( ! acd->DataURL->url().isEmpty() && dataFile.open( QIODevice::ReadOnly ) ) {
00189         acd->DataFileBox->clear();
00190         QTextStream dataStream( &dataFile );
00191         acd->DataFileBox->addItems( dataStream.readAll().split( "\n" ) );
00192         dataFile.close();
00193     }
00194 }
00195 
00196 void AddCatDialog::slotPreviewCatalog() {
00197     if ( validateDataFile() ) {
00198         KMessageBox::informationList( 0, i18n( "Preview of %1", acd->CatalogName->text() ),
00199                                       CatalogContents.split( "\n" ), i18n( "Catalog Preview" ) );
00200     }
00201 }
00202 
00203 void AddCatDialog::slotCreateCatalog() {
00204     if ( validateDataFile() ) {
00205         //CatalogContents now contains the text for the catalog file,
00206         //and objList contains the parsed objects
00207 
00208         //Warn user if file exists!
00209         if ( QFile::exists( acd->CatalogURL->url().path() ) )
00210         {
00211             KUrl u( acd->CatalogURL->url() );
00212             int r=KMessageBox::warningContinueCancel( 0,
00213                     i18n( "A file named \"%1\" already exists. "
00214                           "Overwrite it?", u.fileName() ),
00215                     i18n( "Overwrite File?" ),
00216                     KStandardGuiItem::overwrite() );
00217 
00218             if(r==KMessageBox::Cancel) return;
00219         }
00220 
00221         QFile OutFile( acd->CatalogURL->url().path() );
00222         if ( ! OutFile.open( QIODevice::WriteOnly ) ) {
00223             KMessageBox::sorry( 0,
00224                                 i18n( "Could not open the file %1 for writing.", acd->CatalogURL->url().path() ),
00225                                 i18n( "Error Opening Output File" ) );
00226         } else {
00227             QTextStream outStream( &OutFile );
00228             outStream << CatalogContents;
00229             OutFile.close();
00230 
00231             emit KDialog::accept();
00232             close();
00233         }
00234     }
00235 }
00236 
00237 #include "addcatdialog.moc"

kstars

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

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
Generated for kdeedu by doxygen 1.5.4
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