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 #if QT_VERSION >= QT_VERSION_CHECK(5,11,0)
56  w = qMax(w, fm.horizontalAdvance(str));
57 #else
58  w = qMax(w, fm.width(str));
59 #endif
60  }
61 
62  int h = fm.height() * (m_shaded ? 1 : m_strings.size());
63  // Add padding
64  resize(w + 2 * padX, h + 2 * padY + 2);
65  adjust();
66 }
67 
69 {
70  KStarsData *data = KStarsData::Instance();
71 
72  m_strings.clear();
73  m_strings
74  << i18nc("Local Time", "LT: ") + data->lt().time().toString(QLocale().timeFormat().remove('t')) +
75  " " + // Remove timezone, as timezone of geolocation in KStars might not be same as system locale timezone
76  QLocale().toString(data->lt().date());
77 
78  m_strings << i18nc("Universal Time", "UT: ") + data->ut().time().toString("HH:mm:ss") + " " +
79  QLocale().toString(data->ut().date()); // Do not format UTC according to locale
80 
81  const QString STString = QString::asprintf("%02d:%02d:%02d ", data->lst()->hour(), data->lst()->minute(),
82  data->lst()->second());
83  //Don't use KLocale::formatNumber() for Julian Day because we don't want
84  //thousands-place separators
85  QString JDString = QString::number(data->ut().djd(), 'f', 2);
86  JDString.replace('.', QLocale().decimalPoint());
87  m_strings << i18nc("Sidereal Time", "ST: ") + STString + i18nc("Julian Day", "JD: ") + JDString;
88  updateSize();
89  update();
90 }
91 
93 {
94  GeoLocation *geo = KStarsData::Instance()->geo();
95 
96  m_strings.clear();
97  m_strings << geo->fullName();
98 
99  //m_strings << i18nc("Longitude", "Long:") + ' ' + QLocale().toString(geo->lng()->Degrees(), 3) + " " +
100  // i18nc("Latitude", "Lat:") + ' ' + QLocale().toString(geo->lat()->Degrees(), 3);
101 
102  m_strings << i18nc("Longitude", "Long:") + ' ' + geo->lng()->toDMSString(true) + ' ' +
103  i18nc("Latitude", "Lat:") + ' ' + geo->lat()->toDMSString(true);
104  updateSize();
105  update();
106 }
107 
109 {
110  setPoint(obj->translatedLongName(), obj);
111 }
112 
114 {
115  setPoint(i18n("nothing"), p);
116 }
117 
118 void InfoBoxWidget::setPoint(QString name, SkyPoint *p)
119 {
120  m_strings.clear();
121  m_strings << name;
122  m_strings << i18nc("Right Ascension", "RA") + ": " + p->ra().toHMSString() + " " + i18nc("Declination", "Dec") +
123  ": " + p->dec().toDMSString(true);
124  m_strings << i18nc("Azimuth", "Az") + ": " + p->az().toDMSString(true) + " " + i18nc("Altitude", "Alt") + ": " +
125  p->alt().toDMSString(true);
126 
127  dms lst = KStarsData::Instance()->geo()->GSTtoLST(KStarsData::Instance()->clock()->utc().gst());
128  dms ha(lst - p->ra());
129  QChar sign('+');
130  if (ha.Hours() > 12.0)
131  {
132  ha.setH(24.0 - ha.Hours());
133  sign = '-';
134  }
135  m_strings << i18nc("Hour Angle", "HA") + ": " + sign + ha.toHMSString() + " " + i18nc("Zenith Angle", "ZA") + ": " +
136  dms(90 - p->alt().Degrees()).toDMSString(true);
137  updateSize();
138  update();
139 }
140 
142 {
143  if (!isVisible())
144  return;
145  // X axis
146  int newX = x();
147  int maxX = parentWidget()->width() - width();
148  if (m_anchor & AnchorRight)
149  {
150  newX = maxX;
151  }
152  else
153  {
154  newX = KSUtils::clamp(newX, 0, maxX);
155  if (newX == maxX)
156  m_anchor |= AnchorRight;
157  }
158  // Y axis
159  int newY = y();
160  int maxY = parentWidget()->height() - height();
161  if (m_anchor & AnchorBottom)
162  {
163  newY = maxY;
164  }
165  else
166  {
167  newY = KSUtils::clamp(newY, 0, maxY);
168  if (newY == maxY)
169  m_anchor |= AnchorBottom;
170  }
171  // Do move
172  m_adjusted = true;
173  move(newX, newY);
174 }
175 
176 void InfoBoxWidget::paintEvent(QPaintEvent *)
177 {
178  // If widget contain no strings return
179  if (m_strings.empty())
180  return;
181  // Start with painting
182  ColorScheme *cs = KStarsData::Instance()->colorScheme();
183  QPainter p;
184  p.begin(this);
185 
186  // Draw background
187  QColor colBG = cs->colorNamed("BoxBGColor");
188  colBG.setAlpha(127);
189  p.fillRect(contentsRect(), colBG);
190  // Draw border
191  if (m_grabbed)
192  {
193  p.setPen(cs->colorNamed("BoxGrabColor"));
194  p.drawRect(0, 0, width() - 1, height() - 1);
195  }
196  // Draw text
197  int h = QFontMetrics(font()).height();
198  int y = 0;
199  p.setPen(cs->colorNamed("BoxTextColor"));
200  foreach (const QString &str, m_strings)
201  {
202  y += h;
203  p.drawText(padX, padY + y, str);
204  }
205  // Done
206  p.end();
207 }
208 
209 void InfoBoxWidget::mouseMoveEvent(QMouseEvent *event)
210 {
211  m_grabbed = true;
212  // X axis
213  int newX = x() + event->x();
214  int maxX = parentWidget()->width() - width();
215  if (newX > maxX)
216  {
217  newX = maxX;
218  m_anchor |= AnchorRight;
219  }
220  else
221  {
222  if (newX < 0)
223  newX = 0;
224  m_anchor &= ~AnchorRight;
225  }
226  // Y axis
227  int newY = y() + event->y();
228  int maxY = parentWidget()->height() - height();
229  if (newY > maxY)
230  {
231  newY = maxY;
232  m_anchor |= AnchorBottom;
233  }
234  else
235  {
236  if (newY < 0)
237  newY = 0;
238  m_anchor &= ~AnchorBottom;
239  }
240  // Do move
241  m_adjusted = true;
242  move(newX, newY);
243 }
244 
245 void InfoBoxWidget::mousePressEvent(QMouseEvent *)
246 {
247  emit clicked();
248 }
249 
250 void InfoBoxWidget::showEvent(QShowEvent *)
251 {
252  if (!m_adjusted)
253  adjust();
254 }
255 
256 void InfoBoxWidget::mouseDoubleClickEvent(QMouseEvent *)
257 {
258  m_shaded = !m_shaded;
259  updateSize();
260  update();
261 }
262 
263 void InfoBoxWidget::mouseReleaseEvent(QMouseEvent *)
264 {
265  m_grabbed = false;
266 }
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:150
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:223
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:171
GeoLocation * geo()
Definition: kstarsdata.h:229
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:156
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:86
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-2022 The KDE developers.
Generated on Tue Aug 16 2022 04:00:55 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.