00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00075
00076
00077
00078
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
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 bool AddCatDialog::validateDataFile() {
00116
00117 CatalogContents = writeCatalogHeader();
00118
00119
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
00129
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
00206
00207
00208
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"