1 /*
2     SPDX-FileCopyrightText: 2001-2002 Pablo de Vicente <vicente@oan.es>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <QMap>
10 #include <QString>
11 #include <QDialog>
12 
13 class QStackedWidget;
14 class QTextEdit;
15 class QTreeWidget;
16 class QTreeWidgetItem;
17 
18 /**
19  * @class AstroCalc
20  * @brief This is the base class for the KStars astronomical calculator
21  *
22  * @author: Pablo de Vicente
23  * @version 0.9
24  */
25 class AstroCalc : public QDialog
26 {
27     Q_OBJECT
28 
29   public:
30     explicit AstroCalc(QWidget *parent = nullptr);
31 
32     /** @return suggested size of calculator window. */
33     QSize sizeHint() const override;
34   public slots:
35      // Q: Why is this public when we don't have access to navigationPanel anyway?
36      // Also doesn't seem to be used from outside -- asimha
37     /** Display calculator module or help text based on item selected. */
38     void slotItemSelection(QTreeWidgetItem *it);
39 
40   private:
41     /** Pointer to function which return QWidget */
42     typedef QWidget *(AstroCalc::*WidgetConstructor)();
43     /**
44      * Data structure used for lazy widget construction. This class
45      * construct widget when it requested.
46      */
47     class WidgetThunk
48     {
49       public:
50         /**
51          * Create thunk
52          * @param acalc  pointer to class.
53          * @param f      function which construct widget.
54          */
WidgetThunk(AstroCalc * acalc,const WidgetConstructor & f)55         WidgetThunk(AstroCalc *acalc, const WidgetConstructor& f) : widget(nullptr), calc(acalc), func(f) { }
56         /**
57          * Request widget.
58          * @return newly created widget or cached value.
59          */
60         QWidget *eval();
61 
62       private:
63         /// Cached value
64         QWidget *widget { nullptr };
65         /// Pointer to calculator
66         AstroCalc *calc { nullptr };
67         /// Function call to construct the widget.
68         WidgetConstructor func;
69     };
70 
71     /**
72      * Create widget of type T and put it to widget stack. Widget must
73      * have constructor of type T(QWidget*). Returns constructed widget.
74      */
75     template <typename T>
76     inline QWidget *addToStack();
77 
78     /**
79      * Add top level item to navigation panel. At the same time adds item to htmlTable
80      * @param title name of item
81      * @param html  string to be displayed in splash screen
82      */
83     QTreeWidgetItem *addTreeTopItem(QTreeWidget *parent, const QString &title, const QString &html);
84 
85     /**
86      * Add item to navigation panel. At the same time adds item to dispatchTable Template
87      * parameter is type of widget to be constructed and added to widget stack. It must
88      * have T() constructor.
89      * @param title  name of item
90      */
91     template <typename T>
92     QTreeWidgetItem *addTreeItem(QTreeWidgetItem *parent, const QString &title);
93 
94     /** Lookup table for help texts. Maps navpanel item to help text. */
95     QMap<QTreeWidgetItem *, QString> htmlTable;
96     /** Lookup table for widgets. Maps navpanel item to widget to be displayed. */
97     QMap<QTreeWidgetItem *, WidgetThunk> dispatchTable;
98     QTreeWidget *navigationPanel { nullptr };
99     QStackedWidget *acStack { nullptr };
100     QTextEdit *splashScreen { nullptr };
101 };
102