Kstars

infoboxwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Khudyakov Alexey <[email protected]>
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 
20 const int InfoBoxWidget::padX = 6;
21 const int InfoBoxWidget::padY = 2;
22 
23 InfoBoxes::InfoBoxes(QWidget *parent) : QWidget(parent)
24 {
25  setMouseTracking(true);
26 }
27 
28 void InfoBoxes::addInfoBox(InfoBoxWidget *ibox)
29 {
30  ibox->setParent(this);
31  m_boxes.append(ibox);
32 }
33 
34 void InfoBoxes::resizeEvent(QResizeEvent *)
35 {
36  foreach (InfoBoxWidget *w, m_boxes)
37  w->adjust();
38 }
39 
40 /* ================================================================ */
41 
42 InfoBoxWidget::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 
49 void 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 
114 void 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 
172 void 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 
205 void 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 
241 void InfoBoxWidget::mousePressEvent(QMouseEvent *)
242 {
243  emit clicked();
244 }
245 
246 void InfoBoxWidget::showEvent(QShowEvent *)
247 {
248  if (!m_adjusted)
249  adjust();
250 }
251 
252 void InfoBoxWidget::mouseDoubleClickEvent(QMouseEvent *)
253 {
254  m_shaded = !m_shaded;
255  updateSize();
256  update();
257 }
258 
259 void InfoBoxWidget::mouseReleaseEvent(QMouseEvent *)
260 {
261  m_grabbed = false;
262 }
const dms & alt() const
Definition: skypoint.h:281
void setParent(QWidget *parent)
void setPen(const QColor &color)
long double djd() const
QString number(int n, int base)
QString translatedLongName() const
Definition: skyobject.h:169
const KStarsDateTime & lt() const
Definition: kstarsdata.h:151
Stores dms coordinates for a point in the sky. for converting between coordinate systems.
Definition: skypoint.h:44
void drawRect(const QRectF &rectangle)
void update()
QTime time() const const
CachingDms * lst()
Definition: kstarsdata.h:224
void slotPointChanged(SkyPoint *p)
Set information about pointing.
void clicked()
Emitted when widget is clicked.
void drawText(const QPointF &position, const QString &text)
void fillRect(const QRectF &rectangle, const QBrush &brush)
const QString toHMSString(const bool machineReadable=false, const bool highPrecision=false) const
Definition: dms.cpp:370
bool begin(QPaintDevice *device)
bool isVisible() const const
bool empty() const const
InfoBoxWidget(bool shade, const QPoint &pos, int anchor=0, const QStringList &str=QStringList(), QWidget *parent=nullptr)
Create one infobox.
int size() const const
bool end()
void slotTimeChanged()
Set information about time.
QString i18n(const char *text, const TYPE &arg...)
const QString toDMSString(const bool forceSign=false, const bool machineReadable=false, const bool highPrecision=false) const
Definition: dms.cpp:279
const CachingDms & dec() const
Definition: skypoint.h:269
void slotObjectChanged(SkyObject *obj)
Set information about object.
ColorScheme * colorScheme()
Definition: kstarsdata.h:172
GeoLocation * geo()
Definition: kstarsdata.h:230
void setAlpha(int alpha)
QString toString(qlonglong i) const const
void slotGeoChanged()
Set information about location.
int second() const
Definition: dms.cpp:231
QString & replace(int position, int n, QChar after)
An angle, stored as degrees, but expressible in many ways.
Definition: dms.h:37
const KStarsDateTime & ut() const
Definition: kstarsdata.h:157
void resize(int w, int h)
const CachingDms & ra() const
Definition: skypoint.h:263
void adjust()
Adjust widget's position.
const double & Degrees() const
Definition: dms.h:141
QDate date() const const
The InfoBoxWidget class is a widget that displays a transparent box for display of text messages.
Definition: infoboxwidget.h:44
int hour() const
Definition: dms.h:147
void clear()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString toString(Qt::DateFormat format) const const
void move(int x, int y)
QString asprintf(const char *cformat,...)
int minute() const
Definition: dms.cpp:221
QColor colorNamed(const QString &name) const
Retrieve a color by name.
Definition: colorscheme.cpp:87
QWidget * parentWidget() const const
Information about an object in the sky.
Definition: skyobject.h:41
int height() const const
QRect contentsRect() const const
Relevant data about an observing location on Earth.
Definition: geolocation.h:27
const dms & az() const
Definition: skypoint.h:275
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Dec 5 2023 03:58:28 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.