~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to src/scripting/StelMainScriptAPI.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-02-15 20:48:39 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20100215204839-u3qgbv60rho997yk
Tags: 0.10.3-0ubuntu1
* New upstream release.
  - fixes intel rendering bug (LP: #480553)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Stellarium
 
3
 * Copyright (C) 2009 Matthew Gates
 
4
 * Copyright (C) 2007 Fabien Chereau
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 */
 
20
 
 
21
#include "StelMainScriptAPI.hpp"
 
22
#include "StelMainScriptAPIProxy.hpp"
 
23
#include "StelScriptMgr.hpp"
 
24
 
 
25
#include "ConstellationMgr.hpp"
 
26
#include "GridLinesMgr.hpp"
 
27
#include "LandscapeMgr.hpp"
 
28
#include "MeteorMgr.hpp"
 
29
#include "NebulaMgr.hpp"
 
30
#include "Planet.hpp"
 
31
#include "SolarSystem.hpp"
 
32
#include "StarMgr.hpp"
 
33
#include "StelApp.hpp"
 
34
#include "StelAudioMgr.hpp"
 
35
#include "StelCore.hpp"
 
36
#include "StelFileMgr.hpp"
 
37
#include "StelLocation.hpp"
 
38
#include "StelLocationMgr.hpp"
 
39
#include "StelMainGraphicsView.hpp"
 
40
#include "StelModuleMgr.hpp"
 
41
#include "StelMovementMgr.hpp"
 
42
#include "StelNavigator.hpp"
 
43
#include "StelObject.hpp"
 
44
#include "StelObjectMgr.hpp"
 
45
#include "StelProjector.hpp"
 
46
#include "StelSkyCultureMgr.hpp"
 
47
#include "StelSkyDrawer.hpp"
 
48
#include "StelSkyLayerMgr.hpp"
 
49
#include "StelUtils.hpp"
 
50
#include "StelGuiBase.hpp"
 
51
 
 
52
#include <QAction>
 
53
#include <QDateTime>
 
54
#include <QDebug>
 
55
#include <QDir>
 
56
#include <QFile>
 
57
#include <QFileInfo>
 
58
#include <QRegExp>
 
59
#include <QSet>
 
60
#include <QStringList>
 
61
#include <QTemporaryFile>
 
62
 
 
63
#include <cmath>
 
64
 
 
65
StelMainScriptAPI::StelMainScriptAPI(QObject *parent) : QObject(parent)
 
66
{
 
67
        if(StelSkyLayerMgr* smgr = GETSTELMODULE(StelSkyLayerMgr))
 
68
        {
 
69
                connect(this, SIGNAL(requestLoadSkyImage(const QString&, const QString&, double, double, double, double, double, double, double, double, double, double, bool)), smgr, SLOT(loadSkyImage(const QString&, const QString&, double, double, double, double, double, double, double, double, double, double, bool)));
 
70
                connect(this, SIGNAL(requestRemoveSkyImage(const QString&)), smgr, SLOT(removeSkyLayer(const QString&)));
 
71
        }
 
72
 
 
73
        connect(this, SIGNAL(requestLoadSound(const QString&, const QString&)), StelApp::getInstance().getStelAudioMgr(), SLOT(loadSound(const QString&, const QString&)));
 
74
        connect(this, SIGNAL(requestPlaySound(const QString&)), StelApp::getInstance().getStelAudioMgr(), SLOT(playSound(const QString&)));
 
75
        connect(this, SIGNAL(requestPauseSound(const QString&)), StelApp::getInstance().getStelAudioMgr(), SLOT(pauseSound(const QString&)));
 
76
        connect(this, SIGNAL(requestStopSound(const QString&)), StelApp::getInstance().getStelAudioMgr(), SLOT(stopSound(const QString&)));
 
77
        connect(this, SIGNAL(requestDropSound(const QString&)), StelApp::getInstance().getStelAudioMgr(), SLOT(dropSound(const QString&)));
 
78
        connect(this, SIGNAL(requestExit()), this->parent(), SLOT(stopScript()));
 
79
        connect(this, SIGNAL(requestSetNightMode(bool)), &StelApp::getInstance(), SLOT(setVisionModeNight(bool)));
 
80
        connect(this, SIGNAL(requestSetProjectionMode(QString)), StelApp::getInstance().getCore(), SLOT(setCurrentProjectionTypeKey(QString)));
 
81
        connect(this, SIGNAL(requestSetSkyCulture(QString)), &StelApp::getInstance().getSkyCultureMgr(), SLOT(setCurrentSkyCultureID(QString)));
 
82
        connect(this, SIGNAL(requestSetDiskViewport(bool)), StelMainGraphicsView::getInstance().getMainScriptAPIProxy(), SLOT(setDiskViewport(bool)));
 
83
}
 
84
 
 
85
StelMainScriptAPI::~StelMainScriptAPI()
 
86
{
 
87
}
 
88
 
 
89
ScriptSleeper& StelMainScriptAPI::getScriptSleeper(void)
 
90
{
 
91
        return scriptSleeper;
 
92
}
 
93
 
 
94
//! Set the current date in Julian Day
 
95
//! @param JD the Julian Date
 
96
void StelMainScriptAPI::setJDay(double JD)
 
97
{
 
98
        StelApp::getInstance().getCore()->getNavigator()->setJDay(JD);
 
99
}
 
100
 
 
101
//! Get the current date in Julian Day
 
102
//! @return the Julian Date
 
103
double StelMainScriptAPI::getJDay(void) const
 
104
{
 
105
        return StelApp::getInstance().getCore()->getNavigator()->getJDay();
 
106
}
 
107
 
 
108
void StelMainScriptAPI::setDate(const QString& dt, const QString& spec)
 
109
{
 
110
        StelApp::getInstance().getCore()->getNavigator()->setJDay(jdFromDateString(dt, spec));
 
111
}
 
112
 
 
113
QString StelMainScriptAPI::getDate(const QString& spec)
 
114
{
 
115
        return StelUtils::jdToIsoString(getJDay());
 
116
}
 
117
 
 
118
//! Set time speed in JDay/sec
 
119
//! @param ts time speed in JDay/sec
 
120
void StelMainScriptAPI::setTimeRate(double ts)
 
121
{
 
122
        // 1 second = .00001157407407407407 JDay
 
123
        StelApp::getInstance().getCore()->getNavigator()->setTimeRate(ts * 0.00001157407407407407 * scriptSleeper.getRate());
 
124
}
 
125
 
 
126
//! Get time speed in JDay/sec
 
127
//! @return time speed in JDay/sec
 
128
double StelMainScriptAPI::getTimeRate(void) const
 
129
{
 
130
        return StelApp::getInstance().getCore()->getNavigator()->getTimeRate() / (0.00001157407407407407 * scriptSleeper.getRate());
 
131
}
 
132
 
 
133
bool StelMainScriptAPI::isRealTime()
 
134
{
 
135
        return StelApp::getInstance().getCore()->getNavigator()->getIsTimeNow();
 
136
}
 
137
 
 
138
void StelMainScriptAPI::setRealTime()
 
139
{
 
140
        setTimeRate(1.0);
 
141
        StelApp::getInstance().getCore()->getNavigator()->setTimeNow();
 
142
}
 
143
 
 
144
void StelMainScriptAPI::wait(double t)
 
145
{
 
146
        scriptSleeper.sleep(t*1000);
 
147
}
 
148
 
 
149
void StelMainScriptAPI::waitFor(const QString& dt, const QString& spec)
 
150
{
 
151
        double JD = jdFromDateString(dt, spec);
 
152
        StelNavigator* nav = StelApp::getInstance().getCore()->getNavigator();
 
153
        Q_ASSERT(nav);
 
154
        double timeSpeed = nav->getTimeRate();
 
155
 
 
156
        if (timeSpeed == 0.)
 
157
        {
 
158
                qWarning() << "waitFor called with no time passing - would be infinite. not waiting!";
 
159
                return;
 
160
        }
 
161
        else if (timeSpeed > 0)
 
162
        {
 
163
                while(nav->getJDay() < JD)
 
164
                        scriptSleeper.sleep(200);
 
165
        }
 
166
        else
 
167
        {
 
168
                while(nav->getJDay() > JD)
 
169
                        scriptSleeper.sleep(200);
 
170
        }
 
171
}
 
172
 
 
173
void StelMainScriptAPI::setObserverLocation(double longitude, double latitude, double altitude, double duration, const QString& name, const QString& planet)
 
174
{
 
175
        StelNavigator* nav = StelApp::getInstance().getCore()->getNavigator();
 
176
        Q_ASSERT(nav);
 
177
        SolarSystem* ssmgr = GETSTELMODULE(SolarSystem);
 
178
        Q_ASSERT(ssmgr);
 
179
 
 
180
        StelLocation loc = nav->getCurrentLocation();
 
181
        if (longitude < 180 || longitude > 180)
 
182
                loc.longitude = longitude;
 
183
        if (latitude < 180 || latitude > 180)
 
184
                loc.latitude = latitude;
 
185
        if (altitude < -1000)
 
186
                loc.altitude = altitude;
 
187
        if (ssmgr->searchByName(planet))
 
188
                loc.planetName = planet;
 
189
        loc.name = name;
 
190
 
 
191
        nav->moveObserverTo(loc, duration);
 
192
}
 
193
 
 
194
void StelMainScriptAPI::setObserverLocation(const QString id, double duration)
 
195
{
 
196
        StelNavigator* nav = StelApp::getInstance().getCore()->getNavigator();
 
197
        Q_ASSERT(nav);
 
198
        StelLocation loc = StelApp::getInstance().getLocationMgr().locationForSmallString(id);
 
199
        // How best to test to see if the lookup of the name was a success?
 
200
        // On failure, it returns Paris, but maybe we _want_ Paris.
 
201
        // Ugly. -MNG
 
202
        if (id!="Paris, France" && (loc.name=="Paris" && loc.country=="France"))
 
203
                return; // location find fail
 
204
 
 
205
        nav->moveObserverTo(loc, duration);
 
206
}
 
207
 
 
208
QString StelMainScriptAPI::getObserverLocation()
 
209
{
 
210
        return StelApp::getInstance().getCore()->getNavigator()->getCurrentLocation().getID();
 
211
}
 
212
 
 
213
void StelMainScriptAPI::screenshot(const QString& prefix, bool invert, const QString& dir)
 
214
{
 
215
        bool oldInvertSetting = StelMainGraphicsView::getInstance().getFlagInvertScreenShotColors();
 
216
        StelMainGraphicsView::getInstance().setFlagInvertScreenShotColors(invert);
 
217
        StelMainGraphicsView::getInstance().saveScreenShot(prefix, dir);
 
218
        StelMainGraphicsView::getInstance().setFlagInvertScreenShotColors(oldInvertSetting);
 
219
}
 
220
 
 
221
void StelMainScriptAPI::setGuiVisible(bool b)
 
222
{
 
223
        StelApp::getInstance().getGui()->setVisible(b);
 
224
}
 
225
 
 
226
void StelMainScriptAPI::setMinFps(float m)
 
227
{
 
228
        StelMainGraphicsView::getInstance().setMinFps(m);
 
229
}
 
230
 
 
231
float StelMainScriptAPI::getMinFps()
 
232
{
 
233
        return StelMainGraphicsView::getInstance().getMinFps();
 
234
}
 
235
 
 
236
void StelMainScriptAPI::setMaxFps(float m)
 
237
{
 
238
        StelMainGraphicsView::getInstance().setMaxFps(m);
 
239
}
 
240
 
 
241
float StelMainScriptAPI::getMaxFps()
 
242
{
 
243
        return StelMainGraphicsView::getInstance().getMaxFps();
 
244
}
 
245
 
 
246
QString StelMainScriptAPI::getMountMode()
 
247
{
 
248
        if (GETSTELMODULE(StelMovementMgr)->getMountMode() == StelMovementMgr::MountEquinoxEquatorial)
 
249
                return "equatorial";
 
250
        else
 
251
                return "azimuthal";
 
252
}
 
253
 
 
254
void StelMainScriptAPI::setMountMode(const QString& mode)
 
255
{
 
256
        if (mode=="equatorial")
 
257
                GETSTELMODULE(StelMovementMgr)->setMountMode(StelMovementMgr::MountEquinoxEquatorial);
 
258
        else if (mode=="azimuthal")
 
259
                GETSTELMODULE(StelMovementMgr)->setMountMode(StelMovementMgr::MountAltAzimuthal);
 
260
}
 
261
 
 
262
bool StelMainScriptAPI::getNightMode()
 
263
{
 
264
        return StelApp::getInstance().getVisionModeNight();
 
265
}
 
266
 
 
267
void StelMainScriptAPI::setNightMode(bool b)
 
268
{
 
269
        emit(requestSetNightMode(b));
 
270
}
 
271
 
 
272
QString StelMainScriptAPI::getProjectionMode()
 
273
{
 
274
        return StelApp::getInstance().getCore()->getCurrentProjectionTypeKey();
 
275
}
 
276
 
 
277
void StelMainScriptAPI::setProjectionMode(const QString& id)
 
278
{
 
279
        emit(requestSetProjectionMode(id));
 
280
}
 
281
 
 
282
QStringList StelMainScriptAPI::getAllSkyCultureIDs(void)
 
283
{
 
284
        return StelApp::getInstance().getSkyCultureMgr().getSkyCultureListIDs();
 
285
}
 
286
 
 
287
QString StelMainScriptAPI::getSkyCulture()
 
288
{
 
289
        return StelApp::getInstance().getSkyCultureMgr().getCurrentSkyCultureID();
 
290
}
 
291
 
 
292
void StelMainScriptAPI::setSkyCulture(const QString& id)
 
293
{
 
294
        emit(requestSetSkyCulture(id));
 
295
}
 
296
 
 
297
bool StelMainScriptAPI::getFlagGravityLabels()
 
298
{
 
299
        return StelApp::getInstance().getCore()->getProjection(Mat4d())->getFlagGravityLabels();
 
300
}
 
301
 
 
302
void StelMainScriptAPI::setFlagGravityLabels(bool b)
 
303
{
 
304
        StelApp::getInstance().getCore()->setFlagGravityLabels(b);
 
305
}
 
306
 
 
307
bool StelMainScriptAPI::getDiskViewport()
 
308
{
 
309
        return StelApp::getInstance().getCore()->getProjection(Mat4d())->getMaskType() == StelProjector::MaskDisk;
 
310
}
 
311
 
 
312
void StelMainScriptAPI::setDiskViewport(bool b)
 
313
{
 
314
        emit(requestSetDiskViewport(b));
 
315
}
 
316
 
 
317
void StelMainScriptAPI::loadSkyImage(const QString& id, const QString& filename,
 
318
                                                 double ra0, double dec0,
 
319
                                                 double ra1, double dec1,
 
320
                                                 double ra2, double dec2,
 
321
                                                 double ra3, double dec3,
 
322
                                                                 double minRes, double maxBright, bool visible)
 
323
{
 
324
        QString path = "scripts/" + filename;
 
325
        emit(requestLoadSkyImage(id, path, ra0, dec0, ra1, dec1, ra2, dec2, ra3, dec3, minRes, maxBright, visible));
 
326
}
 
327
 
 
328
void StelMainScriptAPI::loadSkyImage(const QString& id, const QString& filename,
 
329
                                                                         const QString& ra0, const QString& dec0,
 
330
                                                                         const QString& ra1, const QString& dec1,
 
331
                                                                         const QString& ra2, const QString& dec2,
 
332
                                                                         const QString& ra3, const QString& dec3,
 
333
                                                                         double minRes, double maxBright, bool visible)
 
334
{
 
335
        loadSkyImage(id, filename,
 
336
                                 StelUtils::getDecAngle(ra0) *180./M_PI, StelUtils::getDecAngle(dec0)*180./M_PI,
 
337
                                 StelUtils::getDecAngle(ra1) *180./M_PI, StelUtils::getDecAngle(dec1)*180./M_PI,
 
338
                                 StelUtils::getDecAngle(ra2) *180./M_PI, StelUtils::getDecAngle(dec2)*180./M_PI,
 
339
                                 StelUtils::getDecAngle(ra3) *180./M_PI, StelUtils::getDecAngle(dec3)*180./M_PI,
 
340
                                 minRes, maxBright, visible);
 
341
}
 
342
 
 
343
void StelMainScriptAPI::loadSkyImage(const QString& id, const QString& filename,
 
344
                                                                         double ra, double dec, double angSize, double rotation,
 
345
                                                                         double minRes, double maxBright, bool visible)
 
346
{
 
347
        Vec3d XYZ;
 
348
        const double RADIUS_NEB = 1.;
 
349
        StelUtils::spheToRect(ra*M_PI/180., dec*M_PI/180., XYZ);
 
350
        XYZ*=RADIUS_NEB;
 
351
        double texSize = RADIUS_NEB * sin(angSize/2/60*M_PI/180);
 
352
        Mat4f matPrecomp = Mat4f::translation(XYZ) *
 
353
                                           Mat4f::zrotation(ra*M_PI/180.) *
 
354
                                           Mat4f::yrotation(-dec*M_PI/180.) *
 
355
                                           Mat4f::xrotation(rotation*M_PI/180.);
 
356
 
 
357
        Vec3d corners[4];
 
358
                corners[0] = matPrecomp * Vec3d(0.,-texSize,-texSize);
 
359
                corners[1] = matPrecomp * Vec3d(0., texSize,-texSize);
 
360
                corners[2] = matPrecomp * Vec3d(0.,-texSize, texSize);
 
361
                corners[3] = matPrecomp * Vec3d(0., texSize, texSize);
 
362
 
 
363
        // convert back to ra/dec (radians)
 
364
        Vec3d cornersRaDec[4];
 
365
        for(int i=0; i<4; i++)
 
366
                StelUtils::rectToSphe(&cornersRaDec[i][0], &cornersRaDec[i][1], corners[i]);
 
367
 
 
368
        loadSkyImage(id, filename,
 
369
                                 cornersRaDec[0][0]*180./M_PI, cornersRaDec[0][1]*180./M_PI,
 
370
                                 cornersRaDec[1][0]*180./M_PI, cornersRaDec[1][1]*180./M_PI,
 
371
                                 cornersRaDec[3][0]*180./M_PI, cornersRaDec[3][1]*180./M_PI,
 
372
                                 cornersRaDec[2][0]*180./M_PI, cornersRaDec[2][1]*180./M_PI,
 
373
                                 minRes, maxBright, visible);
 
374
}
 
375
 
 
376
void StelMainScriptAPI::loadSkyImage(const QString& id, const QString& filename,
 
377
                                                                         const QString& ra, const QString& dec, double angSize, double rotation,
 
378
                                                                         double minRes, double maxBright, bool visible)
 
379
{
 
380
        loadSkyImage(id, filename, StelUtils::getDecAngle(ra)*180./M_PI,
 
381
                                 StelUtils::getDecAngle(dec)*180./M_PI, angSize,
 
382
                                 rotation, minRes, maxBright, visible);
 
383
}
 
384
 
 
385
void StelMainScriptAPI::removeSkyImage(const QString& id)
 
386
{
 
387
        emit(requestRemoveSkyImage(id));
 
388
}
 
389
 
 
390
void StelMainScriptAPI::loadSound(const QString& filename, const QString& id)
 
391
{
 
392
        QString path;
 
393
        try
 
394
        {
 
395
                path = StelFileMgr::findFile("scripts/" + filename);
 
396
        }
 
397
        catch(std::runtime_error& e)
 
398
        {
 
399
                qWarning() << "cannot play sound" << filename << ":" << e.what();
 
400
                return;
 
401
        }
 
402
 
 
403
        emit(requestLoadSound(path, id));
 
404
}
 
405
 
 
406
void StelMainScriptAPI::playSound(const QString& id)
 
407
{
 
408
        emit(requestPlaySound(id));
 
409
}
 
410
 
 
411
void StelMainScriptAPI::pauseSound(const QString& id)
 
412
{
 
413
        emit(requestPauseSound(id));
 
414
}
 
415
 
 
416
void StelMainScriptAPI::stopSound(const QString& id)
 
417
{
 
418
        emit(requestStopSound(id));
 
419
}
 
420
 
 
421
void StelMainScriptAPI::dropSound(const QString& id)
 
422
{
 
423
        emit(requestDropSound(id));
 
424
}
 
425
 
 
426
int StelMainScriptAPI::getScreenWidth(void)
 
427
{
 
428
        return StelMainGraphicsView::getInstance().size().width();
 
429
}
 
430
 
 
431
int StelMainScriptAPI::getScreenHeight(void)
 
432
{
 
433
        return StelMainGraphicsView::getInstance().size().height();
 
434
}
 
435
 
 
436
double StelMainScriptAPI::getScriptRate(void)
 
437
{
 
438
        return scriptSleeper.getRate();
 
439
}
 
440
 
 
441
void StelMainScriptAPI::setScriptRate(double r)
 
442
{
 
443
        return scriptSleeper.setRate(r);
 
444
}
 
445
 
 
446
void StelMainScriptAPI::setSelectedObjectInfo(const QString& level)
 
447
{
 
448
        if (level == "AllInfo")
 
449
                StelApp::getInstance().getGui()->setInfoTextFilters(StelObject::InfoStringGroup(StelObject::AllInfo));
 
450
        else if (level == "ShortInfo")
 
451
                StelApp::getInstance().getGui()->setInfoTextFilters(StelObject::InfoStringGroup(StelObject::ShortInfo));
 
452
        else if (level == "None")
 
453
                StelApp::getInstance().getGui()->setInfoTextFilters((StelObject::InfoStringGroup)0);
 
454
        else
 
455
                qWarning() << "setSelectedObjectInfo unknown level string \"" << level << "\"";
 
456
}
 
457
 
 
458
void StelMainScriptAPI::exit(void)
 
459
{
 
460
        emit(requestExit());
 
461
}
 
462
 
 
463
void StelMainScriptAPI::quitStellarium(void)
 
464
{
 
465
        QCoreApplication::exit();
 
466
}
 
467
 
 
468
void StelMainScriptAPI::debug(const QString& s)
 
469
{
 
470
        qDebug() << "script: " << s;
 
471
        StelMainGraphicsView::getInstance().getScriptMgr().debug(s);
 
472
}
 
473
 
 
474
double StelMainScriptAPI::jdFromDateString(const QString& dt, const QString& spec)
 
475
{
 
476
        QDateTime qdt;
 
477
        double JD;
 
478
 
 
479
        // 2008-03-24T13:21:01
 
480
        QRegExp isoRe("^\\d{4}[:\\-]\\d\\d[:\\-]\\d\\dT\\d?\\d:\\d\\d:\\d\\d$");
 
481
        QRegExp nowRe("^(now)?(\\s*([+\\-])\\s*(\\d+(\\.\\d+)?)\\s*(second|seconds|minute|minutes|hour|hours|day|days|week|weeks))(\\s+(sidereal)?)?");
 
482
 
 
483
        if (dt == "now")
 
484
                return StelUtils::getJDFromSystem();
 
485
        else if (isoRe.exactMatch(dt))
 
486
        {
 
487
                qdt = QDateTime::fromString(dt, Qt::ISODate);
 
488
 
 
489
                if (spec=="local")
 
490
                        JD = StelUtils::qDateTimeToJd(qdt.toUTC());
 
491
                else
 
492
                        JD = StelUtils::qDateTimeToJd(qdt);
 
493
 
 
494
                return JD;
 
495
        }
 
496
        else if (nowRe.exactMatch(dt))
 
497
        {
 
498
                double delta;
 
499
                double unit;
 
500
                double dayLength = 1.0;
 
501
 
 
502
                if (nowRe.capturedTexts().at(1)=="now")
 
503
                        JD = StelUtils::getJDFromSystem();
 
504
                else
 
505
                        JD = StelApp::getInstance().getCore()->getNavigator()->getJDay();
 
506
 
 
507
                if (nowRe.capturedTexts().at(8) == "sidereal")
 
508
                        dayLength = StelApp::getInstance().getCore()->getNavigator()->getLocalSideralDayLength();
 
509
 
 
510
                QString unitString = nowRe.capturedTexts().at(6);
 
511
                if (unitString == "seconds" || unitString == "second")
 
512
                        unit = dayLength / (24*3600.);
 
513
                else if (unitString == "minutes" || unitString == "minute")
 
514
                        unit = dayLength / (24*60.);
 
515
                else if (unitString == "hours" || unitString == "hour")
 
516
                        unit = dayLength / (24.);
 
517
                else if (unitString == "days" || unitString == "day")
 
518
                        unit = dayLength;
 
519
                else if (unitString == "weeks" || unitString == "week")
 
520
                        unit = dayLength * 7.;
 
521
                else
 
522
                {
 
523
                        qWarning() << "StelMainScriptAPI::setDate - unknown time unit:" << nowRe.capturedTexts().at(4);
 
524
                        unit = 0;
 
525
                }
 
526
 
 
527
                delta = nowRe.capturedTexts().at(4).toDouble();
 
528
 
 
529
                if (nowRe.capturedTexts().at(3) == "+")
 
530
                        JD += (unit * delta);
 
531
                else if (nowRe.capturedTexts().at(3) == "-")
 
532
                        JD -= (unit * delta);
 
533
 
 
534
                return JD;
 
535
        }
 
536
        else
 
537
        {
 
538
                qWarning() << "StelMainScriptAPI::jdFromDateString error - date string" << dt << "not recognised, returning \"now\"";
 
539
                return StelUtils::getJDFromSystem();
 
540
        }
 
541
}
 
542
 
 
543
void StelMainScriptAPI::selectObjectByName(const QString& name, bool pointer)
 
544
{
 
545
        StelObjectMgr* omgr = GETSTELMODULE(StelObjectMgr);
 
546
        omgr->setFlagSelectedObjectPointer(pointer);
 
547
        if (name=="")
 
548
                omgr->unSelect();
 
549
        else
 
550
                omgr->findAndSelect(name);
 
551
}
 
552
 
 
553
QVariantMap StelMainScriptAPI::getObjectPosition(const QString& name)
 
554
{
 
555
        StelObjectMgr* omgr = GETSTELMODULE(StelObjectMgr);
 
556
        StelObjectP obj = omgr->searchByName(name);
 
557
        QVariantMap map;
 
558
 
 
559
        if (!obj)
 
560
        {
 
561
                debug("getObjectData WARNING - object not found: " + name);
 
562
                map.insert("found", false);
 
563
                return map;
 
564
        }
 
565
        else
 
566
        {
 
567
                map.insert("found", true);
 
568
        }
 
569
 
 
570
        StelNavigator* nav = StelApp::getInstance().getCore()->getNavigator();
 
571
        Vec3d pos;
 
572
        double ra, dec, alt, azi;
 
573
 
 
574
        // ra/dec
 
575
        pos = obj->getEquinoxEquatorialPos(nav);
 
576
        StelUtils::rectToSphe(&ra, &dec, pos);
 
577
        map.insert("ra", ra*180./M_PI);
 
578
        map.insert("dec", dec*180./M_PI);
 
579
 
 
580
        // ra/dec in J2000
 
581
        pos = obj->getJ2000EquatorialPos(nav);
 
582
        StelUtils::rectToSphe(&ra, &dec, pos);
 
583
        map.insert("raJ2000", ra*180./M_PI);
 
584
        map.insert("decJ2000", dec*180./M_PI);
 
585
 
 
586
        // altitude/azimuth
 
587
        pos = obj->getAltAzPos(nav);
 
588
        StelUtils::rectToSphe(&azi, &alt, pos);
 
589
        map.insert("altitude", alt*180./M_PI);
 
590
        map.insert("azimuth", azi*180./M_PI);
 
591
 
 
592
        return map;
 
593
}
 
594
 
 
595
void StelMainScriptAPI::clear(const QString& state)
 
596
{
 
597
        LandscapeMgr* lmgr = GETSTELMODULE(LandscapeMgr);
 
598
        SolarSystem* ssmgr = GETSTELMODULE(SolarSystem);
 
599
        MeteorMgr* mmgr = GETSTELMODULE(MeteorMgr);
 
600
        StelSkyDrawer* skyd = StelApp::getInstance().getCore()->getSkyDrawer();
 
601
        ConstellationMgr* cmgr = GETSTELMODULE(ConstellationMgr);
 
602
        StarMgr* smgr = GETSTELMODULE(StarMgr);
 
603
        NebulaMgr* nmgr = GETSTELMODULE(NebulaMgr);
 
604
        GridLinesMgr* glmgr = GETSTELMODULE(GridLinesMgr);
 
605
        StelMovementMgr* movmgr = GETSTELMODULE(StelMovementMgr);
 
606
 
 
607
        if (state.toLower() == "natural")
 
608
        {
 
609
                movmgr->setMountMode(StelMovementMgr::MountAltAzimuthal);
 
610
                skyd->setFlagTwinkle(true);
 
611
                skyd->setFlagLuminanceAdaptation(true);
 
612
                ssmgr->setFlagPlanets(true);
 
613
                ssmgr->setFlagHints(false);
 
614
                ssmgr->setFlagOrbits(false);
 
615
                ssmgr->setFlagMoonScale(false);
 
616
                ssmgr->setFlagTrails(false);
 
617
                mmgr->setZHR(10);
 
618
                glmgr->setFlagAzimuthalGrid(false);
 
619
                glmgr->setFlagEquatorGrid(false);
 
620
                glmgr->setFlagEquatorLine(false);
 
621
                glmgr->setFlagEclipticLine(false);
 
622
                glmgr->setFlagMeridianLine(false);
 
623
                glmgr->setFlagEquatorJ2000Grid(false);
 
624
                lmgr->setFlagCardinalsPoints(false);
 
625
                cmgr->setFlagLines(false);
 
626
                cmgr->setFlagLabels(false);
 
627
                cmgr->setFlagBoundaries(false);
 
628
                cmgr->setFlagArt(false);
 
629
                smgr->setFlagLabels(false);
 
630
                ssmgr->setFlagLabels(false);
 
631
                nmgr->setFlagHints(false);
 
632
                lmgr->setFlagLandscape(true);
 
633
                lmgr->setFlagAtmosphere(true);
 
634
                lmgr->setFlagFog(true);
 
635
        }
 
636
        else if (state.toLower() == "starchart")
 
637
        {
 
638
                movmgr->setMountMode(StelMovementMgr::MountEquinoxEquatorial);
 
639
                skyd->setFlagTwinkle(false);
 
640
                skyd->setFlagLuminanceAdaptation(false);
 
641
                ssmgr->setFlagPlanets(true);
 
642
                ssmgr->setFlagHints(false);
 
643
                ssmgr->setFlagOrbits(false);
 
644
                ssmgr->setFlagMoonScale(false);
 
645
                ssmgr->setFlagTrails(false);
 
646
                mmgr->setZHR(0);
 
647
                glmgr->setFlagAzimuthalGrid(false);
 
648
                glmgr->setFlagEquatorGrid(true);
 
649
                glmgr->setFlagEquatorLine(false);
 
650
                glmgr->setFlagEclipticLine(false);
 
651
                glmgr->setFlagMeridianLine(false);
 
652
                glmgr->setFlagEquatorJ2000Grid(false);
 
653
                lmgr->setFlagCardinalsPoints(false);
 
654
                cmgr->setFlagLines(true);
 
655
                cmgr->setFlagLabels(true);
 
656
                cmgr->setFlagBoundaries(true);
 
657
                cmgr->setFlagArt(false);
 
658
                smgr->setFlagLabels(true);
 
659
                ssmgr->setFlagLabels(true);
 
660
                nmgr->setFlagHints(true);
 
661
                lmgr->setFlagLandscape(false);
 
662
                lmgr->setFlagAtmosphere(false);
 
663
                lmgr->setFlagFog(false);
 
664
        }
 
665
        else if (state.toLower() == "deepspace")
 
666
        {
 
667
                movmgr->setMountMode(StelMovementMgr::MountEquinoxEquatorial);
 
668
                skyd->setFlagTwinkle(false);
 
669
                skyd->setFlagLuminanceAdaptation(false);
 
670
                ssmgr->setFlagPlanets(false);
 
671
                ssmgr->setFlagHints(false);
 
672
                ssmgr->setFlagOrbits(false);
 
673
                ssmgr->setFlagMoonScale(false);
 
674
                ssmgr->setFlagTrails(false);
 
675
                mmgr->setZHR(0);
 
676
                glmgr->setFlagAzimuthalGrid(false);
 
677
                glmgr->setFlagEquatorGrid(false);
 
678
                glmgr->setFlagEquatorLine(false);
 
679
                glmgr->setFlagEclipticLine(false);
 
680
                glmgr->setFlagMeridianLine(false);
 
681
                glmgr->setFlagEquatorJ2000Grid(false);
 
682
                lmgr->setFlagCardinalsPoints(false);
 
683
                cmgr->setFlagLines(false);
 
684
                cmgr->setFlagLabels(false);
 
685
                cmgr->setFlagBoundaries(false);
 
686
                cmgr->setFlagArt(false);
 
687
                smgr->setFlagLabels(false);
 
688
                ssmgr->setFlagLabels(false);
 
689
                nmgr->setFlagHints(false);
 
690
                lmgr->setFlagLandscape(false);
 
691
                lmgr->setFlagAtmosphere(false);
 
692
                lmgr->setFlagFog(false);
 
693
        }
 
694
        else
 
695
        {
 
696
                qWarning() << "WARNING clear(" << state << ") - state not known";
 
697
        }
 
698
}
 
699
 
 
700
double StelMainScriptAPI::getViewAltitudeAngle()
 
701
{
 
702
        const Vec3d& current = StelApp::getInstance().getCore()->getNavigator()->j2000ToAltAz(GETSTELMODULE(StelMovementMgr)->getViewDirectionJ2000());
 
703
        double alt, azi;
 
704
        StelUtils::rectToSphe(&azi, &alt, current);
 
705
        return alt*180/M_PI; // convert to degrees from radians
 
706
}
 
707
 
 
708
double StelMainScriptAPI::getViewAzimuthAngle()
 
709
{
 
710
        const Vec3d& current = StelApp::getInstance().getCore()->getNavigator()->j2000ToAltAz(GETSTELMODULE(StelMovementMgr)->getViewDirectionJ2000());
 
711
        double alt, azi;
 
712
        StelUtils::rectToSphe(&azi, &alt, current);
 
713
        // The returned azimuth angle is in radians and set up such that:
 
714
        // N=+/-PI; E=PI/2; S=0; W=-PI/2;
 
715
        // But we want compass bearings, i.e. N=0, E=90, S=180, W=270
 
716
        return std::fmod(((azi*180/M_PI)*-1)+180., 360.);
 
717
}
 
718
 
 
719
double StelMainScriptAPI::getViewRaAngle()
 
720
{
 
721
        const Vec3d& current = StelApp::getInstance().getCore()->getNavigator()->j2000ToEquinoxEqu(GETSTELMODULE(StelMovementMgr)->getViewDirectionJ2000());
 
722
        double ra, dec;
 
723
        StelUtils::rectToSphe(&ra, &dec, current);
 
724
        // returned RA angle is in range -PI .. PI, but we want 0 .. 360
 
725
        return std::fmod((ra*180/M_PI)+360., 360.); // convert to degrees from radians
 
726
}
 
727
 
 
728
double StelMainScriptAPI::getViewDecAngle()
 
729
{
 
730
        const Vec3d& current = StelApp::getInstance().getCore()->getNavigator()->j2000ToEquinoxEqu(GETSTELMODULE(StelMovementMgr)->getViewDirectionJ2000());
 
731
        double ra, dec;
 
732
        StelUtils::rectToSphe(&ra, &dec, current);
 
733
        return dec*180/M_PI; // convert to degrees from radians
 
734
}
 
735
 
 
736
double StelMainScriptAPI::getViewRaJ2000Angle()
 
737
{
 
738
        Vec3d current = GETSTELMODULE(StelMovementMgr)->getViewDirectionJ2000();
 
739
        double ra, dec;
 
740
        StelUtils::rectToSphe(&ra, &dec, current);
 
741
        // returned RA angle is in range -PI .. PI, but we want 0 .. 360
 
742
        return std::fmod((ra*180/M_PI)+360., 360.); // convert to degrees from radians
 
743
}
 
744
 
 
745
double StelMainScriptAPI::getViewDecJ2000Angle()
 
746
{
 
747
        Vec3d current = GETSTELMODULE(StelMovementMgr)->getViewDirectionJ2000();
 
748
        double ra, dec;
 
749
        StelUtils::rectToSphe(&ra, &dec, current);
 
750
        return dec*180/M_PI; // convert to degrees from radians
 
751
}
 
752
 
 
753
void StelMainScriptAPI::moveToAltAzi(const QString& alt, const QString& azi, float duration)
 
754
{
 
755
        StelMovementMgr* mvmgr = GETSTELMODULE(StelMovementMgr);
 
756
        Q_ASSERT(mvmgr);
 
757
 
 
758
        GETSTELMODULE(StelObjectMgr)->unSelect();
 
759
 
 
760
        Vec3d aim;
 
761
        double dAlt = StelUtils::getDecAngle(alt);
 
762
        double dAzi = M_PI - StelUtils::getDecAngle(azi);
 
763
 
 
764
        StelUtils::spheToRect(dAzi,dAlt,aim);
 
765
        mvmgr->moveToJ2000(StelApp::getInstance().getCore()->getNavigator()->altAzToJ2000(aim), duration);
 
766
}
 
767
 
 
768
void StelMainScriptAPI::moveToRaDec(const QString& ra, const QString& dec, float duration)
 
769
{
 
770
        StelMovementMgr* mvmgr = GETSTELMODULE(StelMovementMgr);
 
771
        Q_ASSERT(mvmgr);
 
772
 
 
773
        GETSTELMODULE(StelObjectMgr)->unSelect();
 
774
 
 
775
        Vec3d aim;
 
776
        double dRa = StelUtils::getDecAngle(ra);
 
777
        double dDec = StelUtils::getDecAngle(dec);
 
778
 
 
779
        StelUtils::spheToRect(dRa,dDec,aim);
 
780
        mvmgr->moveToJ2000(StelApp::getInstance().getCore()->getNavigator()->equinoxEquToJ2000(aim), duration);
 
781
}
 
782
 
 
783
void StelMainScriptAPI::moveToRaDecJ2000(const QString& ra, const QString& dec, float duration)
 
784
{
 
785
        StelMovementMgr* mvmgr = GETSTELMODULE(StelMovementMgr);
 
786
        Q_ASSERT(mvmgr);
 
787
 
 
788
        GETSTELMODULE(StelObjectMgr)->unSelect();
 
789
 
 
790
        Vec3d aimJ2000, aimEquofDate;
 
791
        double dRa = StelUtils::getDecAngle(ra);
 
792
        double dDec = StelUtils::getDecAngle(dec);
 
793
 
 
794
        StelUtils::spheToRect(dRa,dDec,aimJ2000);
 
795
        aimEquofDate = StelApp::getInstance().getCore()->getNavigator()->j2000ToEquinoxEqu(aimJ2000);
 
796
        mvmgr->moveToJ2000(aimEquofDate, duration);
 
797
}
 
798