File indexing completed on 2024-07-21 06:28:42

0001 /*
0002     SPDX-FileCopyrightText: 2015 M.S.Adityan <msadityan@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "constellationartcomponent.h"
0008 
0009 #include "constellationsart.h"
0010 #include "culturelist.h"
0011 #include "kspaths.h"
0012 #include "Options.h"
0013 #ifndef KSTARS_LITE
0014 #include "skymap.h"
0015 #include "skypainter.h"
0016 #endif
0017 
0018 #include <QSqlError>
0019 #include <QSqlQuery>
0020 
0021 ConstellationArtComponent::ConstellationArtComponent(SkyComposite *parent, CultureList *cultures) : SkyComponent(parent)
0022 {
0023     cultureName = cultures->current();
0024     records     = 0;
0025     loadData();
0026 }
0027 
0028 ConstellationArtComponent::~ConstellationArtComponent()
0029 {
0030     deleteData();
0031 }
0032 
0033 void ConstellationArtComponent::deleteData()
0034 {
0035     qDeleteAll(m_ConstList);
0036     m_ConstList.clear();
0037 }
0038 
0039 void ConstellationArtComponent::loadData()
0040 {
0041     if (m_ConstList.isEmpty())
0042     {
0043         QSqlDatabase skydb = QSqlDatabase::addDatabase("QSQLITE", "skycultures");
0044         QString dbfile     = KSPaths::locate(QStandardPaths::AppLocalDataLocation, "skycultures.sqlite");
0045 
0046         skydb.setDatabaseName(dbfile);
0047         if (skydb.open() == false)
0048         {
0049             qWarning() << "Unable to open sky cultures database file " << dbfile;
0050             return;
0051         }
0052         QSqlQuery get_query(skydb);
0053 
0054         if (cultureName == "Western")
0055         {
0056             if (!get_query.exec("SELECT * FROM western"))
0057             {
0058                 qDebug() << Q_FUNC_INFO << get_query.lastError();
0059                 return;
0060             }
0061         }
0062         if (cultureName == "Inuit")
0063         {
0064             if (!get_query.exec("SELECT * FROM inuit"))
0065             {
0066                 qDebug() << Q_FUNC_INFO << get_query.lastError();
0067                 return;
0068             }
0069         }
0070 
0071         while (get_query.next())
0072         {
0073             QString abbreviation = get_query.value("Abbreviation").toString();
0074             QString filename     = get_query.value("Filename").toString();
0075             QString midpointRA   = get_query.value("MidpointRA").toString();
0076             QString midpointDEC  = get_query.value("MidpointDEC").toString();
0077             double pa            = get_query.value("Position Angle").toDouble();
0078             double w             = get_query.value("Width").toDouble();
0079             double h             = get_query.value("Height").toDouble();
0080 
0081             dms midpointra  = dms::fromString(midpointRA, false);
0082             dms midpointdec = dms::fromString(midpointDEC, true);
0083 
0084             // appends constellation info
0085             ConstellationsArt *ca = new ConstellationsArt(midpointra, midpointdec, pa, w, h, abbreviation, filename);
0086             m_ConstList.append(ca);
0087             //qDebug()<<"Successfully read skyculture.sqlite"<<abbreviation<<filename<<midpointRA<<midpointDEC<<pa<<w<<h;
0088             records++;
0089         }
0090         //qDebug()<<"Successfully processed"<<records<<"records for"<<cultureName<<"sky culture";
0091         skydb.close();
0092     }
0093 }
0094 
0095 void ConstellationArtComponent::showList()
0096 {
0097     int i = 0;
0098     for (i = 0; i < m_ConstList.size(); i++)
0099     {
0100         qDebug() << Q_FUNC_INFO << m_ConstList[i]->getAbbrev() << m_ConstList[i]->getImageFileName();
0101         qDebug() << Q_FUNC_INFO << m_ConstList[i]->pa();
0102     }
0103 }
0104 
0105 void ConstellationArtComponent::draw(SkyPainter *skyp)
0106 {
0107     Q_UNUSED(skyp)
0108 #ifndef KSTARS_LITE
0109     if (Options::showConstellationArt() && SkyMap::IsSlewing() == false)
0110     {
0111         for (int i = 0; i < records; i++)
0112             skyp->drawConstellationArtImage(m_ConstList[i]);
0113     }
0114 
0115 //Loops through the QList containing all data required to draw constellations.
0116 #endif
0117 }