Kstars

labelnode.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
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}
QColor colorNamed(const QString &name) const
Retrieve a color by name.
ColorScheme * colorScheme()
Definition kstarsdata.h:174
void createTexture(QColor color=QColor())
Create texture from label's name.
Definition labelnode.cpp:74
LabelNode(SkyObject *skyObject, LabelsItem::label_t type)
Constructor.
Definition labelnode.cpp:15
void setLabelPos(QPointF pos)
set the position of label with the given offset from SkyObject's position and makes the label visible...
virtual ~LabelNode()
Destructor.
Definition labelnode.cpp:28
void initialize()
Convenience function to not to repeat the same code in 2 constructors.
Definition labelnode.cpp:35
virtual void update() override
Update position of label according to labelPos and recreate texture if label's size depends on zoom l...
virtual void changePos(QPointF pos) override
Changes position of the label.
label_t
The label_t enum.
Definition labelsitem.h:62
void resetFont()
sets the font in SkyLabeler and in psky back to the zoom dependent value that was set in reset().
void shrinkFont(int delta)
decreases the size of the font in psky and in the SkyLabeler by the delta points.
Provides virtual functions for update of coordinates and nodes hiding.
Definition skynode.h:28
virtual void show()
shows all child nodes (sets opacity of m_opacity to 1)
Definition skynode.cpp:27
Provides all necessary information about an object in the sky: its coordinates, name(s),...
Definition skyobject.h:42
virtual double labelOffset() const
qreal x() const const
qreal y() const const
QQuickWindow * window() const const
qreal effectiveDevicePixelRatio() const const
qreal x() const const
qreal y() const const
void appendChildNode(QSGNode *node)
void markDirty(DirtyState bits)
void removeChildNode(QSGNode *node)
QRectF rect() const const
void setOwnsTexture(bool owns)
void setRect(const QRectF &r)
void setTexture(QSGTexture *texture)
QSGTexture * texture() const const
virtual QSize textureSize() const const=0
void setMatrix(const QMatrix4x4 &matrix)
int height() const const
int width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.