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

kstars

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

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
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