diff options
author | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-09-10 11:42:13 +0300 |
---|---|---|
committer | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-09-10 12:04:24 +0300 |
commit | a28cdb72a4ea768a898ca07f0df0fa3c17c073a8 (patch) | |
tree | 0bafdcfa99fc783e9f5204539a8242bf6128d795 /src/datavisualization/engine/axisrendercache.cpp | |
parent | f3e38983d77c72f3121c33a149a58fdf9c64158c (diff) |
Module renamed
Task-number: QTRD-2224
Change-Id: Iec18b6121809300b11d85445281d3c626c434f35
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com>
Diffstat (limited to 'src/datavisualization/engine/axisrendercache.cpp')
-rw-r--r-- | src/datavisualization/engine/axisrendercache.cpp | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/datavisualization/engine/axisrendercache.cpp b/src/datavisualization/engine/axisrendercache.cpp new file mode 100644 index 00000000..c525c777 --- /dev/null +++ b/src/datavisualization/engine/axisrendercache.cpp @@ -0,0 +1,186 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#include "axisrendercache_p.h" +#include "qmath.h" +#include <QFontMetrics> + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +AxisRenderCache::AxisRenderCache() + : m_type(Q3DAbstractAxis::AxisTypeNone), + m_min(0.0), + m_max(10.0), + m_segmentCount(5), + m_subSegmentCount(1), + m_font(QFont(QStringLiteral("Arial"))), + m_drawer(0), + m_segmentStep(10.0f), + m_subSegmentStep(10.0f) +{ +} + +AxisRenderCache::~AxisRenderCache() +{ + foreach (LabelItem *label, m_labelItems) + delete label; +} + +void AxisRenderCache::setDrawer(Drawer *drawer) +{ + m_drawer = drawer; + m_font = m_drawer->font(); + if (m_drawer) { + QObject::connect(m_drawer, &Drawer::drawerChanged, this, &AxisRenderCache::updateTextures); + updateTextures(); + } +} + +void AxisRenderCache::setType(Q3DAbstractAxis::AxisType type) +{ + m_type = type; + + // If type is set, it means completely new axis instance, so clear all old data + m_labels.clear(); + m_title.clear(); + m_min = 0.0; + m_max = 10.0; + m_segmentCount = 5; + m_subSegmentCount = 1; + m_labelFormat.clear(); + + m_titleItem.clear(); + foreach (LabelItem *label, m_labelItems) + delete label; + m_labelItems.clear(); + m_segmentStep = 10.0f; + m_subSegmentStep = 10.0f; +} + +void AxisRenderCache::setTitle(const QString &title) +{ + if (m_title != title) { + m_title = title; + // Generate axis label texture + if (m_drawer) + m_drawer->generateLabelItem(m_titleItem, title); + } +} + +void AxisRenderCache::setLabels(const QStringList &labels) +{ + if (m_labels != labels) { + int newSize(labels.size()); + int oldSize(m_labels.size()); + + for (int i = newSize; i < oldSize; i++) + delete m_labelItems.takeLast(); + + m_labelItems.reserve(newSize); + + int widest = maxLabelWidth(labels); + + for (int i = 0; i < newSize; i++) { + if (i >= oldSize) + m_labelItems.append(new LabelItem); + if (m_drawer) { + if (labels.at(i).isEmpty()) + m_labelItems[i]->clear(); + else if (i >= oldSize || labels.at(i) != m_labels.at(i)) + m_drawer->generateLabelItem(*m_labelItems[i], labels.at(i), widest); + } + } + m_labels = labels; + } +} + +void AxisRenderCache::setMin(qreal min) +{ + m_min = min; + updateSegmentStep(); +} + +void AxisRenderCache::setMax(qreal max) +{ + m_max = max; + updateSegmentStep(); +} + +void AxisRenderCache::setSegmentCount(int count) +{ + m_segmentCount = count; + updateSegmentStep(); +} + +void AxisRenderCache::setSubSegmentCount(int count) +{ + m_subSegmentCount = count; + updateSubSegmentStep(); +} + +void AxisRenderCache::updateTextures() +{ + m_font = m_drawer->font(); + + if (m_title.isEmpty()) + m_titleItem.clear(); + else + m_drawer->generateLabelItem(m_titleItem, m_title); + + int widest = maxLabelWidth(m_labels); + + for (int i = 0; i < m_labels.size(); i++) { + if (m_labels.at(i).isEmpty()) + m_labelItems[i]->clear(); + else + m_drawer->generateLabelItem(*m_labelItems[i], m_labels.at(i), widest); + } +} + +void AxisRenderCache::updateSegmentStep() +{ + if (m_segmentCount > 0) + m_segmentStep = qFabs((m_max - m_min) / m_segmentCount); + else + m_segmentStep = 0.0f; // Irrelevant + updateSubSegmentStep(); +} + +void AxisRenderCache::updateSubSegmentStep() +{ + if (m_subSegmentCount > 1) + m_subSegmentStep = m_segmentStep / m_subSegmentCount; + else + m_subSegmentStep = m_segmentStep; +} + +int AxisRenderCache::maxLabelWidth(const QStringList &labels) const +{ + int labelWidth = 0; + QFont labelFont = m_font; + labelFont.setPointSize(50); + QFontMetrics labelFM(labelFont); + for (int i = 0; i < labels.size(); i++) { + int newWidth = labelFM.width(labels.at(i)); + if (labelWidth < newWidth) + labelWidth = newWidth; + } + return labelWidth; +} + +QT_DATAVISUALIZATION_END_NAMESPACE |