ksutils.cpp 57.8 KB
Newer Older
Chris Howells's avatar
adding  
Chris Howells committed
1 2 3 4
/***************************************************************************
                          ksutils.cpp  -  K Desktop Planetarium
                             -------------------
    begin                : Mon Jan  7 10:48:09 EST 2002
5 6
    copyright            : (C) 2002 by Mark Hollomon
    email                : mhh@mindspring.com
Chris Howells's avatar
adding  
Chris Howells committed
7 8 9 10 11 12 13 14 15 16 17 18
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "ksutils.h"
19
#include "config-kstars.h"
20
#include "ksnotification.h"
21
#include "kstars_debug.h"
Chris Howells's avatar
adding  
Chris Howells committed
22

23
#include "deepskyobject.h"
Robert Lancaster's avatar
Robert Lancaster committed
24
#ifndef KSTARS_LITE
25
#include "kswizard.h"
Robert Lancaster's avatar
Robert Lancaster committed
26
#endif
Csaba Kertesz's avatar
Csaba Kertesz committed
27
#include "Options.h"
28
#include "starobject.h"
29
#include "auxiliary/kspaths.h"
30

31 32 33 34
#ifndef KSTARS_LITE
#include <KMessageBox>
#endif

35 36 37 38
#ifdef HAVE_LIBRAW
#include <libraw/libraw.h>
#endif

39 40 41 42
#ifdef HAVE_STELLARSOLVER
#include <stellarsolver.h>
#endif

Csaba Kertesz's avatar
Csaba Kertesz committed
43
#include <QPointer>
44
#include <QProcessEnvironment>
45
#include <QLoggingCategory>
Robert Lancaster's avatar
Robert Lancaster committed
46

47 48
namespace KSUtils
{
49 50 51 52 53 54 55 56 57 58

bool isHardwareLimited()
{
#ifdef __arm__
    return true;
#else
    return false;
#endif
}

59
bool openDataFile(QFile &file, const QString &s)
60
{
61 62
    QString FileName = KSPaths::locate(QStandardPaths::GenericDataLocation, s);
    if (!FileName.isNull())
63
    {
64 65
        file.setFileName(FileName);
        return file.open(QIODevice::ReadOnly);
66
    }
67
    return false;
Chris Howells's avatar
adding  
Chris Howells committed
68
}
69

70
QString getDSSURL(const SkyPoint *const p)
71 72 73
{
    double height, width;
    double dss_default_size = Options::defaultDSSImageSize();
74
    double dss_padding      = Options::dSSPadding();
75

76 77
    Q_ASSERT(p);
    Q_ASSERT(dss_default_size > 0.0 && dss_padding >= 0.0);
78

Jasem Mutlaq's avatar
Jasem Mutlaq committed
79
    const DeepSkyObject *dso = dynamic_cast<const DeepSkyObject *>(p);
80 81

    // Decide what to do about the height and width
82
    if (dso)
83 84 85 86
    {
        // For deep-sky objects, use their height and width information
        double a, b, pa;
        a = dso->a();
87 88
        b = dso->a() *
            dso->e(); // Use a * e instead of b, since e() returns 1 whenever one of the dimensions is zero. This is important for circular objects
89 90 91 92 93 94
        pa = dso->pa() * dms::DegToRad;

        // We now want to convert a, b, and pa into an image
        // height and width -- i.e. a dRA and dDec.
        // DSS uses dDec for height and dRA for width. (i.e. "top" is north in the DSS images, AFAICT)
        // From some trigonometry, assuming we have a rectangular object (worst case), we need:
95 96
        width  = a * sin(pa) + b * cos(pa);
        height = a * cos(pa) + b * sin(pa);
97 98 99 100 101 102 103 104 105 106 107 108 109
        // 'a' and 'b' are in arcminutes, so height and width are in arcminutes

        // Pad the RA and Dec, so that we show more of the sky than just the object.
        height += dss_padding;
        width += dss_padding;
    }
    else
    {
        // For a generic sky object, we don't know what to do. So
        // we just assume the default size.
        height = width = dss_default_size;
    }
    // There's no point in tiny DSS images that are smaller than dss_default_size
110
    if (height < dss_default_size)
111
        height = dss_default_size;
112
    if (width < dss_default_size)
113 114
        width = dss_default_size;

115
    return getDSSURL(p->ra0(), p->dec0(), width, height);
116 117
}

118
QString getDSSURL(const dms &ra, const dms &dec, float width, float height, const QString &type)
119
{
120 121
    const QString URLprefix("http://archive.stsci.edu/cgi-bin/dss_search?");
    QString URLsuffix             = QString("&e=J2000&f=%1&c=none&fov=NONE").arg(type);
122
    const double dss_default_size = Options::defaultDSSImageSize();
123

124 125 126 127
    char decsgn = (dec.Degrees() < 0.0) ? '-' : '+';
    int dd      = abs(dec.degree());
    int dm      = abs(dec.arcmin());
    int ds      = abs(dec.arcsec());
128

Vijay Dhameliya's avatar
Vijay Dhameliya committed
129
    // Infinite and NaN sizes are replaced by the default size and tiny DSS images are resized to default size
130
    if (!qIsFinite(height) || height <= 0.0)
131
        height = dss_default_size;
132
    if (!qIsFinite(width) || width <= 0.0)
133 134 135
        width = dss_default_size;

    // DSS accepts images that are no larger than 75 arcminutes
136
    if (height > 75.0)
137
        height = 75.0;
138
    if (width > 75.0)
139 140 141
        width = 75.0;

    QString RAString, DecString, SizeString;
142 143 144
    DecString  = QString::asprintf("&d=%c%02d+%02d+%02d", decsgn, dd, dm, ds);
    RAString   = QString::asprintf("r=%02d+%02d+%02d", ra.hour(), ra.minute(), ra.second());
    SizeString = QString::asprintf("&h=%02.1f&w=%02.1f", height, width);
145

146
    return (URLprefix + RAString + DecString + SizeString + URLsuffix);
147
}
148

149
QString toDirectionString(dms angle)
150
{
151 152 153 154 155 156
    // TODO: Instead of doing it this way, it would be nicer to
    // compute the string to arbitrary precision. Although that will
    // not be easy to localize.  (Consider, for instance, Indian
    // languages that have special names for the intercardinal points)
    // -- asimha

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    static const char *directions[] = { I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "N"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "NNE"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "NE"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "ENE"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "E"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "ESE"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "SE"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "SSE"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "S"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "SSW"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "SW"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "WSW"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "W"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "WNW"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "NW"),
                                        I18N_NOOP2("Abbreviated cardinal / intercardinal etc. direction", "NNW"),
173 174
                                        I18N_NOOP2("Unknown cardinal / intercardinal direction", "???")
                                      };
175 176

    int index = (int)((angle.reduce().Degrees() + 11.25) / 22.5); // A number between 0 and 16 (inclusive) is expected
177

178
    if (index < 0 || index > 16)
179
        index = 16; // Something went wrong.
Akarsh Simha's avatar
Akarsh Simha committed
180
    else
181
        index = (index == 16 ? 0 : index);
182

183
    return i18nc("Abbreviated cardinal / intercardinal etc. direction", directions[index]);
184
}
185

186
QList<SkyObject *> *castStarObjListToSkyObjList(QList<StarObject *> *starObjList)
187
{
188
    QList<SkyObject *> *skyObjList = new QList<SkyObject *>();
189

190
    foreach (StarObject *so, *starObjList)
191
    {
192
        skyObjList->append(so);
193 194
    }
    return skyObjList;
Jasem Mutlaq's avatar
Jasem Mutlaq committed
195 196
}

197
QString constGenetiveFromAbbrev(const QString &code)
198
{
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    if (code == "And")
        return QString("Andromedae");
    if (code == "Ant")
        return QString("Antliae");
    if (code == "Aps")
        return QString("Apodis");
    if (code == "Aqr")
        return QString("Aquarii");
    if (code == "Aql")
        return QString("Aquilae");
    if (code == "Ara")
        return QString("Arae");
    if (code == "Ari")
        return QString("Arietis");
    if (code == "Aur")
        return QString("Aurigae");
    if (code == "Boo")
        return QString("Bootis");
    if (code == "Cae")
        return QString("Caeli");
    if (code == "Cam")
        return QString("Camelopardalis");
    if (code == "Cnc")
        return QString("Cancri");
    if (code == "CVn")
        return QString("Canum Venaticorum");
    if (code == "CMa")
        return QString("Canis Majoris");
    if (code == "CMi")
        return QString("Canis Minoris");
    if (code == "Cap")
        return QString("Capricorni");
    if (code == "Car")
        return QString("Carinae");
    if (code == "Cas")
        return QString("Cassiopeiae");
    if (code == "Cen")
        return QString("Centauri");
    if (code == "Cep")
        return QString("Cephei");
    if (code == "Cet")
        return QString("Ceti");
    if (code == "Cha")
        return QString("Chamaeleontis");
    if (code == "Cir")
        return QString("Circini");
    if (code == "Col")
        return QString("Columbae");
    if (code == "Com")
        return QString("Comae Berenices");
    if (code == "CrA")
        return QString("Coronae Austrinae");
    if (code == "CrB")
        return QString("Coronae Borealis");
    if (code == "Crv")
        return QString("Corvi");
    if (code == "Crt")
        return QString("Crateris");
    if (code == "Cru")
        return QString("Crucis");
    if (code == "Cyg")
        return QString("Cygni");
    if (code == "Del")
        return QString("Delphini");
    if (code == "Dor")
        return QString("Doradus");
    if (code == "Dra")
        return QString("Draconis");
    if (code == "Equ")
        return QString("Equulei");
    if (code == "Eri")
        return QString("Eridani");
    if (code == "For")
        return QString("Fornacis");
    if (code == "Gem")
        return QString("Geminorum");
    if (code == "Gru")
        return QString("Gruis");
    if (code == "Her")
        return QString("Herculis");
    if (code == "Hor")
        return QString("Horologii");
    if (code == "Hya")
        return QString("Hydrae");
    if (code == "Hyi")
        return QString("Hydri");
    if (code == "Ind")
        return QString("Indi");
    if (code == "Lac")
        return QString("Lacertae");
    if (code == "Leo")
        return QString("Leonis");
    if (code == "LMi")
        return QString("Leonis Minoris");
    if (code == "Lep")
        return QString("Leporis");
    if (code == "Lib")
        return QString("Librae");
    if (code == "Lup")
        return QString("Lupi");
    if (code == "Lyn")
        return QString("Lyncis");
    if (code == "Lyr")
        return QString("Lyrae");
    if (code == "Men")
        return QString("Mensae");
    if (code == "Mic")
        return QString("Microscopii");
    if (code == "Mon")
        return QString("Monocerotis");
    if (code == "Mus")
        return QString("Muscae");
    if (code == "Nor")
        return QString("Normae");
    if (code == "Oct")
        return QString("Octantis");
    if (code == "Oph")
        return QString("Ophiuchi");
    if (code == "Ori")
        return QString("Orionis");
    if (code == "Pav")
        return QString("Pavonis");
    if (code == "Peg")
        return QString("Pegasi");
    if (code == "Per")
        return QString("Persei");
    if (code == "Phe")
        return QString("Phoenicis");
    if (code == "Pic")
        return QString("Pictoris");
    if (code == "Psc")
        return QString("Piscium");
    if (code == "PsA")
        return QString("Piscis Austrini");
    if (code == "Pup")
        return QString("Puppis");
    if (code == "Pyx")
        return QString("Pyxidis");
    if (code == "Ret")
        return QString("Reticuli");
    if (code == "Sge")
        return QString("Sagittae");
    if (code == "Sgr")
        return QString("Sagittarii");
    if (code == "Sco")
        return QString("Scorpii");
    if (code == "Scl")
        return QString("Sculptoris");
    if (code == "Sct")
        return QString("Scuti");
    if (code == "Ser")
        return QString("Serpentis");
    if (code == "Sex")
        return QString("Sextantis");
    if (code == "Tau")
        return QString("Tauri");
    if (code == "Tel")
        return QString("Telescopii");
    if (code == "Tri")
        return QString("Trianguli");
    if (code == "TrA")
        return QString("Trianguli Australis");
    if (code == "Tuc")
        return QString("Tucanae");
    if (code == "UMa")
        return QString("Ursae Majoris");
    if (code == "UMi")
        return QString("Ursae Minoris");
    if (code == "Vel")
        return QString("Velorum");
    if (code == "Vir")
        return QString("Virginis");
    if (code == "Vol")
        return QString("Volantis");
    if (code == "Vul")
        return QString("Vulpeculae");
375 376 377
    return code;
}

378
QString constNameFromAbbrev(const QString &code)
379
{
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    if (code == "And")
        return QString("Andromeda");
    if (code == "Ant")
        return QString("Antlia");
    if (code == "Aps")
        return QString("Apus");
    if (code == "Aqr")
        return QString("Aquarius");
    if (code == "Aql")
        return QString("Aquila");
    if (code == "Ara")
        return QString("Ara");
    if (code == "Ari")
        return QString("Aries");
    if (code == "Aur")
        return QString("Auriga");
    if (code == "Boo")
        return QString("Bootes");
    if (code == "Cae")
        return QString("Caelum");
    if (code == "Cam")
        return QString("Camelopardalis");
    if (code == "Cnc")
        return QString("Cancer");
    if (code == "CVn")
        return QString("Canes Venatici");
    if (code == "CMa")
        return QString("Canis Major");
    if (code == "CMi")
        return QString("Canis Minor");
    if (code == "Cap")
        return QString("Capricornus");
    if (code == "Car")
        return QString("Carina");
    if (code == "Cas")
        return QString("Cassiopeia");
    if (code == "Cen")
        return QString("Centaurus");
    if (code == "Cep")
        return QString("Cepheus");
    if (code == "Cet")
        return QString("Cetus");
    if (code == "Cha")
        return QString("Chamaeleon");
    if (code == "Cir")
        return QString("Circinus");
    if (code == "Col")
        return QString("Columba");
    if (code == "Com")
        return QString("Coma Berenices");
    if (code == "CrA")
        return QString("Corona Australis");
    if (code == "CrB")
        return QString("Corona Borealis");
    if (code == "Crv")
        return QString("Corvus");
    if (code == "Crt")
        return QString("Crater");
    if (code == "Cru")
        return QString("Crux");
    if (code == "Cyg")
        return QString("Cygnus");
    if (code == "Del")
        return QString("Delphinus");
    if (code == "Dor")
        return QString("Doradus");
    if (code == "Dra")
        return QString("Draco");
    if (code == "Equ")
        return QString("Equuleus");
    if (code == "Eri")
        return QString("Eridanus");
    if (code == "For")
        return QString("Fornax");
    if (code == "Gem")
        return QString("Gemini");
    if (code == "Gru")
        return QString("Grus");
    if (code == "Her")
        return QString("Hercules");
    if (code == "Hor")
        return QString("Horologium");
    if (code == "Hya")
        return QString("Hydra");
    if (code == "Hyi")
        return QString("Hydrus");
    if (code == "Ind")
        return QString("Indus");
    if (code == "Lac")
        return QString("Lacerta");
    if (code == "Leo")
        return QString("Leo");
    if (code == "LMi")
        return QString("Leo Minor");
    if (code == "Lep")
        return QString("Lepus");
    if (code == "Lib")
        return QString("Libra");
    if (code == "Lup")
        return QString("Lupus");
    if (code == "Lyn")
        return QString("Lynx");
    if (code == "Lyr")
        return QString("Lyra");
    if (code == "Men")
        return QString("Mensa");
    if (code == "Mic")
        return QString("Microscopium");
    if (code == "Mon")
        return QString("Monoceros");
    if (code == "Mus")
        return QString("Musca");
    if (code == "Nor")
        return QString("Norma");
    if (code == "Oct")
        return QString("Octans");
    if (code == "Oph")
        return QString("Ophiuchus");
    if (code == "Ori")
        return QString("Orion");
    if (code == "Pav")
        return QString("Pavo");
    if (code == "Peg")
        return QString("Pegasus");
    if (code == "Per")
        return QString("Perseus");
    if (code == "Phe")
        return QString("Phoenix");
    if (code == "Pic")
        return QString("Pictor");
    if (code == "Psc")
        return QString("Pisces");
    if (code == "PsA")
        return QString("Piscis Austrinus");
    if (code == "Pup")
        return QString("Puppis");
    if (code == "Pyx")
        return QString("Pyxis");
    if (code == "Ret")
        return QString("Reticulum");
    if (code == "Sge")
        return QString("Sagitta");
    if (code == "Sgr")
        return QString("Sagittarius");
    if (code == "Sco")
        return QString("Scorpius");
    if (code == "Scl")
        return QString("Sculptor");
    if (code == "Sct")
        return QString("Scutum");
    if (code == "Ser")
        return QString("Serpens");
    if (code == "Sex")
        return QString("Sextans");
    if (code == "Tau")
        return QString("Taurus");
    if (code == "Tel")
        return QString("Telescopium");
    if (code == "Tri")
        return QString("Triangulum");
    if (code == "TrA")
        return QString("Triangulum Australe");
    if (code == "Tuc")
        return QString("Tucana");
    if (code == "UMa")
        return QString("Ursa Major");
    if (code == "UMi")
        return QString("Ursa Minor");
    if (code == "Vel")
        return QString("Vela");
    if (code == "Vir")
        return QString("Virgo");
    if (code == "Vol")
        return QString("Volans");
    if (code == "Vul")
        return QString("Vulpecula");
556 557 558
    return code;
}

559
QString constNameToAbbrev(const QString &fullName_)
560
{
561
    QString fullName = fullName_.toLower();
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
    if (fullName == "andromeda")
        return QString("And");
    if (fullName == "antlia")
        return QString("Ant");
    if (fullName == "apus")
        return QString("Aps");
    if (fullName == "aquarius")
        return QString("Aqr");
    if (fullName == "aquila")
        return QString("Aql");
    if (fullName == "ara")
        return QString("Ara");
    if (fullName == "aries")
        return QString("Ari");
    if (fullName == "auriga")
        return QString("Aur");
    if (fullName == "bootes")
        return QString("Boo");
    if (fullName == "caelum")
        return QString("Cae");
    if (fullName == "camelopardalis")
        return QString("Cam");
    if (fullName == "cancer")
        return QString("Cnc");
    if (fullName == "canes venatici")
        return QString("CVn");
    if (fullName == "canis major")
        return QString("CMa");
    if (fullName == "canis minor")
        return QString("CMi");
    if (fullName == "capricornus")
        return QString("Cap");
    if (fullName == "carina")
        return QString("Car");
    if (fullName == "cassiopeia")
        return QString("Cas");
    if (fullName == "centaurus")
        return QString("Cen");
    if (fullName == "cepheus")
        return QString("Cep");
    if (fullName == "cetus")
        return QString("Cet");
    if (fullName == "chamaeleon")
        return QString("Cha");
    if (fullName == "circinus")
        return QString("Cir");
    if (fullName == "columba")
        return QString("Col");
    if (fullName == "coma berenices")
        return QString("Com");
    if (fullName == "corona australis")
        return QString("CrA");
    if (fullName == "corona borealis")
        return QString("CrB");
    if (fullName == "corvus")
        return QString("Crv");
    if (fullName == "crater")
        return QString("Crt");
    if (fullName == "crux")
        return QString("Cru");
    if (fullName == "cygnus")
        return QString("Cyg");
    if (fullName == "delphinus")
        return QString("Del");
    if (fullName == "doradus")
        return QString("Dor");
    if (fullName == "draco")
        return QString("Dra");
    if (fullName == "equuleus")
        return QString("Equ");
    if (fullName == "eridanus")
        return QString("Eri");
    if (fullName == "fornax")
        return QString("For");
    if (fullName == "gemini")
        return QString("Gem");
    if (fullName == "grus")
        return QString("Gru");
    if (fullName == "hercules")
        return QString("Her");
    if (fullName == "horologium")
        return QString("Hor");
    if (fullName == "hydra")
        return QString("Hya");
    if (fullName == "hydrus")
        return QString("Hyi");
    if (fullName == "indus")
        return QString("Ind");
    if (fullName == "lacerta")
        return QString("Lac");
    if (fullName == "leo")
        return QString("Leo");
    if (fullName == "leo minor")
        return QString("LMi");
    if (fullName == "lepus")
        return QString("Lep");
    if (fullName == "libra")
        return QString("Lib");
    if (fullName == "lupus")
        return QString("Lup");
    if (fullName == "lynx")
        return QString("Lyn");
    if (fullName == "lyra")
        return QString("Lyr");
    if (fullName == "mensa")
        return QString("Men");
    if (fullName == "microscopium")
        return QString("Mic");
    if (fullName == "monoceros")
        return QString("Mon");
    if (fullName == "musca")
        return QString("Mus");
    if (fullName == "norma")
        return QString("Nor");
    if (fullName == "octans")
        return QString("Oct");
    if (fullName == "ophiuchus")
        return QString("Oph");
    if (fullName == "orion")
        return QString("Ori");
    if (fullName == "pavo")
        return QString("Pav");
    if (fullName == "pegasus")
        return QString("Peg");
    if (fullName == "perseus")
        return QString("Per");
    if (fullName == "phoenix")
        return QString("Phe");
    if (fullName == "pictor")
        return QString("Pic");
    if (fullName == "pisces")
        return QString("Psc");
    if (fullName == "piscis austrinus")
        return QString("PsA");
    if (fullName == "puppis")
        return QString("Pup");
    if (fullName == "pyxis")
        return QString("Pyx");
    if (fullName == "reticulum")
        return QString("Ret");
    if (fullName == "sagitta")
        return QString("Sge");
    if (fullName == "sagittarius")
        return QString("Sgr");
    if (fullName == "scorpius")
        return QString("Sco");
    if (fullName == "sculptor")
        return QString("Scl");
    if (fullName == "scutum")
        return QString("Sct");
    if (fullName == "serpens")
        return QString("Ser");
    if (fullName == "sextans")
        return QString("Sex");
    if (fullName == "taurus")
        return QString("Tau");
    if (fullName == "telescopium")
        return QString("Tel");
    if (fullName == "triangulum")
        return QString("Tri");
    if (fullName == "triangulum australe")
        return QString("TrA");
    if (fullName == "tucana")
        return QString("Tuc");
    if (fullName == "ursa major")
        return QString("UMa");
    if (fullName == "ursa minor")
        return QString("UMi");
    if (fullName == "vela")
        return QString("Vel");
    if (fullName == "virgo")
        return QString("Vir");
    if (fullName == "volans")
        return QString("Vol");
    if (fullName == "vulpecula")
        return QString("Vul");
738 739 740
    return fullName_;
}

741
QString constGenetiveToAbbrev(const QString &genetive_)
742
{
743
    QString genetive = genetive_.toLower();
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    if (genetive == "andromedae")
        return QString("And");
    if (genetive == "antliae")
        return QString("Ant");
    if (genetive == "apodis")
        return QString("Aps");
    if (genetive == "aquarii")
        return QString("Aqr");
    if (genetive == "aquilae")
        return QString("Aql");
    if (genetive == "arae")
        return QString("Ara");
    if (genetive == "arietis")
        return QString("Ari");
    if (genetive == "aurigae")
        return QString("Aur");
    if (genetive == "bootis")
        return QString("Boo");
    if (genetive == "caeli")
        return QString("Cae");
    if (genetive == "camelopardalis")
        return QString("Cam");
    if (genetive == "cancri")
        return QString("Cnc");
    if (genetive == "canum venaticorum")
        return QString("CVn");
    if (genetive == "canis majoris")
        return QString("CMa");
    if (genetive == "canis minoris")
        return QString("CMi");
    if (genetive == "capricorni")
        return QString("Cap");
    if (genetive == "carinae")
        return QString("Car");
    if (genetive == "cassiopeiae")
        return QString("Cas");
    if (genetive == "centauri")
        return QString("Cen");
    if (genetive == "cephei")
        return QString("Cep");
    if (genetive == "ceti")
        return QString("Cet");
    if (genetive == "chamaeleontis")
        return QString("Cha");
    if (genetive == "circini")
        return QString("Cir");
    if (genetive == "columbae")
        return QString("Col");
    if (genetive == "comae berenices")
        return QString("Com");
    if (genetive == "coronae austrinae")
        return QString("CrA");
    if (genetive == "coronae borealis")
        return QString("CrB");
    if (genetive == "corvi")
        return QString("Crv");
    if (genetive == "crateris")
        return QString("Crt");
    if (genetive == "crucis")
        return QString("Cru");
    if (genetive == "cygni")
        return QString("Cyg");
    if (genetive == "delphini")
        return QString("Del");
    if (genetive == "doradus")
        return QString("Dor");
    if (genetive == "draconis")
        return QString("Dra");
    if (genetive == "equulei")
        return QString("Equ");
    if (genetive == "eridani")
        return QString("Eri");
    if (genetive == "fornacis")
        return QString("For");
    if (genetive == "geminorum")
        return QString("Gem");
    if (genetive == "gruis")
        return QString("Gru");
    if (genetive == "herculis")
        return QString("Her");
    if (genetive == "horologii")
        return QString("Hor");
    if (genetive == "hydrae")
        return QString("Hya");
    if (genetive == "hydri")
        return QString("Hyi");
    if (genetive == "indi")
        return QString("Ind");
    if (genetive == "lacertae")
        return QString("Lac");
    if (genetive == "leonis")
        return QString("Leo");
    if (genetive == "leonis minoris")
        return QString("LMi");
    if (genetive == "leporis")
        return QString("Lep");
    if (genetive == "librae")
        return QString("Lib");
    if (genetive == "lupi")
        return QString("Lup");
    if (genetive == "lyncis")
        return QString("Lyn");
    if (genetive == "lyrae")
        return QString("Lyr");
    if (genetive == "mensae")
        return QString("Men");
    if (genetive == "microscopii")
        return QString("Mic");
    if (genetive == "monocerotis")
        return QString("Mon");
    if (genetive == "muscae")
        return QString("Mus");
    if (genetive == "normae")
        return QString("Nor");
    if (genetive == "octantis")
        return QString("Oct");
    if (genetive == "ophiuchi")
        return QString("Oph");
    if (genetive == "orionis")
        return QString("Ori");
    if (genetive == "pavonis")
        return QString("Pav");
    if (genetive == "pegasi")
        return QString("Peg");
    if (genetive == "persei")
        return QString("Per");
    if (genetive == "phoenicis")
        return QString("Phe");
    if (genetive == "pictoris")
        return QString("Pic");
    if (genetive == "piscium")
        return QString("Psc");
    if (genetive == "piscis austrini")
        return QString("PsA");
    if (genetive == "puppis")
        return QString("Pup");
    if (genetive == "pyxidis")
        return QString("Pyx");
    if (genetive == "reticuli")
        return QString("Ret");
    if (genetive == "sagittae")
        return QString("Sge");
    if (genetive == "sagittarii")
        return QString("Sgr");
    if (genetive == "scorpii")
        return QString("Sco");
    if (genetive == "sculptoris")
        return QString("Scl");
    if (genetive == "scuti")
        return QString("Sct");
    if (genetive == "serpentis")
        return QString("Ser");
    if (genetive == "sextantis")
        return QString("Sex");
    if (genetive == "tauri")
        return QString("Tau");
    if (genetive == "telescopii")
        return QString("Tel");
    if (genetive == "trianguli")
        return QString("Tri");
    if (genetive == "trianguli australis")
        return QString("TrA");
    if (genetive == "tucanae")
        return QString("Tuc");
    if (genetive == "ursae majoris")
        return QString("UMa");
    if (genetive == "ursae minoris")
        return QString("UMi");
    if (genetive == "velorum")
        return QString("Vel");
    if (genetive == "virginis")
        return QString("Vir");
    if (genetive == "volantis")
        return QString("Vol");
    if (genetive == "vulpeculae")
        return QString("Vul");
920 921 922
    return genetive_;
}

923
QString Logging::_filename;
Jasem Mutlaq's avatar
Jasem Mutlaq committed
924

925 926 927 928 929
void Logging::UseFile()
{
    if (_filename.isEmpty())
    {
        QDir dir;
930 931
        QString path = KSPaths::writableLocation(QStandardPaths::GenericDataLocation) + "logs/" +
                       QDateTime::currentDateTime().toString("yyyy-MM-dd");
932
        dir.mkpath(path);
Jasem Mutlaq's avatar
Jasem Mutlaq committed
933
        QString name = "log_" + QDateTime::currentDateTime().toString("HH-mm-ss") + ".txt";
934
        _filename    = path + QStringLiteral("/") + name;
935 936 937 938 939 940

        // Clear file contents
        QFile file(_filename);
        file.open(QFile::WriteOnly);
        file.close();
    }
941

942
    qSetMessagePattern("[%{time yyyy-MM-dd h:mm:ss.zzz t} %{if-debug}DEBG%{endif}%{if-info}INFO%{endif}%{if-warning}WARN%{endif}%{if-critical}CRIT%{endif}%{if-fatal}FATL%{endif}] %{if-category}[%{category}]%{endif} - %{message}");
943 944
    qInstallMessageHandler(File);
}
Jasem Mutlaq's avatar
Jasem Mutlaq committed
945

946
void Logging::File(QtMsgType type, const QMessageLogContext &context, const QString &msg)
947
{
Jasem Mutlaq's avatar
Jasem Mutlaq committed
948
    QFile file(_filename);
949
    if (file.open(QFile::Append | QIODevice::Text))
Jasem Mutlaq's avatar
Jasem Mutlaq committed
950
    {
951
        QTextStream stream(&file);
952
        Write(stream, type, context, msg);
Jasem Mutlaq's avatar
Jasem Mutlaq committed
953
    }
954
}
Jasem Mutlaq's avatar
Jasem Mutlaq committed
955

956 957
void Logging::UseStdout()
{
958
    qSetMessagePattern("[%{time yyyy-MM-dd h:mm:ss.zzz t} %{if-debug}DEBG%{endif}%{if-info}INFO%{endif}%{if-warning}WARN%{endif}%{if-critical}CRIT%{endif}%{if-fatal}FATL%{endif}] %{if-category}[%{category}]%{endif} - %{message}");
Jasem Mutlaq's avatar
Jasem Mutlaq committed
959
    qInstallMessageHandler(Stdout);
960
}
Jasem Mutlaq's avatar
Jasem Mutlaq committed
961

962
void Logging::Stdout(QtMsgType type, const QMessageLogContext &context, const QString &msg)
963
{
Jasem Mutlaq's avatar
Jasem Mutlaq committed
964
    QTextStream stream(stdout, QIODevice::WriteOnly);
965
    Write(stream, type, context, msg);
966
}
Jasem Mutlaq's avatar
Jasem Mutlaq committed
967

968 969
void Logging::UseStderr()
{
Jasem Mutlaq's avatar
Jasem Mutlaq committed
970
    qInstallMessageHandler(Stderr);
971
}
Jasem Mutlaq's avatar
Jasem Mutlaq committed
972

973
void Logging::Stderr(QtMsgType type, const QMessageLogContext &context, const QString &msg)
974
{
Jasem Mutlaq's avatar
Jasem Mutlaq committed
975
    QTextStream stream(stderr, QIODevice::WriteOnly);
976
    Write(stream, type, context,  msg);
977
}
Jasem Mutlaq's avatar
Jasem Mutlaq committed
978

979
void Logging::Write(QTextStream &stream, QtMsgType type, const QMessageLogContext &context, const QString &msg)
980
{
981 982

    stream << QDateTime::currentDateTime().toString("[yyyy-MM-ddThh:mm:ss.zzz t ");
Jasem Mutlaq's avatar
Jasem Mutlaq committed
983

984
    switch (type)
985
    {
986 987 988
        case QtInfoMsg:
            stream << "INFO ]";
            break;
989
        case QtDebugMsg:
990
            stream << "DEBG ]";
991 992
            break;
        case QtWarningMsg:
993
            stream << "WARN ]";
994 995
            break;
        case QtCriticalMsg:
996
            stream << "CRIT ]";
997 998
            break;
        case QtFatalMsg:
999
            stream << "FATL ]";
1000 1001
            break;
        default:
1002
            stream << "UNKN ]";
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1003 1004
    }

1005 1006

    stream << "[" << qSetFieldWidth(30) << context.category << qSetFieldWidth(0) << "] - ";
1007 1008
    stream << msg << '\n';
    stream.flush();
1009
    //stream << qFormatLogMessage(type, context, msg) << endl;
1010
}
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1011

1012 1013
void Logging::UseDefault()
{
1014
    qInstallMessageHandler(nullptr);
1015
}
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1016

1017 1018
void Logging::Disable()
{
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1019
    qInstallMessageHandler(Disabled);
1020 1021 1022 1023 1024
}

void Logging::Disabled(QtMsgType, const QMessageLogContext &, const QString &)
{
}
1025 1026 1027

void Logging::SyncFilterRules()
{
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    //    QString rules = QString("org.kde.kstars.ekos.debug=%1\n"
    //                            "org.kde.kstars.indi.debug=%2\n"
    //                            "org.kde.kstars.fits.debug=%3\n"
    //                            "org.kde.kstars.ekos.capture.debug=%4\n"
    //                            "org.kde.kstars.ekos.focus.debug=%5\n"
    //                            "org.kde.kstars.ekos.guide.debug=%6\n"
    //                            "org.kde.kstars.ekos.align.debug=%7\n"
    //                            "org.kde.kstars.ekos.mount.debug=%8\n"
    //                            "org.kde.kstars.ekos.scheduler.debug=%9\n").arg(
    //                        Options::verboseLogging() ? "true" : "false",
    //                        Options::iNDILogging() ? "true" : "false",
    //                        Options::fITSLogging() ? "true" : "false",
    //                        Options::captureLogging() ? "true" : "false",
    //                        Options::focusLogging() ? "true" : "false",
    //                        Options::guideLogging() ? "true" : "false",
    //                        Options::alignmentLogging() ? "true" : "false",
    //                        Options::mountLogging() ? "true" : "false",
    //                        Options::schedulerLogging() ? "true" : "false")
    //            .append(QString("org.kde.kstars.ekos.observatory.debug=%2\n"
    //                            "org.kde.kstars.debug=%1").arg(
    //                        Options::verboseLogging() ? "true" : "false",
    //                        Options::observatoryLogging() ? "true" : "false"));
1050

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
    QStringList rules;

    rules << "org.kde.kstars.ekos.debug" << (Options::verboseLogging() ? "true" : "false");
    rules << "org.kde.kstars.indi.debug" << (Options::iNDILogging() ? "true" : "false");
    rules << "org.kde.kstars.fits.debug" << (Options::fITSLogging() ? "true" : "false");
    rules << "org.kde.kstars.ekos.capture.debug" << (Options::captureLogging() ? "true" : "false");
    rules << "org.kde.kstars.ekos.focus.debug" << (Options::focusLogging() ? "true" : "false");
    rules << "org.kde.kstars.ekos.guide.debug" << (Options::guideLogging() ? "true" : "false");
    rules << "org.kde.kstars.ekos.align.debug" << (Options::alignmentLogging() ? "true" : "false");
    rules << "org.kde.kstars.ekos.mount.debug" << (Options::mountLogging() ? "true" : "false");
    rules << "org.kde.kstars.ekos.scheduler.debug" << (Options::schedulerLogging() ? "true" : "false");
    rules << "org.kde.kstars.ekos.observatory.debug" << (Options::observatoryLogging() ? "true" : "false");
    rules << "org.kde.kstars.debug" << (Options::verboseLogging() ? "true" : "false");

    QString formattedRules;
    for (int i = 0; i < rules.size(); i += 2)
        formattedRules.append(QString("%1=%2\n").arg(rules[i], rules[i + 1]));

    QLoggingCategory::setFilterRules(formattedRules);
1070 1071
}

1072 1073 1074 1075
/**
  This method provides a centralized location for the default paths to important external files used in the Options
  on different operating systems.  Note that on OS X, if the user builds the app without indi, astrometry, and xplanet internally
  then the options below will be used.  If the user drags the app from a dmg and has to install the KStars data directory,
Yuri Chornoivan's avatar
Yuri Chornoivan committed
1076
  then most of these paths will be overwritten since it is preferred to use the internal versions.
1077 1078
**/

