Kstars

astrocalc.cpp
1 /*
2  SPDX-FileCopyrightText: 2001-2005 Pablo de Vicente <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "astrocalc.h"
8 
9 #include "modcalcjd.h"
10 #include "modcalcgeodcoord.h"
11 #include "modcalcgalcoord.h"
12 #include "modcalcsidtime.h"
13 #include "modcalcapcoord.h"
14 #include "modcalcdaylength.h"
15 #include "modcalcaltaz.h"
16 #include "modcalcplanets.h"
17 #include "modcalceclipticcoords.h"
18 #include "modcalcangdist.h"
19 #include "modcalcvizequinox.h"
20 #include "modcalcvlsr.h"
21 #include "conjunctions.h"
22 #include "eclipsetool.h"
23 
24 #include <QDialogButtonBox>
25 #include <QSplitter>
26 #include <QStackedWidget>
27 #include <QTextEdit>
28 #include <QTreeWidget>
29 #include <QTreeWidgetItem>
30 
31 AstroCalc::AstroCalc(QWidget *parent) : QDialog(parent)
32 {
33 #ifdef Q_OS_OSX
34  setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
35 #endif
36 
37  // List of messages. Maybe there is better place for it...
38  QString message = i18n("<QT>"
39  "<H2>KStars Astrocalculator</H2>"
40  "<P>"
41  "The KStars Astrocalculator contains several <B>modules</b> "
42  "which perform a variety of astronomy-related calculations. "
43  "The modules are organized into several categories: "
44  "<UL>"
45  "<LI><B>Time calculators: </B>"
46  "Convert between time systems, and predict the timing of celestial events</LI>"
47  "<LI><B>Coordinate converters: </B>"
48  "Convert between various coordinate systems</LI>"
49  "<LI><B>Solar system: </B>"
50  "Predict the position of any planet, from a given location on Earth at a given time</LI>"
51  "</UL>"
52  "</QT>");
53  QString messageTime = i18n("<QT>"
54  "Section which includes algorithms for computing time ephemeris"
55  "<UL><LI>"
56  "<B>Julian Day:</B> Julian Day/Calendar conversion"
57  "</LI><LI>"
58  "<B>Sidereal Time:</B> Sidereal/Universal time conversion"
59  "</LI><LI>"
60  "<B>Almanac:</B> Rise/Set/Transit timing and position data "
61  "for the Sun and Moon"
62  "</LI><LI>"
63  "<B>Equinoxes & Solstices:</B> Equinoxes, Solstices and duration of the "
64  "seasons"
65  "</LI></UL>"
66  "</QT>");
67  QString messageCoord = i18n("<QT>"
68  "Section with algorithms for the conversion of "
69  "different astronomical systems of coordinates"
70  "<UL><LI>"
71  "<B>Galactic:</B> Galactic/Equatorial coordinates conversion"
72  "</LI><LI>"
73  "<B>Apparent:</B> Computation of current equatorial coordinates"
74  " from a given epoch"
75  "</LI><LI>"
76  "<B>Ecliptic:</B> Ecliptic/Equatorial coordinates conversion"
77  "</LI><LI>"
78  "<B>Horizontal:</B> Computation of azimuth and elevation for a "
79  "given source, time, and location on the Earth"
80  "</LI><LI>"
81  "<B>Angular Distance:</B> Computation of angular distance between "
82  "two objects whose positions are given in equatorial coordinates"
83  "</LI><LI>"
84  "<B>Geodetic Coords:</B> Geodetic/XYZ coordinate conversion"
85  "</LI><LI>"
86  "<B>LSR Velocity:</B> Computation of the heliocentric, geocentric "
87  "and topocentric radial velocity of a source from its LSR velocity"
88  "</LI></UL>"
89  "</QT>");
90  QString messageSolar = i18n("<QT>"
91  "Section with algorithms regarding information "
92  "on solar system bodies coordinates and times"
93  "<UL><LI>"
94  "<B>Planets Coords:</B> Coordinates for the planets, moon and sun "
95  "at a given time and from a given position on Earth "
96  "</LI></UL>"
97  "</QT>");
98 
99  QSplitter *split = new QSplitter(this);
100 
101  QVBoxLayout *mainLayout = new QVBoxLayout;
102  mainLayout->addWidget(split);
103  setLayout(mainLayout);
104 
106  mainLayout->addWidget(buttonBox);
107  connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
108 
109  setWindowTitle(i18nc("@title:window", "Calculator"));
110 
111  // Create navigation panel
112  navigationPanel = new QTreeWidget(split);
113  navigationPanel->setColumnCount(1);
114  navigationPanel->setHeaderLabels(QStringList(i18n("Calculator modules")));
115  navigationPanel->setSortingEnabled(false);
116  //FIXME: Would be better to make the navigationPanel fit its contents,
117  //but I wasn't able to make it work
118  navigationPanel->setMinimumWidth(200);
119 
120  acStack = new QStackedWidget(split);
121 
122  splashScreen = new QTextEdit(message, acStack);
123  splashScreen->setReadOnly(true);
124  //FIXME: Minimum size is set to resize calculator to correct size
125  //when no modules is loaded. This is simply biggest size of
126  //calculator modules. I think it should be set in more cleverly.
127  splashScreen->setMinimumSize(640, 550);
128  acStack->addWidget(splashScreen);
129 
130  // Load icons
131  // JM 2016-10-02: Those are missing, disabling the icons for now
132  /*
133  QIcon jdIcon = QIcon ("jd.png");
134  QIcon geodIcon = QIcon ("geodetic.png");
135  QIcon solarIcon = QIcon ("geodetic.png");
136  // QIcon sunsetIcon = QIcon ("sunset.png"); // Its usage is commented out.
137  QIcon timeIcon = QIcon ("sunclock.png");*/
138 
139  /* Populate the tree widget and widget stack */
140  // Time-related entries
141  QTreeWidgetItem *timeItem = addTreeTopItem(navigationPanel, i18n("Time Calculators"), messageTime);
142  //timeItem->setIcon(0,timeIcon);
143 
144  //addTreeItem<modCalcJD> (timeItem, i18n("Julian Day"))->setIcon(0,jdIcon);
145  addTreeItem<modCalcJD>(timeItem, i18n("Julian Day"));
146  addTreeItem<modCalcSidTime>(timeItem, i18n("Sidereal Time"));
147  addTreeItem<modCalcDayLength>(timeItem, i18n("Almanac"));
148  addTreeItem<modCalcEquinox>(timeItem, i18n("Equinoxes & Solstices"));
149  // dayItem->setIcon(0,sunsetIcon);
150 
151  // Coordinate-related entries
152  QTreeWidgetItem *coordItem = addTreeTopItem(navigationPanel, i18n("Coordinate Converters"), messageCoord);
153  addTreeItem<modCalcGalCoord>(coordItem, i18n("Equatorial/Galactic"));
154  addTreeItem<modCalcApCoord>(coordItem, i18n("Apparent Coordinates"));
155  addTreeItem<modCalcAltAz>(coordItem, i18n("Horizontal Coordinates"));
156  addTreeItem<modCalcEclCoords>(coordItem, i18n("Ecliptic Coordinates"));
157  addTreeItem<modCalcAngDist>(coordItem, i18n("Angular Distance"));
158  addTreeItem<modCalcGeodCoord>(coordItem, i18n("Geodetic Coordinates"));
159  addTreeItem<modCalcVlsr>(coordItem, i18n("LSR Velocity"));
160 
161  // Solar System related entries
162  QTreeWidgetItem *solarItem = addTreeTopItem(navigationPanel, i18n("Solar System"), messageSolar);
163  //solarItem->setIcon(0,solarIcon);
164  addTreeItem<modCalcPlanets>(solarItem, i18n("Planets Coordinates"));
165  addTreeItem<ConjunctionsTool>(solarItem, i18n("Conjunctions"));
166  addTreeItem<EclipseTool>(solarItem, i18n("Eclipses"));
167 
168  acStack->setCurrentWidget(splashScreen);
169  connect(navigationPanel, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this,
170  SLOT(slotItemSelection(QTreeWidgetItem*)));
171 }
172 
173 template <typename T>
174 QWidget *AstroCalc::addToStack()
175 {
176  T *t = new T(acStack);
177  acStack->addWidget(t);
178  return t;
179 }
180 
181 template <typename T>
182 QTreeWidgetItem *AstroCalc::addTreeItem(QTreeWidgetItem *parent, const QString &title)
183 {
184  QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList(title));
185  dispatchTable.insert(item, WidgetThunk(this, &AstroCalc::addToStack<T>));
186  return item;
187 }
188 
189 QTreeWidgetItem *AstroCalc::addTreeTopItem(QTreeWidget *parent, const QString &title, const QString &html)
190 {
191  QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList(title));
192 
193  htmlTable.insert(item, html);
194  return item;
195 }
196 
198 {
199  if (item == nullptr)
200  return;
201  // Lookup in HTML table
202  QMap<QTreeWidgetItem *, QString>::iterator iterHTML = htmlTable.find(item);
203  if (iterHTML != htmlTable.end())
204  {
205  splashScreen->setHtml(*iterHTML);
206  acStack->setCurrentWidget(splashScreen);
207  return;
208  }
209  // Lookup in frames table
210  QMap<QTreeWidgetItem *, WidgetThunk>::iterator iter = dispatchTable.find(item);
211  if (iter != dispatchTable.end())
212  {
213  acStack->setCurrentWidget(iter->eval());
214  }
215 }
216 
218 {
219  return QSize(640, 430);
220 }
221 
222 QWidget *AstroCalc::WidgetThunk::eval()
223 {
224  if (widget == nullptr)
225  {
226  // This is pointer to member function call.
227  widget = (calc->*func)();
228  }
229  return widget;
230 }
void setCurrentWidget(QWidget *widget)
void slotItemSelection(QTreeWidgetItem *it)
Display calculator module or help text based on item selected.
Definition: astrocalc.cpp:197
QSize sizeHint() const override
Definition: astrocalc.cpp:217
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QMap::iterator insert(const Key &key, const T &value)
QMap::iterator end()
QString i18n(const char *text, const TYPE &arg...)
QMap::iterator find(const Key &key)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setHtml(const QString &text)
QObject * parent() const const
QString message
int addWidget(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 9 2022 04:06:01 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.