PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/kstars/tools/modcalcangdist.cpp

https://gitlab.com/g10h4ck/kstars
C++ | 237 lines | 163 code | 48 blank | 26 comment | 29 complexity | d52bbf671a77efa0d912635f3c82a577 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0
  1. /***************************************************************************
  2. modcalcapcoord.cpp - description
  3. -------------------
  4. begin : Sun May 30 2004
  5. copyright : (C) 2004 by Pablo de Vicente
  6. email : vicente@oan.es
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "modcalcangdist.h"
  17. #include <QTextStream>
  18. #include <KLocalizedString>
  19. #include <KMessageBox>
  20. #include "dms.h"
  21. #include "widgets/dmsbox.h"
  22. #include "skyobjects/skypoint.h"
  23. #include "dialogs/finddialog.h"
  24. #include "kstars.h"
  25. modCalcAngDist::modCalcAngDist(QWidget *parentSplit)
  26. : QFrame(parentSplit) {
  27. setupUi(this);
  28. FirstRA->setDegType(false);
  29. SecondRA->setDegType(false);
  30. connect( FirstRA, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()) );
  31. connect( FirstDec, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()) );
  32. connect( SecondRA, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()) );
  33. connect( SecondDec, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()) );
  34. connect( FirstRA, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()) );
  35. connect( FirstDec, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()) );
  36. connect( SecondRA, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()) );
  37. connect( SecondDec, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()) );
  38. connect( FirstObjectButton, SIGNAL(clicked()), this, SLOT(slotObjectButton()) );
  39. connect( SecondObjectButton, SIGNAL(clicked()), this, SLOT(slotObjectButton()) );
  40. connect( runButtonBatch, SIGNAL(clicked()), this, SLOT(slotRunBatch()) );
  41. show();
  42. slotValidatePositions();
  43. }
  44. modCalcAngDist::~modCalcAngDist(){
  45. }
  46. SkyPoint modCalcAngDist::getCoords (dmsBox* rBox, dmsBox* dBox, bool *ok) {
  47. dms raCoord, decCoord;
  48. bool ok2=false;
  49. raCoord = rBox->createDms(false, &ok2);
  50. if ( ok2 )
  51. decCoord = dBox->createDms(true, &ok2);
  52. if ( ok2 ) {
  53. if ( ok ) *ok = ok2;
  54. return SkyPoint (raCoord, decCoord);
  55. } else {
  56. if ( ok ) *ok = ok2;
  57. return SkyPoint();
  58. }
  59. }
  60. void modCalcAngDist::slotValidatePositions(){
  61. SkyPoint sp0,sp1;
  62. bool ok;
  63. sp0 = getCoords(FirstRA, FirstDec, &ok);
  64. if ( ok )
  65. sp1 = getCoords(SecondRA, SecondDec, &ok);
  66. if ( ok )
  67. AngDist->setText( sp0.angularDistanceTo(&sp1).toDMSString() );
  68. else
  69. AngDist->setText( " .... " );
  70. }
  71. void modCalcAngDist::slotObjectButton() {
  72. QPointer<FindDialog> fd = new FindDialog( this );
  73. if ( fd->exec() == QDialog::Accepted ) {
  74. SkyObject *o = fd->selectedObject();
  75. if ( sender()->objectName() == QString("FirstObjectButton") ) {
  76. FirstRA->showInHours( o->ra() );
  77. FirstDec->showInDegrees( o->dec() );
  78. FirstPositionBox->setTitle( xi18n("First position: %1", o->name()) );
  79. } else {
  80. SecondRA->showInHours( o->ra() );
  81. SecondDec->showInDegrees( o->dec() );
  82. SecondPositionBox->setTitle( xi18n("Second position: %1", o->name()) );
  83. }
  84. slotValidatePositions();
  85. }
  86. delete fd;
  87. }
  88. void modCalcAngDist::slotResetTitle() {
  89. QString name = sender()->objectName();
  90. if ( name.contains( "First" ) )
  91. FirstPositionBox->setTitle( xi18n("First position") );
  92. else
  93. SecondPositionBox->setTitle( xi18n("Second position") );
  94. }
  95. void modCalcAngDist::slotRunBatch() {
  96. QString inputFileName = InputLineEditBatch->url().toLocalFile();
  97. // We open the input file and read its content
  98. if ( QFile::exists(inputFileName) ) {
  99. QFile f( inputFileName );
  100. if ( !f.open( QIODevice::ReadOnly) ) {
  101. QString message = xi18n( "Could not open file %1.", f.fileName() );
  102. KMessageBox::sorry( 0, message, xi18n( "Could Not Open File" ) );
  103. inputFileName.clear();
  104. return;
  105. }
  106. // processLines(&f);
  107. QTextStream istream(&f);
  108. processLines(istream);
  109. // readFile( istream );
  110. f.close();
  111. } else {
  112. QString message = xi18n( "Invalid file: %1", inputFileName );
  113. KMessageBox::sorry( 0, message, xi18n( "Invalid file" ) );
  114. inputFileName.clear();
  115. InputLineEditBatch->setText( inputFileName );
  116. return;
  117. }
  118. }
  119. //void modCalcAngDist::processLines( const QFile * fIn ) {
  120. void modCalcAngDist::processLines( QTextStream &istream ) {
  121. // we open the output file
  122. // QTextStream istream(&fIn);
  123. QString outputFileName;
  124. outputFileName = OutputLineEditBatch->text();
  125. QFile fOut( outputFileName );
  126. fOut.open(QIODevice::WriteOnly);
  127. QTextStream ostream(&fOut);
  128. QString line;
  129. QChar space = ' ';
  130. int i = 0;
  131. SkyPoint sp0, sp1;
  132. dms ra0B, dec0B, ra1B, dec1B, dist;
  133. while ( ! istream.atEnd() ) {
  134. line = istream.readLine();
  135. line.trimmed();
  136. //Go through the line, looking for parameters
  137. QStringList fields = line.split( ' ' );
  138. i = 0;
  139. // Read RA and write in ostream if corresponds
  140. if(ra0CheckBatch->isChecked() ) {
  141. ra0B = dms::fromString( fields[i],false);
  142. i++;
  143. } else
  144. ra0B = ra0BoxBatch->createDms(false);
  145. if ( allRadioBatch->isChecked() )
  146. ostream << ra0B.toHMSString() << space;
  147. else
  148. if(ra0CheckBatch->isChecked() )
  149. ostream << ra0B.toHMSString() << space;
  150. // Read DEC and write in ostream if corresponds
  151. if(dec0CheckBatch->isChecked() ) {
  152. dec0B = dms::fromString( fields[i], true);
  153. i++;
  154. } else
  155. dec0B = dec0BoxBatch->createDms();
  156. if ( allRadioBatch->isChecked() )
  157. ostream << dec0B.toDMSString() << space;
  158. else
  159. if(dec0CheckBatch->isChecked() )
  160. ostream << dec0B.toDMSString() << space;
  161. // Read RA and write in ostream if corresponds
  162. if(ra1CheckBatch->isChecked() ) {
  163. ra1B = dms::fromString( fields[i],false);
  164. i++;
  165. } else
  166. ra1B = ra1BoxBatch->createDms(false);
  167. if ( allRadioBatch->isChecked() )
  168. ostream << ra1B.toHMSString() << space;
  169. else
  170. if(ra1CheckBatch->isChecked() )
  171. ostream << ra1B.toHMSString() << space;
  172. // Read DEC and write in ostream if corresponds
  173. if(dec1CheckBatch->isChecked() ) {
  174. dec1B = dms::fromString( fields[i], true);
  175. i++;
  176. } else
  177. dec1B = dec1BoxBatch->createDms();
  178. if ( allRadioBatch->isChecked() )
  179. ostream << dec1B.toDMSString() << space;
  180. else
  181. if(dec1CheckBatch->isChecked() )
  182. ostream << dec1B.toDMSString() << space;
  183. sp0 = SkyPoint (ra0B, dec0B);
  184. sp1 = SkyPoint (ra1B, dec1B);
  185. dist = sp0.angularDistanceTo(&sp1);
  186. ostream << dist.toDMSString() << endl;
  187. }
  188. fOut.close();
  189. }