-
-
Notifications
You must be signed in to change notification settings - Fork 998
Expand file tree
/
Copy pathStelSkyCultureMgr.cpp
More file actions
870 lines (788 loc) · 30.6 KB
/
StelSkyCultureMgr.cpp
File metadata and controls
870 lines (788 loc) · 30.6 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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
556
557
558
559
560
561
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
738
739
740
741
742
743
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
/*
* Stellarium
* Copyright (C) 2006 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 "StelSkyCultureMgr.hpp"
#include "StelFileMgr.hpp"
#include "StelTranslator.hpp"
#include "StelLocaleMgr.hpp"
#include "StelMainView.hpp"
#include "StelApp.hpp"
#include <md4c-html.h>
#include <QSettings>
#include <QString>
#include <QMessageBox>
#include <QStringList>
#include <QVariant>
#include <QDebug>
#include <QMap>
#include <QMapIterator>
#include <QDir>
#include <QJsonObject>
#include <QJsonDocument>
#include <QRegularExpression>
namespace
{
#if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0))
constexpr auto SkipEmptyParts = Qt::SkipEmptyParts;
#else
constexpr auto SkipEmptyParts = QString::SkipEmptyParts;
#endif
void applyStyleToMarkdown(QString& string)
{
static const auto pPattern = []{
// Locates any paragraph. This will make sure we get the smallest paragraphs. If
// we look for the styled ones from the beginning, we'll get too greedy matches.
QRegularExpression p("<p>.+?</p>", QRegularExpression::DotMatchesEverythingOption);
p.optimize();
return p;
}();
static const auto pStyledPattern = []{
// This is for the actual paragraphs we are interested in.
QRegularExpression sp(R"regex(<p>(.+?){:\s*\.(\S+?)\s*}\s*</p>)regex",
QRegularExpression::DotMatchesEverythingOption);
sp.optimize();
return sp;
}();
bool replaced;
do
{
replaced = false;
for(auto matches = pPattern.globalMatch(string); matches.hasNext(); )
{
const auto& match = matches.next();
const auto substr = match.captured(0);
if(substr.contains(pStyledPattern))
{
// Don't replace the original string directly: otherwise
// we'll again have greedy matches.
auto newSubstr = substr;
newSubstr.replace(pStyledPattern, R"(<p class="\2">\1</p>)");
// Also force the caption to be below the image,
// rather than to the right of it.
static const QRegularExpression re("(<img [^>]+>)");
newSubstr.replace(re, "\\1<br>");
string.replace(substr, newSubstr);
replaced = true;
break;
}
}
} while(replaced);
}
QString markdownToHTML(QString input)
{
const auto inputUTF8 = input.toStdString();
std::string outputUTF8;
::md_html(inputUTF8.data(), inputUTF8.size(),
[](const char* html, const MD_SIZE size, void* output)
{ static_cast<std::string*>(output)->append(html, size); },
&outputUTF8, MD_DIALECT_GITHUB, 0);
auto result = QString::fromStdString(outputUTF8);
applyStyleToMarkdown(result);
return result;
}
QString convertReferenceLinks(QString text)
{
static const QRegularExpression re(" ?\\[#([0-9]+)\\]", QRegularExpression::MultilineOption);
text.replace(re,
"<sup><a href=\"#cite_\\1\">[\\1]</a></sup>");
return text;
}
}
QString StelSkyCultureMgr::getSkyCultureEnglishName(const QString& dir, const QString& idFromJSON, const bool reportErrorsToGUI) const
{
const auto skyCultureId = idFromJSON;
constexpr char descFileName[] = "/description.md";
const auto descPath = QFileInfo(dir).isAbsolute() ? dir + descFileName : StelFileMgr::findFile("skycultures/" + dir + descFileName);
if (descPath.isEmpty())
{
if (reportErrorsToGUI)
{
QMessageBox::critical(&StelMainView::getInstance(), q_("Error loading sky culture"),
q_("Can't find file %1").arg(descPath));
}
else
{
qWarning() << "Can't find description for skyculture" << skyCultureId;
}
return idFromJSON;
}
QFile f(descPath);
if (!f.open(QIODevice::ReadOnly))
{
if (reportErrorsToGUI)
{
QMessageBox::critical(&StelMainView::getInstance(), q_("Error loading sky culture"),
q_("Failed to open file %1").arg(descPath));
}
else
{
qWarning().nospace() << "Failed to open sky culture description file " << descPath << ": " << f.errorString();
}
return idFromJSON;
}
for (int lineNum = 1;; ++lineNum)
{
const auto line = QString::fromUtf8(f.readLine()).trimmed();
if (line.isEmpty()) continue;
if (!line.startsWith("#"))
{
if (reportErrorsToGUI)
{
QMessageBox::critical(&StelMainView::getInstance(), q_("Error loading sky culture"),
q_("Sky culture description file %1 at line %2 has wrong format "
"(expected a top-level header, got:\n%3").arg(descPath).arg(lineNum).arg(line));
}
else
{
qWarning().nospace() << "Sky culture description file " << descPath << " at line "
<< lineNum << " has wrong format (expected a top-level header, got " << line;
}
return idFromJSON;
}
return line.mid(1).trimmed();
}
qWarning() << "Failed to find sky culture name in" << descPath;
return idFromJSON;
}
StelSkyCultureMgr::StelSkyCultureMgr()
{
setObjectName("StelSkyCultureMgr");
}
StelSkyCultureMgr::~StelSkyCultureMgr()
{
}
void StelSkyCultureMgr::addCustomCulture(const QString& dir, const bool reportErrorsToGUI)
{
if (!addNewCulture(dir, reportErrorsToGUI)) return;
additionalCultureDirs << dir;
QSettings*const conf = StelApp::getInstance().getSettings();
conf->beginGroup("skycultures");
conf->beginWriteArray("additional_cultures");
int i = 0;
for (const auto& dir : additionalCultureDirs)
{
conf->setArrayIndex(i);
conf->setValue("dir", dir);
++i;
}
conf->endArray();
conf->endGroup();
auto& culture = dirToNameEnglish[dir];
qInfo() << "Added a new sky culture:" << culture.englishName;
}
bool StelSkyCultureMgr::addNewCulture(const QString& dir, const bool reportErrorsToGUI)
{
constexpr char indexFileName[] = "/index.json";
const QString filePath = QFileInfo(dir).isAbsolute() ? dir + indexFileName : StelFileMgr::findFile("skycultures/" + dir + indexFileName);
if (filePath.isEmpty())
{
if (reportErrorsToGUI)
{
QMessageBox::critical(&StelMainView::getInstance(), q_("Error loading sky culture"),
q_("Failed to find %1 file in %2").arg(indexFileName, QDir::toNativeSeparators(dir)));
}
else
{
qCritical() << "Failed to find" << indexFileName << "file in sky culture directory" << QDir::toNativeSeparators(dir);
}
return false;
}
QFile file(filePath);
if (!file.open(QFile::ReadOnly))
{
if (reportErrorsToGUI)
{
QMessageBox::critical(&StelMainView::getInstance(), q_("Error loading sky culture"),
q_("Failed to open file %1").arg(QDir::toNativeSeparators(filePath)));
}
else
{
qCritical() << "Failed to open" << indexFileName << "file in sky culture directory" << QDir::toNativeSeparators(dir);
}
return false;
}
const auto jsonText = file.readAll();
if (jsonText.isEmpty())
{
if (reportErrorsToGUI)
{
QMessageBox::critical(&StelMainView::getInstance(), q_("Error loading sky culture"),
q_("Failed to read data from file %1").arg(QDir::toNativeSeparators(filePath)));
}
else
{
qCritical() << "Failed to read data from" << indexFileName << "file in sky culture directory"
<< QDir::toNativeSeparators(dir);
}
return false;
}
QJsonParseError error;
const auto jsonDoc = QJsonDocument::fromJson(jsonText, &error);
if (error.error != QJsonParseError::NoError)
{
if (reportErrorsToGUI)
{
QMessageBox::critical(&StelMainView::getInstance(), q_("Error loading sky culture"),
q_("Failed to parse file %1").arg(QDir::toNativeSeparators(filePath)));
}
else
{
qCritical().nospace() << "Failed to parse " << indexFileName << " from sky culture directory "
<< QDir::toNativeSeparators(dir) << ": " << error.errorString();
}
return false;
}
if (!jsonDoc.isObject())
{
if (reportErrorsToGUI)
{
QMessageBox::critical(&StelMainView::getInstance(), q_("Error loading sky culture"),
q_("Failed to find the expected JSON structure in %1").arg(QDir::toNativeSeparators(filePath)));
}
else
{
qCritical() << "Failed to find the expected JSON structure in" << indexFileName << " from sky culture directory"
<< QDir::toNativeSeparators(dir);
}
return false;
}
const auto data = jsonDoc.object();
auto& culture = dirToNameEnglish[dir];
culture.path = StelFileMgr::dirName(filePath);
const auto id = data["id"].toString();
const auto dirFileName = QFileInfo(dir).fileName();
if(id != dirFileName)
qWarning() << "Sky culture id" << id << "doesn't match directory name" << dirFileName;
culture.id = id;
culture.englishName = getSkyCultureEnglishName(dir, id, reportErrorsToGUI);
culture.region = data["region"].toString();
if (culture.region.length()==0)
{
qWarning() << "No geographic region declared in skyculture" << id << ". setting \"World\"";
culture.region = "World";
}
if (data["constellations"].isArray())
{
culture.constellations = data["constellations"].toArray();
}
else
{
qWarning() << "No \"constellations\" array found in JSON data in sky culture directory"
<< QDir::toNativeSeparators(dir);
}
culture.asterisms = data["asterisms"].toArray();
culture.langsUseNativeNames = data["langs_use_native_names"].toArray();
culture.boundariesType = StelSkyCulture::BoundariesType::None; // default value if not specified in the JSON file
if (data.contains("edges") && data.contains("edges_type"))
{
const QString type = data["edges_type"].toString();
const QString typeSimp = type.simplified().toUpper();
static const QMap<QString, StelSkyCulture::BoundariesType> map={
{"IAU", StelSkyCulture::BoundariesType::IAU},
{"OWN", StelSkyCulture::BoundariesType::Own},
{"NONE", StelSkyCulture::BoundariesType::None}
};
if (!map.contains(typeSimp))
qWarning().nospace() << "Unexpected edges_type value in sky culture " << dir
<< ": " << type << ". Will resort to Own.";
culture.boundariesType = map.value(typeSimp, StelSkyCulture::BoundariesType::Own);
}
culture.boundaries = data["edges"].toArray();
culture.boundariesEpoch = data["edges_epoch"].toString("J2000");
culture.fallbackToInternationalNames = data["fallback_to_international_names"].toBool();
culture.names = data["common_names"].toObject();
const auto classifications = data["classification"].toArray();
if (classifications.isEmpty())
{
culture.classification = StelSkyCulture::INCOMPLETE;
}
else
{
static const QMap <QString, StelSkyCulture::CLASSIFICATION>classificationMap={
{ "traditional", StelSkyCulture::TRADITIONAL},
{ "historical", StelSkyCulture::HISTORICAL},
{ "ethnographic", StelSkyCulture::ETHNOGRAPHIC},
{ "single", StelSkyCulture::SINGLE},
{ "comparative", StelSkyCulture::COMPARATIVE},
{ "personal", StelSkyCulture::PERSONAL},
{ "incomplete", StelSkyCulture::INCOMPLETE},
};
const auto classificationStr = classifications[0].toString(); // We'll take only the first item for now.
const auto classification=classificationMap.value(classificationStr.toLower(), StelSkyCulture::INCOMPLETE);
if (classificationMap.constFind(classificationStr.toLower()) == classificationMap.constEnd()) // not included
{
qWarning() << "Skyculture " << dir << "has UNKNOWN classification: " << classificationStr;
qWarning() << "Please edit info.ini and change to a supported value. For now, this equals 'incomplete'";
}
culture.classification = classification;
}
return true;
}
void StelSkyCultureMgr::makeCulturesList()
{
QSet<QString> cultureDirNames = StelFileMgr::listContents("skycultures",StelFileMgr::Directory);
cultureDirNames += additionalCultureDirs;
for (const auto& dir : std::as_const(cultureDirNames))
addNewCulture(dir);
}
//! Init itself from a config file.
void StelSkyCultureMgr::init()
{
QSettings*const conf = StelApp::getInstance().getSettings();
conf->beginGroup("skycultures");
const int size = conf->beginReadArray("additional_cultures");
for (int i = 0; i < size; i++)
{
conf->setArrayIndex(i);
additionalCultureDirs << conf->value("dir").toString();
}
conf->endArray();
conf->endGroup();
makeCulturesList();
defaultSkyCultureID = StelApp::getInstance().getSettings()->value("localization/sky_culture", "modern").toString();
if (defaultSkyCultureID=="western") // switch to new Sky Culture ID
defaultSkyCultureID = "modern";
setCurrentSkyCultureID(defaultSkyCultureID);
}
void StelSkyCultureMgr::reloadSkyCulture()
{
emit currentSkyCultureIDChanged(currentSkyCulture.id);
emit currentSkyCultureChanged(currentSkyCulture);
}
//! Set the current sky culture from the passed directory
bool StelSkyCultureMgr::setCurrentSkyCultureID(const QString& cultureDir)
{
//prevent unnecessary changes
if(cultureDir==currentSkyCulture.id)
return false;
QString scID = cultureDir;
bool result = true;
// make sure culture definition exists before attempting or will die
if (directoryToSkyCultureEnglish(cultureDir) == "")
{
qWarning() << "Invalid sky culture directory: " << QDir::toNativeSeparators(cultureDir);
scID = "modern";
result = false;
}
currentSkyCulture = dirToNameEnglish[scID];
emit currentSkyCultureChanged(currentSkyCulture);
emit currentSkyCultureIDChanged(currentSkyCulture.id);
return result;
}
// Set the default sky culture from the ID.
bool StelSkyCultureMgr::setDefaultSkyCultureID(const QString& id)
{
// make sure culture definition exists before attempting or will die
if (directoryToSkyCultureEnglish(id) == "")
{
qWarning() << "Invalid sky culture ID: " << id;
return false;
}
defaultSkyCultureID = id;
QSettings* conf = StelApp::getInstance().getSettings();
Q_ASSERT(conf);
conf->setValue("localization/sky_culture", id);
emit defaultSkyCultureIDChanged(id);
return true;
}
QString StelSkyCultureMgr::getCurrentSkyCultureNameI18() const
{
const StelTranslator& trans = StelApp::getInstance().getLocaleMgr().getSkyCultureDescriptionsTranslator();
return trans.qtranslate(currentSkyCulture.englishName, "sky culture");
}
QString StelSkyCultureMgr::getCurrentSkyCultureEnglishName() const
{
return currentSkyCulture.englishName;
}
StelSkyCulture::BoundariesType StelSkyCultureMgr::getCurrentSkyCultureBoundariesType() const
{
return currentSkyCulture.boundariesType;
}
int StelSkyCultureMgr::getCurrentSkyCultureClassificationIdx() const
{
return currentSkyCulture.classification;
}
QString StelSkyCultureMgr::getCurrentSkyCultureHtmlClassification() const
{
QString classification, description, color;
switch (currentSkyCulture.classification)
{
case StelSkyCulture::ETHNOGRAPHIC:
color = "#33ff33"; // "green" area
classification = qc_("ethnographic", "sky culture classification");
description = q_("Provided by ethnographic researchers based on interviews of indigenous people.");
break;
case StelSkyCulture::HISTORICAL:
color = "#33ff33"; // "green" area
classification = qc_("historical", "sky culture classification");
description = q_("Provided by historians based on historical written sources from a (usually short) period of the past.");
break;
case StelSkyCulture::SINGLE:
color = "#33ff33"; // "green" area
classification = qc_("single", "sky culture classification");
description = q_("Represents a single source like a historical atlas, or publications of a single author.");
break;
case StelSkyCulture::COMPARATIVE:
color = "#2090ff"; // "blue" area
classification = qc_("comparative", "sky culture classification");
description = q_("Compares and confronts elements from at least two sky cultures with each other.");
break;
case StelSkyCulture::TRADITIONAL:
color = "#33ff33"; // "green" area
classification = qc_("traditional", "sky culture classification");
description = q_("Content represents 'common' knowledge by several members of an ethnic community, and the sky culture has been developed by members of such community.");
break;
case StelSkyCulture::PERSONAL:
color = "#ffff00"; // "yellow" area
classification = qc_("personal", "sky culture classification");
description = q_("This is a personally developed sky culture which is not founded in published historical or ethnological research. Stellarium may include it when it is 'pretty enough' without really approving its contents.");
break;
case StelSkyCulture::INCOMPLETE:
color = "#ff6633"; // "red" area
classification = qc_("incomplete", "sky culture classification");
description = q_("The accuracy of the sky culture description cannot be given, although it looks like it is built on a solid background. More work would be needed.");
break;
default: // undefined
color = "#ff00cc";
classification = qc_("undefined", "sky culture classification");
description = QString();
break;
}
QString html = QString();
if (!description.isEmpty()) // additional info for sky culture (metainfo): let's use italic
html = QString("<dl><dt><span style='color:%4;'>%5</span> <strong>%1: %2</strong></dt><dd><em>%3</em></dd></dl>").arg(q_("Classification"), classification, description, color, QChar(0x25CF));
return html;
}
std::pair<QString/*color*/,QString/*info*/> StelSkyCultureMgr::getLicenseDescription(const QString& license, const bool singleLicenseForAll) const
{
QString color, description;
if (license.isEmpty())
{
color = "#2090ff"; // "blue" area
description = q_("This sky culture is provided under unknown license. Please ask authors for details about license for this sky culture.");
}
else if (license.contains("GPL", Qt::CaseSensitive))
{
color = "#33ff33"; // "green" area; free license
if (singleLicenseForAll)
description = q_("This sky culture is provided under GNU General Public License. You can use it for commercial "
"and non-commercial purposes, freely adapt it and share adapted work.");
else
description = q_("You can use it for commercial and non-commercial purposes, freely adapt it and share adapted work.");
}
else if (license.contains("MIT", Qt::CaseSensitive))
{
color = "#33ff33"; // "green" area; free license
if (singleLicenseForAll)
description = q_("This sky culture is provided under MIT License. You can use it for commercial and non-commercial "
"purposes, freely adapt it and share adapted work.");
else
description = q_("You can use it for commercial and non-commercial purposes, freely adapt it and share adapted work.");
}
else if (license.contains("Public Domain"))
{
color = "#33ff33"; // "green" area; free license
if (singleLicenseForAll)
description = q_("This sky culture is distributed as public domain.");
else
description = q_("This is distributed as public domain.");
}
else if (license.startsWith("CC", Qt::CaseSensitive) || license.contains("Creative Commons", Qt::CaseInsensitive))
{
if (singleLicenseForAll)
description = q_("This sky culture is provided under Creative Commons License.");
QStringList details = license.split(" ", SkipEmptyParts);
const QMap<QString, QString>options = {
{ "BY", q_("You may distribute, remix, adapt, and build upon this sky culture, even commercially, as long "
"as you credit authors for the original creation.") },
{ "BY-SA", q_("You may remix, adapt, and build upon this sky culture even for commercial purposes, as long as "
"you credit authors and license the new creations under the identical terms. This license is often "
"compared to “copyleft” free and open source software licenses.") },
{ "BY-ND", q_("You may reuse this sky culture for any purpose, including commercially; however, adapted work "
"cannot be shared with others, and credit must be provided by you.") },
{ "BY-NC", q_("You may remix, adapt, and build upon this sky culture non-commercially, and although your new works "
"must also acknowledge authors and be non-commercial, you don’t have to license your derivative works "
"on the same terms.") },
{ "BY-NC-SA", q_("You may remix, adapt, and build upon this sky culture non-commercially, as long as you credit "
"authors and license your new creations under the identical terms.") },
{ "BY-NC-ND", q_("You may use this sky culture and share them with others as long as you credit authors, but you can’t "
"change it in any way or use it commercially.") },
};
color = "#33ff33"; // "green" area; free license
if (license.contains("ND", Qt::CaseSensitive))
color = "#ffff00"; // "yellow" area; nonfree license - weak restrictions
if (license.contains("NC", Qt::CaseSensitive))
color = "#ff6633"; // "red" area; nonfree license - strong restrictions
if (!details.at(0).startsWith("CC0", Qt::CaseInsensitive)) // Not public domain!
description.append(QString(" %1").arg(options.value(details.at(1), "")));
else
description = q_("This sky culture is distributed as public domain.");
}
else if (license.contains("FAL", Qt::CaseSensitive) || license.contains("Free Art License", Qt::CaseSensitive))
{
color = "#33ff33"; // "green" area; free license
description.append(QString(" %1").arg(q_("Free Art License grants the right to freely copy, distribute, and transform.")));
}
return std::make_pair(color, description);
}
QString StelSkyCultureMgr::getCurrentSkyCultureHtmlLicense() const
{
static const QRegularExpression licRe("\\s*\n+\\s*");
const auto lines = currentSkyCulture.license.split(licRe, SkipEmptyParts);
if (lines.isEmpty()) return "";
if (lines.size() == 1)
{
const auto parts = lines[0].split(":", SkipEmptyParts);
const auto licenseName = convertReferenceLinks(parts.size() == 1 ? parts[0] : parts[1]);
const auto [color, description] = getLicenseDescription(licenseName, true);
if (!description.isEmpty())
{
return QString("<dl><dt><span style='color:%4;'>%5</span> <strong>%1: %2</strong></dt><dd><em>%3</em></dd></dl>")
.arg(q_("License"),
currentSkyCulture.license.isEmpty() ? q_("unknown") : licenseName,
description, color, QChar(0x25CF));
}
else
{
return QString("<dl><dt><span style='color:%3;'>%4</span> <strong>%1: %2</strong></dt></dl>")
.arg(q_("License"),
currentSkyCulture.license.isEmpty() ? q_("unknown") : licenseName, color, QChar(0x25CF));
}
return QString{};
}
else
{
QString html = "<h1>" + q_("License") + "</h1>\n";
QString addendum;
for (const auto& line : lines)
{
static const QRegularExpression re("\\s*:\\s*");
const auto parts = line.split(re, SkipEmptyParts);
if (parts.size() == 1)
{
addendum += line + "<br>\n";
continue;
}
const auto [color, description] = getLicenseDescription(parts[1], false);
if (description.isEmpty())
{
html += QString("<dl><dt><span style='color:%2;'>%3</span> <strong>%1</strong></dt></dl>")
.arg(convertReferenceLinks(line), color, QChar(0x25CF));
}
else
{
html += QString("<dl><dt><span style='color:%3;'>%4</span> <strong>%1</strong></dt><dd><em>%2</em></dd></dl>")
.arg(convertReferenceLinks(line), description, color, QChar(0x25CF));
}
}
return html + addendum;
}
}
QString StelSkyCultureMgr::getCurrentSkyCultureHtmlRegion() const
{
QString html = "", region = currentSkyCulture.region.trimmed();
QString description = q_("The region indicates the geographical area of origin of a given sky culture.");
// special case: modern sky culture
if (getCurrentSkyCultureID().contains("modern", Qt::CaseInsensitive))
{
// TRANSLATIONS: This is the name of a geographical "pseudo-region" on Earth
region = N_("World");
description = q_("All 'modern' sky cultures are based on the IAU-approved 88 constellations with standardized boundaries and are used worldwide. The origins of these constellations are pan-European.");
}
if (!region.isEmpty()) // Region marker is always 'green'
html = QString("<dl><dt><span style='color:#33ff33;'>%4</span> <strong>%1 %2</strong></dt><dd><em>%3</em></dd></dl>").arg(q_("Region:"), q_(region), description, QChar(0x25CF));
return html;
}
bool StelSkyCultureMgr::setCurrentSkyCultureNameI18(const QString& cultureName)
{
return setCurrentSkyCultureID(skyCultureI18ToDirectory(cultureName));
}
//! returns newline delimited list of human readable culture names in english
QString StelSkyCultureMgr::getSkyCultureListEnglish(void) const
{
QString cultures;
QMapIterator<QString, StelSkyCulture> i(dirToNameEnglish);
while(i.hasNext())
{
i.next();
cultures += QString("%1\n").arg(i.value().englishName);
}
return cultures;
}
//! returns newline delimited list of human readable culture names translated to current locale
QStringList StelSkyCultureMgr::getSkyCultureListI18(void) const
{
const StelTranslator& trans = StelApp::getInstance().getLocaleMgr().getSkyCultureDescriptionsTranslator();
QStringList cultures;
QMapIterator<QString, StelSkyCulture> i(dirToNameEnglish);
while (i.hasNext())
{
i.next();
cultures += trans.qtranslate(i.value().englishName, "sky culture");
}
// Sort for GUI use.
std::sort(cultures.begin(), cultures.end(), [](const QString& a, const QString& b)
{ return a.localeAwareCompare(b) < 0; });
return cultures;
}
QStringList StelSkyCultureMgr::getSkyCultureListIDs(void) const
{
return dirToNameEnglish.keys();
}
QString StelSkyCultureMgr::convertMarkdownLevel2Section(const QString& markdown, const QString& sectionName,
const qsizetype bodyStartPos, const qsizetype bodyEndPos,
const StelTranslator& trans)
{
auto text = markdown.mid(bodyStartPos, bodyEndPos - bodyStartPos);
static const QRegularExpression re("^\n*|\n*$");
text.replace(re, "");
text = trans.qtranslate(text);
if (sectionName.trimmed() == "References")
{
static const QRegularExpression refRe("^ *- \\[#([0-9]+)\\]: (.*)$", QRegularExpression::MultilineOption);
text.replace(refRe, "\\1. <span id=\"cite_\\1\">\\2</span>");
}
else
{
text = convertReferenceLinks(text);
}
if (sectionName.trimmed() == "License")
{
currentSkyCulture.license = text;
return "";
}
return markdownToHTML(text);
}
QString StelSkyCultureMgr::descriptionMarkdownToHTML(const QString& markdownInput, const QString& descrPath)
{
// Section names should be available for translation
(void)NC_("Extras" , "Name of a section in sky culture description");
(void)NC_("References" , "Name of a section in sky culture description");
(void)NC_("Authors" , "Name of a section in sky culture description");
const StelTranslator& trans = StelApp::getInstance().getLocaleMgr().getSkyCultureDescriptionsTranslator();
// Strip comments before translating
static const QRegularExpression commentPat("<!--.*?-->");
const auto markdown = QString(markdownInput).replace(commentPat, "");
static const QRegularExpression headerPat("^# +(.+)$", QRegularExpression::MultilineOption);
const auto match = headerPat.match(markdown);
QString name;
if (match.isValid())
{
name = match.captured(1);
}
else
{
qCritical().nospace() << "Failed to get sky culture name in file " << descrPath
<< ": got " << match.lastCapturedIndex() << " matches instead of 1";
name = "Unknown";
}
QString text = "<h1>" + trans.qtranslate(name, "sky culture") + "</h1>";
static const QRegularExpression sectionNamePat("^## +(.+)$", QRegularExpression::MultilineOption);
QString prevSectionName;
qsizetype prevBodyStartPos = -1;
for (auto it = sectionNamePat.globalMatch(markdown); it.hasNext(); )
{
const auto match = it.next();
const auto sectionName = match.captured(1);
const auto nameStartPos = match.capturedStart(0);
const auto bodyStartPos = match.capturedEnd(0);
if (!prevSectionName.isEmpty())
{
const auto sectionText = convertMarkdownLevel2Section(markdown, prevSectionName, prevBodyStartPos, nameStartPos, trans);
if(prevSectionName != "Introduction" && prevSectionName != "Description")
text += "<h2>" + qc_(prevSectionName, "Name of a section in sky culture description") + "</h2>\n";
if (!sectionText.isEmpty())
{
text += sectionText + "\n";
}
}
prevBodyStartPos = bodyStartPos;
prevSectionName = sectionName;
}
if (prevBodyStartPos >= 0)
{
const auto sectionText = convertMarkdownLevel2Section(markdown, prevSectionName, prevBodyStartPos, markdown.size(), trans);
if (!sectionText.isEmpty())
{
text += "<h2>" + qc_(prevSectionName, "Name of a section in sky culture description") + "</h2>\n";
text += sectionText;
}
}
return text;
}
QString StelSkyCultureMgr::getCurrentSkyCultureHtmlDescription()
{
QString lang = StelApp::getInstance().getLocaleMgr().getAppLanguage();
if (!QString("pt_BR zh_CN zh_HK zh_TW").contains(lang))
{
lang = lang.split("_").at(0);
}
const QString descPath = currentSkyCulture.path + "/description.md";
const bool pathExists = QFileInfo::exists(descPath);
if (!pathExists)
qWarning() << "Can't find description for skyculture" << currentSkyCulture.id;
QString description;
if (!pathExists)
{
description = QString("<h2>%1</2><p>%2</p>").arg(getCurrentSkyCultureNameI18(), q_("No description"));
}
else
{
QFile f(descPath);
if(f.open(QIODevice::ReadOnly))
{
const auto markdown = QString::fromUtf8(f.readAll());
description = descriptionMarkdownToHTML(markdown, descPath);
}
else
{
qWarning().nospace() << "Failed to open sky culture description file " << descPath << ": " << f.errorString();
}
}
description.append(getCurrentSkyCultureHtmlLicense());
description.append(getCurrentSkyCultureHtmlClassification());
description.append(getCurrentSkyCultureHtmlRegion());
return description;
}
QString StelSkyCultureMgr::directoryToSkyCultureEnglish(const QString& directory) const
{
return dirToNameEnglish[directory].englishName;
}
QString StelSkyCultureMgr::directoryToSkyCultureI18(const QString& directory) const
{
QString culture = dirToNameEnglish[directory].englishName;
if (culture=="")
{
qWarning().nospace() << "StelSkyCultureMgr::directoryToSkyCultureI18("
<< QDir::toNativeSeparators(directory) << "): could not find directory";
return "";
}
return q_(culture);
}
QString StelSkyCultureMgr::skyCultureI18ToDirectory(const QString& cultureName) const
{
const StelTranslator& trans = StelApp::getInstance().getLocaleMgr().getSkyCultureDescriptionsTranslator();
QMapIterator<QString, StelSkyCulture> i(dirToNameEnglish);
while (i.hasNext())
{
i.next();
if (trans.qtranslate(i.value().englishName, "sky culture") == cultureName)
return i.key();
}
return "";
}