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

0001 /*
0002     SPDX-FileCopyrightText: 2005 Jason Harris <kstars@30doradus.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "listcomponent.h"
0008 
0009 #include "kstarsdata.h"
0010 #ifndef KSTARS_LITE
0011 #include "skymap.h"
0012 #endif
0013 
0014 ListComponent::ListComponent(SkyComposite *parent) : SkyComponent(parent)
0015 {
0016 }
0017 
0018 ListComponent::~ListComponent()
0019 {
0020     qDeleteAll(m_ObjectList);
0021     m_ObjectList.clear();
0022     m_ObjectHash.clear();
0023 
0024     clear();
0025 }
0026 
0027 void ListComponent::clear()
0028 {
0029     while (!m_ObjectList.isEmpty())
0030     {
0031         SkyObject *o = m_ObjectList.takeFirst();
0032         removeFromNames(o);
0033         delete o;
0034     }
0035 }
0036 
0037 void ListComponent::appendListObject(SkyObject *object)
0038 {
0039     // Append to the Object List
0040     m_ObjectList.append(object);
0041 
0042     // Insert multiple Names
0043     m_ObjectHash.insert(object->name().toLower(), object);
0044     m_ObjectHash.insert(object->longname().toLower(), object);
0045     m_ObjectHash.insert(object->name2().toLower(), object);
0046 }
0047 
0048 void ListComponent::update(KSNumbers *num)
0049 {
0050     if (!selected())
0051         return;
0052     KStarsData *data = KStarsData::Instance();
0053     foreach (SkyObject *o, m_ObjectList)
0054     {
0055         if (num)
0056             o->updateCoords(num);
0057         o->EquatorialToHorizontal(data->lst(), data->geo()->lat());
0058     }
0059 }
0060 
0061 SkyObject *ListComponent::findByName(const QString &name, bool exact)
0062 {
0063     Q_UNUSED(exact)
0064     return m_ObjectHash[name.toLower()]; // == nullptr if not found.
0065 }
0066 
0067 SkyObject *ListComponent::objectNearest(SkyPoint *p, double &maxrad)
0068 {
0069     if (!selected())
0070         return nullptr;
0071 
0072     SkyObject *oBest = nullptr;
0073     foreach (SkyObject *o, m_ObjectList)
0074     {
0075         double r = o->angularDistanceTo(p).Degrees();
0076         if (r < maxrad)
0077         {
0078             oBest  = o;
0079             maxrad = r;
0080         }
0081     }
0082     return oBest;
0083 }