File indexing completed on 2024-04-21 14:47:13

0001 /*
0002     SPDX-FileCopyrightText: 2010 Akarsh Simha <akarsh.simha@kdemail.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "skymapqdraw.h"
0008 #include "skymapcomposite.h"
0009 #include "skyqpainter.h"
0010 #include "skymap.h"
0011 #include "projections/projector.h"
0012 #include "printing/legend.h"
0013 #include "kstars_debug.h"
0014 #include <QPainterPath>
0015 
0016 SkyMapQDraw::SkyMapQDraw(SkyMap *sm) : QWidget(sm), SkyMapDrawAbstract(sm)
0017 {
0018     m_SkyPixmap = new QPixmap(width(), height());
0019     m_SkyPainter.reset(new SkyQPainter(this, m_SkyPixmap));
0020 }
0021 
0022 SkyMapQDraw::~SkyMapQDraw()
0023 {
0024     delete m_SkyPixmap;
0025 }
0026 
0027 void SkyMapQDraw::paintEvent(QPaintEvent *event)
0028 {
0029     Q_UNUSED(event)
0030     // This is machinery to prevent multiple concurrent paint events / recursive paint events
0031     if (m_DrawLock)
0032     {
0033         qCDebug(KSTARS) << "Prevented a recursive / concurrent draw!";
0034         return;
0035     }
0036     setDrawLock(true);
0037 
0038     // JM 2016-05-03: Not needed since we're not using OpenGL for now
0039     //calculateFPS();
0040 
0041     //If computeSkymap is false, then we just refresh the window using the stored sky pixmap
0042     //and draw the "overlays" on top.  This lets us update the overlay information rapidly
0043     //without needing to recompute the entire skymap.
0044     //use update() to trigger this "short" paint event; to force a full "recompute"
0045     //of the skymap, use forceUpdate().
0046 
0047     if (!m_SkyMap->computeSkymap)
0048     {
0049         QPainter p;
0050         p.begin(this);
0051         p.drawLine(0, 0, 1, 1); // Dummy operation to circumvent bug. TODO: Add details
0052         p.drawPixmap(0, 0, *m_SkyPixmap);
0053         drawOverlays(p);
0054         p.end();
0055 
0056         setDrawLock(false);
0057         return; // exit because the pixmap is repainted and that's all what we want
0058     }
0059 
0060     m_SkyMap->updateInfoBoxes();
0061     m_SkyMap->setupProjector();
0062 
0063     m_SkyPixmap->fill(Qt::black);
0064     m_SkyPainter->setPaintDevice(m_SkyPixmap);
0065 
0066     //FIXME: we may want to move this into the components.
0067     m_SkyPainter->begin();
0068 
0069     //Draw all sky elements
0070     m_SkyPainter->drawSkyBackground();
0071 
0072     // Set Clipping
0073     QPainterPath path;
0074     path.addPolygon(m_SkyMap->projector()->clipPoly());
0075     m_SkyPainter->setClipPath(path);
0076     m_SkyPainter->setClipping(true);
0077 
0078     m_KStarsData->skyComposite()->draw(m_SkyPainter.data());
0079     //Finish up
0080     m_SkyPainter->end();
0081 
0082     QPainter psky2;
0083     psky2.begin(this);
0084     psky2.drawLine(0, 0, 1, 1); // Dummy op.
0085     psky2.drawPixmap(0, 0, *m_SkyPixmap);
0086     drawOverlays(psky2);
0087     psky2.end();
0088 
0089     if (m_SkyMap->m_previewLegend)
0090     {
0091         m_SkyMap->m_legend.paintLegend(m_SkyPixmap);
0092     }
0093 
0094     m_SkyMap->computeSkymap = false; // use forceUpdate() to compute new skymap else old pixmap will be shown
0095 
0096     setDrawLock(false);
0097 }
0098 
0099 void SkyMapQDraw::resizeEvent(QResizeEvent *e)
0100 {
0101     Q_UNUSED(e)
0102     delete m_SkyPixmap;
0103     m_SkyPixmap = new QPixmap(width(), height());
0104 }