Kstars

ellipsenode.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
3 SPDX-License-Identifier: GPL-2.0-or-later
4*/
5
6#include "ellipsenode.h"
7
8#include <QSGFlatColorMaterial>
9#include <QSGGeometry>
10#include <QSGGeometryNode>
11
12#include <cmath>
13
14EllipseNode::EllipseNode(const QColor &color, int width)
15 : m_geometryNode(new QSGGeometryNode), m_material(new QSGFlatColorMaterial)
16{
18 m_geometry->allocate(60);
19 m_geometryNode->setGeometry(m_geometry);
20 m_geometryNode->setFlag(QSGNode::OwnsGeometry);
21
22 m_geometryNode->setOpaqueMaterial(m_material);
23 m_geometryNode->setFlag(QSGNode::OwnsMaterial);
24
25 if (color.isValid())
26 {
27 setColor(color);
28 }
29 setLineWidth(width);
30
31 appendChildNode(m_geometryNode);
32}
33
34void EllipseNode::setColor(QColor color)
35{
36 if (color != m_material->color())
37 {
38 m_material->setColor(color);
39 m_geometryNode->markDirty(QSGNode::DirtyMaterial);
40 }
41}
42
43void EllipseNode::setLineWidth(int width)
44{
45 if (width != m_geometry->lineWidth())
46 {
47 m_geometry->setLineWidth(width);
48 m_geometryNode->markDirty(QSGNode::DirtyGeometry);
49 }
50}
51
52void EllipseNode::updateGeometry(float x, float y, int width, int height, bool filled)
53{
54 if (filled)
55 {
56 m_geometry->setDrawingMode(GL_TRIANGLE_FAN);
57 }
58 else
59 {
60 m_geometry->setDrawingMode(GL_LINE_LOOP);
61 }
62
64
65 float rad = M_PI / 180;
66
67 width /= 2;
68 height /= 2;
69
70 if (m_width != width || m_height != height)
71 {
72 for (int i = 0; i < 360; i += 6)
73 {
74 vertex[i / 6].x = width * cos(i * rad);
75 vertex[i / 6].y = height * sin(i * rad);
76 }
77 m_geometryNode->markDirty(QSGNode::DirtyGeometry);
78
79 m_width = width;
80 m_height = height;
81 }
82 if (m_x != x || m_y != y)
83 {
84 QMatrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, 0, 0, 0, 0, 1);
85 setMatrix(m);
87
88 m_x = x;
89 m_y = y;
90 }
91}
void updateGeometry(float x, float y, int width, int height, bool filled)
Redraw ellipse with the given width, height and positions (x,y)
bool isValid() const const
const QColor & color() const const
void setColor(const QColor &color)
const AttributeSet & defaultAttributes_Point2D()
float lineWidth() const const
void setDrawingMode(unsigned int mode)
void setLineWidth(float width)
Point2D * vertexDataAsPoint2D()
void markDirty(DirtyState bits)
void setMatrix(const QMatrix4x4 &matrix)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:03 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.