Kstars

infoboxwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Khudyakov Alexey <alexey.skladnoy@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "infoboxwidget.h"
8
9#include "colorscheme.h"
10#include "kstarsdata.h"
11#include "ksutils.h"
12
13#include <KLocalizedString>
14
15#include <QPainter>
16#include <QMouseEvent>
17#include <QFontMetrics>
18#include <QLocale>
19
20const int InfoBoxWidget::padX = 6;
21const int InfoBoxWidget::padY = 2;
22
23InfoBoxes::InfoBoxes(QWidget *parent) : QWidget(parent)
24{
25 setMouseTracking(true);
26}
27
28void InfoBoxes::addInfoBox(InfoBoxWidget *ibox)
29{
30 ibox->setParent(this);
31 m_boxes.append(ibox);
32}
33
34void InfoBoxes::resizeEvent(QResizeEvent *)
35{
36 foreach (InfoBoxWidget *w, m_boxes)
37 w->adjust();
38}
39
40/* ================================================================ */
41
42InfoBoxWidget::InfoBoxWidget(bool shade, const QPoint &pos, int anchor, const QStringList &str, QWidget *parent)
43 : QWidget(parent), m_strings(str), m_adjusted(false), m_grabbed(false), m_shaded(shade), m_anchor(anchor)
44{
45 move(pos);
46 updateSize();
47}
48
49void InfoBoxWidget::updateSize()
50{
51 QFontMetrics fm(font());
52 int w = 0;
53 foreach (const QString &str, m_strings)
54 {
55 w = qMax(w, fm.horizontalAdvance(str));
56 }
57
58 int h = fm.height() * (m_shaded ? 1 : m_strings.size());
59 // Add padding
60 resize(w + 2 * padX, h + 2 * padY + 2);
61 adjust();
62}
63
65{
66 KStarsData *data = KStarsData::Instance();
67
68 m_strings.clear();
69 m_strings
70 << i18nc("Local Time", "LT: ") + data->lt().time().toString(QLocale().timeFormat().remove('t')) +
71 " " + // Remove timezone, as timezone of geolocation in KStars might not be same as system locale timezone
72 QLocale().toString(data->lt().date());
73
74 m_strings << i18nc("Universal Time", "UT: ") + data->ut().time().toString("HH:mm:ss") + " " +
75 QLocale().toString(data->ut().date()); // Do not format UTC according to locale
76
77 const QString STString = QString::asprintf("%02d:%02d:%02d ", data->lst()->hour(), data->lst()->minute(),
78 data->lst()->second());
79 //Don't use KLocale::formatNumber() for Julian Day because we don't want
80 //thousands-place separators
81 QString JDString = QString::number(data->ut().djd(), 'f', 2);
82 JDString.replace('.', QLocale().decimalPoint());
83 m_strings << i18nc("Sidereal Time", "ST: ") + STString + i18nc("Julian Day", "JD: ") + JDString;
84 updateSize();
85 update();
86}
87
89{
90 GeoLocation *geo = KStarsData::Instance()->geo();
91
92 m_strings.clear();
93 m_strings << geo->fullName();
94
95 //m_strings << i18nc("Longitude", "Long:") + ' ' + QLocale().toString(geo->lng()->Degrees(), 3) + " " +
96 // i18nc("Latitude", "Lat:") + ' ' + QLocale().toString(geo->lat()->Degrees(), 3);
97
98 m_strings << i18nc("Longitude", "Long:") + ' ' + geo->lng()->toDMSString(true) + ' ' +
99 i18nc("Latitude", "Lat:") + ' ' + geo->lat()->toDMSString(true);
100 updateSize();
101 update();
102}
103
105{
106 setPoint(obj->translatedLongName(), obj);
107}
108
110{
111 setPoint(i18n("nothing"), p);
112}
113
114void InfoBoxWidget::setPoint(QString name, SkyPoint *p)
115{
116 m_strings.clear();
117 m_strings << name;
118 m_strings << i18nc("Right Ascension", "RA") + ": " + p->ra().toHMSString() + " " + i18nc("Declination", "Dec") +
119 ": " + p->dec().toDMSString(true);
120 m_strings << i18nc("Azimuth", "Az") + ": " + p->az().toDMSString(true) + " " + i18nc("Altitude", "Alt") + ": " +
121 p->alt().toDMSString(true);
122
123 dms lst = KStarsData::Instance()->geo()->GSTtoLST(KStarsData::Instance()->clock()->utc().gst());
124 dms ha(lst - p->ra());
125 QChar sign('+');
126 if (ha.Hours() > 12.0)
127 {
128 ha.setH(24.0 - ha.Hours());
129 sign = '-';
130 }
131 m_strings << i18nc("Hour Angle", "HA") + ": " + sign + ha.toHMSString() + " " + i18nc("Zenith Angle", "ZA") + ": " +
132 dms(90 - p->alt().Degrees()).toDMSString(true);
133 updateSize();
134 update();
135}
136
138{
139 if (!isVisible())
140 return;
141 // X axis
142 int newX = x();
143 int maxX = parentWidget()->width() - width();
144 if (m_anchor & AnchorRight)
145 {
146 newX = maxX;
147 }
148 else
149 {
150 newX = KSUtils::clamp(newX, 0, maxX);
151 if (newX == maxX)
152 m_anchor |= AnchorRight;
153 }
154 // Y axis
155 int newY = y();
156 int maxY = parentWidget()->height() - height();
157 if (m_anchor & AnchorBottom)
158 {
159 newY = maxY;
160 }
161 else
162 {
163 newY = KSUtils::clamp(newY, 0, maxY);
164 if (newY == maxY)
165 m_anchor |= AnchorBottom;
166 }
167 // Do move
168 m_adjusted = true;
169 move(newX, newY);
170}
171
172void InfoBoxWidget::paintEvent(QPaintEvent *)
173{
174 // If widget contain no strings return
175 if (m_strings.empty())
176 return;
177 // Start with painting
178 ColorScheme *cs = KStarsData::Instance()->colorScheme();
179 QPainter p;
180 p.begin(this);
181
182 // Draw background
183 QColor colBG = cs->colorNamed("BoxBGColor");
184 colBG.setAlpha(127);
185 p.fillRect(contentsRect(), colBG);
186 // Draw border
187 if (m_grabbed)
188 {
189 p.setPen(cs->colorNamed("BoxGrabColor"));
190 p.drawRect(0, 0, width() - 1, height() - 1);
191 }
192 // Draw text
193 int h = QFontMetrics(font()).height();
194 int y = 0;
195 p.setPen(cs->colorNamed("BoxTextColor"));
196 foreach (const QString &str, m_strings)
197 {
198 y += h;
199 p.drawText(padX, padY + y, str);
200 }
201 // Done
202 p.end();
203}
204
205void InfoBoxWidget::mouseMoveEvent(QMouseEvent *event)
206{
207 m_grabbed = true;
208 // X axis
209 int newX = x() + event->x();
210 int maxX = parentWidget()->width() - width();
211 if (newX > maxX)
212 {
213 newX = maxX;
214 m_anchor |= AnchorRight;
215 }
216 else
217 {
218 if (newX < 0)
219 newX = 0;
220 m_anchor &= ~AnchorRight;
221 }
222 // Y axis
223 int newY = y() + event->y();
224 int maxY = parentWidget()->height() - height();
225 if (newY > maxY)
226 {
227 newY = maxY;
228 m_anchor |= AnchorBottom;
229 }
230 else
231 {
232 if (newY < 0)
233 newY = 0;
234 m_anchor &= ~AnchorBottom;
235 }
236 // Do move
237 m_adjusted = true;
238 move(newX, newY);
239}
240
241void InfoBoxWidget::mousePressEvent(QMouseEvent *)
242{
243 emit clicked();
244}
245
246void InfoBoxWidget::showEvent(QShowEvent *)
247{
248 if (!m_adjusted)
249 adjust();
250}
251
252void InfoBoxWidget::mouseDoubleClickEvent(QMouseEvent *)
253{
254 m_shaded = !m_shaded;
255 updateSize();
256 update();
257}
258
259void InfoBoxWidget::mouseReleaseEvent(QMouseEvent *)
260{
261 m_grabbed = false;
262}
This class stores all of the adjustable colors in KStars, in a QMap object keyed by the names of the ...
Definition colorscheme.h:27
QColor colorNamed(const QString &name) const
Retrieve a color by name.
Contains all relevant information for specifying a location on Earth: City Name, State/Province name,...
Definition geolocation.h:28
The InfoBoxWidget class is a widget that displays a transparent box for display of text messages.
InfoBoxWidget(bool shade, const QPoint &pos, int anchor=0, const QStringList &str=QStringList(), QWidget *parent=nullptr)
Create one infobox.
void adjust()
Adjust widget's position.
void slotObjectChanged(SkyObject *obj)
Set information about object.
void clicked()
Emitted when widget is clicked.
void slotGeoChanged()
Set information about location.
void slotPointChanged(SkyPoint *p)
Set information about pointing.
void slotTimeChanged()
Set information about time.
KStarsData is the backbone of KStars.
Definition kstarsdata.h:74
const KStarsDateTime & lt() const
Definition kstarsdata.h:153
CachingDms * lst()
Definition kstarsdata.h:226
ColorScheme * colorScheme()
Definition kstarsdata.h:174
const KStarsDateTime & ut() const
Definition kstarsdata.h:159
GeoLocation * geo()
Definition kstarsdata.h:232
long double djd() const
Provides all necessary information about an object in the sky: its coordinates, name(s),...
Definition skyobject.h:42
QString translatedLongName() const
Definition skyobject.h:170
The sky coordinates of a point in the sky.
Definition skypoint.h:45
const CachingDms & dec() const
Definition skypoint.h:269
const CachingDms & ra() const
Definition skypoint.h:263
const dms & az() const
Definition skypoint.h:275
const dms & alt() const
Definition skypoint.h:281
An angle, stored as degrees, but expressible in many ways.
Definition dms.h:38
int second() const
Definition dms.cpp:231
const QString toDMSString(const bool forceSign=false, const bool machineReadable=false, const bool highPrecision=false) const
Definition dms.cpp:287
int minute() const
Definition dms.cpp:221
const QString toHMSString(const bool machineReadable=false, const bool highPrecision=false) const
Definition dms.cpp:378
int hour() const
Definition dms.h:147
const double & Degrees() const
Definition dms.h:141
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void setAlpha(int alpha)
QDate date() const const
QTime time() const const
int height() const const
void append(QList< T > &&value)
void clear()
bool empty() const const
qsizetype size() const const
QString toString(QDate date, FormatType format) const const
bool begin(QPaintDevice *device)
void drawRect(const QRect &rectangle)
void drawText(const QPoint &position, const QString &text)
bool end()
void fillRect(const QRect &rectangle, QGradient::Preset preset)
void setPen(Qt::PenStyle style)
QString asprintf(const char *cformat,...)
QString number(double n, char format, int precision)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString toString(QStringView format) const const
QRect contentsRect() const const
QWidget * parentWidget() const const
void move(const QPoint &)
void setParent(QWidget *parent)
void resize(const QSize &)
void update()
bool isVisible() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.