-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathStelSkyLayerMgr.cpp
More file actions
360 lines (325 loc) · 10.4 KB
/
StelSkyLayerMgr.cpp
File metadata and controls
360 lines (325 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
/*
* Stellarium
* Copyright (C) 2008 Fabien Chereau
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/
#include "StelSkyLayerMgr.hpp"
#include "StelApp.hpp"
#include "StelCore.hpp"
#include "StelFileMgr.hpp"
#include "StelSkyImageTile.hpp"
#include "StelModuleMgr.hpp"
#include "StelPainter.hpp"
#include "MilkyWay.hpp"
#include "StelTranslator.hpp"
#include "StelProgressController.hpp"
#include <QNetworkAccessManager>
#include <stdexcept>
#include <QDebug>
#include <QString>
#include <QVariantMap>
#include <QVariantList>
#include <QDir>
#include <QSettings>
StelSkyLayerMgr::StelSkyLayerMgr(void) : flagShow(true)
{
setObjectName("StelSkyLayerMgr");
}
StelSkyLayerMgr::~StelSkyLayerMgr()
{
for (auto* s : qAsConst(allSkyLayers))
delete s;
}
/*************************************************************************
Reimplementation of the getCallOrder method
*************************************************************************/
double StelSkyLayerMgr::getCallOrder(StelModuleActionName actionName) const
{
if (actionName==StelModule::ActionDraw)
return GETSTELMODULE(MilkyWay)->getCallOrder(actionName)+5;
return 0;
}
// read from stream
void StelSkyLayerMgr::init()
{
QSettings* conf = StelApp::getInstance().getSettings();
const int decimation=conf->value("astro/nebula_texture_decimation", 1).toInt();
loadCollection(decimation);
conf->beginGroup("skylayers");
for (const auto& key : conf->childKeys())
{
QString uri = conf->value(key, "").toString();
if (!uri.isEmpty())
{
if (key=="clilayer")
insertSkyImage(uri, "Command-line layer");
else
insertSkyImage(uri);
}
}
conf->endGroup();
setFlagShow(!conf->value("astro/flag_nebula_display_no_texture", false).toBool());
addAction("actionShow_DSO_Textures", N_("Display Options"), N_("Deep-sky objects background images"), "flagShow", "I");
addAction("actionShow_DSO_Textures_Reload", N_("Display Options"), N_("Reload the deep-sky objects background images"), "loadCollection()", "Ctrl+I");
}
void StelSkyLayerMgr::loadCollection(int decimateBy)
{
if (!allSkyLayers.isEmpty())
allSkyLayers.clear();
QString path = StelFileMgr::findFile("nebulae/default/textures.json");
if (path.isEmpty())
qWarning() << "ERROR while loading nebula texture set default";
else
insertSkyImage(path, QString(), true, decimateBy);
}
QString StelSkyLayerMgr::insertSkyLayer(StelSkyLayerP tile, const QString& keyHint, bool ashow)
{
SkyLayerElem* bEl = new SkyLayerElem(tile, ashow);
QString key = tile->getKeyHint();
if (key.isEmpty() || key=="no name")
{
if (!keyHint.isEmpty())
{
key = keyHint;
}
else
{
key = "no name";
}
}
if (allSkyLayers.contains(key))
{
QString suffix = "_01";
int i=1;
while (allSkyLayers.contains(key+suffix))
{
suffix=QString("_%1").arg(i);
++i;
}
key+=suffix;
}
allSkyLayers.insert(key,bEl);
connect(bEl->layer.data(), SIGNAL(loadingStateChanged(bool)), this, SLOT(loadingStateChanged(bool)));
connect(bEl->layer.data(), SIGNAL(percentLoadedChanged(int)), this, SLOT(percentLoadedChanged(int)));
return key;
}
// Add a new image from its URI (URL or local file name)
QString StelSkyLayerMgr::insertSkyImage(const QString& uri, const QString& keyHint, bool ashow, int decimateBy)
{
return insertSkyLayer(StelSkyLayerP(new StelSkyImageTile(uri, Q_NULLPTR, decimateBy)), keyHint, ashow);
}
// Remove a sky image tile from the list of background images
void StelSkyLayerMgr::removeSkyLayer(const QString& key)
{
//qDebug() << "StelSkyLayerMgr::removeSkyImage removing image:" << key;
if (allSkyLayers.contains(key))
{
SkyLayerElem* bEl = allSkyLayers[key];
disconnect(bEl->layer.data(), SIGNAL(loadingStateChanged(bool)), this, SLOT(loadingStateChanged(bool)));
disconnect(bEl->layer.data(), SIGNAL(percentLoadedChanged(int)), this, SLOT(percentLoadedChanged(int)));
delete bEl;
allSkyLayers.remove(key);
}
else
{
qDebug() << "StelSkyLayerMgr::removeSkyLayer there is no such key" << key << "nothing is removed";
}
}
// Remove a sky image tile from the list of background images
void StelSkyLayerMgr::removeSkyLayer(StelSkyLayerP l)
{
const QString k = keyForLayer(l.data());
if (!k.isEmpty())
removeSkyLayer(k);
}
// Draw all the multi-res images collection
void StelSkyLayerMgr::draw(StelCore* core)
{
if (!flagShow)
return;
StelPainter sPainter(core->getProjection(StelCore::FrameJ2000));
sPainter.setBlending(true, GL_ONE, GL_ONE); //additive blending
for (auto* s : qAsConst(allSkyLayers))
{
if (s->show)
{
sPainter.setProjector(core->getProjection(s->layer->getFrameType()));
s->layer->draw(core, sPainter, 1.);
}
}
}
void noDelete(StelSkyLayer*) {}
// Called when loading of data started or stopped for one collection
void StelSkyLayerMgr::loadingStateChanged(bool b)
{
StelSkyLayer* tile = qobject_cast<StelSkyLayer*>(QObject::sender());
Q_ASSERT(tile!=Q_NULLPTR);
SkyLayerElem* elem = skyLayerElemForLayer(tile);
Q_ASSERT(elem!=Q_NULLPTR);
if (b)
{
Q_ASSERT(elem->progressBar==Q_NULLPTR);
elem->progressBar = StelApp::getInstance().addProgressBar();
QString serverStr = elem->layer->getShortServerCredits();
QString shortName = elem->layer->getShortName();
if (serverStr.isEmpty())
{
// TRANSLATORS: The full phrase is "Loading '%SHORT_NAME%'" in progress bar
elem->progressBar->setFormat(q_("Loading '%1'").arg(shortName));
}
else
{
// TRANSLATORS: The full phrase is "Loading '%SHORT_NAME%' from %URL%" in progress bar
elem->progressBar->setFormat(q_("Loading '%1' from %2").arg(shortName, serverStr));
}
elem->progressBar->setRange(0,100);
}
else
{
Q_ASSERT(elem->progressBar!=Q_NULLPTR);
StelApp::getInstance().removeProgressBar(elem->progressBar);
elem->progressBar = Q_NULLPTR;
}
}
// Called when the percentage of loading tiles/tiles to be displayed changed for one collection
void StelSkyLayerMgr::percentLoadedChanged(int percentage)
{
StelSkyLayer* tile = qobject_cast<StelSkyLayer*>(QObject::sender());
Q_ASSERT(tile!=Q_NULLPTR);
SkyLayerElem* elem = skyLayerElemForLayer(tile);
Q_ASSERT(elem!=Q_NULLPTR);
Q_ASSERT(elem->progressBar!=Q_NULLPTR);
elem->progressBar->setValue(percentage);
}
StelSkyLayerMgr::SkyLayerElem* StelSkyLayerMgr::skyLayerElemForLayer(const StelSkyLayer* t)
{
for (auto* e : qAsConst(allSkyLayers))
{
if (e->layer==t)
{
return e;
}
}
return Q_NULLPTR;
}
QString StelSkyLayerMgr::keyForLayer(const StelSkyLayer* t)
{
return allSkyLayers.key(skyLayerElemForLayer(t));
}
StelSkyLayerMgr::SkyLayerElem::SkyLayerElem(StelSkyLayerP t, bool ashow) : layer(t), progressBar(Q_NULLPTR), show(ashow)
{}
StelSkyLayerMgr::SkyLayerElem::~SkyLayerElem()
{
if (progressBar)
StelApp::getInstance().removeProgressBar(progressBar);
progressBar = Q_NULLPTR;
}
bool StelSkyLayerMgr::loadSkyImage(const QString& id, const QString& filename,
double long0, double lat0,
double long1, double lat1,
double long2, double lat2,
double long3, double lat3,
double minRes, double maxBright, bool visible, StelCore::FrameType frameType, bool withAberration, int decimateBy)
{
if (allSkyLayers.contains(id))
{
qWarning() << "Image ID" << id << "already exists, removing old image before loading";
removeSkyLayer(id);
}
QString path = StelFileMgr::findFile(filename);
if (path.isEmpty())
{
qWarning() << "Could not find image" << QDir::toNativeSeparators(filename);
return false;
}
QVariantMap vm;
QVariantList cl; // coordinates list for adding worldCoords and textureCoords
QVariantList c; // a list for a pair of coordinates
QVariantList ol; // outer list - we want a structure 3 levels deep...
vm["imageUrl"] = QVariant(path);
vm["maxBrightness"] = QVariant(maxBright);
vm["minResolution"] = QVariant(minRes);
vm["shortName"] = QVariant(id);
vm["withAberration"] = QVariant(withAberration);
// textureCoords (define the ordering of worldCoords)
cl.clear();
ol.clear();
c.clear(); c.append(0); c.append(0); cl.append(QVariant(c));
c.clear(); c.append(1); c.append(0); cl.append(QVariant(c));
c.clear(); c.append(1); c.append(1); cl.append(QVariant(c));
c.clear(); c.append(0); c.append(1); cl.append(QVariant(c));
ol.append(QVariant(cl));
vm["textureCoords"] = ol;
// world coordinates
cl.clear();
ol.clear();
c.clear(); c.append(long0); c.append(lat0); cl.append(QVariant(c));
c.clear(); c.append(long1); c.append(lat1); cl.append(QVariant(c));
c.clear(); c.append(long2); c.append(lat2); cl.append(QVariant(c));
c.clear(); c.append(long3); c.append(lat3); cl.append(QVariant(c));
ol.append(QVariant(cl));
vm["worldCoords"] = ol;
vm["alphaBlend"] = true; // new 2017-3: Make black correctly see-through.
StelSkyLayerP tile = StelSkyLayerP(new StelSkyImageTile(vm, Q_NULLPTR));
tile->setFrameType(frameType);
try
{
QString key = insertSkyLayer(tile, filename, visible);
if (key == id)
return true;
else
return false;
}
catch (std::runtime_error& e)
{
qWarning() << e.what();
return false;
}
}
void StelSkyLayerMgr::showLayer(const QString& id, bool b)
{
if (allSkyLayers.contains(id))
{
if (allSkyLayers[id]!=Q_NULLPTR)
allSkyLayers[id]->show = b;
}
}
bool StelSkyLayerMgr::getShowLayer(const QString& id) const
{
if (allSkyLayers.contains(id))
{
if (allSkyLayers[id]!=Q_NULLPTR)
return allSkyLayers[id]->show;
}
return false;
}
//! Get the list of all the currently loaded layers.
QMap<QString, StelSkyLayerP> StelSkyLayerMgr::getAllSkyLayers() const
{
QMap<QString, StelSkyLayerP> res;
for (QMap<QString, StelSkyLayerMgr::SkyLayerElem*>::ConstIterator iter=allSkyLayers.constBegin();iter!=allSkyLayers.constEnd();++iter)
{
//qDebug() << iter.key() << iter.value()->layer->getShortName();
res.insert(iter.key(), iter.value()->layer);
}
return res;
}
StelSkyLayerP StelSkyLayerMgr::getSkyLayer(const QString& key) const
{
if (allSkyLayers.contains(key))
return allSkyLayers[key]->layer;
return StelSkyLayerP();
}