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

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 "skycomposite.h"
0008 
0009 #include "skyobjects/skyobject.h"
0010 #include <qdebug.h>
0011 
0012 SkyComposite::SkyComposite(SkyComposite *parent) : SkyComponent(parent)
0013 {
0014 }
0015 
0016 SkyComposite::~SkyComposite()
0017 {
0018     qDeleteAll(components());
0019     m_Components.clear();
0020 }
0021 
0022 void SkyComposite::addComponent(SkyComponent *component, int priority)
0023 {
0024     //qDebug() << Q_FUNC_INFO << "Adding sky component " << component << " of type " << typeid( *component ).name() << " with priority " << priority;
0025     m_Components.insert(priority, component);
0026     /*
0027     foreach( SkyComponent *p, components() ) {
0028         qDebug() << Q_FUNC_INFO << "List now has: " << p << " of type " << typeid( *p ).name();
0029     }
0030     */
0031 }
0032 
0033 void SkyComposite::removeComponent(SkyComponent *const component)
0034 {
0035     // qDebug() << Q_FUNC_INFO << "Removing sky component " << component << " of type " << typeid( *component ).name();
0036     QMap<int, SkyComponent *>::iterator it;
0037     for (it = m_Components.begin(); it != m_Components.end();)
0038     {
0039         if (it.value() == component)
0040             it = m_Components.erase(it);
0041         else
0042             ++it;
0043     }
0044     /*
0045     foreach( SkyComponent *p, components() ) {
0046         qDebug() << Q_FUNC_INFO << "List now has: " << p << " of type " << typeid( *p ).name();
0047     }
0048     */
0049 }
0050 
0051 void SkyComposite::draw(SkyPainter *skyp)
0052 {
0053     if (selected())
0054         foreach (SkyComponent *component, components())
0055             component->draw(skyp);
0056 }
0057 
0058 void SkyComposite::update(KSNumbers *num)
0059 {
0060     foreach (SkyComponent *component, components())
0061         component->update(num);
0062 }
0063 
0064 SkyObject *SkyComposite::findByName(const QString &name, bool exact)
0065 {
0066     for (const auto &oneComponent : components())
0067     {
0068         SkyObject *o = oneComponent->findByName(name, exact);
0069         if (o)
0070             return o;
0071     }
0072     return nullptr;
0073 }
0074 
0075 SkyObject *SkyComposite::objectNearest(SkyPoint *p, double &maxrad)
0076 {
0077     if (!selected())
0078         return nullptr;
0079     SkyObject *oBest = nullptr;
0080     foreach (SkyComponent *comp, components())
0081     {
0082         //qDebug() << Q_FUNC_INFO << "Checking " << typeid( *comp ).name() <<" for oBest";
0083         SkyObject *oTry = comp->objectNearest(p, maxrad);
0084         if (oTry)
0085         {
0086             oBest = oTry;
0087             maxrad =
0088                 p->angularDistanceTo(oBest).Degrees() *
0089                 0.95; // Set a new maxrad, smaller than original to give priority to the earlier objects in the list.
0090             // qDebug() << Q_FUNC_INFO << "oBest = " << oBest << " of type " << typeid( *oTry ).name() << "; updated maxrad = " << maxrad;
0091         }
0092     }
0093     // qDebug() << Q_FUNC_INFO << "Returning best match: oBest = " << oBest;
0094     return oBest; //will be 0 if no object nearer than maxrad was found
0095 }