1079
QString getDefaultPath(const QString &option)
1080
{
1081 1082 1083
    // We support running within Snaps, Flatpaks, and AppImage
    // The path should accomodate the differences between the different
    // packaging solutions
1084
    QString snap = QProcessEnvironment::systemEnvironment().value("SNAP");
1085
    QString flat = QProcessEnvironment::systemEnvironment().value("FLATPAK_DEST");
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
    QString appimg = QProcessEnvironment::systemEnvironment().value("APPDIR");

    // User prefix is the primary mounting point
    QString userPrefix = "/usr";
    // By default /usr is the prefix
    QString prefix = userPrefix;
    // Detect if we are within an App Image
    if (QProcessEnvironment::systemEnvironment().value("APPIMAGE").isEmpty() == false &&
            appimg.isEmpty() == false)
        prefix = appimg + userPrefix;
    else if (flat.isEmpty() == false)
        // Detect if we are within a Flatpak
        prefix = flat;
    // Detect if we are within a snap
    else if (snap.isEmpty() == false)
        prefix = snap + userPrefix;

1103

1104
    if (option == "fitsDir")
1105
    {
1106
        return QDir::homePath();
1107
    }
1108
    else if (option == "indiServer")
1109
    {
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1110
#if defined(Q_OS_OSX)
1111 1112
        return "/usr/local/bin/indiserver";
#endif
1113
        return prefix + "/bin/indiserver";
1114
    }
1115 1116 1117 1118 1119
    else if (option == "INDIHubAgent")
    {
#if defined(Q_OS_OSX)
        return "/usr/local/bin/indihub-agent";
#endif
1120
        return prefix + "/bin/indihub-agent";
1121
    }
1122
    else if (option == "indiDriversDir")
1123
    {
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1124
#if defined(Q_OS_OSX)
1125 1126
        return "/usr/local/share/indi";
#elif defined(Q_OS_LINUX)
1127
        return prefix + "/share/indi";
1128 1129 1130 1131
#else
        return QStandardPaths::locate(QStandardPaths::GenericDataLocation, "indi", QStandardPaths::LocateDirectory);
#endif
    }
1132
    else if (option == "AstrometrySolverBinary")
1133
    {
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1134
#if defined(Q_OS_OSX)
1135
        return "/usr/local/bin/solve-field";
1136 1137
#elif defined(Q_OS_WIN)
        return QDir::homePath() + "/AppData/Local/cygwin_ansvr/lib/astrometry/bin/solve-field.exe";
1138
#endif
1139
        return prefix + "/bin/solve-field";
1140
    }
1141 1142 1143 1144 1145
    else if (option == "SextractorBinary")
    {
#if defined(Q_OS_OSX)
        return "/usr/local/bin/sex";
#endif
1146
        return prefix + "/bin/sextractor";
1147
    }
1148
    else if (option == "AstrometryWCSInfo")
1149
    {
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1150
#if defined(Q_OS_OSX)
1151
        return "/usr/local/bin/wcsinfo";
1152 1153
#elif defined(Q_OS_WIN)
        return QDir::homePath() + "/AppData/Local/cygwin_ansvr/lib/astrometry/bin/wcsinfo.exe";
1154
#endif
1155
        return prefix + "/bin/wcsinfo";
1156
    }
1157
    else if (option == "AstrometryConfFile")
1158
    {
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1159
#if defined(Q_OS_OSX)
1160
        return "/usr/local/etc/astrometry.cfg";
1161 1162
#elif defined(Q_OS_WIN)
        return QDir::homePath() + "/AppData/Local/cygwin_ansvr/etc/astrometry/backend.cfg";
1163
#endif
1164 1165 1166
        // We move /usr
        prefix.remove(userPrefix);
        return prefix + "/etc/astrometry.cfg";
1167
    }
1168 1169
    else if (option == "AstrometryIndexFileLocation")
    {
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1170
#if defined(Q_OS_OSX)
1171 1172
        return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/Astrometry/";
#endif
1173
        return prefix + "/share/astrometry/";
1174
    }
1175 1176 1177 1178
    else if (option == "AstrometryLogFilepath")
    {
        return QDir::tempPath() + "/astrometryLog.txt";
    }
1179
    else if (option == "XplanetPath")
1180
    {
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1181
#if defined(Q_OS_OSX)
1182 1183
        return "/usr/local/bin/xplanet";
#endif
1184
        return prefix + "/bin/xplanet";
1185
    }
1186 1187 1188
    else if (option == "ASTAP")
    {
#if defined(Q_OS_OSX)
1189
        return "/Applications/ASTAP.app/Contents/MacOS/astap";
1190
#elif defined(Q_OS_WIN)
1191
        return "C:/Program Files/astap/astap.exe";
1192
#endif
1193
        return "/opt/astap/astap";
1194
    }
1195 1196 1197

    return QString();
}
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1198

1199
QStringList getAstrometryDefaultIndexFolderPaths()
Jasem Mutlaq's avatar
Jasem Mutlaq committed
1200
{