1 /*
2     SPDX-FileCopyrightText: 2005 Jason Harris <kstars@30doradus.org>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "milkyway.h"
8 
9 #include "ksfilereader.h"
10 #include "kstarsdata.h"
11 #ifdef KSTARS_LITE
12 #include "skymaplite.h"
13 #else
14 #include "skymap.h"
15 #endif
16 #include "Options.h"
17 #include "skypainter.h"
18 #include "skycomponents/skiphashlist.h"
19 
20 #include <QtConcurrent>
21 
MilkyWay(SkyComposite * parent)22 MilkyWay::MilkyWay(SkyComposite *parent) : LineListIndex(parent, i18n("Milky Way"))
23 {
24     intro();
25     // Milky way
26     //loadContours("milkyway.dat", i18n("Loading Milky Way"));
27     // Magellanic clouds
28     //loadContours("lmc.dat", i18n("Loading Large Magellanic Clouds"));
29     //loadContours("smc.dat", i18n("Loading Small Magellanic Clouds"));
30     //summary();
31 
32     QtConcurrent::run(this, &MilkyWay::loadContours, QString("milkyway.dat"), i18n("Loading Milky Way"));
33     QtConcurrent::run(this, &MilkyWay::loadContours, QString("lmc.dat"), i18n("Loading Large Magellanic Clouds"));
34     QtConcurrent::run(this, &MilkyWay::loadContours, QString("smc.dat"), i18n("Loading Small Magellanic Clouds"));
35 }
36 
getIndexHash(LineList * lineList)37 const IndexHash &MilkyWay::getIndexHash(LineList *lineList)
38 {
39     SkipHashList *skipList = dynamic_cast<SkipHashList *>(lineList);
40     return skyMesh()->indexLine(skipList->points(), skipList->skipHash());
41 }
42 
skipList(LineList * lineList)43 SkipHashList *MilkyWay::skipList(LineList *lineList)
44 {
45     return dynamic_cast<SkipHashList *>(lineList);
46 }
47 
selected()48 bool MilkyWay::selected()
49 {
50 #ifndef KSTARS_LITE
51     return Options::showMilkyWay() && !(Options::hideOnSlew() && Options::hideMilkyWay() && SkyMap::IsSlewing());
52 #else
53     return Options::showMilkyWay() && !(Options::hideOnSlew() && Options::hideMilkyWay() && SkyMapLite::IsSlewing());
54 #endif
55 }
56 
draw(SkyPainter * skyp)57 void MilkyWay::draw(SkyPainter *skyp)
58 {
59     if (!selected())
60         return;
61 
62     QColor color = KStarsData::Instance()->colorScheme()->colorNamed("MWColor");
63     skyp->setPen(QPen(color, 3, Qt::SolidLine));
64     skyp->setBrush(QBrush(color));
65 
66     if (Options::fillMilkyWay())
67     {
68         drawFilled(skyp);
69     }
70     else
71     {
72         drawLines(skyp);
73     }
74 }
75 
loadContours(QString fname,QString greeting)76 void MilkyWay::loadContours(QString fname, QString greeting)
77 {
78     KSFileReader fileReader;
79     std::shared_ptr<LineList> skipList;
80     int iSkip = 0;
81 
82     if (!fileReader.open(fname))
83         return;
84 
85     fileReader.setProgress(greeting, 2136, 5);
86     while (fileReader.hasMoreLines())
87     {
88         QString line = fileReader.readLine();
89         QChar firstChar = line.at(0);
90 
91         fileReader.showProgress();
92         if (firstChar == '#')
93             continue;
94 
95         bool okRA = false, okDec = false;
96         double ra  = line.midRef(2, 8).toDouble(&okRA);
97         double dec = line.midRef(11, 8).toDouble(&okDec);
98 
99         if (!okRA || !okDec)
100         {
101             qDebug() << QString("%1: conversion error on line: %2\n").arg(fname).arg(fileReader.lineNumber());
102             continue;
103         }
104 
105         if (firstChar == 'M')
106         {
107             if (skipList.get())
108                 appendBoth(skipList);
109             skipList.reset();
110             iSkip    = 0;
111         }
112 
113         if (!skipList.get())
114             skipList.reset(new SkipHashList());
115 
116         std::shared_ptr<SkyPoint> point(new SkyPoint(ra, dec));
117 
118         skipList->append(std::move(point));
119         if (firstChar == 'S')
120             static_cast<SkipHashList*>(skipList.get())->setSkip(iSkip);
121 
122         iSkip++;
123     }
124     if (skipList.get())
125         appendBoth(skipList);
126 }
127