File indexing completed on 2024-07-14 07:17:24

0001 /*
0002     SPDX-FileCopyrightText: 2005 Thomas Kabelmann <thomas.kabelmann@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "solarsystemsinglecomponent.h"
0008 #include "solarsystemcomposite.h"
0009 #include "skycomponent.h"
0010 #include <KLocalizedString>
0011 
0012 #include "dms.h"
0013 #include "kstarsdata.h"
0014 #include "skyobjects/starobject.h"
0015 #include "skyobjects/ksplanetbase.h"
0016 #include "skyobjects/ksplanet.h"
0017 #ifdef KSTARS_LITE
0018 #include "skymaplite.h"
0019 #else
0020 #include "skymap.h"
0021 #endif
0022 
0023 #include "Options.h"
0024 #include "skylabeler.h"
0025 
0026 #include "skypainter.h"
0027 #include "projections/projector.h"
0028 
0029 SolarSystemSingleComponent::SolarSystemSingleComponent(SolarSystemComposite *parent, KSPlanetBase *kspb,
0030         bool (*visibleMethod)(), bool isMoon)
0031     : SkyComponent(parent), visible(visibleMethod), m_isMoon(isMoon), m_Earth(parent->earth()), m_Planet(kspb)
0032 {
0033     m_Planet->loadData();
0034     if (!m_Planet->name().isEmpty())
0035     {
0036         objectNames(m_Planet->type()).append(m_Planet->name());
0037         objectLists(m_Planet->type()).append(QPair<QString, const SkyObject *>(m_Planet->name(), m_Planet));
0038     }
0039     if (!m_Planet->longname().isEmpty() && m_Planet->longname() != m_Planet->name())
0040     {
0041         objectNames(m_Planet->type()).append(m_Planet->longname());
0042         objectLists(m_Planet->type()).append(QPair<QString, const SkyObject *>(m_Planet->longname(), m_Planet));
0043     }
0044 }
0045 
0046 SolarSystemSingleComponent::~SolarSystemSingleComponent()
0047 {
0048     removeFromNames(m_Planet);
0049     removeFromLists(m_Planet);
0050     delete m_Planet;
0051 }
0052 
0053 bool SolarSystemSingleComponent::selected()
0054 {
0055     return visible();
0056 }
0057 
0058 SkyObject *SolarSystemSingleComponent::findByName(const QString &name, bool exact)
0059 {
0060     Q_UNUSED(exact)
0061     if (QString::compare(m_Planet->name(), name, Qt::CaseInsensitive) == 0 ||
0062             QString::compare(m_Planet->longname(), name, Qt::CaseInsensitive) == 0 ||
0063             QString::compare(m_Planet->name2(), name, Qt::CaseInsensitive) == 0)
0064         return m_Planet;
0065     return nullptr;
0066 }
0067 
0068 SkyObject *SolarSystemSingleComponent::objectNearest(SkyPoint *p, double &maxrad)
0069 {
0070     double r = m_Planet->angularDistanceTo(p).Degrees();
0071     if (r < maxrad)
0072     {
0073         maxrad = r;
0074         return m_Planet;
0075     }
0076     return nullptr;
0077 }
0078 
0079 void SolarSystemSingleComponent::update(KSNumbers *)
0080 {
0081     KStarsData *data = KStarsData::Instance();
0082     if (selected())
0083         m_Planet->EquatorialToHorizontal(data->lst(), data->geo()->lat());
0084 }
0085 
0086 void SolarSystemSingleComponent::updateSolarSystemBodies(KSNumbers *num)
0087 {
0088     if (!m_isMoon && selected())
0089     {
0090         KStarsData *data = KStarsData::Instance();
0091         m_Planet->findPosition(num, data->geo()->lat(), data->lst(), m_Earth);
0092         m_Planet->EquatorialToHorizontal(data->lst(), data->geo()->lat());
0093         if (m_Planet->hasTrail())
0094             m_Planet->updateTrail(data->lst(), data->geo()->lat());
0095     }
0096 }
0097 
0098 // NOTE: This seems like code duplication, and yes IT IS. But there may be some
0099 // NOTE: changes to be made to it later on, and calling `updateSolarSystemBodies`
0100 // NOTE: is ugly.
0101 void SolarSystemSingleComponent::updateMoons(KSNumbers *num)
0102 {
0103     KStarsData *data = KStarsData::Instance();
0104     m_Planet->findPosition(num, data->geo()->lat(), data->lst(), m_Earth);
0105     m_Planet->EquatorialToHorizontal(data->lst(), data->geo()->lat());
0106     if (m_Planet->hasTrail())
0107         m_Planet->updateTrail(data->lst(), data->geo()->lat());
0108 }
0109 
0110 void SolarSystemSingleComponent::draw(SkyPainter *skyp)
0111 {
0112     if (!selected())
0113         return;
0114 
0115     skyp->setPen(m_Planet->color());
0116     skyp->setBrush(m_Planet->color());
0117 
0118     bool drawn = skyp->drawPlanet(m_Planet);
0119     if (drawn && Options::showPlanetNames())
0120         SkyLabeler::AddLabel(m_Planet, SkyLabeler::PLANET_LABEL);
0121 }
0122 
0123 void SolarSystemSingleComponent::drawTrails(SkyPainter *skyp)
0124 {
0125     if (selected())
0126         m_Planet->drawTrail(skyp);
0127 }