9#include "ReverseMapper.h"
16#include <QPainterPath>
17#include <QGraphicsScene>
19#include "KChartAbstractDiagram.h"
20#include "ChartGraphicsItem.h"
24ReverseMapper::ReverseMapper()
25 : m_diagram( nullptr )
31 : m_diagram( diagram )
36ReverseMapper::~ReverseMapper()
47void ReverseMapper::clear()
55QModelIndexList ReverseMapper::indexesIn(
const QRect& rect )
const
59 if ( m_scene->sceneRect().intersects( rect ) ) {
60 const QList<QGraphicsItem *> items = m_scene->items( rect );
61 QModelIndexList indexes;
62 for ( QGraphicsItem* item : items ) {
63 ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
65 QModelIndex index ( m_diagram->model()->index( i->row(), i->column(), m_diagram->rootIndex() ) );
71 return QModelIndexList();
75QModelIndexList ReverseMapper::indexesAt(
const QPointF& point )
const
79 if ( m_scene->sceneRect().contains( point ) ) {
80 const QList<QGraphicsItem *> items = m_scene->items( point );
81 QModelIndexList indexes;
82 for ( QGraphicsItem* item : items ) {
83 ChartGraphicsItem* i = qgraphicsitem_cast<ChartGraphicsItem*>( item );
85 QModelIndex index ( m_diagram->model()->index( i->row(), i->column(), m_diagram->rootIndex() ) );
86 if ( !indexes.contains(index) )
92 return QModelIndexList();
96QPolygonF ReverseMapper::polygon(
int row,
int column )
const
98 if ( !m_diagram->model()->hasIndex( row, column, m_diagram->rootIndex() ) )
100 const QModelIndex index = m_diagram->model()->index( row, column, m_diagram->rootIndex() );
101 return m_polygons.value(index);
104QRectF ReverseMapper::boundingRect(
int row,
int column )
const
106 return polygon(row, column).boundingRect();
109void ReverseMapper::addRect(
int row,
int column,
const QRectF& rect )
111 addPolygon( row, column, QPolygonF( rect ) );
114void ReverseMapper::addPolygon(
int row,
int column,
const QPolygonF& polygon )
116 const auto index = m_diagram->
model()->
index( row, column, m_diagram->rootIndex() );
117 auto &value = m_polygons[index];
118 if (value.isEmpty()) {
121 value = value.united(polygon);
127void ReverseMapper::addCircle(
int row,
int column,
const QPointF& location,
const QSizeF& diameter )
130 QPointF ossfet( -0.5*diameter.
width(), -0.5*diameter.
height() );
131 path.addEllipse( QRectF( location + ossfet, diameter ) );
132 addPolygon( row, column, QPolygonF(
path.toFillPolygon() ) );
135void ReverseMapper::addLine(
int row,
int column,
const QPointF& from,
const QPointF& to )
140 addCircle( row, column, from, QSizeF( 1.5, 1.5 ) );
148 if ( from.
x() < to.
x() ) {
155 const QPointF lineVector( right - left );
156 const qreal lineVectorLength = sqrt( lineVector.x() * lineVector.x() + lineVector.y() * lineVector.y() );
157 const QPointF lineVectorUnit( lineVector / lineVectorLength );
158 const QPointF normOfLineVectorUnit( -lineVectorUnit.y(), lineVectorUnit.x() );
160 const QPointF one( left - lineVectorUnit + normOfLineVectorUnit );
161 const QPointF two( left - lineVectorUnit - normOfLineVectorUnit );
162 const QPointF three( right + lineVectorUnit - normOfLineVectorUnit );
163 const QPointF four( right + lineVectorUnit + normOfLineVectorUnit );
164 addPolygon( row, column, QPolygonF() << one << two << three << four );
167void ReverseMapper::populateScene()
const
171 Q_ASSERT( m_diagram );
178 m_scene =
new QGraphicsScene;
182 for (
auto it = m_polygons.constBegin(), end = m_polygons.constEnd(); it != end; ++it) {
183 auto* item =
new ChartGraphicsItem( it.key().row(), it.key().column() );
184 item->setPolygon( it.value() );
185 m_scene->addItem( item );
186 boundingRect |= it.value().boundingRect();
189 m_scene->setSceneRect(boundingRect);
190 m_sceneDirty =
false;
AbstractDiagram defines the interface for diagram classes.
QString path(const QString &relativePath)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
const QAbstractItemModel * model() const const
qreal height() const const
qreal width() const const
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)