kstars
addcatdialog.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00068
00069
00070
00071
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
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 bool AddCatDialog::validateDataFile() {
00109 KStars *ksw = (KStars*) topLevelWidget()->parent();
00110
00111
00112 CatalogContents = writeCatalogHeader();
00113
00114
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
00124 KTempFile ktf;
00125 QFile tmpFile( ktf.name() );
00126 ktf.unlink();
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 );
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
00205
00206
00207
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"