declarativescatterseries.cpp
108 lines
| 3.2 KiB
| text/x-c
|
CppLexer
/ qmlplugin / declarativescatterseries.cpp
Tero Ahola
|
r196 | #include "declarativescatterseries.h" | ||
#include "declarativechart.h" | ||||
Tero Ahola
|
r722 | #include "declarativetablemodel.h" | ||
Tero Ahola
|
r196 | #include "qchart.h" | ||
#include "qscatterseries.h" | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
DeclarativeScatterSeries::DeclarativeScatterSeries(QDeclarativeItem *parent) : | ||||
QDeclarativeItem(parent), | ||||
m_chart(0), | ||||
Tero Ahola
|
r722 | m_series(0), | ||
m_model(0), | ||||
m_xColumn(0), | ||||
m_yColumn(1) | ||||
Tero Ahola
|
r196 | { | ||
setFlag(QGraphicsItem::ItemHasNoContents, false); | ||||
} | ||||
Tero Ahola
|
r722 | DeclarativeScatterSeries::~DeclarativeScatterSeries() | ||
Tero Ahola
|
r196 | { | ||
} | ||||
Tero Ahola
|
r722 | void DeclarativeScatterSeries::componentComplete() | ||
Tero Ahola
|
r196 | { | ||
Q_ASSERT(!m_series); | ||||
DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent()); | ||||
if (declarativeChart) { | ||||
Tero Ahola
|
r722 | m_chart = qobject_cast<QChart *>(declarativeChart->m_chart); | ||
qDebug() << "creating scatter series for chart: " << m_chart; | ||||
Q_ASSERT(m_chart); | ||||
Tero Ahola
|
r196 | |||
m_series = new QScatterSeries(); | ||||
Tero Ahola
|
r722 | // if (!m_model) | ||
// m_model = new DeclarativeTableModel(); | ||||
if (m_model) { | ||||
m_series->setModel(m_model); | ||||
m_series->setModelMapping(m_xColumn, m_yColumn); | ||||
} | ||||
Tero Ahola
|
r196 | for (int i(0); i < m_data.count(); i++) { | ||
Tero Ahola
|
r722 | DeclarativeXyPoint *element = m_data.at(i); | ||
Tero Ahola
|
r196 | *m_series << QPointF(element->x(), element->y()); | ||
} | ||||
Tero Ahola
|
r722 | m_chart->addSeries(m_series); | ||
Tero Ahola
|
r196 | } | ||
} | ||||
Tero Ahola
|
r722 | QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeScatterSeries::data() | ||
Tero Ahola
|
r196 | { | ||
Tero Ahola
|
r722 | return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0, | ||
Tero Ahola
|
r196 | &DeclarativeScatterSeries::appendData); | ||
} | ||||
Tero Ahola
|
r722 | void DeclarativeScatterSeries::appendData(QDeclarativeListProperty<DeclarativeXyPoint> *list, | ||
DeclarativeXyPoint *element) | ||||
Tero Ahola
|
r196 | { | ||
DeclarativeScatterSeries *series = qobject_cast<DeclarativeScatterSeries *>(list->object); | ||||
qDebug() << "appendData: " << series; | ||||
qDebug() << "appendData: " << element; | ||||
qDebug() << "appendData: " << element->x(); | ||||
qDebug() << "appendData: " << element->y(); | ||||
qDebug() << "appendData: " << series->m_series; | ||||
if (series) { | ||||
series->m_data.append(element); | ||||
if (series->m_series) | ||||
Tero Ahola
|
r358 | series->m_series->add(element->x(), element->y()); | ||
Tero Ahola
|
r196 | } | ||
} | ||||
Tero Ahola
|
r722 | DeclarativeTableModel *DeclarativeScatterSeries::model() | ||
{ | ||||
if (m_series) | ||||
return (DeclarativeTableModel *) m_series->model(); | ||||
else | ||||
return m_model; | ||||
} | ||||
void DeclarativeScatterSeries::setModel(DeclarativeTableModel *model) | ||||
{ | ||||
m_model = model; | ||||
if (m_chart && m_series) { | ||||
// Hack: remove and add the series to force an update for the chart range | ||||
m_chart->removeSeries(m_series); | ||||
m_series = new QScatterSeries(); | ||||
m_series->setModel(m_model); | ||||
m_series->setModelMapping(m_xColumn, m_yColumn); | ||||
m_chart->addSeries(m_series); | ||||
} | ||||
} | ||||
void DeclarativeScatterSeries::setXColumn(int xColumn) | ||||
{ | ||||
m_xColumn = xColumn; | ||||
if (m_series && m_series->model()) | ||||
m_series->setModelMapping(m_xColumn, m_yColumn); | ||||
} | ||||
void DeclarativeScatterSeries::setYColumn(int yColumn) | ||||
{ | ||||
m_yColumn = yColumn; | ||||
if (m_series && m_series->model()) | ||||
m_series->setModelMapping(m_xColumn, m_yColumn); | ||||
} | ||||
Tero Ahola
|
r196 | #include "moc_declarativescatterseries.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||