File indexing completed on 2024-04-28 03:44:25

0001 /*
0002     SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "guidelabelnode.h"
0007 
0008 #include "Options.h"
0009 #include "skymaplite.h"
0010 #include "skyobject.h"
0011 
0012 #include <QSGSimpleTextureNode>
0013 
0014 GuideLabelNode::GuideLabelNode(QString name, LabelsItem::label_t type)
0015     : m_textTexture(new QSGSimpleTextureNode), m_name(name)
0016 {
0017     appendChildNode(&debugRect);
0018     debugRect.setColor(QColor("green"));
0019     QColor color;
0020     switch (type)
0021     {
0022         case LabelsItem::label_t::CONSTEL_NAME_LABEL:
0023             color = KStarsData::Instance()->colorScheme()->colorNamed("CNameColor");
0024             break;
0025         case LabelsItem::label_t::HORIZON_LABEL:
0026             color = KStarsData::Instance()->colorScheme()->colorNamed("CompassColor");
0027             break;
0028         default:
0029             color = KStarsData::Instance()->colorScheme()->colorNamed("UserLabelColor");
0030     }
0031 
0032     m_textTexture->setTexture(SkyMapLite::Instance()->textToTexture(name, color));
0033     m_opacity->appendChildNode(m_textTexture);
0034 
0035     m_textSize     = m_textTexture->texture()->textureSize();
0036     QRectF oldRect = m_textTexture->rect();
0037     m_textTexture->setRect(QRect(oldRect.x(), oldRect.y(), m_textSize.width(), m_textSize.height()));
0038 }
0039 
0040 void GuideLabelNode::changePos(QPointF pos)
0041 {
0042     // otherwise draw the label and return true
0043     //m_p.rotate( angle );                        //rotate the coordinate system
0044     //m_p.drawText( QPointF( -w2, h ), text );
0045     //m_p.restore();                              //reset coordinate system
0046 
0047     //return true;*/
0048 
0049     //QSizeF size = m_point->size();
0050     QMatrix4x4 m(1, 0, 0, pos.x(), 0, 1, 0, pos.y(), 0, 0, 1, 0, 0, 0, 0, 1);
0051     //m.translate(m_translatePos.x(), m_translatePos.y());
0052     m.rotate(m_angle, 0, 0, 1);
0053 
0054     setMatrix(m);
0055     markDirty(QSGNode::DirtyMatrix);
0056 }
0057 
0058 void GuideLabelNode::setLabelPos(QPointF pos, float angle)
0059 {
0060     show();
0061     //We need to subtract the height of texture from final y to follow the way QPainter draws the text
0062     m_angle        = angle;
0063     m_translatePos = pos;
0064 
0065     //QFontMetricsF fontMetrics = SkyLabeler::Instance()->fontMetrics();
0066     // Create bounding rectangle by rotating the (height x width) rectangle
0067     qreal h = m_textSize.height(); //fontMetrics.height();
0068     qreal w = m_textSize.width();  //fontMetrics.width( m_name );
0069 
0070     qreal s = sin(angle * dms::PI / 180.0);
0071     qreal c = cos(angle * dms::PI / 180.0);
0072 
0073     qreal w2 = w / 2.0;
0074 
0075     // These numbers really do depend on the sign of the angle like this
0076     if (angle >= 0.0)
0077     {
0078         top   = pos.y() - s * w2;
0079         bot   = pos.y() + c * h + s * w2;
0080         left  = pos.x() - c * w2 - s * h;
0081         right = pos.x() + c * w2;
0082     }
0083     else
0084     {
0085         top   = pos.y() + s * w2;
0086         bot   = pos.y() + c * h - s * w2;
0087         left  = pos.x() - c * w2;
0088         right = pos.x() + c * w2 - s * h;
0089     }
0090 
0091     //We need to translate matrix with the value of pos point
0092 
0093     labelPos = QPointF(pos.x() - w2, pos.y() + h);
0094 
0095     /*debugRect.setRect(QRectF(QPointF(left,top),QPointF(right,bot)));
0096      debugRect.markDirty(QSGNode::DirtyGeometry);*/
0097 
0098     // return false if label would overlap existing label
0099     //    if ( ! markRegion( left, right, top, bot) )
0100 }
0101 
0102 void GuideLabelNode::update()
0103 {
0104     changePos(labelPos);
0105 }