Kstars

guidelabelnode.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Artem Fedoskin <[email protected]>
3  SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "guidelabelnode.h"
7 
8 #include "Options.h"
9 #include "skymaplite.h"
10 #include "skyobject.h"
11 
12 #include <QSGSimpleTextureNode>
13 
15  : m_textTexture(new QSGSimpleTextureNode), m_name(name)
16 {
17  appendChildNode(&debugRect);
18  debugRect.setColor(QColor("green"));
19  QColor color;
20  switch (type)
21  {
22  case LabelsItem::label_t::CONSTEL_NAME_LABEL:
23  color = KStarsData::Instance()->colorScheme()->colorNamed("CNameColor");
24  break;
25  case LabelsItem::label_t::HORIZON_LABEL:
26  color = KStarsData::Instance()->colorScheme()->colorNamed("CompassColor");
27  break;
28  default:
29  color = KStarsData::Instance()->colorScheme()->colorNamed("UserLabelColor");
30  }
31 
32  m_textTexture->setTexture(SkyMapLite::Instance()->textToTexture(name, color));
33  m_opacity->appendChildNode(m_textTexture);
34 
35  m_textSize = m_textTexture->texture()->textureSize();
36  QRectF oldRect = m_textTexture->rect();
37  m_textTexture->setRect(QRect(oldRect.x(), oldRect.y(), m_textSize.width(), m_textSize.height()));
38 }
39 
41 {
42  // otherwise draw the label and return true
43  //m_p.rotate( angle ); //rotate the coordinate system
44  //m_p.drawText( QPointF( -w2, h ), text );
45  //m_p.restore(); //reset coordinate system
46 
47  //return true;*/
48 
49  //QSizeF size = m_point->size();
50  QMatrix4x4 m(1, 0, 0, pos.x(), 0, 1, 0, pos.y(), 0, 0, 1, 0, 0, 0, 0, 1);
51  //m.translate(m_translatePos.x(), m_translatePos.y());
52  m.rotate(m_angle, 0, 0, 1);
53 
54  setMatrix(m);
56 }
57 
58 void GuideLabelNode::setLabelPos(QPointF pos, float angle)
59 {
60  show();
61  //We need to subtract the height of texture from final y to follow the way QPainter draws the text
62  m_angle = angle;
63  m_translatePos = pos;
64 
65  //QFontMetricsF fontMetrics = SkyLabeler::Instance()->fontMetrics();
66  // Create bounding rectangle by rotating the (height x width) rectangle
67  qreal h = m_textSize.height(); //fontMetrics.height();
68  qreal w = m_textSize.width(); //fontMetrics.width( m_name );
69 
70  qreal s = sin(angle * dms::PI / 180.0);
71  qreal c = cos(angle * dms::PI / 180.0);
72 
73  qreal w2 = w / 2.0;
74 
75  // These numbers really do depend on the sign of the angle like this
76  if (angle >= 0.0)
77  {
78  top = pos.y() - s * w2;
79  bot = pos.y() + c * h + s * w2;
80  left = pos.x() - c * w2 - s * h;
81  right = pos.x() + c * w2;
82  }
83  else
84  {
85  top = pos.y() + s * w2;
86  bot = pos.y() + c * h - s * w2;
87  left = pos.x() - c * w2;
88  right = pos.x() + c * w2 - s * h;
89  }
90 
91  //We need to translate matrix with the value of pos point
92 
93  labelPos = QPointF(pos.x() - w2, pos.y() + h);
94 
95  /*debugRect.setRect(QRectF(QPointF(left,top),QPointF(right,bot)));
96  debugRect.markDirty(QSGNode::DirtyGeometry);*/
97 
98  // return false if label would overlap existing label
99  // if ( ! markRegion( left, right, top, bot) )
100 }
101 
103 {
104  changePos(labelPos);
105 }
virtual void changePos(QPointF pos) override
changePos changes the position m_point
static constexpr double PI
PI is a const static member; it's public so that it can be used anywhere, as long as dms....
Definition: dms.h:380
void setColor(const QColor &color)
virtual QSize textureSize() const const=0
qreal x() const const
qreal y() const const
void appendChildNode(QSGNode *node)
void setRect(const QRectF &r)
int width() const const
virtual void update() override
Updates coordinate of the object on SkyMapLite.
QRectF rect() const const
int height() const const
label_t
The label_t enum.
Definition: labelsitem.h:61
ColorScheme * colorScheme()
Definition: kstarsdata.h:171
QSGTexture * texture() const const
void setMatrix(const QMatrix4x4 &matrix)
void markDirty(QSGNode::DirtyState bits)
qreal x() const const
qreal y() const const
void rotate(float angle, const QVector3D &vector)
QSGNode::NodeType type() const const
void setLabelPos(QPointF pos, float angle)
setLabelPos sets the position of label with the given offset from SkyObject's position and makes the ...
virtual void show()
shows all child nodes (sets opacity of m_opacity to 1)
Definition: skynode.cpp:27
GuideLabelNode(QString name, LabelsItem::label_t type)
Constructor.
void setTexture(QSGTexture *texture)
QColor colorNamed(const QString &name) const
Retrieve a color by name.
Definition: colorscheme.cpp:86
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 19 2022 03:57:51 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.