File indexing completed on 2024-03-24 15:18:15

0001 /*
0002     SPDX-FileCopyrightText: 2010 Akarsh Simha <akarshsimha@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <KColorScheme>
0010 
0011 #include <QWidget>
0012 #include <QCalendarWidget>
0013 
0014 class KSMoon;
0015 class KSSun;
0016 class KStarsDateTime;
0017 
0018 class MoonPhaseCalendar : public QCalendarWidget
0019 {
0020     Q_OBJECT
0021 
0022   public:
0023     /**
0024      * Constructor
0025      * @param moon A reference to a (non-const) KSMoon object, that will be updated
0026      * @param sun A reference to a (non-const) KSSun object, that will be updated
0027      * @param parent Parent widget
0028      */
0029     explicit MoonPhaseCalendar(KSMoon &moon, KSSun &sun, QWidget *parent = nullptr);
0030 
0031     ~MoonPhaseCalendar();
0032 
0033     /** @return a suggested size for the widget */
0034     virtual QSize sizeHint() const;
0035 
0036   public slots:
0037 
0038     /**
0039      * Set the geometry of the moon phase calendar (overloaded from QWidget).
0040      * Resizes the cells so as to fill the space of the calendar.
0041      * @note This is called automatically by resize events.
0042      * @p x the x-position of the widget
0043      * @p y the y-position of the widget
0044      * @p w the width of the widget
0045      * @p h the height of the widget
0046      */
0047     virtual void setGeometry(int x, int y, int w, int h);
0048 
0049     virtual void setGeometry(const QRect &r);
0050 
0051   protected:
0052     /**
0053      * Overrides KDateTable::paintEvent() to draw moon phases on the
0054      * calendar cells by calling this->paintCell()
0055      * @note Most of this code is copied from KDateTable::paintEvent()
0056      */
0057     virtual void paintEvent(QPaintEvent *e);
0058 
0059     /**
0060      * Replaces KDateTable::paintCell() to draw moon phases on the calendar cells
0061      * @note Most of this code is copied from KDateTable::paintCell()
0062      */
0063     void paintCell(QPainter *painter, int row, int col, const KColorScheme &colorScheme);
0064 
0065     /**
0066      * @short Loads the moon images, appropriately resized depending
0067      * on the current cell size.
0068      *
0069      * @note This method is very slow and one must avoid calling it more than once.
0070      */
0071     void loadImages();
0072 
0073     /** @short Computes the optimum moon image size */
0074     void computeMoonImageSize();
0075 
0076   private:
0077     /**
0078      * @short Computes the moon phase for the given date.
0079      * @param date  Date / Time of the computation
0080      * @return the _integer_ phase for the given date
0081      */
0082     unsigned short computeMoonPhase(const KStarsDateTime &date);
0083 
0084     QPixmap m_Images[36]; // Array storing moon images against integer phase
0085 
0086     double cellWidth { 0 };
0087     double cellHeight { 0 };
0088     int numWeekRows { 0 };
0089     int numDayColumns { 0 };
0090     int MoonImageSize { 0 };
0091     bool imagesLoaded { false };
0092 
0093     KSMoon &m_Moon;
0094     KSSun &m_Sun;
0095 };