axisitem.cpp
164 lines
| 4.1 KiB
| text/x-c
|
CppLexer
/ src / axisitem.cpp
Michal Klocek
|
r67 | #include "axisitem_p.h" | ||
#include <QPainter> | ||||
#include <QDebug> | ||||
Michal Klocek
|
r85 | #define LABEL_PADDING 5 | ||
Michal Klocek
|
r67 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
Tero Ahola
|
r103 | AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) : | ||
Tero Ahola
|
r104 | ChartItem(parent), | ||
Tero Ahola
|
r103 | m_ticks(4), | ||
m_type(type) | ||||
Michal Klocek
|
r67 | { | ||
} | ||||
AxisItem::~AxisItem() | ||||
{ | ||||
} | ||||
void AxisItem::setLength(int length) | ||||
{ | ||||
QPainterPath path; | ||||
path.moveTo(QPointF(0,0)); | ||||
path.lineTo(length,0); | ||||
// path.lineTo(length-4,0); | ||||
// path.lineTo(length,3); | ||||
// path.lineTo(length-4,6); | ||||
// path.lineTo(length-4,4); | ||||
// path.lineTo(0,4); | ||||
// path.lineTo(0,2); | ||||
m_path=path; | ||||
update(); | ||||
} | ||||
QRectF AxisItem::boundingRect() const | ||||
{ | ||||
return m_rect; | ||||
} | ||||
void AxisItem::setPlotDomain(const PlotDomain& plotDomain) | ||||
{ | ||||
m_plotDomain = plotDomain; | ||||
Michal Klocek
|
r85 | createItems(); | ||
Michal Klocek
|
r67 | } | ||
Tero Ahola
|
r103 | |||
Michal Klocek
|
r115 | void AxisItem::setSize(const QSizeF &size) | ||
Tero Ahola
|
r103 | { | ||
m_rect = QRectF(QPoint(0,0),size); | ||||
createItems(); | ||||
} | ||||
Michal Klocek
|
r85 | /* | ||
Michal Klocek
|
r67 | void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) | ||
{ | ||||
if (!m_rect.isValid()) | ||||
Michal Klocek
|
r85 | return; | ||
Michal Klocek
|
r67 | |||
if(m_type==X_AXIS) { | ||||
Michal Klocek
|
r85 | const qreal deltaX = m_rect.width() / m_ticks; | ||
for (int i = 0; i <= m_ticks; ++i) { | ||||
int x = i * deltaX + m_rect.left(); | ||||
Michal Klocek
|
r67 | |||
Michal Klocek
|
r85 | if(i==0) x--; | ||
if(i==m_ticks) x++; | ||||
Michal Klocek
|
r67 | |||
Michal Klocek
|
r85 | qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX() | ||
/ m_ticks); | ||||
painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1); | ||||
// painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5); | ||||
Michal Klocek
|
r67 | |||
Michal Klocek
|
r85 | painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label)); | ||
} | ||||
Michal Klocek
|
r67 | } | ||
if(m_type==Y_AXIS) { | ||||
Michal Klocek
|
r85 | const qreal deltaY = (m_rect.height()) / m_ticks; | ||
Michal Klocek
|
r67 | |||
Michal Klocek
|
r85 | for (int j = 0; j <= m_ticks; ++j) { | ||
Michal Klocek
|
r67 | |||
Michal Klocek
|
r85 | int y = j * -deltaY + m_rect.bottom(); | ||
Michal Klocek
|
r67 | |||
Michal Klocek
|
r85 | if(j==0) y++; | ||
if(j==m_ticks) y--; | ||||
qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY() | ||||
/ m_ticks); | ||||
painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y); | ||||
//painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y); | ||||
//TODO : margin = 50 ; | ||||
painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20, | ||||
Qt::AlignRight | Qt::AlignVCenter, | ||||
QString::number(label)); | ||||
} | ||||
Michal Klocek
|
r67 | } | ||
//painter->drawRect(m_rect.adjusted(0, 0, -1, -1)); | ||||
Michal Klocek
|
r85 | } | ||
*/ | ||||
void AxisItem::createItems() | ||||
{ | ||||
//TODO: this is very inefficient handling | ||||
qDeleteAll(m_shades); | ||||
m_shades.clear(); | ||||
qDeleteAll(m_grid); | ||||
m_grid.clear(); | ||||
qDeleteAll(m_labels); | ||||
m_labels.clear(); | ||||
if(m_type==X_AXIS) { | ||||
const qreal deltaX = m_rect.width() / m_ticks; | ||||
for (int i = 0; i <= m_ticks; ++i) { | ||||
int x = i * deltaX + m_rect.left(); | ||||
qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks); | ||||
Michal Klocek
|
r86 | m_grid<<new QGraphicsLineItem(x, m_rect.top(), x, m_rect.bottom(),this); | ||
Michal Klocek
|
r85 | |||
QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this); | ||||
QPointF center = text->boundingRect().center(); | ||||
text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING); | ||||
//text->rotate(-45); | ||||
m_labels<<text; | ||||
} | ||||
} | ||||
if(m_type==Y_AXIS) { | ||||
const qreal deltaY = m_rect.height()/ m_ticks; | ||||
for (int j = 0; j <= m_ticks; ++j) { | ||||
int y = j * -deltaY + m_rect.bottom(); | ||||
qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY() | ||||
/ m_ticks); | ||||
Michal Klocek
|
r86 | m_grid<<new QGraphicsLineItem(m_rect.left() , y, m_rect.right(), y,this); | ||
Michal Klocek
|
r85 | QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this); | ||
QPointF center = text->boundingRect().center(); | ||||
text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y()); | ||||
//text->rotate(-45); | ||||
m_labels<<text; | ||||
} | ||||
} | ||||
Michal Klocek
|
r67 | |||
Michal Klocek
|
r85 | //painter->drawRect(m_rect.adjusted(0, 0, -1, -1)); | ||
Michal Klocek
|
r67 | } | ||
//TODO "nice numbers algorithm" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||