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 #pragma once
0008 
0009 #include "skycomponent.h"
0010 
0011 #include <QList>
0012 #include <QMap>
0013 
0014 class KSNumbers;
0015 
0016 /**
0017  * @class SkyComposite
0018  * SkyComposite is a kind of container class for SkyComponent objects. The SkyComposite is
0019  * responsible for distributing calls to functions like draw() and update() to its children,
0020  * which can be SkyComponents or other SkyComposites with their own children. This is based
0021  * on the "composite/component" design pattern.
0022  *
0023  * Performance tuning: Sometimes it will be better to override a virtual function and do
0024  * the work in the composite instead of delegating the request to all sub components.
0025  *
0026  * @author Thomas Kabelmann
0027  * @version 0.1
0028  */
0029 class SkyComposite : public SkyComponent
0030 {
0031     public:
0032         /**
0033          * @short Constructor
0034          * @p parent pointer to the parent SkyComponent
0035          */
0036         explicit SkyComposite(SkyComposite *parent = nullptr);
0037 
0038         /** *@short Destructor */
0039         ~SkyComposite() override;
0040 
0041         /**
0042          * @short Delegate draw requests to all sub components
0043          * @p psky Reference to the QPainter on which to paint
0044          */
0045         void draw(SkyPainter *skyp) override;
0046 
0047         /**
0048          * @short Delegate update-position requests to all sub components
0049          *
0050          * This function usually just updates the Horizontal (Azimuth/Altitude) coordinates.
0051          * However, the precession and nutation must also be recomputed periodically. Requests to
0052          * do so are sent through the doPrecess parameter.
0053          * @p num Pointer to the KSNumbers object
0054          * @sa updatePlanets()
0055          * @sa updateMoons()
0056          * @note By default, the num parameter is nullptr, indicating that Precession/Nutation
0057          * computation should be skipped; this computation is only occasionally required.
0058          */
0059         void update(KSNumbers *num = nullptr) override;
0060 
0061         /**
0062          * @short Add a new sub component to the composite
0063          * @p comp Pointer to the SkyComponent to be added
0064          * @p priority A priority ordering for various operations on the list of all sky components
0065          * (notably objectNearest())
0066          */
0067         void addComponent(SkyComponent *comp, int priority = 1024);
0068 
0069         /**
0070          * @short Remove a sub component from the composite
0071          * @p comp Pointer to the SkyComponent to be removed.
0072          */
0073         void removeComponent(SkyComponent *const comp);
0074 
0075         /**
0076          * @short Search the children of this SkyComposite for a SkyObject whose name matches
0077          * the argument.
0078          *
0079          * The objects' primary, secondary and long-form names will all be checked for a match.
0080          * @p name the name to be matched
0081          * @p exact If true, it will return an exact match, otherwise it can return
0082          * a partial match.
0083          * @return a pointer to the SkyObject whose name matches
0084          * the argument, or a nullptr pointer if no match was found.
0085          */
0086         SkyObject *findByName(const QString &name, bool exact = true) override;
0087 
0088         /**
0089          * @short Identify the nearest SkyObject to the given SkyPoint, among the children of
0090          * this SkyComposite
0091          * @p p pointer to the SkyPoint around which to search.
0092          * @p maxrad reference to current search radius
0093          * @return a pointer to the nearest SkyObject
0094          */
0095         SkyObject *objectNearest(SkyPoint *p, double &maxrad) override;
0096 
0097         QList<SkyComponent *> components()
0098         {
0099             return m_Components.values();
0100         }
0101 
0102         QMap<int, SkyComponent *> &componentsWithPriorities()
0103         {
0104             return m_Components;
0105         }
0106 
0107     private:
0108         QMultiMap<int, SkyComponent *> m_Components;
0109 };