modcalcapcoord.cpp 8.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/***************************************************************************
                          modcalcapcoord.cpp  -  description
                             -------------------
    begin                : Wed Apr 10 2002
    copyright            : (C) 2002 by Pablo de Vicente
    email                : vicente@oan.es
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

18 19
#include "modcalcapcoord.h"

20
#include <QTextStream>
Jasem Mutlaq's avatar
Jasem Mutlaq committed
21
#include <QFileDialog>
22
#include <QPointer>
23

Jasem Mutlaq's avatar
Jasem Mutlaq committed
24 25
#include <KLocalizedString>
#include <KMessageBox>
26

27
#include "kstars.h"
28
#include "dms.h"
29
#include "skyobjects/skypoint.h"
30
#include "skyobjects/skyobject.h"
31
#include "kstarsdatetime.h"
32
#include "widgets/dmsbox.h"
33
#include "dialogs/finddialog.h"
34

35
modCalcApCoord::modCalcApCoord(QWidget *parentSplit) : QFrame(parentSplit)
36
{
37 38 39 40 41
    setupUi(this);
    showCurrentTime();
    RACat->setDegType(false);
    DecCat->setDegType(true);

42 43 44 45 46 47
    connect(ObjectButton, SIGNAL(clicked()), this, SLOT(slotObject()));
    connect(NowButton, SIGNAL(clicked()), this, SLOT(showCurrentTime()));
    connect(RACat, SIGNAL(editingFinished()), this, SLOT(slotCompute()));
    connect(DecCat, SIGNAL(editingFinished()), this, SLOT(slotCompute()));
    connect(UT, SIGNAL(timeChanged(const QTime &)), this, SLOT(slotCompute()));
    connect(Date, SIGNAL(dateChanged(const QDate &)), this, SLOT(slotCompute()));
48

49 50 51 52 53 54
    connect(utCheckBatch, SIGNAL(clicked()), this, SLOT(slotUtCheckedBatch()));
    connect(dateCheckBatch, SIGNAL(clicked()), this, SLOT(slotDateCheckedBatch()));
    connect(raCheckBatch, SIGNAL(clicked()), this, SLOT(slotRaCheckedBatch()));
    connect(decCheckBatch, SIGNAL(clicked()), this, SLOT(slotDecCheckedBatch()));
    connect(epochCheckBatch, SIGNAL(clicked()), this, SLOT(slotEpochCheckedBatch()));
    connect(runButtonBatch, SIGNAL(clicked()), this, SLOT(slotRunBatch()));
55 56

    show();
57 58
}

59 60
modCalcApCoord::~modCalcApCoord()
{
61 62
}

63
void modCalcApCoord::showCurrentTime(void)
64
{
65 66 67 68
    KStarsDateTime dt(KStarsDateTime::currentDateTime());
    Date->setDate(dt.date());
    UT->setTime(dt.time());
    EpochTarget->setText(QString::number(dt.epoch(), 'f', 3));
69 70
}

71 72
void modCalcApCoord::slotCompute()
{
73
    KStarsDateTime dt(Date->date(), UT->time());
74
    long double jd = dt.djd();
75

76
    dt.setFromEpoch(EpochCat->value());
77
    long double jd0 = dt.djd();
78

79
    SkyPoint sp(RACat->createDms(false), DecCat->createDms());
80
    sp.apparentCoord(jd0, jd);
81

82 83
    RA->setText(sp.ra().toHMSString());
    Dec->setText(sp.dec().toDMSString());
84 85
}

86 87
void modCalcApCoord::slotObject()
{
88 89
    QPointer<FindDialog> fd = new FindDialog(this);
    if (fd->exec() == QDialog::Accepted)
90
    {
91 92 93 94
        SkyObject *o = fd->targetObject();
        RACat->showInHours(o->ra0());
        DecCat->showInDegrees(o->dec0());
        EpochCat->setValue(2000.0);
95 96 97

        slotCompute();
    }
98
    delete fd;
99 100
}

101 102
void modCalcApCoord::slotUtCheckedBatch()
{
103 104
    if (utCheckBatch->isChecked())
        utBoxBatch->setEnabled(false);
105 106
    else
    {
107
        utBoxBatch->setEnabled(true);
108
    }
109 110
}

111 112
void modCalcApCoord::slotDateCheckedBatch()
{
113 114
    if (dateCheckBatch->isChecked())
        dateBoxBatch->setEnabled(false);
115 116
    else
    {
117
        dateBoxBatch->setEnabled(true);
118
    }
119 120
}

121 122
void modCalcApCoord::slotRaCheckedBatch()
{
123 124
    if (raCheckBatch->isChecked())
        raBoxBatch->setEnabled(false);
125 126
    else
    {
127
        raBoxBatch->setEnabled(true);
128
    }
129 130
}

131 132
void modCalcApCoord::slotDecCheckedBatch()
{
133 134
    if (decCheckBatch->isChecked())
        decBoxBatch->setEnabled(false);
135 136
    else
    {
137
        decBoxBatch->setEnabled(true);
138
    }
139 140
}

141 142
void modCalcApCoord::slotEpochCheckedBatch()
{
143 144
    if (epochCheckBatch->isChecked())
        epochBoxBatch->setEnabled(false);
145 146
    else
    {
147
        epochBoxBatch->setEnabled(true);
148
    }
149 150
}

151 152
void modCalcApCoord::slotRunBatch()
{
153
    QString inputFileName = InputLineEditBatch->url().toLocalFile();
154 155 156

    // We open the input file and read its content

157
    if (QFile::exists(inputFileName))
158
    {
159 160
        QFile f(inputFileName);
        if (!f.open(QIODevice::ReadOnly))
161
        {
162 163
            QString message = i18n("Could not open file %1.", f.fileName());
            KMessageBox::sorry(0, message, i18n("Could Not Open File"));
164
            inputFileName.clear();
165 166 167 168 169 170 171 172
            return;
        }

        //		processLines(&f);
        QTextStream istream(&f);
        processLines(istream);
        //		readFile( istream );
        f.close();
173 174 175
    }
    else
    {
176 177
        QString message = i18n("Invalid file: %1", inputFileName);
        KMessageBox::sorry(0, message, i18n("Invalid file"));
178
        inputFileName.clear();
179
        InputLineEditBatch->setText(inputFileName);
180 181
        return;
    }
182 183 184
}

//void modCalcApCoord::processLines( const QFile * fIn ) {
185
void modCalcApCoord::processLines(QTextStream &istream)
186
{
187 188 189 190 191
    // we open the output file

    //	QTextStream istream(&fIn);
    QString outputFileName;
    outputFileName = OutputLineEditBatch->text();
192
    QFile fOut(outputFileName);
193 194 195 196
    fOut.open(QIODevice::WriteOnly);
    QTextStream ostream(&fOut);

    QString line;
197
    QChar space = ' ';
198
    int i       = 0;
199 200 201
    long double jd, jd0;
    SkyPoint sp;
    QTime utB;
202
    QDate dtB;
203 204 205
    dms raB, decB;
    QString epoch0B;

206
    while (!istream.atEnd())
207
    {
208 209 210 211 212
        line = istream.readLine();
        line.trimmed();

        //Go through the line, looking for parameters

213
        QStringList fields = line.split(' ');
214 215 216 217 218

        i = 0;

        // Read Ut and write in ostream if corresponds

219
        if (utCheckBatch->isChecked())
220
        {
221
            utB = QTime::fromString(fields[i]);
222
            i++;
223 224
        }
        else
225 226
            utB = utBoxBatch->time();

227 228 229 230
        if (allRadioBatch->isChecked())
            ostream << QLocale().toString(utB) << space;
        else if (utCheckBatch->isChecked())
            ostream << QLocale().toString(utB) << space;
231 232 233

        // Read date and write in ostream if corresponds

234
        if (dateCheckBatch->isChecked())
235
        {
236
            dtB = QDate::fromString(fields[i]);
237
            i++;
238 239
        }
        else
240 241
            dtB = dateBoxBatch->date();

242 243 244 245
        if (allRadioBatch->isChecked())
            ostream << QLocale().toString(dtB, QLocale::LongFormat).append(space);
        else if (dateCheckBatch->isChecked())
            ostream << QLocale().toString(dtB, QLocale::LongFormat).append(space);
246 247 248

        // Read RA and write in ostream if corresponds

249
        if (raCheckBatch->isChecked())
250
        {
251
            raB = dms::fromString(fields[i], false);
252
            i++;
253 254
        }
        else
255 256
            raB = raBoxBatch->createDms(false);

257
        if (allRadioBatch->isChecked())
258
            ostream << raB.toHMSString() << space;
259
        else if (raCheckBatch->isChecked())
260
            ostream << raB.toHMSString() << space;
261 262 263

        // Read DEC and write in ostream if corresponds

264
        if (decCheckBatch->isChecked())
265
        {
266
            decB = dms::fromString(fields[i], true);
267
            i++;
268 269
        }
        else
270 271
            decB = decBoxBatch->createDms();

272
        if (allRadioBatch->isChecked())
273
            ostream << decB.toDMSString() << space;
274
        else if (decCheckBatch->isChecked())
275
            ostream << decB.toHMSString() << space;
276 277 278

        // Read Epoch and write in ostream if corresponds

279
        if (epochCheckBatch->isChecked())
280
        {
281 282
            epoch0B = fields[i];
            i++;
283 284
        }
        else
285 286
            epoch0B = epochBoxBatch->text();

287
        if (allRadioBatch->isChecked())
288
            ostream << epoch0B;
289
        else if (decCheckBatch->isChecked())
290
            ostream << epoch0B;
291 292

        KStarsDateTime dt;
293 294
        dt.setFromEpoch(epoch0B);
        jd  = KStarsDateTime(dtB, utB).djd();
295
        jd0 = dt.djd();
296
        sp  = SkyPoint(raB, decB);
297 298
        sp.apparentCoord(jd0, jd);

299
        ostream << sp.ra().toHMSString() << sp.dec().toDMSString() << endl;
300 301 302
    }

    fOut.close();
303
}