File indexing completed on 2024-04-14 03:42:49

0001 #include <QSGTexture>
0002 #include <QQuickWindow>
0003 
0004 #include "rootnode.h"
0005 #include "skymaplite.h"
0006 #include "projections/projector.h"
0007 #include "skymapcomposite.h"
0008 #include "solarsystemcomposite.h"
0009 #include "Options.h"
0010 
0011 #include "constellationboundarylines.h"
0012 #include "constellationlines.h"
0013 #include "starcomponent.h"
0014 
0015 #include "horizontalcoordinategrid.h"
0016 #include "equatorialcoordinategrid.h"
0017 
0018 #include "ecliptic.h"
0019 #include "equator.h"
0020 
0021 //SkyItems
0022 #include "kstarslite/skyitems/staritem.h"
0023 #include "kstarslite/skyitems/deepskyitem.h"
0024 #include "kstarslite/skyitems/planetsitem.h"
0025 #include "kstarslite/skyitems/asteroidsitem.h"
0026 #include "kstarslite/skyitems/cometsitem.h"
0027 #include "kstarslite/skyitems/horizonitem.h"
0028 #include "kstarslite/skyitems/lines/linesitem.h"
0029 #include "kstarslite/skyitems/labelsitem.h"
0030 #include "kstarslite/skyitems/constellationnamesitem.h"
0031 #include "kstarslite/skyitems/constellationartitem.h"
0032 #include "kstarslite/skyitems/satellitesitem.h"
0033 #include "kstarslite/skyitems/supernovaeitem.h"
0034 
0035 //Lines
0036 #include "kstarslite/skyitems/lines/equatoritem.h"
0037 #include "kstarslite/skyitems/lines/eclipticitem.h"
0038 #include "kstarslite/skyitems/lines/milkywayitem.h"
0039 
0040 //Symbols
0041 #include "kstarslite/skyitems/telescopesymbolsitem.h"
0042 
0043 #include "kstarslite/skyitems/fovitem.h"
0044 
0045 #include <QSGFlatColorMaterial>
0046 
0047 RootNode::RootNode() : m_skyMapLite(SkyMapLite::Instance())
0048     {
0049     SkyMapLite::setRootNode(this);
0050     genCachedTextures();
0051 
0052     m_skyComposite = KStarsData::Instance()->skyComposite();
0053 
0054     // LabelsItem needs to be created first so that other items could insert their labels in labelsList
0055     m_labelsItem = new LabelsItem();
0056 
0057     m_MWItem = new MilkyWayItem(m_skyComposite->milkyWay(), this);
0058 
0059     m_linesItem = new LinesItem(this);
0060 
0061     m_linesItem->addLinesComponent(m_skyComposite->equatorialCoordGrid(), "EquatorialGridColor", 1, Qt::DotLine);
0062     m_linesItem->addLinesComponent(m_skyComposite->horizontalCoordGrid(), "HorizontalGridColor", 2, Qt::DotLine);
0063 
0064     if (m_skyComposite->currentCulture() == "Western")
0065     {
0066         m_linesItem->addLinesComponent(m_skyComposite->constellationBoundary(), "CBoundColor", 1, Qt::SolidLine);
0067     }
0068 
0069     m_artItem = new ConstellationArtItem(m_skyComposite->constellationArt(), this);
0070 
0071     m_linesItem->addLinesComponent(m_skyComposite->constellationLines(), "CLineColor", 1, Qt::SolidLine);
0072 
0073     m_equator  = new EquatorItem(m_skyComposite->equator(), this);
0074     m_ecliptic = new EclipticItem(m_skyComposite->ecliptic(), this);
0075 
0076     m_dsoItem  = new DeepSkyItem(m_skyComposite->deepSkyComponent(), this);
0077     m_starItem = new StarItem(StarComponent::Instance(), this);
0078 
0079     m_solarSystem = m_skyComposite->solarSystemComposite();
0080 
0081     m_planetsItem   = new PlanetsItem(m_solarSystem->planets(), /* m_solarSystem->planetMoonsComponent(),*/ this);
0082     m_asteroidsItem = new AsteroidsItem(m_solarSystem->asteroids(), this);
0083     m_cometsItem    = new CometsItem(m_solarSystem->comets(), this);
0084 
0085     m_constelNamesItem = new ConstellationNamesItem(m_skyComposite->constellationNamesComponent(), this);
0086 
0087     m_satItem   = new SatellitesItem(m_skyComposite->satellites(), this);
0088     m_snovaItem = new SupernovaeItem(m_skyComposite->supernovaeComponent(), this);
0089 
0090     m_horizonItem = new HorizonItem(m_skyComposite->horizon(), this);
0091 
0092     m_FOVItem = new FOVItem(this);
0093 
0094     m_telescopeSymbols = new TelescopeSymbolsItem(this);
0095 
0096     setIsRectangular(false);
0097     updateClipPoly();
0098 
0099     m_labelsItem->setRootNode(this);
0100 }
0101 
0102 void RootNode::testLeakAdd()
0103 {
0104     /*    m_linesItem = new LinesItem(this);
0105 
0106     m_linesItem->addLinesComponent( m_skyComposite->equatorialCoordGrid(), "EquatorialGridColor", 1, Qt::DotLine );
0107     m_linesItem->addLinesComponent( m_skyComposite->horizontalCoordGrid(), "HorizontalGridColor", 2, Qt::DotLine );
0108 
0109     m_linesItem->addLinesComponent( m_skyComposite->constellationBoundary(), "CBoundColor", 1, Qt::SolidLine );
0110     m_linesItem->addLinesComponent( m_skyComposite->constellationLines(), "CLineColor", 1, Qt::SolidLine );
0111 
0112     m_starItem = new StarItem(m_skyComposite->starComponent(), this);
0113 
0114     m_solarSystem = m_skyComposite->solarSystemComposite();
0115 
0116     m_equator = new EquatorItem(m_skyComposite->equator(),this);
0117     m_ecliptic = new EclipticItem(m_skyComposite->ecliptic(),this);
0118 
0119     m_planetsItem = new PlanetsItem(m_solarSystem->planets(), m_solarSystem->planetMoonsComponent(), this);
0120     m_asteroidsItem = new AsteroidsItem(m_solarSystem->asteroids(), this);
0121     m_cometsItem = new CometsItem(m_solarSystem->comets(), this);
0122 
0123     m_constelNamesItem = new ConstellationNamesItem(m_skyComposite->constellationNamesComponent(), this);
0124     m_horizonItem = new HorizonItem(m_skyComposite->horizon(), this);*/
0125 }
0126 
0127 void RootNode::testLeakDelete()
0128 {
0129     /*removeChildNode(m_linesItem);
0130     delete m_linesItem;
0131 
0132     removeChildNode(m_starItem);
0133     delete m_starItem;
0134 
0135     removeChildNode(m_equator);
0136     delete m_equator;
0137     removeChildNode(m_ecliptic);
0138     delete m_ecliptic;
0139 
0140     removeChildNode(m_planetsItem);
0141     delete m_planetsItem;
0142     removeChildNode(m_asteroidsItem);
0143     delete m_asteroidsItem;
0144     removeChildNode(m_cometsItem);
0145     delete m_cometsItem;
0146 
0147     removeChildNode(m_constelNamesItem);
0148     delete m_constelNamesItem;
0149 
0150     removeChildNode(m_horizonItem);
0151     delete m_horizonItem;*/
0152 }
0153 
0154 RootNode::~RootNode()
0155 {
0156     for (int i = 0; i < m_textureCache.length(); ++i)
0157     {
0158         for (int c = 0; c < m_textureCache[i].size(); ++c)
0159         {
0160             delete m_textureCache[i][c];
0161         }
0162     }
0163 }
0164 
0165 void RootNode::genCachedTextures()
0166 {
0167     QVector<QVector<QPixmap *>> images = m_skyMapLite->getImageCache();
0168 
0169     QQuickWindow *win = m_skyMapLite->window();
0170 
0171     m_textureCache = QVector<QVector<QSGTexture *>>(images.length());
0172 
0173     for (int i = 0; i < m_textureCache.length(); ++i)
0174     {
0175         int length        = images[i].length();
0176         m_textureCache[i] = QVector<QSGTexture *>(length);
0177         for (int c = 1; c < length; ++c)
0178         {
0179             m_textureCache[i][c] =
0180                 win->createTextureFromImage(images[i][c]->toImage(), QQuickWindow::TextureCanUseAtlas);
0181         }
0182     }
0183 }
0184 
0185 QSGTexture *RootNode::getCachedTexture(int size, char spType)
0186 {
0187     return m_textureCache[SkyMapLite::Instance()->harvardToIndex(spType)][size];
0188 }
0189 
0190 void RootNode::updateClipPoly()
0191 {
0192     QPolygonF newClip = m_skyMapLite->projector()->clipPoly();
0193     m_clipPoly        = newClip;
0194 
0195     const int size = m_clipPoly.size();
0196     if (!m_clipGeometry)
0197     {
0198         m_clipGeometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), size);
0199         m_clipGeometry->setDrawingMode(GL_TRIANGLE_FAN);
0200         setGeometry(m_clipGeometry);
0201         setFlag(QSGNode::OwnsGeometry);
0202     }
0203     m_clipGeometry->allocate(size);
0204 
0205     QSGGeometry::Point2D *vertex = m_clipGeometry->vertexDataAsPoint2D();
0206     for (int i = 0; i < size; i++)
0207     {
0208         vertex[i].x = m_clipPoly[i].x();
0209         vertex[i].y = m_clipPoly[i].y();
0210     }
0211     m_clipGeometry->markIndexDataDirty();
0212     m_clipGeometry->markVertexDataDirty();
0213     markDirty(QSGNode::DirtyGeometry);
0214 }
0215 
0216 void RootNode::update(bool clearTextures)
0217 {
0218     updateClipPoly();
0219     if (clearTextures)
0220     {
0221         //First we need to create new textures and only after all PointNodes changed their textures we
0222         //can delete old textures
0223         genCachedTextures();
0224     }
0225 
0226     m_MWItem->update();
0227 
0228     m_artItem->update();
0229 
0230     m_linesItem->update();
0231 
0232     m_equator->update();
0233     m_ecliptic->update();
0234 
0235     m_dsoItem->update();
0236     m_starItem->update();
0237 
0238     //TODO: Move this check somewhere else (create a separate function)
0239     if (Options::showSolarSystem())
0240     {
0241         m_planetsItem->update();
0242         if (!Options::showAsteroids())
0243         {
0244             if (m_asteroidsItem)
0245                 delete m_asteroidsItem;
0246         }
0247         else
0248         {
0249             if (!m_asteroidsItem)
0250                 m_asteroidsItem = new AsteroidsItem(m_solarSystem->asteroids(), this);
0251             m_asteroidsItem->update();
0252         }
0253 
0254         if (!Options::showComets())
0255         {
0256             if (m_cometsItem)
0257                 delete m_cometsItem;
0258         }
0259         else
0260         {
0261             if (!m_cometsItem)
0262                 m_cometsItem = new CometsItem(m_solarSystem->comets(), this);
0263             m_cometsItem->update();
0264         }
0265     }
0266     else
0267     {
0268         m_planetsItem->hide();
0269         if (m_asteroidsItem)
0270         {
0271             delete m_asteroidsItem;
0272             m_asteroidsItem = 0;
0273         }
0274         if (m_cometsItem)
0275         {
0276             delete m_cometsItem;
0277             m_cometsItem = 0;
0278         }
0279     }
0280 
0281     m_constelNamesItem->update();
0282 
0283     m_satItem->update();
0284     m_snovaItem->update();
0285     m_horizonItem->update();
0286 
0287     m_telescopeSymbols->update();
0288     m_labelsItem->update();
0289 
0290     m_FOVItem->update();
0291 
0292     if (clearTextures)
0293     {
0294         //Delete old textures
0295         if (m_oldTextureCache.length())
0296         {
0297             foreach (QVector<QSGTexture *> textures, m_oldTextureCache)
0298             {
0299                 qDeleteAll(textures.begin(), textures.end());
0300             }
0301         }
0302     }
0303 }