File indexing completed on 2024-04-21 03:44:48

0001 /*
0002     SPDX-FileCopyrightText: 2001-2002 Pablo de Vicente <vicente@oan.es>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QMap>
0010 #include <QString>
0011 #include <QDialog>
0012 
0013 class QStackedWidget;
0014 class QTextEdit;
0015 class QTreeWidget;
0016 class QTreeWidgetItem;
0017 
0018 /**
0019  * @class AstroCalc
0020  * @brief This is the base class for the KStars astronomical calculator
0021  *
0022  * @author: Pablo de Vicente
0023  * @version 0.9
0024  */
0025 class AstroCalc : public QDialog
0026 {
0027     Q_OBJECT
0028 
0029   public:
0030     explicit AstroCalc(QWidget *parent = nullptr);
0031 
0032     /** @return suggested size of calculator window. */
0033     QSize sizeHint() const override;
0034   public slots:
0035      // Q: Why is this public when we don't have access to navigationPanel anyway?
0036      // Also doesn't seem to be used from outside -- asimha
0037     /** Display calculator module or help text based on item selected. */
0038     void slotItemSelection(QTreeWidgetItem *it);
0039 
0040   private:
0041     /** Pointer to function which return QWidget */
0042     typedef QWidget *(AstroCalc::*WidgetConstructor)();
0043     /**
0044      * Data structure used for lazy widget construction. This class
0045      * construct widget when it requested.
0046      */
0047     class WidgetThunk
0048     {
0049       public:
0050         /**
0051          * Create thunk
0052          * @param acalc  pointer to class.
0053          * @param f      function which construct widget.
0054          */
0055         WidgetThunk(AstroCalc *acalc, const WidgetConstructor& f) : widget(nullptr), calc(acalc), func(f) { }
0056         /**
0057          * Request widget.
0058          * @return newly created widget or cached value.
0059          */
0060         QWidget *eval();
0061 
0062       private:
0063         /// Cached value
0064         QWidget *widget { nullptr };
0065         /// Pointer to calculator
0066         AstroCalc *calc { nullptr };
0067         /// Function call to construct the widget.
0068         WidgetConstructor func;
0069     };
0070 
0071     /**
0072      * Create widget of type T and put it to widget stack. Widget must
0073      * have constructor of type T(QWidget*). Returns constructed widget.
0074      */
0075     template <typename T>
0076     inline QWidget *addToStack();
0077 
0078     /**
0079      * Add top level item to navigation panel. At the same time adds item to htmlTable
0080      * @param title name of item
0081      * @param html  string to be displayed in splash screen
0082      */
0083     QTreeWidgetItem *addTreeTopItem(QTreeWidget *parent, const QString &title, const QString &html);
0084 
0085     /**
0086      * Add item to navigation panel. At the same time adds item to dispatchTable Template
0087      * parameter is type of widget to be constructed and added to widget stack. It must
0088      * have T() constructor.
0089      * @param title  name of item
0090      */
0091     template <typename T>
0092     QTreeWidgetItem *addTreeItem(QTreeWidgetItem *parent, const QString &title);
0093 
0094     /** Lookup table for help texts. Maps navpanel item to help text. */
0095     QMap<QTreeWidgetItem *, QString> htmlTable;
0096     /** Lookup table for widgets. Maps navpanel item to widget to be displayed. */
0097     QMap<QTreeWidgetItem *, WidgetThunk> dispatchTable;
0098     QTreeWidget *navigationPanel { nullptr };
0099     QStackedWidget *acStack { nullptr };
0100     QTextEdit *splashScreen { nullptr };
0101 };