File indexing completed on 2024-04-21 03:56:41
0001 /* 0002 * This file is part of KQuickCharts 0003 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "GridLines.h" 0009 0010 #include "LineGridNode.h" 0011 #include "XYChart.h" 0012 0013 LinePropertiesGroup::LinePropertiesGroup(GridLines *parent) 0014 : QObject(parent) 0015 { 0016 m_parent = parent; 0017 } 0018 0019 bool LinePropertiesGroup::visible() const 0020 { 0021 return m_visible; 0022 } 0023 0024 void LinePropertiesGroup::setVisible(bool newVisible) 0025 { 0026 if (newVisible == m_visible) { 0027 return; 0028 } 0029 0030 m_visible = newVisible; 0031 Q_EMIT propertiesChanged(); 0032 } 0033 0034 QColor LinePropertiesGroup::color() const 0035 { 0036 return m_color; 0037 } 0038 0039 void LinePropertiesGroup::setColor(const QColor &newColor) 0040 { 0041 if (newColor == m_color) { 0042 return; 0043 } 0044 0045 m_color = newColor; 0046 Q_EMIT propertiesChanged(); 0047 } 0048 0049 float LinePropertiesGroup::lineWidth() const 0050 { 0051 return m_lineWidth; 0052 } 0053 0054 void LinePropertiesGroup::setLineWidth(float newLineWidth) 0055 { 0056 if (newLineWidth == m_lineWidth) { 0057 return; 0058 } 0059 0060 m_lineWidth = newLineWidth; 0061 Q_EMIT propertiesChanged(); 0062 } 0063 0064 int LinePropertiesGroup::frequency() const 0065 { 0066 return m_frequency; 0067 } 0068 0069 void LinePropertiesGroup::setFrequency(int newFrequency) 0070 { 0071 if (newFrequency == m_frequency) { 0072 return; 0073 } 0074 0075 m_frequency = newFrequency; 0076 Q_EMIT propertiesChanged(); 0077 } 0078 0079 int LinePropertiesGroup::count() const 0080 { 0081 return m_count; 0082 } 0083 0084 void LinePropertiesGroup::setCount(int newCount) 0085 { 0086 if (newCount == m_count) { 0087 return; 0088 } 0089 0090 m_count = newCount; 0091 Q_EMIT propertiesChanged(); 0092 } 0093 0094 GridLines::GridLines(QQuickItem *parent) 0095 : QQuickItem(parent) 0096 { 0097 setFlag(QQuickItem::ItemHasContents); 0098 0099 m_major = std::make_unique<LinePropertiesGroup>(this); 0100 connect(m_major.get(), &LinePropertiesGroup::propertiesChanged, this, &GridLines::update); 0101 m_minor = std::make_unique<LinePropertiesGroup>(this); 0102 connect(m_minor.get(), &LinePropertiesGroup::propertiesChanged, this, &GridLines::update); 0103 } 0104 0105 GridLines::Direction GridLines::direction() const 0106 { 0107 return m_direction; 0108 } 0109 0110 void GridLines::setDirection(GridLines::Direction newDirection) 0111 { 0112 if (newDirection == m_direction) { 0113 return; 0114 } 0115 0116 m_direction = newDirection; 0117 update(); 0118 Q_EMIT directionChanged(); 0119 } 0120 0121 XYChart *GridLines::chart() const 0122 { 0123 return m_chart; 0124 } 0125 0126 void GridLines::setChart(XYChart *newChart) 0127 { 0128 if (newChart == m_chart) { 0129 return; 0130 } 0131 0132 if (m_chart) { 0133 disconnect(m_chart, &XYChart::computedRangeChanged, this, &GridLines::update); 0134 } 0135 0136 m_chart = newChart; 0137 0138 if (m_chart) { 0139 connect(m_chart, &XYChart::computedRangeChanged, this, &GridLines::update); 0140 } 0141 0142 update(); 0143 Q_EMIT chartChanged(); 0144 } 0145 0146 float GridLines::spacing() const 0147 { 0148 return m_spacing; 0149 } 0150 0151 void GridLines::setSpacing(float newSpacing) 0152 { 0153 if (newSpacing == m_spacing || m_chart != nullptr) { 0154 return; 0155 } 0156 0157 m_spacing = newSpacing; 0158 update(); 0159 Q_EMIT spacingChanged(); 0160 } 0161 0162 LinePropertiesGroup *GridLines::majorGroup() const 0163 { 0164 return m_major.get(); 0165 } 0166 0167 LinePropertiesGroup *GridLines::minorGroup() const 0168 { 0169 return m_minor.get(); 0170 } 0171 0172 QSGNode *GridLines::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *) 0173 { 0174 if (!node) { 0175 node = new QSGNode{}; 0176 node->appendChildNode(new LineGridNode{}); 0177 node->appendChildNode(new LineGridNode{}); 0178 } 0179 0180 if (m_chart) { 0181 if (m_direction == Direction::Horizontal) { 0182 m_spacing = width() / (m_chart->computedRange().distanceX - 1); 0183 } else { 0184 m_spacing = height() / (m_chart->computedRange().distanceY); 0185 } 0186 } 0187 0188 updateLines(static_cast<LineGridNode *>(node->childAtIndex(0)), m_minor.get()); 0189 updateLines(static_cast<LineGridNode *>(node->childAtIndex(1)), m_major.get()); 0190 0191 return node; 0192 } 0193 0194 void GridLines::updateLines(LineGridNode *node, LinePropertiesGroup *properties) 0195 { 0196 node->setVisible(properties->visible()); 0197 node->setRect(boundingRect()); 0198 node->setVertical(m_direction == Direction::Vertical); 0199 node->setColor(properties->color()); 0200 node->setLineWidth(properties->lineWidth()); 0201 if (properties->count() > 0) { 0202 node->setSpacing(m_direction == Direction::Horizontal ? width() / (properties->count() + 1) : height() / (properties->count() + 1)); 0203 } else { 0204 node->setSpacing(m_spacing * properties->frequency()); 0205 } 0206 node->update(); 0207 } 0208 0209 #include "moc_GridLines.cpp"