Kstars

labelnode.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Artem Fedoskin <[email protected]>
3  SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "skyobject.h"
7 #include "Options.h"
8 
9 #include <QSGSimpleTextureNode>
10 #include <QQuickWindow>
11 
12 #include "skymaplite.h"
13 #include "labelnode.h"
14 
16  : SkyNode(skyObject), m_name(skyObject->labelString()), m_textTexture(new QSGSimpleTextureNode), m_labelType(type),
17  m_fontSize(0), m_zoomFont(false)
18 {
19  initialize();
20 }
21 
23  : m_name(name), m_textTexture(new QSGSimpleTextureNode), m_labelType(type), m_fontSize(0), m_zoomFont(false)
24 {
25  initialize();
26 }
27 
29 {
30  m_opacity->removeChildNode(m_textTexture);
31  m_textTexture->setOwnsTexture(true);
32  delete m_textTexture;
33 }
34 
36 {
37  switch (m_labelType)
38  {
39  case LabelsItem::label_t::PLANET_LABEL:
40  case LabelsItem::label_t::SATURN_MOON_LABEL:
41  case LabelsItem::label_t::JUPITER_MOON_LABEL:
42  case LabelsItem::label_t::COMET_LABEL:
43  case LabelsItem::label_t::RUDE_LABEL:
44  case LabelsItem::label_t::ASTEROID_LABEL:
45  m_schemeColor = "PNameColor";
46  m_zoomFont = true;
47  break;
48  case LabelsItem::label_t::DEEP_SKY_LABEL:
49  case LabelsItem::label_t::DSO_OTHER_LABEL:
50  case LabelsItem::label_t::CATALOG_DSO_LABEL:
51  m_schemeColor = "DSNameColor";
52  m_zoomFont = true;
53  break;
54  case LabelsItem::label_t::TELESCOPE_SYMBOL:
55  m_schemeColor = "TargetColor";
56  m_zoomFont = true;
57  break;
58  case LabelsItem::label_t::CONSTEL_NAME_LABEL:
59  m_schemeColor = "CNameColor";
60  break;
61  case LabelsItem::label_t::STAR_LABEL:
62  case LabelsItem::label_t::CATALOG_STAR_LABEL:
63  m_schemeColor = "SNameColor";
64  break;
65  default:
66  m_schemeColor = "UserLabelColor";
67  break;
68  }
69 
70  createTexture(KStarsData::Instance()->colorScheme()->colorNamed(m_schemeColor));
71  m_opacity->appendChildNode(m_textTexture);
72 }
73 
75 {
76  switch (m_labelType)
77  {
78  case LabelsItem::label_t::SATURN_MOON_LABEL:
79  case LabelsItem::label_t::JUPITER_MOON_LABEL:
80  SkyLabeler::Instance()->shrinkFont(2);
81  break;
82  default:
83  break;
84  }
85 
86  QSGTexture *oldTexture = m_textTexture->texture();
87 
88  m_textTexture->setTexture(SkyMapLite::Instance()->textToTexture(m_name, color, m_zoomFont));
89  m_color = color;
90 
91  if (m_zoomFont)
92  m_fontSize = SkyLabeler::Instance()->drawFont().pointSize();
93 
94  switch (m_labelType)
95  {
96  case LabelsItem::label_t::SATURN_MOON_LABEL:
97  case LabelsItem::label_t::JUPITER_MOON_LABEL:
98  SkyLabeler::Instance()->resetFont();
99  break;
100  default:
101  break;
102  }
103 
104  if (oldTexture)
105  delete oldTexture;
106 
107  m_textSize = m_textTexture->texture()->textureSize();
108  QRectF oldRect = m_textTexture->rect();
109  qreal ratio = SkyMapLite::Instance()->window()->effectiveDevicePixelRatio();
110 
111  m_textTexture->setRect(QRect(oldRect.x(), oldRect.y(), m_textSize.width() / ratio, m_textSize.height() / ratio));
112 }
113 
115 {
116  QMatrix4x4 m(1, 0, 0, pos.x(), 0, 1, 0, pos.y(), 0, 0, 1, 0, 0, 0, 0, 1);
117  //m.translate(-0.5*size.width(), -0.5*size.height());
118 
119  setMatrix(m);
121 }
122 
124 {
125  show();
126  qreal ratio = SkyMapLite::Instance()->window()->effectiveDevicePixelRatio();
127  //We need to subtract the height of texture from final y to follow the way QPainter draws the text
128  if (m_skyObject)
129  labelPos = QPointF(pos.x() + m_skyObject->labelOffset(),
130  pos.y() + m_skyObject->labelOffset() - m_textSize.height() / ratio);
131  else
132  labelPos = QPointF(pos.x(), pos.y());
133 }
134 
136 {
137  QColor newColor = KStarsData::Instance()->colorScheme()->colorNamed(m_schemeColor);
138  if ((m_zoomFont && m_fontSize != SkyLabeler::Instance()->skyFont().pointSize()) || m_color != newColor)
139  {
140  createTexture(newColor);
141  }
142  changePos(labelPos);
143 }
void createTexture(QColor color=QColor())
Create texture from label's name.
Definition: labelnode.cpp:74
virtual ~LabelNode()
Destructor.
Definition: labelnode.cpp:28
virtual QSize textureSize() const const=0
qreal x() const const
qreal y() const const
QQuickWindow * window() const const
void appendChildNode(QSGNode *node)
void resetFont()
sets the font in SkyLabeler and in psky back to the zoom dependent value that was set in reset().
Definition: skylabeler.cpp:222
void setRect(const QRectF &r)
int width() const const
void shrinkFont(int delta)
decreases the size of the font in psky and in the SkyLabeler by the delta points.
Definition: skylabeler.cpp:206
QRectF rect() const const
int height() const const
label_t
The label_t enum.
Definition: labelsitem.h:61
void initialize()
Convenience function to not to repeat the same code in 2 constructors.
Definition: labelnode.cpp:35
LabelNode(SkyObject *skyObject, LabelsItem::label_t type)
Constructor.
Definition: labelnode.cpp:15
virtual void update() override
Update position of label according to labelPos and recreate texture if label's size depends on zoom l...
Definition: labelnode.cpp:135
ColorScheme * colorScheme()
Definition: kstarsdata.h:171
virtual double labelOffset() const
Definition: skyobject.cpp:454
void setOwnsTexture(bool owns)
QSGTexture * texture() const const
void setMatrix(const QMatrix4x4 &matrix)
void markDirty(QSGNode::DirtyState bits)
virtual void changePos(QPointF pos) override
Changes position of the label.
Definition: labelnode.cpp:114
qreal x() const const
qreal y() const const
void setLabelPos(QPointF pos)
set the position of label with the given offset from SkyObject's position and makes the label visible...
Definition: labelnode.cpp:123
virtual void show()
shows all child nodes (sets opacity of m_opacity to 1)
Definition: skynode.cpp:27
Provides virtual functions for update of coordinates and nodes hiding.
Definition: skynode.h:27
void setTexture(QSGTexture *texture)
QColor colorNamed(const QString &name) const
Retrieve a color by name.
Definition: colorscheme.cpp:86
Information about an object in the sky.
Definition: skyobject.h:41
qreal effectiveDevicePixelRatio() const const
void removeChildNode(QSGNode *node)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:55 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.