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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Vipul Kumar Singh Médéric Boquien <vipulkrsingh@gmail.com, mboquien@free.fr>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "planetmoonscomponent.h"
0008 
0009 #include "kstarsdata.h"
0010 #include "Options.h"
0011 #include "skylabeler.h"
0012 #include "skypainter.h"
0013 #include "kssun.h"
0014 #include "solarsystemcomposite.h"
0015 #include "solarsystemsinglecomponent.h"
0016 #include "projections/projector.h"
0017 #include "skyobjects/jupitermoons.h"
0018 
0019 PlanetMoonsComponent::PlanetMoonsComponent(SkyComposite *p, SolarSystemSingleComponent *planetComponent,
0020         KSPlanetBase::Planets &_planet)
0021     : SkyComponent(p), planet(_planet), m_Planet(planetComponent)
0022 {
0023     /*
0024     if (planet == KSPlanetBase::JUPITER)
0025         pmoons = new JupiterMoons();
0026     else
0027         pmoons = new SaturnMoons();
0028     */
0029     Q_ASSERT(planet == KSPlanetBase::JUPITER);
0030     //    delete pmoons;
0031     //    pmoons = new JupiterMoons();
0032     //    int nmoons = pmoons->nMoons();
0033 
0034     //    for (int i = 0; i < nmoons; ++i)
0035     //    {
0036     //        objectNames(SkyObject::MOON).append( pmoons->name(i) );
0037     //        objectLists(SkyObject::MOON).append( QPair<QString, const SkyObject*>(pmoons->name(i),pmoons->moon(i)) );
0038     //    }
0039 }
0040 
0041 bool PlanetMoonsComponent::selected()
0042 {
0043     return m_Planet->selected();
0044 }
0045 
0046 #ifndef KSTARS_LITE
0047 void PlanetMoonsComponent::update(KSNumbers *)
0048 {
0049     KStarsData *data = KStarsData::Instance();
0050     if (selected())
0051         pmoons->EquatorialToHorizontal(data->lst(), data->geo()->lat());
0052 }
0053 #endif
0054 
0055 void PlanetMoonsComponent::updateMoons(KSNumbers *num)
0056 {
0057     //FIXME: evil cast
0058     if (selected())
0059         pmoons->findPosition(num, m_Planet->planet(), dynamic_cast<KSSun *>(parent()->findByName(i18n("Sun"))));
0060 }
0061 
0062 SkyObject *PlanetMoonsComponent::findByName(const QString &name, bool exact)
0063 {
0064     Q_UNUSED(exact)
0065     int nmoons = pmoons->nMoons();
0066 
0067     for (int i = 0; i < nmoons; ++i)
0068     {
0069         TrailObject *moon = pmoons->moon(i);
0070         if (QString::compare(moon->name(), name, Qt::CaseInsensitive) == 0 ||
0071                 QString::compare(moon->longname(), name, Qt::CaseInsensitive) == 0 ||
0072                 QString::compare(moon->name2(), name, Qt::CaseInsensitive) == 0)
0073             return moon;
0074     }
0075 
0076     return nullptr;
0077 }
0078 
0079 #ifdef KSTARS_LITE
0080 KSPlanetBase *PlanetMoonsComponent::getPlanet() const
0081 {
0082     return m_Planet->planet();
0083 }
0084 #endif
0085 
0086 SkyObject *PlanetMoonsComponent::objectNearest(SkyPoint *p, double &maxrad)
0087 {
0088     SkyObject *oBest = nullptr;
0089     int nmoons       = pmoons->nMoons();
0090 
0091     if (Options::zoomFactor() < 3000)
0092         return nullptr;
0093 
0094     for (int i = 0; i < nmoons; ++i)
0095     {
0096         SkyObject *moon = pmoons->moon(i);
0097         double r        = moon->angularDistanceTo(p).Degrees();
0098         if (r < maxrad)
0099         {
0100             maxrad = r;
0101             oBest  = moon;
0102         }
0103     }
0104     return oBest;
0105 }
0106 
0107 void PlanetMoonsComponent::draw(SkyPainter *skyp)
0108 {
0109     if (!(planet == KSPlanetBase::JUPITER && Options::showJupiter()))
0110         return;
0111 
0112     //In order to get the z-order right for the moons and the planet,
0113     //we need to first draw the moons that are further away than the planet,
0114     //then re-draw the planet, then draw the moons nearer than the planet.
0115     QList<TrailObject *> frontMoons;
0116     int nmoons = pmoons->nMoons();
0117 
0118     for (int i = 0; i < nmoons; ++i)
0119     {
0120         if (pmoons->z(i) < 0.0) //Moon is nearer than the planet
0121         {
0122             frontMoons.append(pmoons->moon(i));
0123         }
0124         else
0125         {
0126             //Draw Moons that are further than the planet
0127             skyp->drawPointSource(pmoons->moon(i), pmoons->moon(i)->mag());
0128         }
0129     }
0130 
0131     //Now redraw the planet
0132     m_Planet->draw(skyp);
0133 
0134     //Now draw the remaining moons, as stored in frontMoons
0135     foreach (TrailObject *moon, frontMoons)
0136     {
0137         skyp->drawPointSource(moon, moon->mag());
0138     }
0139 
0140     //Draw Moon name labels if at high zoom
0141     if (!(Options::showPlanetNames() && Options::zoomFactor() > 50. * MINZOOM))
0142         return;
0143     for (int i = 0; i < nmoons; ++i)
0144     {
0145         /*
0146         if (planet ==KSPlanetBase::SATURN)
0147             SkyLabeler::AddLabel( pmoons->moon(i), SkyLabeler::SATURN_MOON_LABEL );
0148         else
0149         */
0150         SkyLabeler::AddLabel(pmoons->moon(i), SkyLabeler::JUPITER_MOON_LABEL);
0151     }
0152 }
0153 
0154 void PlanetMoonsComponent::drawTrails(SkyPainter *skyp)
0155 {
0156     if (!selected())
0157         return;
0158     int nmoons = pmoons->nMoons();
0159     for (int i = 0; i < nmoons; ++i)
0160         pmoons->moon(i)->drawTrail(skyp);
0161 }