diff --git a/demos/audio/widget.cpp b/demos/audio/widget.cpp index 0d16b9d..9bca182 100644 --- a/demos/audio/widget.cpp +++ b/demos/audio/widget.cpp @@ -25,11 +25,11 @@ Widget::Widget(QWidget *parent) QValueAxis *axisX = new QValueAxis; axisX->setRange(0, 2000); axisX->setLabelFormat("%g"); - axisX->setTitle("Samples"); + axisX->setTitleText("Samples"); QValueAxis *axisY = new QValueAxis; axisY->setRange(-1, 1); - axisY->setTitle("Audio level"); - m_chart->setAxisX(axisX, m_series); + axisY->setTitleText("Audio level"); + m_chart->setAxisX(axisX, m_series); m_chart->setAxisY(axisY, m_series); m_chart->legend()->hide(); m_chart->setTitle("Data from the microphone"); diff --git a/demos/chartthemes/themewidget.cpp b/demos/chartthemes/themewidget.cpp index e5a07b8..031a55e 100644 --- a/demos/chartthemes/themewidget.cpp +++ b/demos/chartthemes/themewidget.cpp @@ -179,8 +179,6 @@ QComboBox *ThemeWidget::createLegendBox() const QChart *ThemeWidget::createAreaChart() const { QChart *chart = new QChart(); -// chart->axisX()->setNiceNumbersEnabled(true); -// chart->axisY()->setNiceNumbersEnabled(true); chart->setTitle("Area chart"); // The lower series initialized to zero values diff --git a/demos/chartviewer/charts/axis/barcategoryaxisx.cpp b/demos/chartviewer/charts/axis/barcategoryaxisx.cpp index 787d195..ce8cff3 100644 --- a/demos/chartviewer/charts/axis/barcategoryaxisx.cpp +++ b/demos/chartviewer/charts/axis/barcategoryaxisx.cpp @@ -69,8 +69,8 @@ class BarCategoryAxisXTitle: public BarCategoryAxisX QChart *createChart(const DataTable &table) { QChart *chart = BarCategoryAxisX::createChart(table); - chart->axisX()->setTitle("Axis X"); - chart->axisY()->setTitle("Axis Y"); + chart->axisX()->setTitleText("Axis X"); + chart->axisY()->setTitleText("Axis Y"); chart->setTitle(" BarCateogry X , Value Y, title"); return chart; } diff --git a/demos/chartviewer/charts/axis/barcategoryaxisy.cpp b/demos/chartviewer/charts/axis/barcategoryaxisy.cpp index 8952a6a..ad1859c 100644 --- a/demos/chartviewer/charts/axis/barcategoryaxisy.cpp +++ b/demos/chartviewer/charts/axis/barcategoryaxisy.cpp @@ -73,8 +73,8 @@ public: QChart *createChart(const DataTable &table) { QChart *chart = BarCategoryAxisY::createChart(table); - chart->axisX()->setTitle("Axis X"); - chart->axisY()->setTitle("Axis Y"); + chart->axisX()->setTitleText("Axis X"); + chart->axisY()->setTitleText("Axis Y"); chart->setTitle(" BarCateogry Y , Value X, title"); return chart; } diff --git a/demos/chartviewer/charts/axis/categoryaxis.cpp b/demos/chartviewer/charts/axis/categoryaxis.cpp index c77836b..6899722 100644 --- a/demos/chartviewer/charts/axis/categoryaxis.cpp +++ b/demos/chartviewer/charts/axis/categoryaxis.cpp @@ -72,8 +72,8 @@ public: QChart *createChart(const DataTable &table) { QChart *chart = CategoryLineChart::createChart(table); - chart->axisX()->setTitle("Axis X"); - chart->axisY()->setTitle("Axis Y"); + chart->axisX()->setTitleText("Axis X"); + chart->axisY()->setTitleText("Axis Y"); chart->setTitle("Category X , Category Y,title "); return chart; } diff --git a/demos/chartviewer/charts/axis/datetimeaxisx.cpp b/demos/chartviewer/charts/axis/datetimeaxisx.cpp index cca9efd..2306467 100644 --- a/demos/chartviewer/charts/axis/datetimeaxisx.cpp +++ b/demos/chartviewer/charts/axis/datetimeaxisx.cpp @@ -69,8 +69,8 @@ public: QChart *createChart(const DataTable &table) { QChart *chart = DateTimeAxisX::createChart(table); - chart->axisX()->setTitle("Axis X"); - chart->axisY()->setTitle("Axis Y"); + chart->axisX()->setTitleText("Axis X"); + chart->axisY()->setTitleText("Axis Y"); chart->setTitle("DateTime X , Value Y, Title"); return chart; } diff --git a/demos/chartviewer/charts/axis/datetimeaxisy.cpp b/demos/chartviewer/charts/axis/datetimeaxisy.cpp index b2b0cb7..04d1fee 100644 --- a/demos/chartviewer/charts/axis/datetimeaxisy.cpp +++ b/demos/chartviewer/charts/axis/datetimeaxisy.cpp @@ -69,8 +69,8 @@ public: QChart *createChart(const DataTable &table) { QChart *chart = DateTimeAxisY::createChart(table); - chart->axisX()->setTitle("Axis X"); - chart->axisY()->setTitle("Axis Y"); + chart->axisX()->setTitleText("Axis X"); + chart->axisY()->setTitleText("Axis Y"); chart->setTitle("Value X , DateTime Y, Title"); return chart; } diff --git a/demos/chartviewer/charts/axis/valueaxis.cpp b/demos/chartviewer/charts/axis/valueaxis.cpp index 5ea9563..b37608c 100644 --- a/demos/chartviewer/charts/axis/valueaxis.cpp +++ b/demos/chartviewer/charts/axis/valueaxis.cpp @@ -64,8 +64,8 @@ public: QChart *createChart(const DataTable &table) { QChart *chart = ValueAxis::createChart(table); - chart->axisX()->setTitle("Axis X"); - chart->axisY()->setTitle("Axis Y"); + chart->axisX()->setTitleText("Axis X"); + chart->axisY()->setTitleText("Axis Y"); chart->setTitle("Value X , Value Y, title"); return chart; } diff --git a/demos/chartviewer/charts/barseries/verticalpercentbarchart.cpp b/demos/chartviewer/charts/barseries/verticalpercentbarchart.cpp index ddb5c96..a2710b0 100644 --- a/demos/chartviewer/charts/barseries/verticalpercentbarchart.cpp +++ b/demos/chartviewer/charts/barseries/verticalpercentbarchart.cpp @@ -33,7 +33,7 @@ public: QChart *createChart(const DataTable &table) { QChart *chart = new QChart(); - chart->setTitle("Stacked bar chart"); + chart->setTitle("Percent bar chart"); QPercentBarSeries *series = new QPercentBarSeries(chart); for (int i(0); i < table.count(); i++) { QBarSet *set = new QBarSet("Bar set " + QString::number(i)); diff --git a/demos/chartviewer/charts/font/font.cpp b/demos/chartviewer/charts/font/font.cpp index 8b8ad26..d8a100f 100644 --- a/demos/chartviewer/charts/font/font.cpp +++ b/demos/chartviewer/charts/font/font.cpp @@ -50,8 +50,8 @@ public: chart->setTitleFont(font); chart->axisX()->setLabelsFont(font); chart->axisY()->setLabelsFont(font); - chart->axisX()->setTitle("Axis X"); - chart->axisY()->setTitle("Axis Y"); + chart->axisX()->setTitleText("Axis X"); + chart->axisY()->setTitleText("Axis Y"); chart->axisX()->setTitleFont(font); chart->axisY()->setTitleFont(font); return chart; diff --git a/demos/chartviewer/charts/multiaxis/multivalueaxis.cpp b/demos/chartviewer/charts/multiaxis/multivalueaxis.cpp index a128f59..6e5ead2 100644 --- a/demos/chartviewer/charts/multiaxis/multivalueaxis.cpp +++ b/demos/chartviewer/charts/multiaxis/multivalueaxis.cpp @@ -59,20 +59,14 @@ public: chart->addSeries(series); axisX = new QValueAxis(); axisX->setLinePenColor(series->pen().color()); - if (nameIndex % 2) - axisX->setAlignment(Qt::AlignTop); - else - axisX->setAlignment(Qt::AlignBottom); axisY = new QValueAxis(); axisY->setLinePenColor(series->pen().color()); - if (nameIndex % 2) - axisY->setAlignment(Qt::AlignRight); - else - axisY->setAlignment(Qt::AlignLeft); + chart->addAxis(axisX,nameIndex % 2?Qt::AlignTop:Qt::AlignBottom); + chart->addAxis(axisY,nameIndex % 2?Qt::AlignRight:Qt::AlignLeft); + series->attachAxis(axisX); + series->attachAxis(axisY); - chart->setAxisX(axisX, series); - chart->setAxisY(axisY, series); nameIndex++; } diff --git a/demos/chartviewer/charts/multiaxis/multivalueaxis2.cpp b/demos/chartviewer/charts/multiaxis/multivalueaxis2.cpp index 907545c..16c55d4 100644 --- a/demos/chartviewer/charts/multiaxis/multivalueaxis2.cpp +++ b/demos/chartviewer/charts/multiaxis/multivalueaxis2.cpp @@ -59,20 +59,14 @@ public: chart->addSeries(series); axisX = new QValueAxis(); axisX->setLinePenColor(series->pen().color()); - if (nameIndex % 2) - axisX->setAlignment(Qt::AlignTop); - else - axisX->setAlignment(Qt::AlignBottom); + axisY = new QValueAxis(); axisY->setLinePenColor(series->pen().color()); - if (nameIndex % 2) - axisY->setAlignment(Qt::AlignRight); - else - axisY->setAlignment(Qt::AlignLeft); - - chart->setAxisX(axisX, series); - chart->setAxisY(axisY, series); + chart->addAxis(axisX, nameIndex % 2?Qt::AlignTop:Qt::AlignBottom); + chart->addAxis(axisY, nameIndex % 2?Qt::AlignRight:Qt::AlignLeft); + series->attachAxis(axisX); + series->attachAxis(axisY); nameIndex++; } diff --git a/demos/chartviewer/charts/multiaxis/multivalueaxis3.cpp b/demos/chartviewer/charts/multiaxis/multivalueaxis3.cpp index 1fa5dfa..b4cd80d 100644 --- a/demos/chartviewer/charts/multiaxis/multivalueaxis3.cpp +++ b/demos/chartviewer/charts/multiaxis/multivalueaxis3.cpp @@ -59,25 +59,16 @@ public: chart->addSeries(series); axisX = new QValueAxis(); axisX->setLinePenColor(series->pen().color()); - if (nameIndex % 2){ - axisX->setAlignment(Qt::AlignTop); - } else{ - axisX->setAlignment(Qt::AlignBottom); - } - axisX->setTitle("ValueAxis for series" + QString::number(nameIndex)); + axisX->setTitleText("ValueAxis for series" + QString::number(nameIndex)); axisY = new QValueAxis(); axisY->setLinePenColor(series->pen().color()); + axisY->setTitleText("ValueAxis for series" + QString::number(nameIndex)); - if (nameIndex % 2){ - axisY->setAlignment(Qt::AlignRight); - }else{ - axisY->setAlignment(Qt::AlignLeft); - } - axisY->setTitle("ValueAxis for series" + QString::number(nameIndex)); - - chart->setAxisX(axisX, series); - chart->setAxisY(axisY, series); + chart->addAxis(axisX, nameIndex % 2?Qt::AlignTop:Qt::AlignBottom); + chart->addAxis(axisY, nameIndex % 2?Qt::AlignRight:Qt::AlignLeft); + series->attachAxis(axisX); + series->attachAxis(axisY); nameIndex++; } diff --git a/demos/chartviewer/charts/multiaxis/multivalueaxis4.cpp b/demos/chartviewer/charts/multiaxis/multivalueaxis4.cpp index 5ab36d1..c35370c 100644 --- a/demos/chartviewer/charts/multiaxis/multivalueaxis4.cpp +++ b/demos/chartviewer/charts/multiaxis/multivalueaxis4.cpp @@ -59,22 +59,15 @@ public: chart->addSeries(series); axisX = new QValueAxis(); axisX->setLinePenColor(series->pen().color()); - axisX->setTitle("ValueAxis for series" + QString::number(nameIndex)); - if (nameIndex % 2) - axisX->setAlignment(Qt::AlignTop); - else - axisX->setAlignment(Qt::AlignBottom); + axisX->setTitleText("ValueAxis for series" + QString::number(nameIndex)); axisY = new QValueAxis(); axisY->setLinePenColor(series->pen().color()); - axisY->setTitle("ValueAxis for series" + QString::number(nameIndex)); + axisY->setTitleText("ValueAxis for series" + QString::number(nameIndex)); - if (nameIndex % 2) - axisY->setAlignment(Qt::AlignRight); - else - axisY->setAlignment(Qt::AlignLeft); - - chart->setAxisX(axisX, series); - chart->setAxisY(axisY, series); + chart->addAxis(axisX, nameIndex % 2?Qt::AlignTop:Qt::AlignBottom); + chart->addAxis(axisY, nameIndex % 2?Qt::AlignRight:Qt::AlignLeft); + series->attachAxis(axisX); + series->attachAxis(axisY); nameIndex++; } diff --git a/demos/chartviewer/charts/multiaxis/multivaluebaraxis.cpp b/demos/chartviewer/charts/multiaxis/multivaluebaraxis.cpp index 8dc7de7..09bfba1 100644 --- a/demos/chartviewer/charts/multiaxis/multivaluebaraxis.cpp +++ b/demos/chartviewer/charts/multiaxis/multivaluebaraxis.cpp @@ -64,22 +64,16 @@ public: chart->addSeries(series); axisX = new QValueAxis(); axisX->setLinePenColor(series->pen().color()); - axisX->setTitle("ValueAxis for series" + QString::number(nameIndex)); - if (nameIndex % 2) - axisX->setAlignment(Qt::AlignTop); - else - axisX->setAlignment(Qt::AlignBottom); + axisX->setTitleText("ValueAxis for series" + QString::number(nameIndex)); + axisY = new QValueAxis(); axisY->setLinePenColor(series->pen().color()); - axisY->setTitle("ValueAxis for series" + QString::number(nameIndex)); - - if (nameIndex % 2) - axisY->setAlignment(Qt::AlignRight); - else - axisY->setAlignment(Qt::AlignLeft); + axisY->setTitleText("ValueAxis for series" + QString::number(nameIndex)); - chart->setAxisX(axisX, series); - chart->setAxisY(axisY, series); + chart->addAxis(axisX, nameIndex % 2?Qt::AlignTop:Qt::AlignBottom); + chart->addAxis(axisY, nameIndex % 2?Qt::AlignRight:Qt::AlignLeft); + series->attachAxis(axisX); + series->attachAxis(axisY); nameIndex++; } @@ -96,7 +90,7 @@ public: barcategory->setLinePen(set->brush().color()); barcategory->setGridLinePen(set->brush().color()); - barcategory->setTitle("BarCategoryAxis Title"); + barcategory->setTitleText("BarCategoryAxis Title"); chart->setAxisX(barcategory, series); diff --git a/examples/barchart/main.cpp b/examples/barchart/main.cpp index 8a3728f..66c8812 100644 --- a/examples/barchart/main.cpp +++ b/examples/barchart/main.cpp @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) QBarCategoryAxis *axis = new QBarCategoryAxis(); axis->append(categories); chart->createDefaultAxes(); - chart->setAxisX(axis, series); + // chart->setAxisX(axis, series); //![4] //![5] diff --git a/examples/datetimeaxis/main.cpp b/examples/datetimeaxis/main.cpp index 1516ba3..88a4628 100644 --- a/examples/datetimeaxis/main.cpp +++ b/examples/datetimeaxis/main.cpp @@ -74,9 +74,9 @@ int main(int argc, char *argv[]) QDateTimeAxis *axisX = new QDateTimeAxis; axisX->setTickCount(10); axisX->setFormat("MMM yyyy"); - axisX->setTitle("Date"); + axisX->setTitleText("Date"); chart->setAxisX(axisX, series); - chart->axisY(series)->setTitle("Sunspots count"); + chart->axisY(series)->setTitleText("Sunspots count"); //![4] //![5] diff --git a/examples/multiaxis/main.cpp b/examples/multiaxis/main.cpp index 617e9f0..e681d23 100644 --- a/examples/multiaxis/main.cpp +++ b/examples/multiaxis/main.cpp @@ -49,23 +49,14 @@ int main(int argc, char *argv[]) axisX->setTickCount(7 + i); axisX->setLinePenColor(series->pen().color()); - if (i % 2) - axisX->setAlignment(Qt::AlignTop); - else - axisX->setAlignment(Qt::AlignBottom); - axisY = new QValueAxis; axisY->setTickCount(7 + i); axisY->setLinePenColor(series->pen().color()); - if (i % 2) - axisY->setAlignment(Qt::AlignRight); - else - axisY->setAlignment(Qt::AlignLeft); - - - chart->setAxisX(axisX, series); - chart->setAxisY(axisY, series); + chart->addAxis(axisX,i % 2?Qt::AlignTop:Qt::AlignBottom); + chart->addAxis(axisY,i % 2?Qt::AlignRight:Qt::AlignLeft); + series->attachAxis(axisX); + series->attachAxis(axisY); } //![2] diff --git a/examples/stackedbarchartdrilldown/drilldownchart.cpp b/examples/stackedbarchartdrilldown/drilldownchart.cpp index 6f1b134..e15817f 100644 --- a/examples/stackedbarchartdrilldown/drilldownchart.cpp +++ b/examples/stackedbarchartdrilldown/drilldownchart.cpp @@ -45,7 +45,7 @@ void DrilldownChart::changeSeries(DrilldownBarSeries *series) createDefaultAxes(); setAxisX(axis, series); - axisY()->setTitle("Crops"); + axisY()->setTitleText("Crops"); setTitle(series->name()); } diff --git a/examples/temperaturerecords/main.cpp b/examples/temperaturerecords/main.cpp index 8655124..ad2f81b 100644 --- a/examples/temperaturerecords/main.cpp +++ b/examples/temperaturerecords/main.cpp @@ -60,11 +60,11 @@ int main(int argc, char *argv[]) QBarCategoryAxis *axis = new QBarCategoryAxis(); axis->append(categories); - axis->setTitle("Month"); + axis->setTitleText("Month"); chart->createDefaultAxes(); chart->setAxisX(axis, series); chart->axisY(series)->setRange(-52, 52); - chart->axisY(series)->setTitle(QString::fromUtf8("Temperature [\u00B0C]")); + chart->axisY(series)->setTitleText(QString::fromUtf8("Temperature [\u00B0C]")); //![4] //![5] diff --git a/plugins/declarative/declarativechart.cpp b/plugins/declarative/declarativechart.cpp index 14d70fa..1a94cbe 100644 --- a/plugins/declarative/declarativechart.cpp +++ b/plugins/declarative/declarativechart.cpp @@ -32,6 +32,7 @@ #include "qcategoryaxis.h" #include "qabstractseries_p.h" #include "declarativemargins.h" +#include "chartdataset_p.h" #include "qchart_p.h" #ifndef QT_ON_ARM diff --git a/src/areachart/areachartitem.cpp b/src/areachart/areachartitem.cpp index 040d7f3..7bfa73d 100644 --- a/src/areachart/areachartitem.cpp +++ b/src/areachart/areachartitem.cpp @@ -33,8 +33,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE //TODO: optimize : remove points which are not visible -AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter) - : ChartItem(presenter), +AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, QGraphicsItem* item) + : ChartItem(areaSeries->d_func(),item), //TODO: fix me m_series(areaSeries), m_upper(0), m_lower(0), @@ -42,9 +42,9 @@ AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter) { setAcceptHoverEvents(true); setZValue(ChartPresenter::LineChartZValue); - m_upper = new AreaBoundItem(this, m_series->upperSeries(), presenter); + m_upper = new AreaBoundItem(this, m_series->upperSeries()); if (m_series->lowerSeries()) - m_lower = new AreaBoundItem(this, m_series->lowerSeries(), presenter); + m_lower = new AreaBoundItem(this, m_series->lowerSeries()); QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); @@ -61,6 +61,15 @@ AreaChartItem::~AreaChartItem() delete m_lower; } +void AreaChartItem::setPresenter(ChartPresenter *presenter) +{ + m_upper->setPresenter(presenter); + if (m_lower) { + m_lower->setPresenter(presenter); + } + ChartItem::setPresenter(presenter); +} + QRectF AreaChartItem::boundingRect() const { return m_rect; @@ -74,6 +83,7 @@ QPainterPath AreaChartItem::shape() const void AreaChartItem::updatePath() { QPainterPath path; + QRectF rect(QPointF(0,0),domain()->size()); path = m_upper->path(); @@ -82,8 +92,8 @@ void AreaChartItem::updatePath() } else { QPointF first = path.pointAtPercent(0); QPointF last = path.pointAtPercent(1); - path.lineTo(last.x(), m_clipRect.bottom()); - path.lineTo(first.x(), m_clipRect.bottom()); + path.lineTo(last.x(), rect.bottom()); + path.lineTo(first.x(), rect.bottom()); } path.closeSubpath(); prepareGeometryChange(); @@ -101,38 +111,33 @@ void AreaChartItem::handleUpdated() m_pointPen = m_series->pen(); m_pointPen.setWidthF(2 * m_pointPen.width()); setOpacity(m_series->opacity()); - update(); } void AreaChartItem::handleDomainUpdated() { - m_upper->setDomain(domain()); - m_upper->handleDomainUpdated(); + Domain* d = m_upper->domain().data(); + + d->setSize(domain()->size()); + d->setRange(domain()->minX(),domain()->maxX(),domain()->minY(),domain()->maxY()); + m_upper->handleDomainUpdated(); + if (m_lower) { - m_lower->setDomain(domain()); - m_lower->handleDomainUpdated(); + Domain* d = m_lower->domain().data(); + d->setSize(domain()->size()); + d->setRange(domain()->minX(),domain()->maxX(),domain()->minY(),domain()->maxY()); + m_lower->handleDomainUpdated(); } } -void AreaChartItem::handleGeometryChanged(const QRectF &rect) -{ - m_clipRect = rect.translated(-rect.topLeft()); - setPos(rect.topLeft()); - m_upper->handleGeometryChanged(rect); - if (m_lower) - m_lower->handleGeometryChanged(rect); -} - void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(widget) Q_UNUSED(option) - painter->save(); painter->setPen(m_linePen); painter->setBrush(m_brush); - painter->setClipRect(m_clipRect); + painter->setClipRect(QRectF(QPointF(0,0),domain()->size())); painter->drawPath(m_path); if (m_pointsVisible) { painter->setPen(m_pointPen); @@ -145,20 +150,20 @@ void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt void AreaChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { - emit clicked(m_upper->calculateDomainPoint(event->pos())); + emit clicked(m_upper->domain()->calculateDomainPoint(event->pos())); ChartItem::mousePressEvent(event); } void AreaChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { - emit hovered(m_upper->calculateDomainPoint(event->pos()), true); + emit hovered(domain()->calculateDomainPoint(event->pos()), true); event->accept(); // QGraphicsItem::hoverEnterEvent(event); } void AreaChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { - emit hovered(m_upper->calculateDomainPoint(event->pos()), false); + emit hovered(domain()->calculateDomainPoint(event->pos()), false); event->accept(); // QGraphicsItem::hoverEnterEvent(event); } diff --git a/src/areachart/areachartitem_p.h b/src/areachart/areachartitem_p.h index bb2fc55..0522096 100644 --- a/src/areachart/areachartitem_p.h +++ b/src/areachart/areachartitem_p.h @@ -43,7 +43,7 @@ class AreaChartItem : public ChartItem { Q_OBJECT public: - AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter); + AreaChartItem(QAreaSeries *areaSeries, QGraphicsItem* item = 0); ~AreaChartItem(); //from QGraphicsItem @@ -56,6 +56,7 @@ public: void updatePath(); + void setPresenter(ChartPresenter *presenter); protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event); @@ -68,7 +69,6 @@ Q_SIGNALS: public Q_SLOTS: void handleUpdated(); void handleDomainUpdated(); - void handleGeometryChanged(const QRectF &size); private: QAreaSeries *m_series; @@ -76,7 +76,6 @@ private: LineChartItem *m_lower; QPainterPath m_path; QRectF m_rect; - QRectF m_clipRect; QPen m_linePen; QPen m_pointPen; QBrush m_brush; @@ -87,14 +86,13 @@ private: class AreaBoundItem : public LineChartItem { public: - AreaBoundItem(AreaChartItem *item, QLineSeries *lineSeries, ChartPresenter *presenter) - : LineChartItem(lineSeries, 0), m_item(item) + AreaBoundItem(AreaChartItem *area, QLineSeries *lineSeries,QGraphicsItem* item = 0) + : LineChartItem(lineSeries, item), m_item(area) { - setPresenter(presenter); } ~AreaBoundItem() {} - void updateGeometry() + void updateGeometry() { LineChartItem::updateGeometry(); m_item->updatePath(); diff --git a/src/areachart/qareaseries.cpp b/src/areachart/qareaseries.cpp index 9d82664..144e515 100644 --- a/src/areachart/qareaseries.cpp +++ b/src/areachart/qareaseries.cpp @@ -27,6 +27,7 @@ #include "charttheme_p.h" #include "qvalueaxis.h" #include "qarealegendmarker.h" +#include "qchart_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -180,8 +181,8 @@ QAreaSeries::QAreaSeries(QObject *parent) QAreaSeries::~QAreaSeries() { Q_D(QAreaSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } /*! @@ -198,7 +199,10 @@ QAbstractSeries::SeriesType QAreaSeries::type() const void QAreaSeries::setUpperSeries(QLineSeries *series) { Q_D(QAreaSeries); - d->m_upperSeries = series; + if(d->m_upperSeries!=series){ + d->m_upperSeries = series; + //TODO: + } } QLineSeries *QAreaSeries::upperSeries() const @@ -322,14 +326,14 @@ QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lo { } -void QAreaSeriesPrivate::scaleDomain(Domain &domain) +void QAreaSeriesPrivate::initializeDomain() { Q_Q(QAreaSeries); - qreal minX(domain.minX()); - qreal minY(domain.minY()); - qreal maxX(domain.maxX()); - qreal maxY(domain.maxY()); + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); QLineSeries *upperSeries = q->upperSeries(); QLineSeries *lowerSeries = q->lowerSeries(); @@ -358,21 +362,30 @@ void QAreaSeriesPrivate::scaleDomain(Domain &domain) } } - domain.setRange(minX, maxX, minY, maxY); + domain()->setRange(minX, maxX, minY, maxY); } -ChartElement *QAreaSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QAreaSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QAreaSeries); - - AreaChartItem *area = new AreaChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { + AreaChartItem *area = new AreaChartItem(q,parent); + m_item.reset(area); + QAbstractSeriesPrivate::initializeGraphics(parent); +} +void QAreaSeriesPrivate::initializeAnimations(QChart::AnimationOptions options) +{ + Q_Q(QAreaSeries); + AreaChartItem *area = static_cast(m_item.data()); + if (options.testFlag(QChart::SeriesAnimations)) { area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem())); if (q->lowerSeries()) - area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem())); + area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem())); + }else{ + area->upperLineItem()->setAnimation(0); + if (q->lowerSeries()) + area->lowerLineItem()->setAnimation(0); } - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return area; + QAbstractSeriesPrivate::initializeAnimations(options); } QList QAreaSeriesPrivate::createLegendMarkers(QLegend* legend) @@ -382,9 +395,10 @@ QList QAreaSeriesPrivate::createLegendMarkers(QLegend* legend) return list << new QAreaLegendMarker(q,legend); } -void QAreaSeriesPrivate::initializeAxis(QAbstractAxis *axis) + +void QAreaSeriesPrivate::initializeAxes() { - Q_UNUSED(axis); + } QAbstractAxis::AxisType QAreaSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const @@ -393,6 +407,34 @@ QAbstractAxis::AxisType QAreaSeriesPrivate::defaultAxisType(Qt::Orientation orie return QAbstractAxis::AxisTypeValue; } +QAbstractAxis* QAreaSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const +{ + Q_UNUSED(orientation); + return 0; +} + +void QAreaSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced) +{ + Q_Q(QAreaSeries); + QPen pen; + QBrush brush; + + const QList gradients = theme->seriesGradients(); + const QList colors = theme->seriesColors(); + + if (forced || pen == m_pen) { + pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0)); + pen.setWidthF(2); + q->setPen(pen); + } + + if (forced || brush == m_brush) { + QBrush brush(colors.at(index % colors.size())); + q->setBrush(brush); + } +} + + #include "moc_qareaseries.cpp" #include "moc_qareaseries_p.cpp" diff --git a/src/areachart/qareaseries_p.h b/src/areachart/qareaseries_p.h index e2fe3be..bdaab1f 100644 --- a/src/areachart/qareaseries_p.h +++ b/src/areachart/qareaseries_p.h @@ -43,12 +43,16 @@ class QAreaSeriesPrivate: public QAbstractSeriesPrivate public: QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries, QAreaSeries *q); - void scaleDomain(Domain &domain); - ChartElement *createGraphics(ChartPresenter *presenter); - QList createLegendMarkers(QLegend* legend); - void initializeAxis(QAbstractAxis *axis); + void initializeDomain(); + void initializeAxes(); + void initializeGraphics(QGraphicsItem* parent); + void initializeTheme(int index, ChartTheme* theme, bool forced = false); + void initializeAnimations(QChart::AnimationOptions options); + + QList createLegendMarkers(QLegend *legend); QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const; + QAbstractAxis* createDefaultAxis(Qt::Orientation) const; Q_SIGNALS: void updated(); diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp b/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp index 3498230..b45de8a 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisx.cpp @@ -27,8 +27,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter) - : HorizontalAxis(axis, presenter, true), +ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, QGraphicsItem* item) + : HorizontalAxis(axis, item, true), m_categoriesAxis(axis) { @@ -93,12 +93,15 @@ void ChartBarCategoryAxisX::updateGeometry() void ChartBarCategoryAxisX::handleAxisUpdated() { + //TODO: + /* if (m_categoriesAxis->categories() != m_categories) { m_categories = m_categoriesAxis->categories(); if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2) updateGeometry(); } - ChartAxis::handleAxisUpdated(); + */ + //TODO: ChartAxis::handleAxisUpdated(); } QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisx_p.h b/src/axis/barcategoryaxis/chartbarcategoryaxisx_p.h index c7df385..ff6e53c 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisx_p.h +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisx_p.h @@ -40,7 +40,7 @@ class QBarCategoryAxis; class ChartBarCategoryAxisX : public HorizontalAxis { public: - ChartBarCategoryAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter); + ChartBarCategoryAxisX(QBarCategoryAxis *axis, QGraphicsItem* item = 0); ~ChartBarCategoryAxisX(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const; diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp b/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp index d87b6cb..c0e6b8d 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisy.cpp @@ -27,8 +27,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter) - : VerticalAxis(axis, presenter, true), +ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, QGraphicsItem* item) + : VerticalAxis(axis, item, true), m_categoriesAxis(axis) { } @@ -91,13 +91,14 @@ void ChartBarCategoryAxisY::updateGeometry() void ChartBarCategoryAxisY::handleAxisUpdated() { - + //TODO: + /* if (m_categoriesAxis->categories() != m_categories) { m_categories = m_categoriesAxis->categories(); if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2) updateGeometry(); - } - ChartAxis::handleAxisUpdated(); + }*/ + //TODO :: ChartAxis::handleAxisUpdated(); } QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const diff --git a/src/axis/barcategoryaxis/chartbarcategoryaxisy_p.h b/src/axis/barcategoryaxis/chartbarcategoryaxisy_p.h index d0ffb15..d06956b 100644 --- a/src/axis/barcategoryaxis/chartbarcategoryaxisy_p.h +++ b/src/axis/barcategoryaxis/chartbarcategoryaxisy_p.h @@ -40,7 +40,7 @@ class ChartPresenter; class ChartBarCategoryAxisY : public VerticalAxis { public: - ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter); + ChartBarCategoryAxisY(QBarCategoryAxis *axis, QGraphicsItem* item = 0); ~ChartBarCategoryAxisY(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const; diff --git a/src/axis/barcategoryaxis/qbarcategoryaxis.cpp b/src/axis/barcategoryaxis/qbarcategoryaxis.cpp index e3ee753..9f4f4bd 100644 --- a/src/axis/barcategoryaxis/qbarcategoryaxis.cpp +++ b/src/axis/barcategoryaxis/qbarcategoryaxis.cpp @@ -23,7 +23,7 @@ #include "chartbarcategoryaxisx_p.h" #include "chartbarcategoryaxisy_p.h" #include "domain_p.h" -#include "chartdataset_p.h" +#include "qchart.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -87,7 +87,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Defines the minimum value on the axis. */ /*! - \qmlproperty QString BarCategoryAxis::min + \qmlproperty QString Bar if (d->m_chart) + d->m_chart->removeAxis(this);CategoryAxis::min Defines the minimum value on the axis. */ @@ -160,8 +161,8 @@ QBarCategoryAxis::QBarCategoryAxis(QObject *parent): QBarCategoryAxis::~QBarCategoryAxis() { Q_D(QBarCategoryAxis); - if (d->m_dataset) - d->m_dataset->removeAxis(this); + if (d->m_chart) + d->m_chart->removeAxis(this); } /*! @@ -248,7 +249,7 @@ void QBarCategoryAxis::remove(const QString &category) setRange(d->m_minCategory, d->m_categories.last()); } else { d->updateCategoryDomain(); - d->emitUpdated(); + //TODO:: d->emitUpdated(); } } else { setRange(QString::null, QString::null); @@ -282,7 +283,7 @@ void QBarCategoryAxis::insert(int index, const QString &category) setRange(d->m_minCategory, d->m_categories.last()); } else { d->updateCategoryDomain(); - d->emitUpdated(); + //TODO:: d->emitUpdated(); } emit categoriesChanged(); @@ -307,7 +308,7 @@ void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCat } else if (d->m_maxCategory == oldCategory) { setRange(d->m_minCategory, newCategory); } else { - d->emitUpdated(); + //TODO:: d->emitUpdated(); } emit categoriesChanged(); emit countChanged(); @@ -375,7 +376,7 @@ QString QBarCategoryAxis::at(int index) const void QBarCategoryAxis::setMin(const QString &min) { Q_D(QBarCategoryAxis); - setRange(min, d->m_maxCategory); + d->setRange(min, d->m_maxCategory); } /*! @@ -393,7 +394,7 @@ QString QBarCategoryAxis::min() const void QBarCategoryAxis::setMax(const QString &max) { Q_D(QBarCategoryAxis); - setRange(d->m_minCategory, max); + d->setRange(d->m_minCategory, max); } /*! @@ -411,44 +412,7 @@ QString QBarCategoryAxis::max() const void QBarCategoryAxis::setRange(const QString &minCategory, const QString &maxCategory) { Q_D(QBarCategoryAxis); - - bool changed = false; - - //special case in case or clearing all categories - if (minCategory.isNull() && maxCategory.isNull()) { - d->m_minCategory = minCategory; - d->m_maxCategory = maxCategory; - d->m_min = 0; - d->m_max = 0; - emit minChanged(minCategory); - emit maxChanged(maxCategory); - d->m_count = 0; - emit rangeChanged(d->m_minCategory, d->m_maxCategory); - d->emitUpdated(); - } - - if (d->m_categories.indexOf(maxCategory) < d->m_categories.indexOf(minCategory)) - return; - - if (!minCategory.isEmpty() && d->m_minCategory != minCategory && d->m_categories.contains(minCategory)) { - d->m_minCategory = minCategory; - d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5; - changed = true; - emit minChanged(minCategory); - } - - if (!maxCategory.isEmpty() && d->m_maxCategory != maxCategory && d->m_categories.contains(maxCategory)) { - d->m_maxCategory = maxCategory; - d->m_max = d->m_categories.indexOf(d->m_maxCategory) + 0.5; - changed = true; - emit maxChanged(maxCategory); - } - - if (changed) { - d->m_count = d->m_max - d->m_min; - emit rangeChanged(d->m_minCategory, d->m_maxCategory); - d->emitUpdated(); - } + d->setRange(minCategory,maxCategory); } /*! @@ -487,55 +451,114 @@ void QBarCategoryAxisPrivate::setMax(const QVariant &max) void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max) { - Q_Q(QBarCategoryAxis); QString value1 = min.toString(); QString value2 = max.toString(); - q->setRange(value1, value2); + setRange(value1, value2); } -void QBarCategoryAxisPrivate::handleDomainUpdated() +void QBarCategoryAxisPrivate::setRange(qreal min, qreal max) { Q_Q(QBarCategoryAxis); - Domain *domain = qobject_cast(sender()); - - if (m_orientation == Qt::Horizontal) { - m_min = domain->minX(); - m_max = domain->maxX(); - } else if (m_orientation == Qt::Vertical) { - m_min = domain->minY(); - m_max = domain->maxY(); - } + bool categoryChanged = false; bool changed = false; - int min = m_min + 0.5; - if (min >= 0 && min < m_categories.count()) { - QString minCategory = m_categories.at(min); - if (m_minCategory != minCategory && !minCategory.isEmpty()) { - m_minCategory = minCategory; - changed = true; - emit q->minChanged(minCategory); + if (min > max) + return; + + if (!qFuzzyIsNull(m_min - min)) { + m_min = min; + changed = true; + + int imin = m_min + 0.5; + if (imin >= 0 && imin < m_categories.count()) { + QString minCategory = m_categories.at(imin); + if (m_minCategory != minCategory && !minCategory.isEmpty()) { + m_minCategory = minCategory; + categoryChanged = true; + emit q->minChanged(minCategory); + } } + } - int max = m_max - 0.5; - if (max >= 0 && max < m_categories.count()) { - QString maxCategory = m_categories.at(max); - if (m_maxCategory != maxCategory && !maxCategory.isEmpty()) { - m_maxCategory = maxCategory; - emit q->maxChanged(maxCategory); + + if (!qFuzzyIsNull(m_max - max)) { + m_max = max; + changed = true; + + int imax = m_max - 0.5; + if (imax >= 0 && imax < m_categories.count()) { + QString maxCategory = m_categories.at(imax); + if (m_maxCategory != maxCategory && !maxCategory.isEmpty()) { + m_maxCategory = maxCategory; + categoryChanged = true; + emit q->maxChanged(maxCategory); + } } } - if (changed) + if (categoryChanged){ emit q->rangeChanged(m_minCategory, m_maxCategory); + } + + if (changed) { + emit rangeChanged(m_min,m_max); + } } -ChartAxis *QBarCategoryAxisPrivate::createGraphics(ChartPresenter *presenter) +void QBarCategoryAxisPrivate::setRange(const QString &minCategory, const QString &maxCategory) +{ + Q_Q(QBarCategoryAxis); + bool changed = false; + + //special case in case or clearing all categories + if (minCategory.isNull() && maxCategory.isNull()) { + m_minCategory = minCategory; + m_maxCategory = maxCategory; + m_min = 0; + m_max = 0; + m_count = 0; + emit q->minChanged(minCategory); + emit q->maxChanged(maxCategory); + emit q->rangeChanged(m_minCategory, m_maxCategory); + emit rangeChanged(m_min,m_max); + } + + if (m_categories.indexOf(maxCategory) < m_categories.indexOf(minCategory)) + return; + + if (!minCategory.isEmpty() && m_minCategory != minCategory && m_categories.contains(minCategory)) { + m_minCategory = minCategory; + m_min = m_categories.indexOf(m_minCategory) - 0.5; + changed = true; + emit q->minChanged(minCategory); + } + + if (!maxCategory.isEmpty() && m_maxCategory != maxCategory && m_categories.contains(maxCategory)) { + m_maxCategory = maxCategory; + m_max = m_categories.indexOf(m_maxCategory) + 0.5; + changed = true; + emit q->maxChanged(maxCategory); + } + + if (changed) { + m_count = m_max - m_min; + emit q->rangeChanged(m_minCategory, m_maxCategory); + emit rangeChanged(m_min,m_max); + } +} + +void QBarCategoryAxisPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QBarCategoryAxis); - if (m_orientation == Qt::Vertical) - return new ChartBarCategoryAxisY(q, presenter); - return new ChartBarCategoryAxisX(q, presenter); + ChartAxis* axis(0); + if (orientation() == Qt::Vertical) + axis = new ChartBarCategoryAxisY(q,parent); + if (orientation() == Qt::Horizontal) + axis = new ChartBarCategoryAxisX(q,parent); + + m_item.reset(axis); + QAbstractAxisPrivate::initializeGraphics(parent); } void QBarCategoryAxisPrivate::updateCategoryDomain() @@ -546,14 +569,13 @@ void QBarCategoryAxisPrivate::updateCategoryDomain() } -void QBarCategoryAxisPrivate::intializeDomain(Domain *domain) +void QBarCategoryAxisPrivate::initializeDomain(Domain *domain) { - Q_Q(QBarCategoryAxis); if (m_max == m_min) { int min; int max; - if (m_orientation == Qt::Vertical) { + if (orientation() == Qt::Vertical) { min = domain->minY() + 0.5; max = domain->maxY() - 0.5; } else { @@ -564,7 +586,7 @@ void QBarCategoryAxisPrivate::intializeDomain(Domain *domain) if (min > 0 && min < m_categories.count() && max > 0 && max < m_categories.count()) q->setRange(m_categories.at(min), m_categories.at(max)); } else { - if (m_orientation == Qt::Vertical) + if (orientation() == Qt::Vertical) domain->setRangeY(m_min, m_max); else domain->setRangeX(m_min, m_max); diff --git a/src/axis/barcategoryaxis/qbarcategoryaxis_p.h b/src/axis/barcategoryaxis/qbarcategoryaxis_p.h index cd581d3..aaa7056 100644 --- a/src/axis/barcategoryaxis/qbarcategoryaxis_p.h +++ b/src/axis/barcategoryaxis/qbarcategoryaxis_p.h @@ -46,19 +46,23 @@ public: ~QBarCategoryAxisPrivate(); public: - ChartAxis *createGraphics(ChartPresenter *presenter); - void intializeDomain(Domain *domain); - void handleDomainUpdated(); + void initializeGraphics(QGraphicsItem* parent); + void initializeDomain(Domain *domain); void updateCategoryDomain(); + + //interface for manipulating range form base class + void setRange(const QVariant &min, const QVariant &max); + void setMin(const QVariant &min); + void setMax(const QVariant &max); + + //interface manipulating range form domain qreal min() { return m_min; } qreal max() { return m_max; } - int count() const { return m_count; } + void setRange(qreal min,qreal max); private: //range handling - void setMin(const QVariant &min); - void setMax(const QVariant &max); - void setRange(const QVariant &min, const QVariant &max); + void setRange(const QString &minCategory, const QString &maxCategory); private: QStringList m_categories; diff --git a/src/axis/categoryaxis/chartcategoryaxisx.cpp b/src/axis/categoryaxis/chartcategoryaxisx.cpp index 49a880c..ca9e176 100644 --- a/src/axis/categoryaxis/chartcategoryaxisx.cpp +++ b/src/axis/categoryaxis/chartcategoryaxisx.cpp @@ -28,8 +28,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis, ChartPresenter *presenter) - : HorizontalAxis(axis, presenter, true), +ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis, QGraphicsItem* item) + : HorizontalAxis(axis, item, true), m_axis(axis) { } @@ -75,7 +75,7 @@ void ChartCategoryAxisX::updateGeometry() void ChartCategoryAxisX::handleAxisUpdated() { updateGeometry(); - ChartAxis::handleAxisUpdated(); + //TODO :: ChartAxis::handleAxisUpdated(); } QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const diff --git a/src/axis/categoryaxis/chartcategoryaxisx_p.h b/src/axis/categoryaxis/chartcategoryaxisx_p.h index 6f30f3e..1689840 100644 --- a/src/axis/categoryaxis/chartcategoryaxisx_p.h +++ b/src/axis/categoryaxis/chartcategoryaxisx_p.h @@ -40,7 +40,7 @@ class ChartPresenter; class ChartCategoryAxisX : public HorizontalAxis { public: - ChartCategoryAxisX(QCategoryAxis *axis, ChartPresenter *presenter); + ChartCategoryAxisX(QCategoryAxis *axis, QGraphicsItem* item = 0); ~ChartCategoryAxisX(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const; diff --git a/src/axis/categoryaxis/chartcategoryaxisy.cpp b/src/axis/categoryaxis/chartcategoryaxisy.cpp index f1d3ba2..ec0b4da 100644 --- a/src/axis/categoryaxis/chartcategoryaxisy.cpp +++ b/src/axis/categoryaxis/chartcategoryaxisy.cpp @@ -29,8 +29,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, ChartPresenter *presenter) - : VerticalAxis(axis, presenter, true), +ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, QGraphicsItem* item) + : VerticalAxis(axis, item, true), m_axis(axis) { } @@ -75,7 +75,7 @@ void ChartCategoryAxisY::updateGeometry() void ChartCategoryAxisY::handleAxisUpdated() { updateGeometry(); - ChartAxis::handleAxisUpdated(); + //TODO:: ChartAxis::handleAxisUpdated(); } QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const diff --git a/src/axis/categoryaxis/chartcategoryaxisy_p.h b/src/axis/categoryaxis/chartcategoryaxisy_p.h index 296f331..5261c6f 100644 --- a/src/axis/categoryaxis/chartcategoryaxisy_p.h +++ b/src/axis/categoryaxis/chartcategoryaxisy_p.h @@ -40,7 +40,7 @@ class ChartPresenter; class ChartCategoryAxisY : public VerticalAxis { public: - ChartCategoryAxisY(QCategoryAxis *axis, ChartPresenter *presenter); + ChartCategoryAxisY(QCategoryAxis *axis, QGraphicsItem* item = 0); ~ChartCategoryAxisY(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const; diff --git a/src/axis/categoryaxis/qcategoryaxis.cpp b/src/axis/categoryaxis/qcategoryaxis.cpp index 9fab1ae..0db64fd 100644 --- a/src/axis/categoryaxis/qcategoryaxis.cpp +++ b/src/axis/categoryaxis/qcategoryaxis.cpp @@ -22,6 +22,7 @@ #include "qcategoryaxis_p.h" #include "chartcategoryaxisx_p.h" #include "chartcategoryaxisy_p.h" +#include "qchart.h" #include #include @@ -114,6 +115,9 @@ QCategoryAxis::QCategoryAxis(QObject *parent): */ QCategoryAxis::~QCategoryAxis() { + Q_D(QCategoryAxis); + if (d->m_chart) + d->m_chart->removeAxis(this); } /*! @@ -223,7 +227,7 @@ void QCategoryAxis::remove(const QString &categoryLabel) d->m_categoriesMap.insert(label, range); } } - d->emitUpdated(); + //TODO:: d->emitUpdated(); } } @@ -247,7 +251,7 @@ void QCategoryAxis::replaceLabel(const QString &oldLabel, const QString &newLabe Range range = d->m_categoriesMap.value(oldLabel); d->m_categoriesMap.remove(oldLabel); d->m_categoriesMap.insert(newLabel, range); - d->emitUpdated(); + //TODO:: d->emitUpdated(); } } @@ -296,19 +300,17 @@ int QCategoryAxisPrivate::ticksCount() const return m_categories.count() + 1; } -void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max, int count) -{ - Q_UNUSED(count); - Q_UNUSED(min); - Q_UNUSED(max); -} - -ChartAxis *QCategoryAxisPrivate::createGraphics(ChartPresenter *presenter) +void QCategoryAxisPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QCategoryAxis); - if (m_orientation == Qt::Vertical) - return new ChartCategoryAxisY(q, presenter); - return new ChartCategoryAxisX(q, presenter); + ChartAxis* axis(0); + if (orientation() == Qt::Vertical) + axis = new ChartCategoryAxisY(q,parent); + else if(orientation() == Qt::Horizontal) + axis = new ChartCategoryAxisX(q,parent); + + m_item.reset(axis); + QAbstractAxisPrivate::initializeGraphics(parent); } #include "moc_qcategoryaxis.cpp" diff --git a/src/axis/categoryaxis/qcategoryaxis_p.h b/src/axis/categoryaxis/qcategoryaxis_p.h index 02db3ce..a8e52ca 100644 --- a/src/axis/categoryaxis/qcategoryaxis_p.h +++ b/src/axis/categoryaxis/qcategoryaxis_p.h @@ -45,15 +45,12 @@ public: QCategoryAxisPrivate(QCategoryAxis *q); ~QCategoryAxisPrivate(); - ChartAxis *createGraphics(ChartPresenter *presenter); + void initializeGraphics(QGraphicsItem* parent); int ticksCount() const; Q_SIGNALS: void changed(qreal min, qreal max, int tickCount, bool niceNumbers); -public Q_SLOTS: - void handleAxisRangeChanged(qreal min, qreal max, int count); - private: QMap m_categoriesMap; QStringList m_categories; diff --git a/src/axis/chartaxis.cpp b/src/axis/chartaxis.cpp index 031975c..cd3c020 100644 --- a/src/axis/chartaxis.cpp +++ b/src/axis/chartaxis.cpp @@ -32,34 +32,56 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartAxis::ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis) - : ChartElement(presenter), - m_chartAxis(axis), +ChartAxis::ChartAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis) + : ChartElement(item), + m_axis(axis), m_labelsAngle(0), - m_grid(new QGraphicsItemGroup(presenter->rootItem())), - m_shades(new QGraphicsItemGroup(presenter->rootItem())), - m_labels(new QGraphicsItemGroup(presenter->rootItem())), - m_arrow(new QGraphicsItemGroup(presenter->rootItem())), - m_title(new QGraphicsSimpleTextItem(presenter->rootItem())), - m_min(0), - m_max(0), + m_grid(new QGraphicsItemGroup(item)), + m_arrow(new QGraphicsItemGroup(item)), + m_shades(new QGraphicsItemGroup(item)), + m_labels(new QGraphicsItemGroup(item)), + m_title(new QGraphicsSimpleTextItem(item)), m_animation(0), m_labelPadding(5), m_intervalAxis(intervalAxis) { + Q_ASSERT(item); //initial initialization - m_arrow->setZValue(ChartPresenter::AxisZValue); m_arrow->setHandlesChildEvents(false); + m_arrow->setZValue(ChartPresenter::AxisZValue); + m_arrow->setVisible(m_axis->isLineVisible()); m_labels->setZValue(ChartPresenter::AxisZValue); + m_labels->setVisible(m_axis->labelsVisible()); m_shades->setZValue(ChartPresenter::ShadesZValue); + m_shades->setVisible(m_axis->shadesVisible()); m_grid->setZValue(ChartPresenter::GridZValue); + m_grid->setVisible(m_axis->isGridLineVisible()); m_title->setZValue(ChartPresenter::GridZValue); + connectSlots(); - QObject::connect(m_chartAxis->d_ptr.data(), SIGNAL(updated()), this, SLOT(handleAxisUpdated())); - - QGraphicsSimpleTextItem item; - m_font = item.font(); + setFlag(QGraphicsItem::ItemHasNoContents,true); +} +void ChartAxis::connectSlots() +{ + QObject::connect(m_axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleVisibleChanged(bool))); + QObject::connect(m_axis,SIGNAL(lineVisibleChanged(bool)),this,SLOT(handleArrowVisibleChanged(bool))); + QObject::connect(m_axis,SIGNAL(gridLineVisibleChanged(bool)),this,SLOT(handleGridVisibleChanged(bool))); + QObject::connect(m_axis,SIGNAL(labelsVisibleChanged(bool)),this,SLOT(handleLabelsVisibleChanged(bool))); + QObject::connect(m_axis,SIGNAL(shadesVisibleChanged(bool)),this,SLOT(handleShadesVisibleChanged(bool))); + QObject::connect(m_axis,SIGNAL(labelsAngleChanged(int)),this,SLOT(handleLabelsAngleChanged(int))); + QObject::connect(m_axis,SIGNAL(linePenChanged(const QPen&)),this,SLOT(handleArrowPenChanged(const QPen&))); + QObject::connect(m_axis,SIGNAL(labelsPenChanged(const QPen&)),this,SLOT(handleLabelsPenChanged(const QPen&))); + QObject::connect(m_axis,SIGNAL(labelsBrushChanged(const QBrush&)),this,SLOT(handleLabelsBrushChanged(const QBrush&))); + QObject::connect(m_axis,SIGNAL(labelsFontChanged(const QFont&)),this,SLOT(handleLabelsFontChanged(const QFont&))); + QObject::connect(m_axis,SIGNAL(gridLinePenChanged(const QPen&)),this,SLOT(handleGridPenChanged(const QPen&))); + QObject::connect(m_axis,SIGNAL(shadesPenChanged(const QPen&)),this,SLOT(handleShadesPenChanged(const QPen&))); + QObject::connect(m_axis,SIGNAL(shadesBrushChanged(const QBrush&)),this,SLOT(handleShadesBrushChanged(const QBrush&))); + QObject::connect(m_axis,SIGNAL(titleTextChanged(const QString&)),this,SLOT(handleTitleTextChanged(const QString&))); + QObject::connect(m_axis,SIGNAL(titleFontChanged(const QFont&)),this,SLOT(handleTitleFontChanged(const QFont&))); + QObject::connect(m_axis,SIGNAL(titlePenChanged(const QPen&)),this,SLOT(handleTitlePenChanged(const QPen&))); + QObject::connect(m_axis,SIGNAL(titleBrushChanged(const QBrush&)),this,SLOT(handleTitleBrushChanged(const QBrush&))); + QObject::connect(m_axis->d_ptr.data(),SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(handleRangeChanged(qreal,qreal))); } ChartAxis::~ChartAxis() @@ -78,23 +100,41 @@ void ChartAxis::setLayout(QVector &layout) void ChartAxis::createItems(int count) { - if (m_arrow->childItems().size() == 0) - m_arrow->addToGroup(new AxisItem(this, presenter()->rootItem())); + if (m_arrow->children().size() == 0){ + QGraphicsLineItem* arrow = new ArrowItem(this, this); + arrow->setPen(m_axis->linePen()); + m_arrow->addToGroup(arrow); + } - if (m_intervalAxis && m_grid->childItems().size() == 0) { - for (int i = 0 ; i < 2 ; i ++) - m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); + if (m_intervalAxis && m_grid->children().size() == 0) { + for (int i = 0 ; i < 2 ; i ++){ + QGraphicsLineItem* item = new QGraphicsLineItem(this); + item->setPen(m_axis->gridLinePen()); + m_grid->addToGroup(item); + } } for (int i = 0; i < count; ++i) { - m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); - QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(presenter()->rootItem()); - label->setFont(m_font); + QGraphicsLineItem* arrow = new QGraphicsLineItem(this); + arrow->setPen(m_axis->linePen()); + QGraphicsLineItem* grid = new QGraphicsLineItem(this); + grid->setPen(m_axis->gridLinePen()); + QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(this); + label->setFont(m_axis->labelsFont()); + label->setPen(m_axis->labelsPen()); + label->setBrush(m_axis->labelsBrush()); + m_arrow->addToGroup(arrow); + m_grid->addToGroup(grid); m_labels->addToGroup(label); - m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); - if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2) - m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem())); + + if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2){ + QGraphicsRectItem* shades = new QGraphicsRectItem(this); + shades->setPen(m_axis->shadesPen()); + shades->setBrush(m_axis->shadesBrush()); + m_shades->addToGroup(shades); + } } + } void ChartAxis::deleteItems(int count) @@ -122,8 +162,6 @@ void ChartAxis::updateLayout(QVector &layout) else if (diff < 0) createItems(-diff); - if (diff < 0) handleAxisUpdated(); - if (m_animation) { switch (presenter()->state()) { case ChartPresenter::ZoomInState: @@ -154,67 +192,103 @@ void ChartAxis::updateLayout(QVector &layout) } } -void ChartAxis::setArrowOpacity(qreal opacity) +void ChartAxis::setLabelPadding(int padding) +{ + m_labelPadding = padding; +} + +bool ChartAxis::isEmpty() +{ + return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(min(),max()); +} + +void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid) +{ + m_gridRect = grid; + m_axisRect = axis; + + if (isEmpty()) + return; + + QVector layout = calculateLayout(); + updateLayout(layout); +} + +qreal ChartAxis::min() const { - m_arrow->setOpacity(opacity); + return m_axis->d_ptr->min(); } -qreal ChartAxis::arrowOpacity() const +qreal ChartAxis::max() const { - return m_arrow->opacity(); + return m_axis->d_ptr->max(); } -void ChartAxis::setArrowVisibility(bool visible) +QFont ChartAxis::font() const { - m_arrow->setOpacity(visible); + return m_axis->labelsFont(); } -void ChartAxis::setGridOpacity(qreal opacity) +QFont ChartAxis::titleFont() const { - m_grid->setOpacity(opacity); + return m_axis->titleFont(); } -qreal ChartAxis::gridOpacity() const +QString ChartAxis::titleText() const { - return m_grid->opacity(); + return m_axis->titleText(); } -void ChartAxis::setGridVisibility(bool visible) +void ChartAxis::axisSelected() { - m_grid->setOpacity(visible); + emit clicked(); } -void ChartAxis::setLabelsOpacity(qreal opacity) +Qt::Orientation ChartAxis::orientation() const { - m_labels->setOpacity(opacity); + return m_axis->orientation(); } -qreal ChartAxis::labelsOpacity() const +Qt::Alignment ChartAxis::alignment() const +{ + return m_axis->alignment(); +} + +void ChartAxis::setLabels(const QStringList &labels) +{ + m_labelsList = labels; +} + +QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const { - return m_labels->opacity(); + Q_UNUSED(which); + Q_UNUSED(constraint); + return QSizeF(); } -void ChartAxis::setLabelsVisibility(bool visible) +//handlers + +void ChartAxis::handleArrowVisibleChanged(bool visible) { - m_labels->setOpacity(visible); + m_arrow->setVisible(visible); } -void ChartAxis::setShadesOpacity(qreal opacity) +void ChartAxis::handleGridVisibleChanged(bool visible) { - m_shades->setOpacity(opacity); + m_grid->setVisible(visible); } -qreal ChartAxis::shadesOpacity() const +void ChartAxis::handleLabelsVisibleChanged(bool visible) { - return m_shades->opacity(); + m_labels->setVisible(visible); } -void ChartAxis::setShadesVisibility(bool visible) +void ChartAxis::handleShadesVisibleChanged(bool visible) { m_shades->setVisible(visible); } -void ChartAxis::setLabelsAngle(int angle) +void ChartAxis::handleLabelsAngleChanged(int angle) { foreach (QGraphicsItem *item, m_labels->childItems()) item->setRotation(angle); @@ -222,145 +296,69 @@ void ChartAxis::setLabelsAngle(int angle) m_labelsAngle = angle; } -void ChartAxis::setLabelsPen(const QPen &pen) +void ChartAxis::handleLabelsPenChanged(const QPen &pen) { foreach (QGraphicsItem *item , m_labels->childItems()) static_cast(item)->setPen(pen); } -void ChartAxis::setLabelsBrush(const QBrush &brush) +void ChartAxis::handleLabelsBrushChanged(const QBrush &brush) { foreach (QGraphicsItem *item , m_labels->childItems()) static_cast(item)->setBrush(brush); } -void ChartAxis::setLabelsFont(const QFont &font) +void ChartAxis::handleLabelsFontChanged(const QFont &font) { - if (m_font != font) { - m_font = font; - foreach (QGraphicsItem *item , m_labels->childItems()) - static_cast(item)->setFont(font); - QGraphicsLayoutItem::updateGeometry(); - presenter()->layout()->invalidate(); - } + foreach (QGraphicsItem *item , m_labels->childItems()) + static_cast(item)->setFont(font); + QGraphicsLayoutItem::updateGeometry(); + presenter()->layout()->invalidate(); } -void ChartAxis::setShadesBrush(const QBrush &brush) +void ChartAxis::handleShadesBrushChanged(const QBrush &brush) { foreach (QGraphicsItem *item , m_shades->childItems()) static_cast(item)->setBrush(brush); } -void ChartAxis::setShadesPen(const QPen &pen) +void ChartAxis::handleShadesPenChanged(const QPen &pen) { foreach (QGraphicsItem *item , m_shades->childItems()) static_cast(item)->setPen(pen); } -void ChartAxis::setArrowPen(const QPen &pen) +void ChartAxis::handleArrowPenChanged(const QPen &pen) { foreach (QGraphicsItem *item , m_arrow->childItems()) static_cast(item)->setPen(pen); } -void ChartAxis::setGridPen(const QPen &pen) +void ChartAxis::handleGridPenChanged(const QPen &pen) { foreach (QGraphicsItem *item , m_grid->childItems()) static_cast(item)->setPen(pen); } -void ChartAxis::setLabelPadding(int padding) -{ - m_labelPadding = padding; -} - -bool ChartAxis::isEmpty() -{ - return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(m_min, m_max); -} - -void ChartAxis::handleDomainUpdated() +void ChartAxis::handleTitleTextChanged(const QString &title) { - Domain *domain = qobject_cast(sender()); - qreal min(0); - qreal max(0); - - if (m_chartAxis->orientation() == Qt::Horizontal) { - min = domain->minX(); - max = domain->maxX(); - } else if (m_chartAxis->orientation() == Qt::Vertical) { - min = domain->minY(); - max = domain->maxY(); - } - - if (!qFuzzyCompare(m_min, min) || !qFuzzyCompare(m_max, max)) { - m_min = min; - m_max = max; - - if (!isEmpty()) { - - QVector layout = calculateLayout(); - updateLayout(layout); - QSizeF before = effectiveSizeHint(Qt::PreferredSize); - QSizeF after = sizeHint(Qt::PreferredSize); - - if (before != after) { - QGraphicsLayoutItem::updateGeometry(); - //we don't want to call invalidate on layout, since it will change minimum size of component, - //which we would like to avoid since it causes nasty flips when scrolling or zooming, - //instead recalculate layout and use plotArea for extra space. - presenter()->layout()->setGeometry(presenter()->layout()->geometry()); - } - } - } + Q_UNUSED(title) + QGraphicsLayoutItem::updateGeometry(); + presenter()->layout()->invalidate(); } -void ChartAxis::handleAxisUpdated() -{ - if (isEmpty()) - return; - - bool visible = m_chartAxis->isVisible(); - - //TODO: split this into separate signal/slots ? - setArrowVisibility(visible && m_chartAxis->isLineVisible()); - setGridVisibility(visible && m_chartAxis->isGridLineVisible()); - setLabelsVisibility(visible && m_chartAxis->labelsVisible()); - setShadesVisibility(visible && m_chartAxis->shadesVisible()); - setLabelsAngle(m_chartAxis->labelsAngle()); - setArrowPen(m_chartAxis->linePen()); - setLabelsPen(m_chartAxis->labelsPen()); - setLabelsBrush(m_chartAxis->labelsBrush()); - setLabelsFont(m_chartAxis->labelsFont()); - setGridPen(m_chartAxis->gridLinePen()); - setShadesPen(m_chartAxis->shadesPen()); - setShadesBrush(m_chartAxis->shadesBrush()); - setTitleText(m_chartAxis->title()); - setTitleFont(m_chartAxis->titleFont()); - setTitlePen(m_chartAxis->titlePen()); - setTitleBrush(m_chartAxis->titleBrush()); -} - -void ChartAxis::setTitleText(const QString &title) -{ - if (m_titleText != title) { - m_titleText = title; - QGraphicsLayoutItem::updateGeometry(); - presenter()->layout()->invalidate(); - } -} -void ChartAxis::setTitlePen(const QPen &pen) +void ChartAxis::handleTitlePenChanged(const QPen &pen) { m_title->setPen(pen); } -void ChartAxis::setTitleBrush(const QBrush &brush) +void ChartAxis::handleTitleBrushChanged(const QBrush &brush) { m_title->setBrush(brush); } -void ChartAxis::setTitleFont(const QFont &font) +void ChartAxis::handleTitleFontChanged(const QFont &font) { if(m_title->font() != font){ m_title->setFont(font); @@ -369,80 +367,59 @@ void ChartAxis::setTitleFont(const QFont &font) } } -QFont ChartAxis::titleFont() const -{ - return m_title->font(); -} - -void ChartAxis::hide() +void ChartAxis::handleVisibleChanged(bool visible) { - setArrowVisibility(false); - setGridVisibility(false); - setLabelsVisibility(false); - setShadesVisibility(false); + setVisible(visible); } -void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid) +void ChartAxis::handleRangeChanged(qreal min, qreal max) { - m_gridRect = grid; - m_axisRect = axis; - - if (isEmpty()) - return; - - QVector layout = calculateLayout(); - updateLayout(layout); + Q_UNUSED(min); + Q_UNUSED(max); -} + if (!isEmpty()) { -void ChartAxis::axisSelected() -{ - //TODO: axis clicked; -} + QVector layout = calculateLayout(); + updateLayout(layout); + QSizeF before = effectiveSizeHint(Qt::PreferredSize); + QSizeF after = sizeHint(Qt::PreferredSize); -Qt::Orientation ChartAxis::orientation() const -{ - return m_chartAxis->orientation(); -} + if (before != after) { + QGraphicsLayoutItem::updateGeometry(); + //we don't want to call invalidate on layout, since it will change minimum size of component, + //which we would like to avoid since it causes nasty flips when scrolling or zooming, + //instead recalculate layout and use plotArea for extra space. + presenter()->layout()->setGeometry(presenter()->layout()->geometry()); + } + } -Qt::Alignment ChartAxis::alignment() const -{ - return m_chartAxis->alignment(); -} - -bool ChartAxis::isVisible() -{ - return m_chartAxis->isVisible(); } -void ChartAxis::setLabels(const QStringList &labels) -{ - m_labelsList = labels; -} +//helpers -QStringList ChartAxis::createValueLabels(int ticks) const +QStringList ChartAxis::createValueLabels(qreal min, qreal max, int ticks,const QString& format) { - Q_ASSERT(m_max > m_min); - Q_ASSERT(ticks > 1); + //TODO: Q_ASSERT(m_max > m_min); + //TODO: Q_ASSERT(ticks > 1); QStringList labels; - int n = qMax(int(-qFloor(log10((m_max - m_min) / (ticks - 1)))), 0); - n++; - - QValueAxis *axis = qobject_cast(m_chartAxis); + if(max <= min || ticks < 1){ + return labels; + } - QString format = axis->labelFormat(); + int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0); + n++; if (format.isNull()) { for (int i = 0; i < ticks; i++) { - qreal value = m_min + (i * (m_max - m_min) / (ticks - 1)); + qreal value = min + (i * (max - min) / (ticks - 1)); labels << QString::number(value, 'f', n); } } else { QByteArray array = format.toLatin1(); for (int i = 0; i < ticks; i++) { - qreal value = m_min + (i * (m_max - m_min) / (ticks - 1)); + qreal value = min + (i * (max - min) / (ticks - 1)); if (format.contains("d") || format.contains("i") || format.contains("c")) @@ -461,27 +438,25 @@ QStringList ChartAxis::createValueLabels(int ticks) const return labels; } -QStringList ChartAxis::createDateTimeLabels(const QString &format, int ticks) const +QStringList ChartAxis::createDateTimeLabels(qreal min, qreal max,int ticks,const QString& format) { - Q_ASSERT(m_max > m_min); - Q_ASSERT(ticks > 1); + //TODO: Q_ASSERT(m_max > m_min); + //TODO: Q_ASSERT(ticks > 1); QStringList labels; - int n = qMax(int(-floor(log10((m_max - m_min) / (ticks - 1)))), 0); + + if(max <= min || ticks < 1) { + return labels; + } + + int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0); n++; for (int i = 0; i < ticks; i++) { - qreal value = m_min + (i * (m_max - m_min) / (ticks - 1)); + qreal value = min + (i * (max - min) / (ticks - 1)); labels << QDateTime::fromMSecsSinceEpoch(value).toString(format); } return labels; } -QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const -{ - Q_UNUSED(which); - Q_UNUSED(constraint); - return QSizeF(); -} - #include "moc_chartaxis_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/chartaxis_p.h b/src/axis/chartaxis_p.h index 9c855c0..cda88cb 100644 --- a/src/axis/chartaxis_p.h +++ b/src/axis/chartaxis_p.h @@ -48,48 +48,16 @@ class ChartAxis : public ChartElement, public QGraphicsLayoutItem Q_INTERFACES(QGraphicsLayoutItem) public: - ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis = false); + ChartAxis(QAbstractAxis *axis, QGraphicsItem* item = 0, bool intervalAxis = false); ~ChartAxis(); - QAbstractAxis* axis() const { return m_chartAxis; } + QAbstractAxis* axis() const { return m_axis; } - void setArrowOpacity(qreal opacity); - qreal arrowOpacity() const; - void setArrowVisibility(bool visible); - - void setGridOpacity(qreal opacity); - qreal gridOpacity() const; - void setGridVisibility(bool visible); - - void setLabelsOpacity(qreal opacity); - qreal labelsOpacity() const; - void setLabelsVisibility(bool visible); - - void setShadesOpacity(qreal opacity); - qreal shadesOpacity() const; - void setShadesVisibility(bool visible); - - void setLabelsAngle(int angle); - int labelsAngle()const { return m_labelsAngle; } - - void setShadesBrush(const QBrush &brush); - void setShadesPen(const QPen &pen); - - void setArrowPen(const QPen &pen); - void setGridPen(const QPen &pen); - - void setLabelsPen(const QPen &pen); - void setLabelsBrush(const QBrush &brush); - void setLabelsFont(const QFont &font); void setLabelPadding(int padding); int labelPadding() const { return m_labelPadding;}; - void setTitlePen(const QPen &pen); - void setTitleBrush(const QBrush &brush); - void setTitleFont(const QFont &font); QFont titleFont() const; - void setTitleText(const QString &title); - QString titleText() const {return m_titleText; }; + QString titleText() const; void setLayout(QVector &layout); QVector layout() const { return m_layoutVector; } @@ -100,9 +68,6 @@ public: Qt::Orientation orientation() const; Qt::Alignment alignment() const; - bool isVisible(); - void hide(); - void setGeometry(const QRectF &axis, const QRectF &grid); QRectF axisGeometry() const { return m_axisRect; } QRectF gridGeometry() const { return m_gridRect; } @@ -115,6 +80,20 @@ public: virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; + + QRectF boundingRect() const{ + return QRectF(); + } + + void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) + { + + } + +//helpers + static QStringList createValueLabels(qreal max, qreal min, int ticks, const QString &format); + static QStringList createDateTimeLabels(qreal max, qreal min, int ticks, const QString &format); + protected: void setGeometry(const QRectF &size) { Q_UNUSED(size);}; virtual void updateGeometry() = 0; @@ -126,15 +105,33 @@ protected: QList arrowItems() { return m_arrow->childItems();}; QGraphicsSimpleTextItem* titleItem() const { return m_title.data();} - QFont font() const { return m_font; } - qreal min() const {return m_min; } - qreal max() const {return m_max; } - QStringList createValueLabels(int ticks) const; - QStringList createDateTimeLabels(const QString &format, int ticks) const; + QFont font() const; + qreal min() const; + qreal max() const; +//handlers public Q_SLOTS: - virtual void handleAxisUpdated(); - virtual void handleDomainUpdated(); + void handleVisibleChanged(bool visible); + void handleArrowVisibleChanged(bool visible); + void handleGridVisibleChanged(bool visible); + void handleLabelsVisibleChanged(bool visible); + void handleShadesVisibleChanged(bool visible); + void handleLabelsAngleChanged(int angle); + void handleShadesBrushChanged(const QBrush &brush); + void handleShadesPenChanged(const QPen &pen); + void handleArrowPenChanged(const QPen &pen); + void handleGridPenChanged(const QPen &pen); + void handleLabelsPenChanged(const QPen &pen); + void handleLabelsBrushChanged(const QBrush &brush); + void handleLabelsFontChanged(const QFont &font); + void handleTitlePenChanged(const QPen &pen); + void handleTitleBrushChanged(const QBrush &brush); + void handleTitleFontChanged(const QFont &font); + void handleTitleTextChanged(const QString &title); + void handleRangeChanged(qreal min , qreal max); + +Q_SIGNALS: + void clicked(); private: inline bool isEmpty(); @@ -142,37 +139,34 @@ private: void deleteItems(int count); void updateLayout(QVector &layout); void axisSelected(); + void connectSlots(); private: - QAbstractAxis *m_chartAxis; + QAbstractAxis *m_axis; int m_labelsAngle; QRectF m_axisRect; QRectF m_gridRect; QScopedPointer m_grid; + QScopedPointer m_arrow; QScopedPointer m_shades; QScopedPointer m_labels; - QScopedPointer m_arrow; QScopedPointer m_title; QVector m_layoutVector; - qreal m_min; - qreal m_max; AxisAnimation *m_animation; - QFont m_font; - QString m_titleText; int m_labelPadding; QStringList m_labelsList; bool m_intervalAxis; friend class AxisAnimation; - friend class AxisItem; + friend class ArrowItem; }; -class AxisItem: public QGraphicsLineItem +class ArrowItem: public QGraphicsLineItem { public: - explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {} + explicit ArrowItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {} protected: void mousePressEvent(QGraphicsSceneMouseEvent *event) { diff --git a/src/axis/datetimeaxis/chartdatetimeaxisx.cpp b/src/axis/datetimeaxis/chartdatetimeaxisx.cpp index e03f44f..7c9a201 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisx.cpp +++ b/src/axis/datetimeaxis/chartdatetimeaxisx.cpp @@ -29,11 +29,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartDateTimeAxisX::ChartDateTimeAxisX(QDateTimeAxis *axis, ChartPresenter *presenter) - : HorizontalAxis(axis, presenter), - m_tickCount(0), +ChartDateTimeAxisX::ChartDateTimeAxisX(QDateTimeAxis *axis, QGraphicsItem* item) + : HorizontalAxis(axis, item), m_axis(axis) { + QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int))); } ChartDateTimeAxisX::~ChartDateTimeAxisX() @@ -42,13 +42,15 @@ ChartDateTimeAxisX::~ChartDateTimeAxisX() QVector ChartDateTimeAxisX::calculateLayout() const { - Q_ASSERT(m_tickCount >= 2); + int tickCount = m_axis->tickCount(); + + Q_ASSERT(tickCount >= 2); QVector points; - points.resize(m_tickCount); + points.resize(tickCount); const QRectF &gridRect = gridGeometry(); - const qreal deltaX = gridRect.width() / (m_tickCount - 1); - for (int i = 0; i < m_tickCount; ++i) { + const qreal deltaX = gridRect.width() / (tickCount - 1); + for (int i = 0; i < tickCount; ++i) { int x = i * deltaX + gridRect.left(); points[i] = x; } @@ -60,17 +62,14 @@ void ChartDateTimeAxisX::updateGeometry() const QVector& layout = ChartAxis::layout(); if (layout.isEmpty()) return; - setLabels(createDateTimeLabels(m_axis->format(), layout.size())); + setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format())); HorizontalAxis::updateGeometry(); } -void ChartDateTimeAxisX::handleAxisUpdated() +void ChartDateTimeAxisX::handleTickCountChanged(int tick) { - if (m_tickCount != m_axis->tickCount()) { - m_tickCount = m_axis->tickCount(); - presenter()->layout()->invalidate(); - } - ChartAxis::handleAxisUpdated(); + Q_UNUSED(tick) + if(presenter()) presenter()->layout()->invalidate(); } QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const @@ -81,10 +80,14 @@ QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint QSizeF sh; QSizeF base = HorizontalAxis::sizeHint(which, constraint); - QStringList ticksList = createDateTimeLabels(m_axis->format(), m_tickCount); + QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format()); qreal width = 0; qreal height = 0; + if(ticksList.empty()){ + return sh; + } + switch (which) { case Qt::MinimumSize:{ @@ -112,4 +115,6 @@ QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint return sh; } +#include "moc_chartdatetimeaxisx_p.cpp" + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisx_p.h b/src/axis/datetimeaxis/chartdatetimeaxisx_p.h index 90481af..17455c7 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisx_p.h +++ b/src/axis/datetimeaxis/chartdatetimeaxisx_p.h @@ -39,18 +39,19 @@ class ChartPresenter; class ChartDateTimeAxisX : public HorizontalAxis { + Q_OBJECT public: - ChartDateTimeAxisX(QDateTimeAxis *axis, ChartPresenter *presenter); + ChartDateTimeAxisX(QDateTimeAxis *axis, QGraphicsItem* item = 0); ~ChartDateTimeAxisX(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const; protected: - void handleAxisUpdated(); QVector calculateLayout() const; void updateGeometry(); +private Q_SLOTS: + void handleTickCountChanged(int tick); private: - int m_tickCount; QDateTimeAxis *m_axis; }; diff --git a/src/axis/datetimeaxis/chartdatetimeaxisy.cpp b/src/axis/datetimeaxis/chartdatetimeaxisy.cpp index 947eb4b..0ded06b 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisy.cpp +++ b/src/axis/datetimeaxis/chartdatetimeaxisy.cpp @@ -29,11 +29,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartDateTimeAxisY::ChartDateTimeAxisY(QDateTimeAxis *axis, ChartPresenter *presenter) - : VerticalAxis(axis, presenter), - m_tickCount(0), +ChartDateTimeAxisY::ChartDateTimeAxisY(QDateTimeAxis *axis, QGraphicsItem* item) + : VerticalAxis(axis, item), m_axis(axis) { + QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int))); } ChartDateTimeAxisY::~ChartDateTimeAxisY() @@ -42,13 +42,15 @@ ChartDateTimeAxisY::~ChartDateTimeAxisY() QVector ChartDateTimeAxisY::calculateLayout() const { - Q_ASSERT(m_tickCount >= 2); + int tickCount = m_axis->tickCount(); + + Q_ASSERT(tickCount >= 2); QVector points; - points.resize(m_tickCount); + points.resize(tickCount); const QRectF &gridRect = gridGeometry(); - const qreal deltaY = gridRect.height() / (m_tickCount - 1); - for (int i = 0; i < m_tickCount; ++i) { + const qreal deltaY = gridRect.height() / (tickCount - 1); + for (int i = 0; i < tickCount; ++i) { int y = i * -deltaY + gridRect.bottom(); points[i] = y; } @@ -61,17 +63,14 @@ void ChartDateTimeAxisY::updateGeometry() const QVector &layout = ChartAxis::layout(); if (layout.isEmpty()) return; - setLabels(createDateTimeLabels(m_axis->format(), layout.size())); + setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format())); VerticalAxis::updateGeometry(); } -void ChartDateTimeAxisY::handleAxisUpdated() +void ChartDateTimeAxisY::handleTickCountChanged(int tick) { - if (m_tickCount != m_axis->tickCount()) { - m_tickCount = m_axis->tickCount(); - presenter()->layout()->invalidate(); - } - ChartAxis::handleAxisUpdated(); + Q_UNUSED(tick) + if(presenter()) presenter()->layout()->invalidate(); } QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const @@ -82,10 +81,14 @@ QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint QSizeF sh; QSizeF base = VerticalAxis::sizeHint(which, constraint); - QStringList ticksList = createDateTimeLabels(m_axis->format(),m_tickCount); + QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format()); qreal width = 0; qreal height = 0; + if(ticksList.empty()){ + return sh; + } + switch (which) { case Qt::MinimumSize: { width = fn.boundingRect("...").width() + labelPadding(); @@ -111,4 +114,6 @@ QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint return sh; } +#include "moc_chartdatetimeaxisy_p.cpp" + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/datetimeaxis/chartdatetimeaxisy_p.h b/src/axis/datetimeaxis/chartdatetimeaxisy_p.h index b565311..0cf5705 100644 --- a/src/axis/datetimeaxis/chartdatetimeaxisy_p.h +++ b/src/axis/datetimeaxis/chartdatetimeaxisy_p.h @@ -39,17 +39,18 @@ class ChartPresenter; class ChartDateTimeAxisY : public VerticalAxis { + Q_OBJECT public: - ChartDateTimeAxisY(QDateTimeAxis *axis, ChartPresenter *presenter); + ChartDateTimeAxisY(QDateTimeAxis *axis, QGraphicsItem* item = 0); ~ChartDateTimeAxisY(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const; protected: QVector calculateLayout() const; void updateGeometry(); - void handleAxisUpdated(); +private Q_SLOTS: + void handleTickCountChanged(int tick); private: - int m_tickCount; QDateTimeAxis *m_axis; }; diff --git a/src/axis/datetimeaxis/qdatetimeaxis.cpp b/src/axis/datetimeaxis/qdatetimeaxis.cpp index 67f8282..d7af7cf 100644 --- a/src/axis/datetimeaxis/qdatetimeaxis.cpp +++ b/src/axis/datetimeaxis/qdatetimeaxis.cpp @@ -23,6 +23,7 @@ #include "chartdatetimeaxisx_p.h" #include "chartdatetimeaxisy_p.h" #include "domain_p.h" +#include "qchart.h" #include #include @@ -177,33 +178,35 @@ QDateTimeAxis::QDateTimeAxis(QDateTimeAxisPrivate &d, QObject *parent) : QAbstra */ QDateTimeAxis::~QDateTimeAxis() { - + Q_D(QDateTimeAxis); + if (d->m_chart) + d->m_chart->removeAxis(this); } void QDateTimeAxis::setMin(QDateTime min) { Q_D(QDateTimeAxis); if (min.isValid()) - setRange(min, qMax(d->m_max, min)); + d->setRange(min.toMSecsSinceEpoch(), qMax(d->m_max, qreal(min.toMSecsSinceEpoch()))); } QDateTime QDateTimeAxis::min() const { Q_D(const QDateTimeAxis); - return d->m_min; + return QDateTime::fromMSecsSinceEpoch(d->m_min); } void QDateTimeAxis::setMax(QDateTime max) { Q_D(QDateTimeAxis); if (max.isValid()) - setRange(qMin(d->m_min, max), max); + d->setRange(qMin(d->m_min, qreal(max.toMSecsSinceEpoch())), max.toMSecsSinceEpoch()); } QDateTime QDateTimeAxis::max() const { Q_D(const QDateTimeAxis); - return d->m_max; + return QDateTime::fromMSecsSinceEpoch(d->m_max); } /*! @@ -216,23 +219,7 @@ void QDateTimeAxis::setRange(QDateTime min, QDateTime max) if (!min.isValid() || !max.isValid() || min > max) return; - bool changed = false; - if (d->m_min != min) { - d->m_min = min; - changed = true; - emit minChanged(min); - } - - if (d->m_max != max) { - d->m_max = max; - changed = true; - emit maxChanged(max); - } - - if (changed) { - emit rangeChanged(d->m_min, d->m_max); - d->emitUpdated(); - } + d->setRange(min.toMSecsSinceEpoch(),max.toMSecsSinceEpoch()); } void QDateTimeAxis::setFormat(QString format) @@ -258,7 +245,7 @@ void QDateTimeAxis::setTickCount(int count) Q_D(QDateTimeAxis); if (d->m_tickCount != count && count >= 2) { d->m_tickCount = count; - d->emitUpdated(); + emit tickCountChanged(count); } } @@ -284,8 +271,8 @@ QAbstractAxis::AxisType QDateTimeAxis::type() const QDateTimeAxisPrivate::QDateTimeAxisPrivate(QDateTimeAxis *q) : QAbstractAxisPrivate(q), - m_min(QDateTime::fromMSecsSinceEpoch(0)), - m_max(QDateTime::fromMSecsSinceEpoch(0)), + m_min(0), + m_max(0), m_tickCount(5) { m_format = "dd-MM-yyyy\nh:mm"; @@ -296,16 +283,28 @@ QDateTimeAxisPrivate::~QDateTimeAxisPrivate() } -void QDateTimeAxisPrivate::handleDomainUpdated() +void QDateTimeAxisPrivate::setRange(qreal min,qreal max) { Q_Q(QDateTimeAxis); - Domain *domain = qobject_cast(sender()); - Q_ASSERT(domain); - if (orientation() == Qt::Horizontal) - q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX())); - else if (orientation() == Qt::Vertical) - q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY())); + bool changed = false; + + if (m_min != min) { + m_min = min; + changed = true; + emit q->minChanged(QDateTime::fromMSecsSinceEpoch(min)); + } + + if (m_max != max) { + m_max = max; + changed = true; + emit q->maxChanged(QDateTime::fromMSecsSinceEpoch(max)); + } + + if (changed) { + emit q->rangeChanged(QDateTime::fromMSecsSinceEpoch(min), QDateTime::fromMSecsSinceEpoch(max)); + emit rangeChanged(m_min,m_max); + } } @@ -331,27 +330,31 @@ void QDateTimeAxisPrivate::setRange(const QVariant &min, const QVariant &max) q->setRange(min.toDateTime(), max.toDateTime()); } -ChartAxis *QDateTimeAxisPrivate::createGraphics(ChartPresenter *presenter) +void QDateTimeAxisPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QDateTimeAxis); - if (m_orientation == Qt::Vertical) - return new ChartDateTimeAxisY(q, presenter); - return new ChartDateTimeAxisX(q, presenter); + ChartAxis* axis(0); + if (orientation() == Qt::Vertical) + axis = new ChartDateTimeAxisY(q,parent); + if (orientation() == Qt::Horizontal) + axis = new ChartDateTimeAxisX(q,parent); + + m_item.reset(axis); + QAbstractAxisPrivate::initializeGraphics(parent); } -void QDateTimeAxisPrivate::intializeDomain(Domain *domain) +void QDateTimeAxisPrivate::initializeDomain(Domain *domain) { - Q_Q(QDateTimeAxis); if (m_max == m_min) { - if (m_orientation == Qt::Vertical) - q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minY()), QDateTime::fromMSecsSinceEpoch(domain->maxY())); + if (orientation() == Qt::Vertical) + setRange(domain->minY(), domain->maxY()); else - q->setRange(QDateTime::fromMSecsSinceEpoch(domain->minX()), QDateTime::fromMSecsSinceEpoch(domain->maxX())); + setRange(domain->minX(), domain->maxX()); } else { - if (m_orientation == Qt::Vertical) - domain->setRangeY(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch()); + if (orientation() == Qt::Vertical) + domain->setRangeY(m_min, m_max); else - domain->setRangeX(m_min.toMSecsSinceEpoch(), m_max.toMSecsSinceEpoch()); + domain->setRangeX(m_min, m_max); } } diff --git a/src/axis/datetimeaxis/qdatetimeaxis.h b/src/axis/datetimeaxis/qdatetimeaxis.h index 3124950..05db130 100644 --- a/src/axis/datetimeaxis/qdatetimeaxis.h +++ b/src/axis/datetimeaxis/qdatetimeaxis.h @@ -32,7 +32,7 @@ class QDateTimeAxisPrivate; class QTCOMMERCIALCHART_EXPORT QDateTimeAxis : public QAbstractAxis { Q_OBJECT - Q_PROPERTY(int tickCount READ tickCount WRITE setTickCount) + Q_PROPERTY(int tickCount READ tickCount WRITE setTickCount NOTIFY tickCountChanged) Q_PROPERTY(QDateTime min READ min WRITE setMin NOTIFY minChanged) Q_PROPERTY(QDateTime max READ max WRITE setMax NOTIFY maxChanged) Q_PROPERTY(QString format READ format WRITE setFormat NOTIFY formatChanged) @@ -66,6 +66,7 @@ Q_SIGNALS: void maxChanged(QDateTime max); void rangeChanged(QDateTime min, QDateTime max); void formatChanged(QString format); + void tickCountChanged(int tick); private: Q_DECLARE_PRIVATE(QDateTimeAxis) diff --git a/src/axis/datetimeaxis/qdatetimeaxis_p.h b/src/axis/datetimeaxis/qdatetimeaxis_p.h index 22a5504..9605b9b 100644 --- a/src/axis/datetimeaxis/qdatetimeaxis_p.h +++ b/src/axis/datetimeaxis/qdatetimeaxis_p.h @@ -44,22 +44,25 @@ public: ~QDateTimeAxisPrivate(); public: - ChartAxis *createGraphics(ChartPresenter *presenter); - void intializeDomain(Domain *domain); - void handleDomainUpdated(); - qreal min() { return m_min.toMSecsSinceEpoch(); } - qreal max() { return m_max.toMSecsSinceEpoch(); } - int count() const { /*TODO:*/ return 0;} + void initializeGraphics(QGraphicsItem* parent); + void initializeDomain(Domain *domain); -protected: + //interface for manipulating range form base class void setMin(const QVariant &min); void setMax(const QVariant &max); void setRange(const QVariant &min, const QVariant &max); + + //interface manipulating range form domain + qreal min() { return m_min; } + qreal max() { return m_max; } + void setRange(qreal min,qreal max); + +protected: int tickCount() const; protected: - QDateTime m_min; - QDateTime m_max; + qreal m_min; + qreal m_max; int m_tickCount; QString m_format; Q_DECLARE_PUBLIC(QDateTimeAxis) diff --git a/src/axis/horizontalaxis.cpp b/src/axis/horizontalaxis.cpp index 811f895..302f11b 100644 --- a/src/axis/horizontalaxis.cpp +++ b/src/axis/horizontalaxis.cpp @@ -26,8 +26,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis) - : ChartAxis(axis, presenter, intervalAxis) +HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis) + : ChartAxis(axis, item, intervalAxis) { } @@ -198,7 +198,7 @@ QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) co break; case Qt::MaximumSize: case Qt::PreferredSize: - sh = QSizeF(fn.boundingRect(axis()->title()).width(), fn.height()); + sh = QSizeF(fn.boundingRect(axis()->titleText()).width(), fn.height()); break; default: break; diff --git a/src/axis/horizontalaxis_p.h b/src/axis/horizontalaxis_p.h index 8ce0859..284800f 100644 --- a/src/axis/horizontalaxis_p.h +++ b/src/axis/horizontalaxis_p.h @@ -37,7 +37,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class HorizontalAxis : public ChartAxis { public: - HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis = false); + HorizontalAxis(QAbstractAxis *axis, QGraphicsItem* item = 0, bool intervalAxis = false); ~HorizontalAxis(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; protected: diff --git a/src/axis/qabstractaxis.cpp b/src/axis/qabstractaxis.cpp index 5833e90..b126e42 100644 --- a/src/axis/qabstractaxis.cpp +++ b/src/axis/qabstractaxis.cpp @@ -20,6 +20,8 @@ #include "qabstractaxis.h" #include "qabstractaxis_p.h" +#include "chartdataset_p.h" +#include "charttheme_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -308,7 +310,7 @@ QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent) QAbstractAxis::~QAbstractAxis() { - if (d_ptr->m_dataset) + if (d_ptr->m_chart) qFatal("Still binded axis detected !"); } @@ -319,7 +321,7 @@ void QAbstractAxis::setLinePen(const QPen &pen) { if (d_ptr->m_axisPen != pen) { d_ptr->m_axisPen = pen; - d_ptr->emitUpdated(); + emit linePenChanged(pen); } } @@ -331,6 +333,7 @@ QPen QAbstractAxis::linePen() const return d_ptr->m_axisPen; } +//TODO: remove me void QAbstractAxis::setLinePenColor(QColor color) { QPen p = d_ptr->m_axisPen; @@ -353,7 +356,6 @@ void QAbstractAxis::setLineVisible(bool visible) { if (d_ptr->m_arrowVisible != visible) { d_ptr->m_arrowVisible = visible; - d_ptr->emitUpdated(); emit lineVisibleChanged(visible); } } @@ -367,8 +369,7 @@ void QAbstractAxis::setGridLineVisible(bool visible) { if (d_ptr->m_gridLineVisible != visible) { d_ptr->m_gridLineVisible = visible; - d_ptr->emitUpdated(); - emit gridVisibleChanged(visible); + emit gridLineVisibleChanged(visible); } } @@ -384,7 +385,7 @@ void QAbstractAxis::setGridLinePen(const QPen &pen) { if (d_ptr->m_gridLinePen != pen) { d_ptr->m_gridLinePen = pen; - d_ptr->emitUpdated(); + emit gridLinePenChanged(pen); } } @@ -400,7 +401,6 @@ void QAbstractAxis::setLabelsVisible(bool visible) { if (d_ptr->m_labelsVisible != visible) { d_ptr->m_labelsVisible = visible; - d_ptr->emitUpdated(); emit labelsVisibleChanged(visible); } } @@ -417,7 +417,7 @@ void QAbstractAxis::setLabelsPen(const QPen &pen) { if (d_ptr->m_labelsPen != pen) { d_ptr->m_labelsPen = pen; - d_ptr->emitUpdated(); + emit labelsPenChanged(pen); } } @@ -436,7 +436,7 @@ void QAbstractAxis::setLabelsBrush(const QBrush &brush) { if (d_ptr->m_labelsBrush != brush) { d_ptr->m_labelsBrush = brush; - d_ptr->emitUpdated(); + emit labelsBrushChanged(brush); } } @@ -455,7 +455,7 @@ void QAbstractAxis::setLabelsFont(const QFont &font) { if (d_ptr->m_labelsFont != font) { d_ptr->m_labelsFont = font; - d_ptr->emitUpdated(); + emit labelsFontChanged(font); } } @@ -471,7 +471,7 @@ void QAbstractAxis::setLabelsAngle(int angle) { if (d_ptr->m_labelsAngle != angle) { d_ptr->m_labelsAngle = angle; - d_ptr->emitUpdated(); + emit labelsAngleChanged(angle); } } @@ -479,7 +479,7 @@ int QAbstractAxis::labelsAngle() const { return d_ptr->m_labelsAngle; } - +//TODO: remove me void QAbstractAxis::setLabelsColor(QColor color) { QBrush b = d_ptr->m_labelsBrush; @@ -499,7 +499,7 @@ void QAbstractAxis::setTitleVisible(bool visible) { if (d_ptr->m_titleVisible != visible) { d_ptr->m_titleVisible = visible; - d_ptr->emitUpdated(); + emit labelsVisibleChanged(visible); } } @@ -515,7 +515,7 @@ void QAbstractAxis::setTitlePen(const QPen &pen) { if (d_ptr->m_titlePen != pen) { d_ptr->m_titlePen = pen; - d_ptr->emitUpdated(); + emit titlePenChanged(pen); } } @@ -534,7 +534,7 @@ void QAbstractAxis::setTitleBrush(const QBrush &brush) { if (d_ptr->m_titleBrush != brush) { d_ptr->m_titleBrush = brush; - d_ptr->emitUpdated(); + emit titleBrushChanged(brush); } } @@ -553,7 +553,7 @@ void QAbstractAxis::setTitleFont(const QFont &font) { if (d_ptr->m_titleFont != font) { d_ptr->m_titleFont = font; - d_ptr->emitUpdated(); + emit titleFontChanged(font); } } @@ -565,15 +565,15 @@ QFont QAbstractAxis::titleFont() const return d_ptr->m_titleFont; } -void QAbstractAxis::setTitle(const QString &title) +void QAbstractAxis::setTitleText(const QString &title) { if (d_ptr->m_title != title) { d_ptr->m_title = title; - d_ptr->emitUpdated(); + emit titleTextChanged(title); } } -QString QAbstractAxis::title() const +QString QAbstractAxis::titleText() const { return d_ptr->m_title; } @@ -583,7 +583,6 @@ void QAbstractAxis::setShadesVisible(bool visible) { if (d_ptr->m_shadesVisible != visible) { d_ptr->m_shadesVisible = visible; - d_ptr->emitUpdated(); emit shadesVisibleChanged(visible); } } @@ -600,7 +599,7 @@ void QAbstractAxis::setShadesPen(const QPen &pen) { if (d_ptr->m_shadesPen != pen) { d_ptr->m_shadesPen = pen; - d_ptr->emitUpdated(); + emit shadesPenChanged(pen); } } @@ -619,8 +618,7 @@ void QAbstractAxis::setShadesBrush(const QBrush &brush) { if (d_ptr->m_shadesBrush != brush) { d_ptr->m_shadesBrush = brush; - d_ptr->emitUpdated(); - emit shadesColorChanged(brush.color()); + emit shadesBrushChanged(brush); } } @@ -635,8 +633,11 @@ QBrush QAbstractAxis::shadesBrush() const void QAbstractAxis::setShadesColor(QColor color) { QBrush b = d_ptr->m_shadesBrush; - b.setColor(color); - setShadesBrush(b); + if (b.color() != color) { + b.setColor(color); + setShadesBrush(b); + emit shadesColorChanged(color); + } } QColor QAbstractAxis::shadesColor() const @@ -646,9 +647,12 @@ QColor QAbstractAxis::shadesColor() const void QAbstractAxis::setShadesBorderColor(QColor color) { - QPen p = d_ptr->m_shadesPen; - p.setColor(color); - setShadesPen(p); + QPen p = d_ptr->m_shadesPen; + if (p.color() != color) { + p.setColor(color); + setShadesPen(p); + emit shadesColorChanged(color); + } } QColor QAbstractAxis::shadesBorderColor() const @@ -669,7 +673,6 @@ void QAbstractAxis::setVisible(bool visible) { if (d_ptr->m_visible != visible) { d_ptr->m_visible = visible; - d_ptr->emitUpdated(); emit visibleChanged(visible); } } @@ -729,26 +732,21 @@ void QAbstractAxis::setRange(const QVariant &min, const QVariant &max) // http://techbase.kde.org/Policies/Binary_Compatibility_Examples#Change_the_CV-qualifiers_of_a_member_function Qt::Orientation QAbstractAxis::orientation() { - return d_ptr->m_orientation; + return d_ptr->orientation(); } Qt::Alignment QAbstractAxis::alignment() const { - return d_ptr->m_alignment; -} - -void QAbstractAxis::setAlignment(Qt::Alignment alignment) -{ - d_ptr->m_alignment = alignment; + return d_ptr->alignment(); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis *q) : q_ptr(q), + m_chart(0), m_orientation(Qt::Orientation(0)), m_alignment(0), - m_dataset(0), m_visible(true), m_arrowVisible(true), m_gridLineVisible(true), @@ -764,29 +762,103 @@ QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis *q) QAbstractAxisPrivate::~QAbstractAxisPrivate() { +} +void QAbstractAxisPrivate::setAlignment( Qt::Alignment alignment) +{ + switch(alignment) { + case Qt::AlignTop: + case Qt::AlignBottom: + m_orientation = Qt::Horizontal; + break; + case Qt::AlignLeft: + case Qt::AlignRight: + m_orientation = Qt::Vertical; + break; + default: + qWarning()<<"No alignment specified !"; + break; + }; + m_alignment=alignment; } -void QAbstractAxisPrivate::emitUpdated() +void QAbstractAxisPrivate::initializeTheme(ChartTheme* theme, bool forced) { - if (!m_dirty) { - m_dirty = true; - emit updated(); + QPen pen; + QBrush brush; + QFont font; + + bool axisX = m_orientation == Qt::Horizontal; + + if (m_arrowVisible) { + + if (forced || brush == m_labelsBrush){ + q_ptr->setLabelsBrush(theme->labelBrush()); + } + //TODO: introduce axis brush + if (forced || brush == m_titleBrush){ + q_ptr->setTitleBrush(theme->labelBrush()); + } + if (forced || pen == m_labelsPen){ + q_ptr->setLabelsPen(Qt::NoPen);// NoPen for performance reasons + } + if (forced || pen == m_titlePen){ + q_ptr->setTitlePen(Qt::NoPen);// Noen for performance reasons + } + if (forced || m_shadesVisible) { + + if (forced || brush == m_shadesBrush){ + q_ptr->setShadesBrush(theme->backgroundShadesBrush()); + } + if (forced || pen == m_shadesPen){ + q_ptr->setShadesPen(theme->backgroundShadesPen()); + } + if (forced && (theme->backgroundShades() == ChartTheme::BackgroundShadesBoth + || (theme->backgroundShades() == ChartTheme::BackgroundShadesVertical && axisX) + || (theme->backgroundShades() == ChartTheme::BackgroundShadesHorizontal && !axisX))) { + q_ptr->setShadesVisible(true); + } + } + + if (forced || pen == m_axisPen) { + q_ptr->setLinePen(theme->axisLinePen()); + } + + if (forced || pen == m_gridLinePen) { + q_ptr->setGridLinePen(theme->girdLinePen()); + } + + if (forced || font == m_labelsFont){ + q_ptr->setLabelsFont(theme->labelFont()); + } + //TODO: discuss with Tero + if (forced || font == m_titleFont){ + QFont font(m_labelsFont); + font.setBold(true); + q_ptr->setTitleFont(font); + } } } -void QAbstractAxisPrivate::setDirty(bool dirty) +void QAbstractAxisPrivate::handleRangeChanged(qreal min, qreal max) +{ + setRange(min,max); +} + +void QAbstractAxisPrivate::initializeGraphics(QGraphicsItem* parent) { - m_dirty = dirty; + Q_UNUSED(parent); } -void QAbstractAxisPrivate::setOrientation(Qt::Orientation orientation) +void QAbstractAxisPrivate::initializeAnimations(QChart::AnimationOptions options) { - m_orientation = orientation; - if (m_orientation == Qt::Horizontal && !m_alignment) - m_alignment = Qt::AlignBottom; - else if (m_orientation == Qt::Vertical && !m_alignment) - m_alignment = Qt::AlignLeft; + ChartAxis* axis = m_item.data(); + Q_ASSERT(axis); + if(options.testFlag(QChart::GridAxisAnimations)) { + axis->setAnimation(new AxisAnimation(axis)); + }else{ + axis->setAnimation(0); + } } diff --git a/src/axis/qabstractaxis.h b/src/axis/qabstractaxis.h index 04ea81e..8127407 100644 --- a/src/axis/qabstractaxis.h +++ b/src/axis/qabstractaxis.h @@ -33,21 +33,39 @@ class QAbstractAxisPrivate; class QTCOMMERCIALCHART_EXPORT QAbstractAxis : public QObject { Q_OBJECT + //visibility Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged) + //arrow Q_PROPERTY(bool lineVisible READ isLineVisible WRITE setLineVisible NOTIFY lineVisibleChanged) + Q_PROPERTY(QPen linePen READ linePen WRITE setLinePen NOTIFY linePenChanged) + //TODO: make wrapping of color for qml Q_PROPERTY(QColor color READ linePenColor WRITE setLinePenColor NOTIFY colorChanged) + //labels Q_PROPERTY(bool labelsVisible READ labelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged) - Q_PROPERTY(int labelsAngle READ labelsAngle WRITE setLabelsAngle) - Q_PROPERTY(QFont labelsFont READ labelsFont WRITE setLabelsFont) + Q_PROPERTY(QPen lablesPen READ labelsPen WRITE setLabelsPen NOTIFY labelsPenChanged) + Q_PROPERTY(QBrush lablesBrush READ labelsBrush WRITE setLabelsBrush NOTIFY labelsBrushChanged) + //TODO: fix labels angles to work with layout + Q_PROPERTY(int labelsAngle READ labelsAngle WRITE setLabelsAngle NOTIFY labelsAngleChanged) + Q_PROPERTY(QFont labelsFont READ labelsFont WRITE setLabelsFont NOTIFY labelsFontChanged) + //TODO: make wrapping of color for qml Q_PROPERTY(QColor labelsColor READ labelsColor WRITE setLabelsColor NOTIFY labelsColorChanged) - Q_PROPERTY(bool gridVisible READ isGridLineVisible WRITE setGridLineVisible NOTIFY gridVisibleChanged) + //grid + Q_PROPERTY(bool gridVisible READ isGridLineVisible WRITE setGridLineVisible NOTIFY gridLineVisibleChanged) + Q_PROPERTY(QPen girdLinePen READ gridLinePen WRITE setGridLinePen NOTIFY gridLinePenChanged) + //shades Q_PROPERTY(bool shadesVisible READ shadesVisible WRITE setShadesVisible NOTIFY shadesVisibleChanged) + //TODO: make wrapping of color for qml Q_PROPERTY(QColor shadesColor READ shadesColor WRITE setShadesColor NOTIFY shadesColorChanged) + //TODO: make wrapping of border for qml Q_PROPERTY(QColor shadesBorderColor READ shadesBorderColor WRITE setShadesBorderColor NOTIFY shadesBorderColorChanged) + Q_PROPERTY(QPen shadesPen READ shadesPen WRITE setShadesPen NOTIFY shadesPenChanged) + Q_PROPERTY(QBrush shadesBrush READ shadesBrush WRITE setShadesBrush NOTIFY shadesBrushChanged) + //title + Q_PROPERTY(QString titleText READ titleText WRITE setTitleText NOTIFY titleTextChanged) + Q_PROPERTY(QPen titlePen READ titlePen WRITE setTitlePen NOTIFY titlePenChanged) + Q_PROPERTY(QBrush titleBrush READ titleBrush WRITE setTitleBrush NOTIFY titleBrushChanged) Q_PROPERTY(bool titleVisible READ titleVisible WRITE setTitleVisible) Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont) - Q_PROPERTY(QString title READ title WRITE setTitle) - Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment) public: @@ -69,12 +87,13 @@ public: virtual AxisType type() const = 0; - //visibilty handling + //visibility handling bool isVisible() const; void setVisible(bool visible = true); + void show(); + void hide(); - - //axis handling + //arrow handling bool isLineVisible() const; void setLineVisible(bool visible = true); void setLinePen(const QPen &pen); @@ -111,9 +130,8 @@ public: QBrush titleBrush() const; void setTitleFont(const QFont &font); QFont titleFont() const; - void setTitle(const QString &title); - QString title() const; - + void setTitleText(const QString &title); + QString titleText() const; //shades handling bool shadesVisible() const; @@ -127,28 +145,37 @@ public: void setShadesBorderColor(QColor color); QColor shadesBorderColor() const; - Qt::Orientation orientation(); + Qt::Orientation orientation(); //TODO: missing const <- BC Qt::Alignment alignment() const; - void setAlignment(Qt::Alignment alignment); //range handling void setMin(const QVariant &min); void setMax(const QVariant &max); void setRange(const QVariant &min, const QVariant &max); - void show(); - void hide(); - Q_SIGNALS: void visibleChanged(bool visible); + void linePenChanged(const QPen& pen); void lineVisibleChanged(bool visible); void labelsVisibleChanged(bool visible); - void gridVisibleChanged(bool visible); + void labelsPenChanged(const QPen& pen); + void labelsBrushChanged(const QBrush& brush); + void labelsFontChanged(const QFont& pen); + void labelsAngleChanged(int angle); + void gridLinePenChanged(const QPen& pen); + void gridLineVisibleChanged(bool visible); void colorChanged(QColor color); void labelsColorChanged(QColor color); + void titleTextChanged(const QString& title); + void titlePenChanged(const QPen& pen); + void titleBrushChanged(const QBrush brush); + void titleVisibleChanged(bool visible); + void titleFontChanged(const QFont& font); void shadesVisibleChanged(bool visible); void shadesColorChanged(QColor color); void shadesBorderColorChanged(QColor color); + void shadesPenChanged(const QPen& pen); + void shadesBrushChanged(const QBrush brush); protected: QScopedPointer d_ptr; @@ -156,6 +183,7 @@ protected: friend class ChartDataSet; friend class ChartAxis; friend class ChartPresenter; + friend class ChartThemeManager; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/qabstractaxis_p.h b/src/axis/qabstractaxis_p.h index 06845de..4bd20c3 100644 --- a/src/axis/qabstractaxis_p.h +++ b/src/axis/qabstractaxis_p.h @@ -31,13 +31,20 @@ #define QABSTRACTAXIS_P_H #include "qabstractaxis.h" +#include "chartaxis_p.h" +#include "qchart.h" +#include + +class QGraphicsItem; QTCOMMERCIALCHART_BEGIN_NAMESPACE class ChartPresenter; -class ChartAxis; class Domain; -class ChartDataSet; +class QChart; +class QAbstractSeries; +class ChartTheme; +class ChartElement; class QTCOMMERCIALCHART_AUTOTEST_EXPORT QAbstractAxisPrivate : public QObject { @@ -47,38 +54,43 @@ public: ~QAbstractAxisPrivate(); public: - virtual ChartAxis *createGraphics(ChartPresenter *presenter) = 0; - virtual void intializeDomain(Domain *domain) = 0; - - void emitUpdated(); - void setDirty(bool dirty); - bool isDirty() { return m_dirty; } - void setOrientation(Qt::Orientation orientation); + Qt::Alignment alignment() const { return m_alignment; } Qt::Orientation orientation() const { return m_orientation; } + void setAlignment( Qt::Alignment alignment); - virtual void setMin(const QVariant &min) = 0; - virtual qreal min() = 0; + virtual void initializeDomain(Domain *domain) = 0; + virtual void initializeGraphics(QGraphicsItem* parent) = 0; + virtual void initializeTheme(ChartTheme* theme, bool forced = false); + virtual void initializeAnimations(QChart::AnimationOptions options); + //interface for manipulating range form base class + virtual void setMin(const QVariant &min) = 0; virtual void setMax(const QVariant &max) = 0; - virtual qreal max() = 0; + virtual void setRange(const QVariant &min, const QVariant &max) = 0; - virtual int count() const = 0; + //interface manipulating range form domain + virtual void setRange(qreal min, qreal max) = 0; + virtual qreal min() = 0; + virtual qreal max() = 0; - virtual void setRange(const QVariant &min, const QVariant &max) = 0; + ChartAxis* axisItem() { return m_item.data(); } public Q_SLOTS: - virtual void handleDomainUpdated() = 0; + void handleRangeChanged(qreal min, qreal max); Q_SIGNALS: - void updated(); + void rangeChanged(qreal min, qreal max); protected: QAbstractAxis *q_ptr; - Qt::Orientation m_orientation; + QChart *m_chart; + QScopedPointer m_item; +private: + QList m_series; + Qt::Alignment m_alignment; - ChartDataSet *m_dataset; + Qt::Orientation m_orientation; -private: bool m_visible; bool m_arrowVisible; @@ -109,6 +121,7 @@ private: friend class QAbstractAxis; friend class ChartDataSet; + friend class ChartPresenter; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/valueaxis/chartvalueaxisx.cpp b/src/axis/valueaxis/chartvalueaxisx.cpp index 8cffb15..ce9113b 100644 --- a/src/axis/valueaxis/chartvalueaxisx.cpp +++ b/src/axis/valueaxis/chartvalueaxisx.cpp @@ -26,14 +26,16 @@ #include #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartValueAxisX::ChartValueAxisX(QValueAxis *axis, ChartPresenter *presenter) - : HorizontalAxis(axis, presenter), - m_tickCount(0), m_axis(axis) +ChartValueAxisX::ChartValueAxisX(QValueAxis *axis, QGraphicsItem* item ) + : HorizontalAxis(axis, item), + m_axis(axis) { + QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int))); } ChartValueAxisX::~ChartValueAxisX() @@ -42,14 +44,16 @@ ChartValueAxisX::~ChartValueAxisX() QVector ChartValueAxisX::calculateLayout() const { - Q_ASSERT(m_tickCount >= 2); + int tickCount = m_axis->tickCount(); + + Q_ASSERT(tickCount >= 2); QVector points; - points.resize(m_tickCount); + points.resize(tickCount); const QRectF &gridRect = gridGeometry(); - const qreal deltaX = gridRect.width() / (m_tickCount - 1); - for (int i = 0; i < m_tickCount; ++i) { + const qreal deltaX = gridRect.width() / (tickCount - 1); + for (int i = 0; i < tickCount; ++i) { points[i] = i * deltaX + gridRect.left(); } return points; @@ -60,18 +64,16 @@ void ChartValueAxisX::updateGeometry() const QVector& layout = ChartAxis::layout(); if (layout.isEmpty()) return; - setLabels(createValueLabels(layout.size())); + setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat())); HorizontalAxis::updateGeometry(); } -void ChartValueAxisX::handleAxisUpdated() +void ChartValueAxisX::handleTickCountChanged(int tick) { - if (m_tickCount != m_axis->tickCount()) { - m_tickCount = m_axis->tickCount(); - presenter()->layout()->invalidate(); - } - - ChartAxis::handleAxisUpdated(); + Q_UNUSED(tick); + if(presenter()) presenter()->layout()->invalidate(); + //QVector layout = calculateLayout(); + //updateLayout(layout); } QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const @@ -82,13 +84,18 @@ QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c QSizeF sh; QSizeF base = HorizontalAxis::sizeHint(which, constraint); - QStringList ticksList = createValueLabels(m_tickCount); + QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat()); qreal width = 0; qreal height = 0; + int count = 1; + + if(!ticksList.empty()) { + count = qMax(ticksList.last().count(),ticksList.first().count()); + } + switch (which) { case Qt::MinimumSize:{ - int count = qMax(ticksList.last().count(),ticksList.first().count()); count = qMin(count,5); width = fn.averageCharWidth() * count; height = fn.height() + labelPadding(); @@ -98,7 +105,6 @@ QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c break; } case Qt::PreferredSize:{ - int count = qMax(ticksList.last().count(),ticksList.first().count()); width=fn.averageCharWidth() * count; height=fn.height()+labelPadding(); width=qMax(width,base.width()); @@ -113,4 +119,6 @@ QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c return sh; } +#include "moc_chartvalueaxisx_p.cpp" + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/valueaxis/chartvalueaxisx_p.h b/src/axis/valueaxis/chartvalueaxisx_p.h index 45120c0..44047f6 100644 --- a/src/axis/valueaxis/chartvalueaxisx_p.h +++ b/src/axis/valueaxis/chartvalueaxisx_p.h @@ -34,24 +34,23 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -class ChartPresenter; class QValueAxis; +class ChartPresenter; class ChartValueAxisX : public HorizontalAxis { + Q_OBJECT public: - ChartValueAxisX(QValueAxis *axis, ChartPresenter *presenter); + ChartValueAxisX(QValueAxis *axis, QGraphicsItem* item = 0); ~ChartValueAxisX(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const; - protected: - void handleAxisUpdated(); QVector calculateLayout() const; void updateGeometry(); - +private Q_SLOTS: + void handleTickCountChanged(int tick); private: - int m_tickCount; QValueAxis *m_axis; }; diff --git a/src/axis/valueaxis/chartvalueaxisy.cpp b/src/axis/valueaxis/chartvalueaxisy.cpp index a46dc4a..2e2758d 100644 --- a/src/axis/valueaxis/chartvalueaxisy.cpp +++ b/src/axis/valueaxis/chartvalueaxisy.cpp @@ -30,11 +30,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartValueAxisY::ChartValueAxisY(QValueAxis *axis, ChartPresenter *presenter) - : VerticalAxis(axis, presenter), - m_tickCount(0), +ChartValueAxisY::ChartValueAxisY(QValueAxis *axis, QGraphicsItem* item) + : VerticalAxis(axis, item), m_axis(axis) { + QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int))); } ChartValueAxisY::~ChartValueAxisY() @@ -43,15 +43,17 @@ ChartValueAxisY::~ChartValueAxisY() QVector ChartValueAxisY::calculateLayout() const { - Q_ASSERT(m_tickCount >= 2); + int tickCount = m_axis->tickCount(); + + Q_ASSERT(tickCount >= 2); QVector points; - points.resize(m_tickCount); + points.resize(tickCount); const QRectF &gridRect = gridGeometry(); - const qreal deltaY = gridRect.height() / (m_tickCount - 1); - for (int i = 0; i < m_tickCount; ++i) { + const qreal deltaY = gridRect.height() / (tickCount - 1); + for (int i = 0; i < tickCount; ++i) { points[i] = i * -deltaY + gridRect.bottom(); } @@ -63,18 +65,16 @@ void ChartValueAxisY::updateGeometry() const QVector &layout = ChartAxis::layout(); if (layout.isEmpty()) return; - setLabels(createValueLabels(layout.size())); + setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat())); VerticalAxis::updateGeometry(); } -void ChartValueAxisY::handleAxisUpdated() +void ChartValueAxisY::handleTickCountChanged(int tick) { - if (m_tickCount != m_axis->tickCount()) { - m_tickCount = m_axis->tickCount(); - presenter()->layout()->invalidate(); - } - - ChartAxis::handleAxisUpdated(); + Q_UNUSED(tick); + if(presenter()) presenter()->layout()->invalidate(); + //QVector layout = calculateLayout(); + //updateLayout(layout); } QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const @@ -84,10 +84,16 @@ QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c QFontMetrics fn(font()); QSizeF sh; QSizeF base = VerticalAxis::sizeHint(which, constraint); - QStringList ticksList = createValueLabels(m_tickCount); + QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat()); qreal width = 0; qreal height = 0; + int count = 1; + + if(!ticksList.empty()){ + count = qMax(ticksList.last().count(),ticksList.first().count()); + } + switch (which) { case Qt::MinimumSize: { width = fn.boundingRect("...").width() + labelPadding(); @@ -99,7 +105,6 @@ QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c } case Qt::PreferredSize: { - int count = qMax(ticksList.first().count() , ticksList.last().count()); width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance width += base.width(); height = fn.height() * ticksList.count(); @@ -113,4 +118,6 @@ QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c return sh; } +#include "moc_chartvalueaxisy_p.cpp" + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/valueaxis/chartvalueaxisy_p.h b/src/axis/valueaxis/chartvalueaxisy_p.h index 71dc211..2cf1770 100644 --- a/src/axis/valueaxis/chartvalueaxisy_p.h +++ b/src/axis/valueaxis/chartvalueaxisy_p.h @@ -39,17 +39,18 @@ class ChartPresenter; class ChartValueAxisY : public VerticalAxis { + Q_OBJECT public: - ChartValueAxisY(QValueAxis *axis, ChartPresenter *presenter); + ChartValueAxisY(QValueAxis *axis, QGraphicsItem* item = 0); ~ChartValueAxisY(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const; protected: QVector calculateLayout() const; void updateGeometry(); - void handleAxisUpdated(); +private Q_SLOTS: + void handleTickCountChanged(int tick); private: - int m_tickCount; QValueAxis *m_axis; }; diff --git a/src/axis/valueaxis/qvalueaxis.cpp b/src/axis/valueaxis/qvalueaxis.cpp index 08292cd..1be06af 100644 --- a/src/axis/valueaxis/qvalueaxis.cpp +++ b/src/axis/valueaxis/qvalueaxis.cpp @@ -24,7 +24,8 @@ #include "chartvalueaxisy_p.h" #include "domain_p.h" #include "chartdataset_p.h" -#include +#include "chartpresenter_p.h" +#include "charttheme_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -174,8 +175,8 @@ QValueAxis::QValueAxis(QValueAxisPrivate &d, QObject *parent) QValueAxis::~QValueAxis() { Q_D(QValueAxis); - if (d->m_dataset) - d->m_dataset->removeAxis(this); + if (d->m_chart) + d->m_chart->removeAxis(this); } void QValueAxis::setMin(qreal min) @@ -209,34 +210,7 @@ qreal QValueAxis::max() const void QValueAxis::setRange(qreal min, qreal max) { Q_D(QValueAxis); - bool changed = false; - - if (min > max) - return; - - if (d->m_niceNumbers) { - int ticks = d->m_tickCount; - d->looseNiceNumbers(min, max, ticks); - if (ticks != d->m_tickCount) - setTickCount(ticks); - } - - if (!qFuzzyCompare(d->m_min, min)) { - d->m_min = min; - changed = true; - emit minChanged(min); - } - - if (!qFuzzyCompare(d->m_max, max)) { - d->m_max = max; - changed = true; - emit maxChanged(max); - } - - if (changed) { - emit rangeChanged(min, max); - d->emitUpdated(); - } + d->setRange(min,max); } /*! @@ -247,7 +221,7 @@ void QValueAxis::setTickCount(int count) Q_D(QValueAxis); if (d->m_tickCount != count && count >= 2) { d->m_tickCount = count; - d->emitUpdated(); + emit tickCountChanged(count); } } @@ -263,18 +237,14 @@ int QValueAxis::tickCount() const void QValueAxis::setNiceNumbersEnabled(bool enable) { - Q_D(QValueAxis); - if (d->m_niceNumbers != enable) { - d->m_niceNumbers = enable; - if (enable && !qFuzzyCompare(d->m_max, d->m_min)) - setRange(d->m_min, d->m_max); - } + Q_UNUSED(enable); + qWarning()<<"This function is depreciated.Use applyNiceNumbers()."; } -bool QValueAxis::niceNumbersEnabled() const +bool QValueAxis::niceNumbersEnabled() const { - Q_D(const QValueAxis); - return d->m_niceNumbers; + qWarning()<<"This function is depreciated.Use applyNiceNumbers()."; + return false; } void QValueAxis::setLabelFormat(const QString &format) @@ -297,6 +267,17 @@ QAbstractAxis::AxisType QValueAxis::type() const return AxisTypeValue; } +void QValueAxis::applyNiceNumbers() +{ + Q_D(QValueAxis); + qreal min = d->m_min; + qreal max = d->m_max; + int ticks = d->m_tickCount; + Domain::looseNiceNumbers(min,max,ticks); + d->setRange(min,max); + setTickCount(ticks); +} + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// QValueAxisPrivate::QValueAxisPrivate(QValueAxis *q) @@ -304,7 +285,6 @@ QValueAxisPrivate::QValueAxisPrivate(QValueAxis *q) m_min(0), m_max(0), m_tickCount(5), - m_niceNumbers(false), m_format(QString::null) { @@ -315,19 +295,6 @@ QValueAxisPrivate::~QValueAxisPrivate() } -void QValueAxisPrivate::handleDomainUpdated() -{ - Q_Q(QValueAxis); - Domain *domain = qobject_cast(sender()); - Q_ASSERT(domain); - - if (orientation() == Qt::Horizontal) - q->setRange(domain->minX(), domain->maxX()); - else if (orientation() == Qt::Vertical) - q->setRange(domain->minY(), domain->maxY()); -} - - void QValueAxisPrivate::setMin(const QVariant &min) { Q_Q(QValueAxis); @@ -358,62 +325,64 @@ void QValueAxisPrivate::setRange(const QVariant &min, const QVariant &max) q->setRange(value1, value2); } -ChartAxis *QValueAxisPrivate::createGraphics(ChartPresenter *presenter) +void QValueAxisPrivate::setRange(qreal min, qreal max) { - Q_Q(QValueAxis); - if (m_orientation == Qt::Vertical) - return new ChartValueAxisY(q, presenter); - return new ChartValueAxisX(q, presenter); -} + Q_Q(QValueAxis); + bool changed = false; -void QValueAxisPrivate::intializeDomain(Domain *domain) -{ - Q_Q(QValueAxis); - if (qFuzzyCompare(m_max, m_min)) { - if (m_orientation == Qt::Vertical) - q->setRange(domain->minY(), domain->maxY()); - else - q->setRange(domain->minX(), domain->maxX()); - } else { - if (m_orientation == Qt::Vertical) - domain->setRangeY(m_min, m_max); - else - domain->setRangeX(m_min, m_max); + if (min > max) + return; + + if (!qFuzzyCompare(m_min,min)) { + m_min = min; + changed = true; + emit q->minChanged(min); + } + + if (!qFuzzyCompare(m_max,max)) { + m_max = max; + changed = true; + emit q->maxChanged(max); } -} -//algorithm defined by Paul S.Heckbert GraphicalGems I + if (changed) { + emit q->rangeChanged(min, max); + emit rangeChanged(min,max); + } +} -void QValueAxisPrivate::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const +void QValueAxisPrivate::initializeGraphics(QGraphicsItem* parent) { - qreal range = niceNumber(max - min, true); //range with ceiling - qreal step = niceNumber(range / (ticksCount - 1), false); - min = qFloor(min / step); - max = qCeil(max / step); - ticksCount = int(max - min) + 1; - min *= step; - max *= step; + Q_Q(QValueAxis); + ChartAxis* axis(0); + if (orientation() == Qt::Vertical) + axis = new ChartValueAxisY(q,parent); + if (orientation() == Qt::Horizontal) + axis = new ChartValueAxisX(q,parent); + + m_item.reset(axis); + QAbstractAxisPrivate::initializeGraphics(parent); } -//nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n -qreal QValueAxisPrivate::niceNumber(qreal x, bool ceiling) const +void QValueAxisPrivate::initializeDomain(Domain *domain) { - qreal z = qPow(10, qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x - qreal q = x / z; //q<10 && q>=1; - - if (ceiling) { - if (q <= 1.0) q = 1; - else if (q <= 2.0) q = 2; - else if (q <= 5.0) q = 5; - else q = 10; - } else { - if (q < 1.5) q = 1; - else if (q < 3.0) q = 2; - else if (q < 7.0) q = 5; - else q = 10; + if (orientation() == Qt::Vertical) { + if(!qFuzzyIsNull(m_max - m_min)) { + domain->setRangeY(m_min, m_max); + } + else { + setRange(domain->minY(), domain->maxY()); + } + } + if (orientation() == Qt::Horizontal) { + if(!qFuzzyIsNull(m_max - m_min)) { + domain->setRangeX(m_min, m_max); + } + else { + setRange(domain->minX(), domain->maxX()); + } } - return q * z; } #include "moc_qvalueaxis.cpp" diff --git a/src/axis/valueaxis/qvalueaxis.h b/src/axis/valueaxis/qvalueaxis.h index 6bc8636..ecc08bc 100644 --- a/src/axis/valueaxis/qvalueaxis.h +++ b/src/axis/valueaxis/qvalueaxis.h @@ -30,7 +30,7 @@ class QValueAxisPrivate; class QTCOMMERCIALCHART_EXPORT QValueAxis : public QAbstractAxis { Q_OBJECT - Q_PROPERTY(int tickCount READ tickCount WRITE setTickCount) + Q_PROPERTY(int tickCount READ tickCount WRITE setTickCount NOTIFY tickCountChanged) Q_PROPERTY(bool niceNumbersEnabled READ niceNumbersEnabled WRITE setNiceNumbersEnabled) Q_PROPERTY(qreal min READ min WRITE setMin NOTIFY minChanged) Q_PROPERTY(qreal max READ max WRITE setMax NOTIFY maxChanged) @@ -60,6 +60,9 @@ public: void setLabelFormat(const QString &format); QString labelFormat() const; + void applyNiceNumbers(); + + //TODO: depreciated ! void setNiceNumbersEnabled(bool enable = true); bool niceNumbersEnabled() const; @@ -67,6 +70,7 @@ Q_SIGNALS: void minChanged(qreal min); void maxChanged(qreal max); void rangeChanged(qreal min, qreal max); + void tickCountChanged(int ticks); private: Q_DECLARE_PRIVATE(QValueAxis) diff --git a/src/axis/valueaxis/qvalueaxis_p.h b/src/axis/valueaxis/qvalueaxis_p.h index 3edabac..15b92ca 100644 --- a/src/axis/valueaxis/qvalueaxis_p.h +++ b/src/axis/valueaxis/qvalueaxis_p.h @@ -43,12 +43,12 @@ public: ~QValueAxisPrivate(); public: - ChartAxis *createGraphics(ChartPresenter *presenter); - void intializeDomain(Domain *domain); - void handleDomainUpdated(); + void initializeGraphics(QGraphicsItem* parent); + void initializeDomain(Domain *domain); + qreal min() { return m_min; }; qreal max() { return m_max; }; - int count() const { return m_tickCount; } + void setRange(qreal min,qreal max); protected: void setMin(const QVariant &min); @@ -56,14 +56,9 @@ protected: void setRange(const QVariant &min, const QVariant &max); private: - void looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const; - qreal niceNumber(qreal x, bool ceiling) const; - -private: qreal m_min; qreal m_max; int m_tickCount; - bool m_niceNumbers; QString m_format; Q_DECLARE_PUBLIC(QValueAxis) }; diff --git a/src/axis/verticalaxis.cpp b/src/axis/verticalaxis.cpp index 415d6f9..448f8f9 100644 --- a/src/axis/verticalaxis.cpp +++ b/src/axis/verticalaxis.cpp @@ -25,8 +25,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -VerticalAxis::VerticalAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis) - : ChartAxis(axis, presenter, intervalAxis) +VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem* item, bool intervalAxis) + : ChartAxis(axis, item, intervalAxis) { } @@ -200,7 +200,7 @@ QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) cons break; case Qt::MaximumSize: case Qt::PreferredSize: - sh = QSizeF(fn.height(), fn.boundingRect(axis()->title()).width()); + sh = QSizeF(fn.height(), fn.boundingRect(axis()->titleText()).width()); break; default: break; diff --git a/src/axis/verticalaxis_p.h b/src/axis/verticalaxis_p.h index 69c81f6..f483ef6 100644 --- a/src/axis/verticalaxis_p.h +++ b/src/axis/verticalaxis_p.h @@ -37,7 +37,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class VerticalAxis : public ChartAxis { public: - VerticalAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis = false); + VerticalAxis(QAbstractAxis *axis, QGraphicsItem* item = 0, bool intervalAxis = false); ~VerticalAxis(); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; protected: diff --git a/src/barchart/abstractbarchartitem.cpp b/src/barchart/abstractbarchartitem.cpp index ec2d7d6..2ca2e15 100644 --- a/src/barchart/abstractbarchartitem.cpp +++ b/src/barchart/abstractbarchartitem.cpp @@ -33,8 +33,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) : - ChartItem(presenter), +AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) : + ChartItem(series->d_func(),item), m_animation(0), m_series(series) { @@ -100,13 +100,14 @@ void AbstractBarChartItem::handleDomainUpdated() m_domainMaxX = domain()->maxX(); m_domainMinY = domain()->minY(); m_domainMaxY = domain()->maxY(); - handleLayoutChanged(); -} -void AbstractBarChartItem::handleGeometryChanged(const QRectF &rect) -{ - prepareGeometryChange(); - m_rect = rect; + QRectF rect(QPointF(0,0),domain()->size()); + + if(m_rect != rect){ + prepareGeometryChange(); + m_rect = rect; + } + handleLayoutChanged(); } @@ -157,7 +158,7 @@ void AbstractBarChartItem::handleDataStructureChanged() } } - presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); + if(themeManager()) themeManager()->updateSeries(m_series); handleLayoutChanged(); } diff --git a/src/barchart/abstractbarchartitem_p.h b/src/barchart/abstractbarchartitem_p.h index 374bbd8..a128513 100644 --- a/src/barchart/abstractbarchartitem_p.h +++ b/src/barchart/abstractbarchartitem_p.h @@ -47,7 +47,7 @@ class AbstractBarChartItem : public ChartItem { Q_OBJECT public: - AbstractBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); virtual ~AbstractBarChartItem(); public: @@ -63,7 +63,6 @@ public: public Q_SLOTS: void handleDomainUpdated(); - void handleGeometryChanged(const QRectF &size); void handleLayoutChanged(); void handleLabelsVisibleChanged(bool visible); void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items diff --git a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp index c2361cb..9ebf7d4 100644 --- a/src/barchart/horizontal/bar/horizontalbarchartitem.cpp +++ b/src/barchart/horizontal/bar/horizontalbarchartitem.cpp @@ -25,8 +25,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) - : AbstractBarChartItem(series, presenter) +HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) + : AbstractBarChartItem(series, item) { } diff --git a/src/barchart/horizontal/bar/horizontalbarchartitem_p.h b/src/barchart/horizontal/bar/horizontalbarchartitem_p.h index 9d9ce22..4d7aeb9 100644 --- a/src/barchart/horizontal/bar/horizontalbarchartitem_p.h +++ b/src/barchart/horizontal/bar/horizontalbarchartitem_p.h @@ -39,7 +39,7 @@ class HorizontalBarChartItem : public AbstractBarChartItem { Q_OBJECT public: - HorizontalBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + HorizontalBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); private: virtual QVector calculateLayout(); diff --git a/src/barchart/horizontal/bar/qhorizontalbarseries.cpp b/src/barchart/horizontal/bar/qhorizontalbarseries.cpp index 6c404a9..54a8d74 100644 --- a/src/barchart/horizontal/bar/qhorizontalbarseries.cpp +++ b/src/barchart/horizontal/bar/qhorizontalbarseries.cpp @@ -72,8 +72,8 @@ QHorizontalBarSeries::QHorizontalBarSeries(QObject *parent) QHorizontalBarSeries::~QHorizontalBarSeries() { Q_D(QHorizontalBarSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } /*! @@ -92,12 +92,12 @@ QHorizontalBarSeriesPrivate::QHorizontalBarSeriesPrivate(QHorizontalBarSeries *q } -void QHorizontalBarSeriesPrivate::scaleDomain(Domain &domain) +void QHorizontalBarSeriesPrivate::initializeDomain() { - qreal minX(domain.minX()); - qreal minY(domain.minY()); - qreal maxX(domain.maxX()); - qreal maxY(domain.maxY()); + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); qreal y = categoryCount(); minX = qMin(minX, min()); @@ -105,18 +105,27 @@ void QHorizontalBarSeriesPrivate::scaleDomain(Domain &domain) maxX = qMax(maxX, max()); maxY = qMax(maxY, y - (qreal)0.5); - domain.setRange(minX, maxX, minY, maxY); + domain()->setRange(minX, maxX, minY, maxY); } -ChartElement *QHorizontalBarSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QHorizontalBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QHorizontalBarSeries); + HorizontalBarChartItem *bar = new HorizontalBarChartItem(q,parent); + m_item.reset(bar); + QAbstractSeriesPrivate::initializeGraphics(parent); +} - HorizontalBarChartItem *bar = new HorizontalBarChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) +void QHorizontalBarSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options) +{ + HorizontalBarChartItem *bar = static_cast(m_item.data()); + Q_ASSERT(bar); + if (options.testFlag(QChart::SeriesAnimations)) { bar->setAnimation(new HorizontalBarAnimation(bar)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return bar; + }else{ + bar->setAnimation(0); + } + QAbstractSeriesPrivate::initializeAnimations(options); } #include "moc_qhorizontalbarseries.cpp" diff --git a/src/barchart/horizontal/bar/qhorizontalbarseries_p.h b/src/barchart/horizontal/bar/qhorizontalbarseries_p.h index 4663494..5d9d440 100644 --- a/src/barchart/horizontal/bar/qhorizontalbarseries_p.h +++ b/src/barchart/horizontal/bar/qhorizontalbarseries_p.h @@ -39,8 +39,9 @@ class QHorizontalBarSeriesPrivate: public QAbstractBarSeriesPrivate { public: QHorizontalBarSeriesPrivate(QHorizontalBarSeries *q); - ChartElement *createGraphics(ChartPresenter *presenter); - void scaleDomain(Domain &domain); + void initializeGraphics(QGraphicsItem* parent); + void initializeAnimations(QtCommercialChart::QChart::AnimationOptions options); + void initializeDomain(); private: Q_DECLARE_PUBLIC(QHorizontalBarSeries) }; diff --git a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp index 9263c40..82eb77f 100644 --- a/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp +++ b/src/barchart/horizontal/percent/horizontalpercentbarchartitem.cpp @@ -25,8 +25,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -HorizontalPercentBarChartItem::HorizontalPercentBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) - : AbstractBarChartItem(series, presenter) +HorizontalPercentBarChartItem::HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) + : AbstractBarChartItem(series, item) { } diff --git a/src/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h b/src/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h index aa7fc29..364af93 100644 --- a/src/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h +++ b/src/barchart/horizontal/percent/horizontalpercentbarchartitem_p.h @@ -39,7 +39,7 @@ class HorizontalPercentBarChartItem : public AbstractBarChartItem { Q_OBJECT public: - HorizontalPercentBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); private: virtual QVector calculateLayout(); diff --git a/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp b/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp index 3f2bf57..2fbf46a 100644 --- a/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp +++ b/src/barchart/horizontal/percent/qhorizontalpercentbarseries.cpp @@ -77,8 +77,8 @@ QAbstractSeries::SeriesType QHorizontalPercentBarSeries::type() const QHorizontalPercentBarSeries::~QHorizontalPercentBarSeries() { Q_D(QHorizontalPercentBarSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -88,12 +88,12 @@ QHorizontalPercentBarSeriesPrivate::QHorizontalPercentBarSeriesPrivate(QHorizont } -void QHorizontalPercentBarSeriesPrivate::scaleDomain(Domain &domain) +void QHorizontalPercentBarSeriesPrivate::initializeDomain() { - qreal minX(domain.minX()); - qreal minY(domain.minY()); - qreal maxX(domain.maxX()); - qreal maxY(domain.maxY()); + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); qreal y = categoryCount(); minX = 0; @@ -101,20 +101,28 @@ void QHorizontalPercentBarSeriesPrivate::scaleDomain(Domain &domain) minY = qMin(minY, - (qreal)0.5); maxY = qMax(maxY, y - (qreal)0.5); - domain.setRange(minX, maxX, minY, maxY); + domain()->setRange(minX, maxX, minY, maxY); } -ChartElement *QHorizontalPercentBarSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QHorizontalPercentBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QHorizontalPercentBarSeries); + HorizontalPercentBarChartItem *bar = new HorizontalPercentBarChartItem(q,parent); + m_item.reset(bar); + QAbstractSeriesPrivate::initializeGraphics(parent); +} - HorizontalPercentBarChartItem *bar = new HorizontalPercentBarChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) +void QHorizontalPercentBarSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options) +{ + HorizontalPercentBarChartItem *bar = static_cast(m_item.data()); + Q_ASSERT(bar); + if (options.testFlag(QChart::SeriesAnimations)) { bar->setAnimation(new HorizontalPercentBarAnimation(bar)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return bar; + }else{ + bar->setAnimation(0); + } + QAbstractSeriesPrivate::initializeAnimations(options); } - #include "moc_qhorizontalpercentbarseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/horizontal/percent/qhorizontalpercentbarseries_p.h b/src/barchart/horizontal/percent/qhorizontalpercentbarseries_p.h index 9aa9267..a23c234 100644 --- a/src/barchart/horizontal/percent/qhorizontalpercentbarseries_p.h +++ b/src/barchart/horizontal/percent/qhorizontalpercentbarseries_p.h @@ -39,8 +39,9 @@ class QHorizontalPercentBarSeriesPrivate: public QAbstractBarSeriesPrivate { public: QHorizontalPercentBarSeriesPrivate(QHorizontalPercentBarSeries *q); - ChartElement *createGraphics(ChartPresenter *presenter); - void scaleDomain(Domain &domain); + void initializeGraphics(QGraphicsItem* parent); + void initializeAnimations(QtCommercialChart::QChart::AnimationOptions options); + void initializeDomain(); private: Q_DECLARE_PUBLIC(QHorizontalPercentBarSeries) }; diff --git a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp index b43d652..fee1f97 100644 --- a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp +++ b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem.cpp @@ -25,8 +25,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -HorizontalStackedBarChartItem::HorizontalStackedBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) - : AbstractBarChartItem(series, presenter) +HorizontalStackedBarChartItem::HorizontalStackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) + : AbstractBarChartItem(series, item) { } diff --git a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem_p.h b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem_p.h index 7d4814a..25ecab4 100644 --- a/src/barchart/horizontal/stacked/horizontalstackedbarchartitem_p.h +++ b/src/barchart/horizontal/stacked/horizontalstackedbarchartitem_p.h @@ -39,7 +39,7 @@ class HorizontalStackedBarChartItem : public AbstractBarChartItem { Q_OBJECT public: - HorizontalStackedBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + HorizontalStackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); private: virtual QVector calculateLayout(); diff --git a/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp b/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp index 4eb1b22..d478908 100644 --- a/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp +++ b/src/barchart/horizontal/stacked/qhorizontalstackedbarseries.cpp @@ -68,8 +68,8 @@ QHorizontalStackedBarSeries::QHorizontalStackedBarSeries(QObject *parent) QHorizontalStackedBarSeries::~QHorizontalStackedBarSeries() { Q_D(QHorizontalStackedBarSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } /*! @@ -87,12 +87,12 @@ QHorizontalStackedBarSeriesPrivate::QHorizontalStackedBarSeriesPrivate(QHorizont } -void QHorizontalStackedBarSeriesPrivate::scaleDomain(Domain &domain) +void QHorizontalStackedBarSeriesPrivate::initializeDomain() { - qreal minX(domain.minX()); - qreal minY(domain.minY()); - qreal maxX(domain.maxX()); - qreal maxY(domain.maxY()); + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); qreal y = categoryCount(); minX = qMin(minX, bottom()); @@ -100,20 +100,28 @@ void QHorizontalStackedBarSeriesPrivate::scaleDomain(Domain &domain) maxX = qMax(maxX, top()); maxY = qMax(maxY, y - (qreal)0.5); - domain.setRange(minX, maxX, minY, maxY); + domain()->setRange(minX, maxX, minY, maxY); } -ChartElement *QHorizontalStackedBarSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QHorizontalStackedBarSeriesPrivate::initializeGraphics(QGraphicsItem *parent) { Q_Q(QHorizontalStackedBarSeries); + HorizontalStackedBarChartItem *bar = new HorizontalStackedBarChartItem(q,parent); + m_item.reset(bar); + QAbstractSeriesPrivate::initializeGraphics(parent); +} - HorizontalStackedBarChartItem *bar = new HorizontalStackedBarChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) +void QHorizontalStackedBarSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options) +{ + HorizontalStackedBarChartItem *bar = static_cast(m_item.data()); + Q_ASSERT(bar); + if (options.testFlag(QChart::SeriesAnimations)) { bar->setAnimation(new HorizontalStackedBarAnimation(bar)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return bar; + }else{ + bar->setAnimation(0); + } + QAbstractSeriesPrivate::initializeAnimations(options); } - #include "moc_qhorizontalstackedbarseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/horizontal/stacked/qhorizontalstackedbarseries_p.h b/src/barchart/horizontal/stacked/qhorizontalstackedbarseries_p.h index 687c482..759b273 100644 --- a/src/barchart/horizontal/stacked/qhorizontalstackedbarseries_p.h +++ b/src/barchart/horizontal/stacked/qhorizontalstackedbarseries_p.h @@ -39,8 +39,9 @@ class QHorizontalStackedBarSeriesPrivate: public QAbstractBarSeriesPrivate { public: QHorizontalStackedBarSeriesPrivate(QHorizontalStackedBarSeries *q); - ChartElement *createGraphics(ChartPresenter *presenter); - void scaleDomain(Domain &domain); + void initializeGraphics(QGraphicsItem* parent); + void initializeAnimations(QtCommercialChart::QChart::AnimationOptions options); + void initializeDomain(); private: Q_DECLARE_PUBLIC(QHorizontalStackedBarSeries) }; diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index 2f92686..4f13530 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -622,12 +622,12 @@ qreal QAbstractBarSeriesPrivate::bottom() } -void QAbstractBarSeriesPrivate::scaleDomain(Domain &domain) +void QAbstractBarSeriesPrivate::initializeDomain() { - qreal minX(domain.minX()); - qreal minY(domain.minY()); - qreal maxX(domain.maxX()); - qreal maxY(domain.maxY()); + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); qreal seriesMinX = this->minX(); qreal seriesMaxX = this->maxX(); @@ -637,14 +637,7 @@ void QAbstractBarSeriesPrivate::scaleDomain(Domain &domain) maxX = qMax(maxX, seriesMaxX + (qreal)0.5); maxY = qMax(maxY, y); - domain.setRange(minX, maxX, minY, maxY); -} - -ChartElement *QAbstractBarSeriesPrivate::createGraphics(ChartPresenter *presenter) -{ - Q_UNUSED(presenter); - qWarning() << "QAbstractBarSeriesPrivate::createGraphics called"; - return 0; + domain()->setRange(minX, maxX, minY, maxY); } QList QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend) @@ -746,27 +739,30 @@ bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set) return true; } -void QAbstractBarSeriesPrivate::initializeAxis(QAbstractAxis *axis) +void QAbstractBarSeriesPrivate::initializeAxes() { Q_Q(QAbstractBarSeries); - if (axis->type() == QAbstractAxis::AxisTypeBarCategory) { - switch (q->type()) { - case QAbstractSeries::SeriesTypeHorizontalBar: - case QAbstractSeries::SeriesTypeHorizontalPercentBar: - case QAbstractSeries::SeriesTypeHorizontalStackedBar: - if (axis->orientation() == Qt::Vertical) + foreach(QAbstractAxis* axis, m_axes) { + + if (axis->type() == QAbstractAxis::AxisTypeBarCategory) { + switch (q->type()) { + case QAbstractSeries::SeriesTypeHorizontalBar: + case QAbstractSeries::SeriesTypeHorizontalPercentBar: + case QAbstractSeries::SeriesTypeHorizontalStackedBar: + if (axis->orientation() == Qt::Vertical) populateCategories(qobject_cast(axis)); - break; - case QAbstractSeries::SeriesTypeBar: - case QAbstractSeries::SeriesTypePercentBar: - case QAbstractSeries::SeriesTypeStackedBar: - if (axis->orientation() == Qt::Horizontal) + break; + case QAbstractSeries::SeriesTypeBar: + case QAbstractSeries::SeriesTypePercentBar: + case QAbstractSeries::SeriesTypeStackedBar: + if (axis->orientation() == Qt::Horizontal) populateCategories(qobject_cast(axis)); - break; - default: - qWarning() << "Unexpected series type"; - break; + break; + default: + qWarning() << "Unexpected series type"; + break; + } } } } @@ -806,6 +802,57 @@ void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis) } } +QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const +{ + Q_UNUSED(orientation); + return 0; +} + +void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced) +{ + const QList gradients = theme->seriesGradients(); + + QBrush brush; + QPen pen; + + qreal takeAtPos = 0.5; + qreal step = 0.2; + if (m_barSets.count() > 1) { + step = 1.0 / (qreal) m_barSets.count(); + if (m_barSets.count() % gradients.count()) + step *= gradients.count(); + else + step *= (gradients.count() - 1); + } + + for (int i(0); i < m_barSets.count(); i++) { + int colorIndex = (index + i) % gradients.count(); + if (i > 0 && i %gradients.count() == 0) { + // There is no dedicated base color for each sets, generate more colors + takeAtPos += step; + if (takeAtPos == 1.0) + takeAtPos += step; + takeAtPos -= (int) takeAtPos; + } + if (forced || brush == m_barSets.at(i)->brush()) + m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos)); + + // Pick label color from the opposite end of the gradient. + // 0.3 as a boundary seems to work well. + if (forced || brush == m_barSets.at(i)->labelBrush()) { + if (takeAtPos < 0.3) + m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1)); + else + m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0)); + } + + if (forced || pen == m_barSets.at(i)->pen()) { + QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0); + m_barSets.at(i)->setPen(c); + } + } +} + #include "moc_qabstractbarseries.cpp" #include "moc_qabstractbarseries_p.cpp" diff --git a/src/barchart/qabstractbarseries_p.h b/src/barchart/qabstractbarseries_p.h index 8b79c68..021751a 100644 --- a/src/barchart/qabstractbarseries_p.h +++ b/src/barchart/qabstractbarseries_p.h @@ -54,12 +54,14 @@ public: void setVisible(bool visible); void setLabelsVisible(bool visible); - void scaleDomain(Domain &domain); - ChartElement *createGraphics(ChartPresenter *presenter); - QList createLegendMarkers(QLegend* legend); + void initializeDomain(); + void initializeAxes(); + void initializeTheme(int index, ChartTheme* theme, bool forced = false); + + QList createLegendMarkers(QLegend *legend); - void initializeAxis(QAbstractAxis *axis); virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const; + QAbstractAxis* createDefaultAxis(Qt::Orientation orientation) const; bool append(QBarSet *set); bool remove(QBarSet *set); diff --git a/src/barchart/vertical/bar/barchartitem.cpp b/src/barchart/vertical/bar/barchartitem.cpp index 8ee5b3d..2029d32 100644 --- a/src/barchart/vertical/bar/barchartitem.cpp +++ b/src/barchart/vertical/bar/barchartitem.cpp @@ -26,8 +26,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -BarChartItem::BarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) : - AbstractBarChartItem(series, presenter) +BarChartItem::BarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) : + AbstractBarChartItem(series, item) { } diff --git a/src/barchart/vertical/bar/barchartitem_p.h b/src/barchart/vertical/bar/barchartitem_p.h index b4e0142..9eb3988 100644 --- a/src/barchart/vertical/bar/barchartitem_p.h +++ b/src/barchart/vertical/bar/barchartitem_p.h @@ -41,7 +41,7 @@ class BarChartItem : public AbstractBarChartItem { Q_OBJECT public: - BarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + BarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); private: virtual QVector calculateLayout(); diff --git a/src/barchart/vertical/bar/qbarseries.cpp b/src/barchart/vertical/bar/qbarseries.cpp index e97ed8f..93ad662 100644 --- a/src/barchart/vertical/bar/qbarseries.cpp +++ b/src/barchart/vertical/bar/qbarseries.cpp @@ -79,8 +79,8 @@ QAbstractSeries::SeriesType QBarSeries::type() const QBarSeries::~QBarSeries() { Q_D(QBarSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -89,12 +89,12 @@ QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : QAbstractBarSeriesPrivate( } -void QBarSeriesPrivate::scaleDomain(Domain &domain) +void QBarSeriesPrivate::initializeDomain() { - qreal minX(domain.minX()); - qreal minY(domain.minY()); - qreal maxX(domain.maxX()); - qreal maxY(domain.maxY()); + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); qreal x = categoryCount(); minX = qMin(minX, - (qreal)0.5); @@ -102,19 +102,28 @@ void QBarSeriesPrivate::scaleDomain(Domain &domain) maxX = qMax(maxX, x - (qreal)0.5); maxY = qMax(maxY, max()); - domain.setRange(minX, maxX, minY, maxY); + domain()->setRange(minX, maxX, minY, maxY); } -ChartElement *QBarSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QBarSeries); + BarChartItem *bar = new BarChartItem(q,parent); + m_item.reset(bar); + QAbstractSeriesPrivate::initializeGraphics(parent); +} - BarChartItem *bar = new BarChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) +void QBarSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options) +{ + BarChartItem *bar = static_cast(m_item.data()); + Q_ASSERT(bar); + if (options.testFlag(QChart::SeriesAnimations)) { bar->setAnimation(new BarAnimation(bar)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return bar; + }else{ + bar->setAnimation(0); + } + QAbstractSeriesPrivate::initializeAnimations(options); } #include "moc_qbarseries.cpp" diff --git a/src/barchart/vertical/bar/qbarseries_p.h b/src/barchart/vertical/bar/qbarseries_p.h index c26e612..5c69928 100644 --- a/src/barchart/vertical/bar/qbarseries_p.h +++ b/src/barchart/vertical/bar/qbarseries_p.h @@ -40,8 +40,10 @@ class QBarSeriesPrivate: public QAbstractBarSeriesPrivate { public: QBarSeriesPrivate(QBarSeries *q); - ChartElement *createGraphics(ChartPresenter *presenter); - void scaleDomain(Domain &domain); + void initializeGraphics(QGraphicsItem* parent); + void initializeAnimations(QtCommercialChart::QChart::AnimationOptions options); + void initializeDomain(); + private: Q_DECLARE_PUBLIC(QBarSeries) }; diff --git a/src/barchart/vertical/percent/percentbarchartitem.cpp b/src/barchart/vertical/percent/percentbarchartitem.cpp index 3ba34a0..5d98b90 100644 --- a/src/barchart/vertical/percent/percentbarchartitem.cpp +++ b/src/barchart/vertical/percent/percentbarchartitem.cpp @@ -26,8 +26,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) : - AbstractBarChartItem(series, presenter) +PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) : + AbstractBarChartItem(series, item) { } diff --git a/src/barchart/vertical/percent/percentbarchartitem_p.h b/src/barchart/vertical/percent/percentbarchartitem_p.h index 6cefd54..f5b610c 100644 --- a/src/barchart/vertical/percent/percentbarchartitem_p.h +++ b/src/barchart/vertical/percent/percentbarchartitem_p.h @@ -42,7 +42,7 @@ class PercentBarChartItem : public AbstractBarChartItem { Q_OBJECT public: - PercentBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0); private: virtual QVector calculateLayout(); diff --git a/src/barchart/vertical/percent/qpercentbarseries.cpp b/src/barchart/vertical/percent/qpercentbarseries.cpp index 5bfe88b..758105e 100644 --- a/src/barchart/vertical/percent/qpercentbarseries.cpp +++ b/src/barchart/vertical/percent/qpercentbarseries.cpp @@ -69,8 +69,8 @@ QPercentBarSeries::QPercentBarSeries(QObject *parent) QPercentBarSeries::~QPercentBarSeries() { Q_D(QPercentBarSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } /*! @@ -88,12 +88,12 @@ QPercentBarSeriesPrivate::QPercentBarSeriesPrivate(QPercentBarSeries *q) : QAbst } -void QPercentBarSeriesPrivate::scaleDomain(Domain &domain) +void QPercentBarSeriesPrivate::initializeDomain() { - qreal minX(domain.minX()); - qreal minY(domain.minY()); - qreal maxX(domain.maxX()); - qreal maxY(domain.maxY()); + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); qreal x = categoryCount(); minX = qMin(minX, - (qreal)0.5); @@ -101,21 +101,29 @@ void QPercentBarSeriesPrivate::scaleDomain(Domain &domain) minY = 0; maxY = 100; - domain.setRange(minX, maxX, minY, maxY); + domain()->setRange(minX, maxX, minY, maxY); } -ChartElement *QPercentBarSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QPercentBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QPercentBarSeries); + PercentBarChartItem *bar = new PercentBarChartItem(q,parent); + m_item.reset(bar); + QAbstractSeriesPrivate::initializeGraphics(parent); +} - PercentBarChartItem *bar = new PercentBarChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) +void QPercentBarSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options) +{ + PercentBarChartItem *bar = static_cast(m_item.data()); + Q_ASSERT(bar); + if (options.testFlag(QChart::SeriesAnimations)) { bar->setAnimation(new PercentBarAnimation(bar)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return bar; + }else{ + bar->setAnimation(0); + } + QAbstractSeriesPrivate::initializeAnimations(options); } - #include "moc_qpercentbarseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/vertical/percent/qpercentbarseries_p.h b/src/barchart/vertical/percent/qpercentbarseries_p.h index 52ff116..644530c 100644 --- a/src/barchart/vertical/percent/qpercentbarseries_p.h +++ b/src/barchart/vertical/percent/qpercentbarseries_p.h @@ -40,8 +40,9 @@ class QPercentBarSeriesPrivate: public QAbstractBarSeriesPrivate { public: QPercentBarSeriesPrivate(QPercentBarSeries *q); - void scaleDomain(Domain &domain); - ChartElement *createGraphics(ChartPresenter *presenter); + void initializeDomain(); + void initializeGraphics(QGraphicsItem* parent); + void initializeAnimations(QtCommercialChart::QChart::AnimationOptions options); private: Q_DECLARE_PUBLIC(QPercentBarSeries) }; diff --git a/src/barchart/vertical/stacked/qstackedbarseries.cpp b/src/barchart/vertical/stacked/qstackedbarseries.cpp index d8d3069..8db9612 100644 --- a/src/barchart/vertical/stacked/qstackedbarseries.cpp +++ b/src/barchart/vertical/stacked/qstackedbarseries.cpp @@ -70,8 +70,8 @@ QStackedBarSeries::QStackedBarSeries(QObject *parent) QStackedBarSeries::~QStackedBarSeries() { Q_D(QStackedBarSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } /*! Returns QChartSeries::SeriesTypeStackedBar. @@ -88,12 +88,12 @@ QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QStackedBarSeries *q) : QAbst } -void QStackedBarSeriesPrivate::scaleDomain(Domain &domain) +void QStackedBarSeriesPrivate::initializeDomain() { - qreal minX(domain.minX()); - qreal minY(domain.minY()); - qreal maxX(domain.maxX()); - qreal maxY(domain.maxY()); + qreal minX(domain()->minX()); + qreal minY(domain()->minY()); + qreal maxX(domain()->maxX()); + qreal maxY(domain()->maxY()); qreal x = categoryCount(); minX = qMin(minX, - (qreal)0.5); @@ -101,20 +101,28 @@ void QStackedBarSeriesPrivate::scaleDomain(Domain &domain) maxX = qMax(maxX, x - (qreal)0.5); maxY = qMax(maxY, top()); - domain.setRange(minX, maxX, minY, maxY); + domain()->setRange(minX, maxX, minY, maxY); } -ChartElement *QStackedBarSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QStackedBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QStackedBarSeries); + StackedBarChartItem *bar = new StackedBarChartItem(q,parent); + m_item.reset(bar); + QAbstractSeriesPrivate::initializeGraphics(parent); +} - StackedBarChartItem *bar = new StackedBarChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) +void QStackedBarSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options) +{ + StackedBarChartItem *bar = static_cast(m_item.data()); + Q_ASSERT(bar); + if (options.testFlag(QChart::SeriesAnimations)) { bar->setAnimation(new StackedBarAnimation(bar)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return bar; + }else{ + bar->setAnimation(0); + } + QAbstractSeriesPrivate::initializeAnimations(options); } - #include "moc_qstackedbarseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/vertical/stacked/qstackedbarseries_p.h b/src/barchart/vertical/stacked/qstackedbarseries_p.h index ba47f94..084b82c 100644 --- a/src/barchart/vertical/stacked/qstackedbarseries_p.h +++ b/src/barchart/vertical/stacked/qstackedbarseries_p.h @@ -40,8 +40,9 @@ class QStackedBarSeriesPrivate: public QAbstractBarSeriesPrivate { public: QStackedBarSeriesPrivate(QStackedBarSeries *q); - ChartElement *createGraphics(ChartPresenter *presenter); - void scaleDomain(Domain &domain); + void initializeGraphics(QGraphicsItem* parent); + void initializeAnimations(QtCommercialChart::QChart::AnimationOptions options); + void initializeDomain(); private: Q_DECLARE_PUBLIC(QStackedBarSeries) }; diff --git a/src/barchart/vertical/stacked/stackedbarchartitem.cpp b/src/barchart/vertical/stacked/stackedbarchartitem.cpp index 548e8b2..73244cf 100644 --- a/src/barchart/vertical/stacked/stackedbarchartitem.cpp +++ b/src/barchart/vertical/stacked/stackedbarchartitem.cpp @@ -26,8 +26,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) : - AbstractBarChartItem(series, presenter) +StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) : + AbstractBarChartItem(series, item) { } diff --git a/src/barchart/vertical/stacked/stackedbarchartitem_p.h b/src/barchart/vertical/stacked/stackedbarchartitem_p.h index 85cb99a..4a24b9a 100644 --- a/src/barchart/vertical/stacked/stackedbarchartitem_p.h +++ b/src/barchart/vertical/stacked/stackedbarchartitem_p.h @@ -41,7 +41,7 @@ class StackedBarChartItem : public AbstractBarChartItem { Q_OBJECT public: - StackedBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter); + StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item =0); private: virtual QVector calculateLayout(); diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index e1816f1..8dc888f 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -19,7 +19,9 @@ ****************************************************************************/ #include "chartdataset_p.h" +#include "chartpresenter_p.h" #include "qchart.h" +#include "qchart_p.h" #include "qvalueaxis.h" #include "qbarcategoryaxis.h" #include "qvalueaxis_p.h" @@ -29,6 +31,7 @@ #include "qstackedbarseries.h" #include "qpercentbarseries.h" #include "qpieseries.h" +#include "chartitem_p.h" #ifndef QT_ON_ARM #include "qdatetimeaxis.h" @@ -36,8 +39,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartDataSet::ChartDataSet(QChart *parent) - : QObject(parent) +ChartDataSet::ChartDataSet(QChart *chart) + : QObject(chart), + m_chart(chart) { } @@ -45,451 +49,358 @@ ChartDataSet::ChartDataSet(QChart *parent) ChartDataSet::~ChartDataSet() { removeAllSeries(); + removeAllAxes(); } +/* + * This method adds series to chartdataset, series ownership is taken from caller. + */ void ChartDataSet::addSeries(QAbstractSeries *series) { - Domain *domain = m_seriesDomainMap.value(series); + if (m_seriesList.contains(series)) { + qWarning() << QObject::tr("Can not add series. Series already on the chart."); + return; + } + + series->d_ptr->initializeDomain(); + m_seriesList.append(series); + + series->setParent(this); // take ownership + series->d_ptr->m_chart = m_chart; + + emit seriesAdded(series); +} - if (domain) { - qWarning() << "Can not add series. Series already on the chart"; +/* + * This method adds axis to chartdataset, axis ownership is taken from caller. + */ +void ChartDataSet::addAxis(QAbstractAxis *axis,Qt::Alignment aligment) +{ + if (m_axisList.contains(axis)) { + qWarning() << QObject::tr("Can not add axis. Axis already on the chart."); return; } - domain = new Domain(series); - m_seriesDomainMap.insert(series, domain); - series->d_ptr->scaleDomain(*domain); + axis->d_ptr->setAlignment(aligment); - createSeriesIndex(series); + if(!axis->alignment()) { + qWarning()<< QObject::tr("No alignment specified !"); + return; + }; - series->setParent(this); // take ownership - series->d_ptr->m_chart = qobject_cast(parent()); - series->d_ptr->m_dataset = this; + QSharedPointer domain(new Domain()); + axis->d_ptr->initializeDomain(domain.data()); - emit seriesAdded(series, domain); + axis->setParent(this); + axis->d_ptr->m_chart = m_chart; + m_axisList.append(axis); + emit axisAdded(axis); } +/* + * This method removes series form chartdataset, series ownership is passed back to caller. + */ void ChartDataSet::removeSeries(QAbstractSeries *series) { - if (!m_seriesDomainMap.contains(series)) { - qWarning() << "Can not remove series. Series not found on the chart."; + if (! m_seriesList.contains(series)) { + qWarning() << QObject::tr("Can not remove series. Series not found on the chart."); return; } emit seriesRemoved(series); - - Domain *domain = m_seriesDomainMap.take(series); - delete domain; - domain = 0; - - removeSeriesIndex(series); + m_seriesList.removeAll(series); series->setParent(0); series->d_ptr->m_chart = 0; - series->d_ptr->m_dataset = 0; - - removeAxes(series); -} - + series->d_ptr->m_domain.clear(); + QList axes = series->d_ptr->m_axes; -void ChartDataSet::createSeriesIndex(QAbstractSeries *series) -{ - QMapIterator i(m_indexSeriesMap); - - int key = 0; - while (i.hasNext()) { - i.next(); - if (i.key() != key) - break; - key++; + foreach(QAbstractAxis* axis, axes) { + axis->d_ptr->m_series.removeAll(series); + series->d_ptr->m_axes.removeAll(axis); } - - m_indexSeriesMap.insert(key, series); } -void ChartDataSet::removeSeriesIndex(QAbstractSeries *series) -{ - int key = seriesIndex(series); - Q_ASSERT(key != -1); - m_indexSeriesMap.remove(key); -} - -void ChartDataSet::createDefaultAxes() +/* + * This method removes axis form chartdataset, series ownership is passed back to caller. + */ +void ChartDataSet::removeAxis(QAbstractAxis *axis) { - if (m_seriesDomainMap.isEmpty()) + if (! m_axisList.contains(axis)) { + qWarning() << QObject::tr("Can not remove axis. Axis not found on the chart."); return; - - QAbstractAxis::AxisTypes typeX(0); - QAbstractAxis::AxisTypes typeY(0); - - // Remove possibly existing axes - QMapIterator i(m_seriesDomainMap); - while (i.hasNext()) { - i.next(); - removeAxes(i.key()); } - i.toFront(); + emit axisRemoved(axis); + m_axisList.removeAll(axis); - // Select the required axis x and axis y types based on the types of the current series - while (i.hasNext()) { - i.next(); - QAbstractAxis *axisX = m_seriesAxisXMap.value(i.key()); - QAbstractAxis *axisY = m_seriesAxisYMap.value(i.key()); - if (axisX) - typeX &= axisX->type(); - else - typeX |= i.key()->d_ptr->defaultAxisType(Qt::Horizontal); - if (axisY) - typeY &= axisY->type(); - else - typeY |= i.key()->d_ptr->defaultAxisType(Qt::Vertical); - } + axis->setParent(0); + axis->d_ptr->m_chart = 0; - // Create the axes of the types selected - createAxes(typeX, Qt::Horizontal); - createAxes(typeY, Qt::Vertical); -} + QList series = axis->d_ptr->m_series; -void ChartDataSet::createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation orientation) -{ - QMapIterator i(m_seriesDomainMap); - - // TODO: Add a descriptive comment of what happens here - if (type.testFlag(QAbstractAxis::AxisTypeValue) && type.testFlag(QAbstractAxis::AxisTypeBarCategory)) { - while (i.hasNext()) { - i.next(); - QAbstractAxis *axis = createAxis(i.key()->d_ptr->defaultAxisType(orientation), orientation); - if (axis) { - initializeAxis(axis, i.key()); - emit axisAdded(axis, i.value()); - } - } - } else if (!type.testFlag(QAbstractAxis::AxisTypeNoAxis)) { - QAbstractAxis *axis = createAxis(QAbstractAxis::AxisType(int(type)), orientation); - i.toFront(); - while (i.hasNext()) { - i.next(); - initializeAxis(axis, i.key()); - } - emit axisAdded(axis, i.value()); + foreach(QAbstractSeries* s, series) { + s->d_ptr->m_axes.removeAll(axis); + axis->d_ptr->m_series.removeAll(s); } } -QAbstractAxis *ChartDataSet::createAxis(QAbstractAxis::AxisType type, Qt::Orientation orientation) +/* + * This method attach axis to series, return true if success. + */ +bool ChartDataSet::attachAxis(QAbstractSeries* series,QAbstractAxis *axis) { - QAbstractAxis *axis = 0; + Q_ASSERT(series); + Q_ASSERT(axis); - switch (type) { - case QAbstractAxis::AxisTypeValue: - axis = new QValueAxis(this); - break; - case QAbstractAxis::AxisTypeBarCategory: - axis = new QBarCategoryAxis(this); - break; - case QAbstractAxis::AxisTypeCategory: - axis = new QCategoryAxis(this); - break; -#ifndef Q_WS_QWS - case QAbstractAxis::AxisTypeDateTime: - axis = new QDateTimeAxis(this); - break; -#endif - default: - axis = 0; - break; - } + QList attachedSeriesList = axis->d_ptr->m_series; + QList attachedAxisList = series->d_ptr->m_axes; + QSharedPointer domain = series->d_ptr->m_domain; - if (axis) - axis->d_ptr->setOrientation(orientation); + if (!m_seriesList.contains(series)) { + qWarning() << QObject::tr("Can not find series on the chart."); + return false; + } - return axis; -} + if (axis && !m_axisList.contains(axis)) { + qWarning() << QObject::tr("Can not find axis on the chart."); + return false; + } -void ChartDataSet::initializeAxis(QAbstractAxis *axis, QAbstractSeries *series) -{ - Domain *domain = m_seriesDomainMap.value(series); - axis->d_ptr->m_dataset = this; - series->d_ptr->initializeAxis(axis); - axis->d_ptr->intializeDomain(domain); - if (axis->orientation() == Qt::Horizontal) { - QObject::connect(axis->d_ptr.data(), SIGNAL(updated()), domain, SLOT(handleAxisUpdated())); - QObject::connect(domain, SIGNAL(updated()), axis->d_ptr.data(), SLOT(handleDomainUpdated())); - m_seriesAxisXMap.insert(series, axis); - } else { - QObject::connect(axis->d_ptr.data(), SIGNAL(updated()), domain, SLOT(handleAxisUpdated())); - QObject::connect(domain, SIGNAL(updated()), axis->d_ptr.data(), SLOT(handleDomainUpdated())); - m_seriesAxisYMap.insert(series, axis); + if (attachedAxisList.contains(axis)) { + qWarning() << QObject::tr("Axis already attached to series."); + return false; } - axis->d_ptr->emitUpdated(); -} -void ChartDataSet::removeAxes(QAbstractSeries *series) -{ - QAbstractAxis *axisX = m_seriesAxisXMap.take(series); + Q_ASSERT(!attachedSeriesList.contains(series)); + Q_ASSERT(!domain.isNull()); - if (axisX) { - QList axesX = m_seriesAxisXMap.values(); - int x = axesX.indexOf(axisX); + if(attachedSeriesList.isEmpty()){ - if (x == -1) { - emit axisRemoved(axisX); - axisX->d_ptr->m_dataset = 0; - axisX->deleteLater(); - } + }else{ + domain = attachedSeriesList.first()->d_ptr->domain(); + Q_ASSERT(!domain.isNull()); + series->d_ptr->setDomain(domain); } - QAbstractAxis *axisY = m_seriesAxisYMap.take(series); + series->d_ptr->m_axes<d_ptr->m_series< axesY = m_seriesAxisYMap.values(); + series->d_ptr->initializeAxes(); + axis->d_ptr->initializeDomain(domain.data()); - int y = axesY.indexOf(axisY); + if(axis->orientation()==Qt::Vertical){ + QObject::connect(axis->d_ptr.data(), SIGNAL(rangeChanged(qreal,qreal)), domain.data(), SLOT(handleVerticalAxisRangeChanged(qreal,qreal))); + QObject::connect(domain.data(), SIGNAL(rangeVerticalChanged(qreal,qreal)), axis->d_ptr.data(), SLOT(handleRangeChanged(qreal,qreal))); + } - if (y == -1) { - emit axisRemoved(axisY); - axisY->d_ptr->m_dataset = 0; - axisY->deleteLater(); - } + if(axis->orientation()==Qt::Horizontal){ + QObject::connect(axis->d_ptr.data(), SIGNAL(rangeChanged(qreal,qreal)), domain.data(), SLOT(handleHorizontalAxisRangeChanged(qreal,qreal))); + QObject::connect(domain.data(), SIGNAL(rangeHorizontalChanged(qreal,qreal)), axis->d_ptr.data(), SLOT(handleRangeChanged(qreal,qreal))); } + + return true; } -void ChartDataSet::removeAxis(QAbstractAxis *axis) +/* + * This method detach axis to series, return true if success. + */ +bool ChartDataSet::detachAxis(QAbstractSeries* series,QAbstractAxis *axis) { - if (!axis->d_ptr->m_dataset) { - qWarning() << "UnBound axis found !"; - return; - } + Q_ASSERT(series); + Q_ASSERT(axis); - QMap *seriesAxisMap; + QList attachedSeriesList = axis->d_ptr->m_series; + QList attachedAxisList = series->d_ptr->m_axes; + QSharedPointer domain = series->d_ptr->m_domain; - if (axis->orientation() == Qt::Vertical) - seriesAxisMap = &m_seriesAxisYMap; - else - seriesAxisMap = &m_seriesAxisXMap; + if (!m_seriesList.contains(series)) { + qWarning() << QObject::tr("Can not find series on the chart."); + return false; + } - QMapIterator i(*seriesAxisMap); + if (axis && !m_axisList.contains(axis)) { + qWarning() << QObject::tr("Can not find axis on the chart."); + return false; + } - while (i.hasNext()) { - i.next(); - if (i.value() == axis) - removeSeries(i.key()); + if (!attachedAxisList.contains(axis)) { + qWarning() << QObject::tr("Axis not attached to series."); + return false; } -} -void ChartDataSet::removeAllSeries() -{ - QList series = m_seriesDomainMap.keys(); - foreach (QAbstractSeries *s , series) - removeSeries(s); + Q_ASSERT(axis->d_ptr->m_series.contains(series)); + + series->d_ptr->m_axes.removeAll(axis); + axis->d_ptr->m_series.removeAll(series); - Q_ASSERT(m_seriesAxisXMap.count() == 0); - Q_ASSERT(m_seriesAxisXMap.count() == 0); - Q_ASSERT(m_seriesDomainMap.count() == 0); + if(axis->orientation()==Qt::Vertical){ + QObject::disconnect(axis->d_ptr.data(), SIGNAL(rangeChanged(qreal,qreal)), domain.data(), SLOT(handleVerticalAxisRangeChanged(qreal,qreal))); + QObject::disconnect(domain.data(), SIGNAL(rangeVerticalChanged(qreal,qreal)), axis->d_ptr.data(), SLOT(handleRangeChanged(qreal,qreal))); + } + + if(axis->orientation()==Qt::Horizontal){ + QObject::disconnect(axis->d_ptr.data(), SIGNAL(rangeChanged(qreal,qreal)), domain.data(), SLOT(handleHorizontalAxisRangeChanged(qreal,qreal))); + QObject::disconnect(domain.data(), SIGNAL(rangeHorizontalChanged(qreal,qreal)), axis->d_ptr.data(), SLOT(handleRangeChanged(qreal,qreal))); + } - qDeleteAll(series); + return true; } -void ChartDataSet::zoomInDomain(const QRectF &rect, const QSizeF &size) +void ChartDataSet::createDefaultAxes() { - //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates + if (m_seriesList.isEmpty()) + return; + QAbstractAxis::AxisTypes typeX(0); + QAbstractAxis::AxisTypes typeY(0); - blockAxisSignals(true); + // Remove possibly existing axes + removeAllAxes(); - QMapIterator i(m_seriesDomainMap); + Q_ASSERT(m_axisList.isEmpty()); - while (i.hasNext()) { - i.next(); - i.value()->zoomIn(rect, size); + // Select the required axis x and axis y types based on the types of the current series + foreach(QAbstractSeries* s, m_seriesList) { + typeX |= s->d_ptr->defaultAxisType(Qt::Horizontal); + typeY |= s->d_ptr->defaultAxisType(Qt::Vertical); } - blockAxisSignals(false); + // Create the axes of the types selected + createAxes(typeX, Qt::Horizontal); + createAxes(typeY, Qt::Vertical); } -void ChartDataSet::zoomOutDomain(const QRectF &rect, const QSizeF &size) +void ChartDataSet::createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation orientation) { - //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates - - blockAxisSignals(true); - - QMapIterator i(m_seriesDomainMap); + QAbstractAxis *axis = 0; + //decide what axis should be created - while (i.hasNext()) { - i.next(); - i.value()->zoomOut(rect, size); + switch (type) { + case QAbstractAxis::AxisTypeValue: + axis = new QValueAxis(this); + break; + case QAbstractAxis::AxisTypeBarCategory: + axis = new QBarCategoryAxis(this); + break; + case QAbstractAxis::AxisTypeCategory: + axis = new QCategoryAxis(this); + break; +#ifndef Q_WS_QWS + case QAbstractAxis::AxisTypeDateTime: + axis = new QDateTimeAxis(this); + break; +#endif + default: + axis = 0; + break; } - blockAxisSignals(false); -} + if (axis) { + //create one axis for all -void ChartDataSet::blockAxisSignals(bool enabled) -{ - QMapIterator i(m_seriesDomainMap); - while (i.hasNext()) { - i.next(); - QAbstractAxis *axisX = m_seriesAxisXMap.value(i.key()); - QAbstractAxis *axisY = m_seriesAxisYMap.value(i.key()); - if (axisX) { - axisX->d_ptr->blockSignals(enabled); - if (!enabled) { - axisX->d_ptr->setDirty(false); - axisX->d_ptr->emitUpdated(); - } - } - if (axisY) { - axisY->d_ptr->blockSignals(enabled); - if (!enabled) { - axisY->d_ptr->setDirty(false); - axisY->d_ptr->emitUpdated(); - } + addAxis(axis,orientation==Qt::Horizontal?Qt::AlignBottom:Qt::AlignLeft); + + foreach(QAbstractSeries *s, m_seriesList) { + attachAxis(s,axis); } - } -} -int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) -{ - int count = 0; - QMapIterator i(m_seriesDomainMap); - while (i.hasNext()) { - i.next(); - if (i.key()->type() == type) - count++; } - return count; -} - -int ChartDataSet::seriesIndex(QAbstractSeries *series) -{ - QMapIterator i(m_indexSeriesMap); - while (i.hasNext()) { - i.next(); - if (i.value() == series) - return i.key(); + else if (!type.testFlag(QAbstractAxis::AxisTypeNoAxis)) { + //create separate axis + foreach(QAbstractSeries *s, m_seriesList) { + QAbstractAxis *axis = s->d_ptr->createDefaultAxis(orientation); + if(axis) { + addAxis(axis,orientation==Qt::Horizontal?Qt::AlignBottom:Qt::AlignLeft); + attachAxis(s,axis); + } + } } - return -1; } -QAbstractAxis *ChartDataSet::axisX(QAbstractSeries *series) const +void ChartDataSet::removeAllSeries() { - if (series == 0) { - - QMapIterator i(m_seriesAxisXMap); + foreach (QAbstractSeries *s , m_seriesList) + removeSeries(s); - while (i.hasNext()) { - i.next(); - if (i.value()->isVisible()) - return i.value(); - } - return 0; - } - return m_seriesAxisXMap.value(series); + Q_ASSERT(m_seriesList.count() == 0); + qDeleteAll(m_seriesList); } -QAbstractAxis *ChartDataSet::axisY(QAbstractSeries *series) const +void ChartDataSet::removeAllAxes() { - if (series == 0) { - QMapIterator i(m_seriesAxisYMap); + foreach (QAbstractAxis *a , m_axisList) + removeAxis(a); - while (i.hasNext()) { - i.next(); - if (i.value()->isVisible()) return i.value(); - } - return 0; - } - return m_seriesAxisYMap.value(series); + Q_ASSERT(m_axisList.count() == 0); + qDeleteAll(m_axisList); } -void ChartDataSet::setAxis(QAbstractSeries *series, QAbstractAxis *axis, Qt::Orientation orientation) +void ChartDataSet::zoomInDomain(const QRectF &rect) { - Q_ASSERT(axis); - - if (!series) { - qWarning() << "Series not found on the chart."; - return; + QList domains; + foreach(QAbstractSeries *s, m_seriesList) { + Domain* domain = s->d_ptr->m_domain.data(); + if(domains.contains(domain)) continue; + s->d_ptr->m_domain->blockAxisSignals(true); + domains<zoomIn(rect); - if (!domain) { - qWarning() << "Series not found on the chart."; - return; - } - - if (orientation == Qt::Horizontal && axis->orientation() == Qt::Vertical) { - qWarning() << "Axis already defined as axis Y"; - return; - } + foreach(Domain *domain, domains) + domain->blockAxisSignals(false); +} - if (orientation == Qt::Vertical && axis->orientation() == Qt::Horizontal) { - qWarning() << "Axis already defined as axis X"; - return; +void ChartDataSet::zoomOutDomain(const QRectF &rect) +{ + QList domains; + foreach(QAbstractSeries *s, m_seriesList) { + Domain* domain = s->d_ptr->m_domain.data(); + if(domains.contains(domain)) continue; + s->d_ptr->m_domain->blockAxisSignals(true); + domains<d_ptr->setOrientation(orientation); - - QMap *seriesAxisMap; + foreach(Domain *domain, domains) + domain->zoomOut(rect); - if (orientation == Qt::Vertical) - seriesAxisMap = &m_seriesAxisYMap; - else - seriesAxisMap = &m_seriesAxisXMap; - - if (seriesAxisMap->value(series) == axis) { - qWarning() << "The axis already set for the series"; - return; - } + foreach(Domain *domain, domains) + domain->blockAxisSignals(false); +} - QAbstractAxis *oldAxis = seriesAxisMap->take(series); - QList axes = seriesAxisMap->values(); - if (oldAxis) { - if (axes.indexOf(oldAxis) == -1) { - emit axisRemoved(oldAxis); - oldAxis->disconnect(); - QObject::disconnect(domain, 0, oldAxis, 0); - oldAxis->d_ptr->m_dataset = 0; - oldAxis->deleteLater(); - } +void ChartDataSet::scrollDomain(qreal dx, qreal dy) +{ + QList domains; + foreach(QAbstractSeries *s, m_seriesList) { + Domain* domain = s->d_ptr->m_domain.data(); + if(domains.contains(domain)) continue; + s->d_ptr->m_domain->blockAxisSignals(true); + domains<move(dx, dy); -Domain *ChartDataSet::domain(QAbstractSeries *series) const -{ - return m_seriesDomainMap.value(series); + foreach(Domain *domain, domains) + domain->blockAxisSignals(false); } -void ChartDataSet::scrollDomain(qreal dx, qreal dy, const QSizeF &size) +QList ChartDataSet::axes() const { - blockAxisSignals(true); - QMapIterator i(m_seriesDomainMap); - while (i.hasNext()) { - i.next(); - i.value()->move(dx, dy, size); - } - blockAxisSignals(false); + return m_axisList; } QList ChartDataSet::series() const { - return m_seriesDomainMap.keys(); + return m_seriesList; } -/* -void ChartDataSet::updateSeries(QAbstractSeries *series) -{ - emit seriesUpdated(series); -} -*/ + #include "moc_chartdataset_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index 630eb53..cd4139f 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -38,59 +38,48 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QAbstractAxis; +class ChartPresenter; class QTCOMMERCIALCHART_AUTOTEST_EXPORT ChartDataSet : public QObject { Q_OBJECT public: - ChartDataSet(QChart *parent = 0); + ChartDataSet(QChart *chart); virtual ~ChartDataSet(); void addSeries(QAbstractSeries *series); void removeSeries(QAbstractSeries *series); + QList series() const; void removeAllSeries(); -// void updateSeries(QAbstractSeries* series); - - void zoomInDomain(const QRectF &rect, const QSizeF &size); - void zoomOutDomain(const QRectF &rect, const QSizeF &size); - void scrollDomain(qreal dx, qreal dy, const QSizeF &size); - - int seriesCount(QAbstractSeries::SeriesType type); - int seriesIndex(QAbstractSeries *series); - - QAbstractAxis *axisX(QAbstractSeries *series) const; - QAbstractAxis *axisY(QAbstractSeries *series) const; - void setAxis(QAbstractSeries *series, QAbstractAxis *axis, Qt::Orientation orientation); + void addAxis(QAbstractAxis *axis,Qt::Alignment aligment); + void removeAxis(QAbstractAxis *axis); + QList axes() const; + void removeAllAxes(); - QList series() const; - Domain *domain(QAbstractSeries *series) const; + bool attachAxis(QAbstractSeries* series,QAbstractAxis *axis); + bool detachAxis(QAbstractSeries* series,QAbstractAxis *axis); - void removeAxis(QAbstractAxis *axis); void createDefaultAxes(); + void zoomInDomain(const QRectF &rect); + void zoomOutDomain(const QRectF &rect); + void scrollDomain(qreal dx, qreal dy); + Q_SIGNALS: - void seriesAdded(QAbstractSeries *series, Domain *domain); - void seriesRemoved(QAbstractSeries *series); -// void seriesUpdated(QAbstractSeries* series); - void axisAdded(QAbstractAxis *axis, Domain *domain); - void axisRemoved(QAbstractAxis *axis); + void axisAdded(QAbstractAxis* axis); + void axisRemoved(QAbstractAxis* axis); + void seriesAdded(QAbstractSeries* series); + void seriesRemoved(QAbstractSeries* series); private: - void calculateDomain(QAbstractSeries *series, Domain *domain); void createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation orientation); QAbstractAxis *createAxis(QAbstractAxis::AxisType type, Qt::Orientation orientation); - void initializeAxis(QAbstractAxis *axis, QAbstractSeries *series); - void removeAxes(QAbstractSeries *series); - void blockAxisSignals(bool enabled); - void createSeriesIndex(QAbstractSeries *series); - void removeSeriesIndex(QAbstractSeries *series); private: - QMap m_seriesAxisXMap; - QMap m_seriesAxisYMap; - QMap m_seriesDomainMap; - QMap m_indexSeriesMap; + QList m_seriesList; + QList m_axisList; + QChart* m_chart; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartelement.cpp b/src/chartelement.cpp index ea73640..a5200c0 100644 --- a/src/chartelement.cpp +++ b/src/chartelement.cpp @@ -24,11 +24,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartElement::ChartElement(ChartPresenter *presenter) - : QObject(presenter), - m_presenter(presenter), - m_domain(0) +ChartElement::ChartElement(QGraphicsItem* item): + QGraphicsObject(item), + m_presenter(0), + m_themeManager(0) { + } void ChartElement::setPresenter(ChartPresenter *presenter) @@ -41,37 +42,14 @@ ChartPresenter *ChartElement::presenter() const return m_presenter; } -void ChartElement::setDomain(Domain *domain) -{ - m_domain = domain; -} - -Domain *ChartElement::domain() const -{ - return m_domain; -} - -void ChartElement::handleGeometryChanged(const QRectF &rect) -{ - Q_UNUSED(rect); - qWarning() << "Slot not implemented"; -} - -void ChartElement::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) +void ChartElement::setThemeManager(ChartThemeManager *manager) { - Q_UNUSED(minX); - Q_UNUSED(maxX); - Q_UNUSED(minY); - Q_UNUSED(maxY); - qWarning() << "Slot not implemented"; + m_themeManager = manager; } -void ChartElement::handleDomainUpdated() +ChartThemeManager* ChartElement::themeManager() const { - qWarning() << "Slot not implemented"; + return m_themeManager; } - -#include "moc_chartelement_p.cpp" - QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartelement_p.h b/src/chartelement_p.h index 65a675f..0d639b7 100644 --- a/src/chartelement_p.h +++ b/src/chartelement_p.h @@ -33,34 +33,31 @@ #include "qchartglobal.h" #include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE class ChartAnimator; class ChartPresenter; class ChartAnimation; +class ChartThemeManager; class Domain; -class ChartElement: public QObject +class ChartElement: public QGraphicsObject { - Q_OBJECT + public: - explicit ChartElement(ChartPresenter *presenter); + explicit ChartElement(QGraphicsItem* item = 0); -public Q_SLOTS: - virtual void handleGeometryChanged(const QRectF &rect); - virtual void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); - virtual void handleDomainUpdated(); virtual ChartAnimation *animation() const { return 0; } - - void setPresenter(ChartPresenter *presenter); + virtual void setPresenter(ChartPresenter *presenter); ChartPresenter *presenter() const; - void setDomain(Domain *domain); - Domain *domain() const; + virtual void setThemeManager(ChartThemeManager *manager); + ChartThemeManager* themeManager() const; private: ChartPresenter *m_presenter; - Domain *m_domain; + ChartThemeManager *m_themeManager; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartitem.cpp b/src/chartitem.cpp new file mode 100644 index 0000000..650b0d6 --- /dev/null +++ b/src/chartitem.cpp @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2012 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 Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial 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 +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "chartitem_p.h" +#include "qabstractseries_p.h" +#include "domain_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +ChartItem::ChartItem(QAbstractSeriesPrivate *series,QGraphicsItem* item): + ChartElement(item), + m_series(series) +{ + +} + +QSharedPointer ChartItem::domain() const +{ + return m_series->domain(); +} + +void ChartItem::handleDomainUpdated() +{ + qWarning() << __FUNCTION__<< "Slot not implemented"; +} + +#include "moc_chartitem_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartitem_p.h b/src/chartitem_p.h index fcdb6b4..edcaa4d 100644 --- a/src/chartitem_p.h +++ b/src/chartitem_p.h @@ -36,11 +36,17 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -class ChartItem : public QGraphicsItem, public ChartElement +class ChartItem : public ChartElement { + Q_OBJECT enum ChartItemTypes { AXIS_ITEM = UserType + 1, XYLINE_ITEM }; public: - ChartItem(ChartPresenter *presenter) : QGraphicsItem(presenter ? presenter->rootItem() : 0), ChartElement(presenter) {} + ChartItem(QAbstractSeriesPrivate *series,QGraphicsItem* item); + QSharedPointer domain() const; +public Q_SLOTS: + virtual void handleDomainUpdated(); +private: + QAbstractSeriesPrivate* m_series; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartlayout.cpp b/src/chartlayout.cpp index 22fd318..a326f45 100644 --- a/src/chartlayout.cpp +++ b/src/chartlayout.cpp @@ -49,7 +49,6 @@ void ChartLayout::setGeometry(const QRectF &rect) return; QList axes = m_presenter->axisItems(); - QList charts = m_presenter->chartItems(); ChartTitle *title = m_presenter->titleElement(); QLegend *legend = m_presenter->legend(); ChartBackground *background = m_presenter->backgroundElement(); @@ -66,7 +65,7 @@ void ChartLayout::setGeometry(const QRectF &rect) contentGeometry = calculateAxisGeometry(contentGeometry, axes); - m_chartsRect = calculateChartsGeometry(contentGeometry, charts); + m_presenter->setGeometry(contentGeometry); QGraphicsLayout::setGeometry(rect); } @@ -120,6 +119,7 @@ QRectF ChartLayout::calculateAxisGeometry(const QRectF &geometry, const QListisVisible()) continue; + QSizeF size = axis->effectiveSizeHint(Qt::PreferredSize); //this is used to get single thick font size QSizeF minSize = axis->effectiveSizeHint(Qt::MinimumSize); @@ -153,6 +153,9 @@ QRectF ChartLayout::calculateAxisGeometry(const QRectF &geometry, const QList(axisElement); if (!axis->isVisible()) continue; @@ -217,7 +223,7 @@ QRectF ChartLayout::calculateAxisMinimum(const QRectF &minimum, const QListeffectiveSizeHint(Qt::MinimumSize); @@ -287,15 +293,6 @@ QRectF ChartLayout::calculateLegendGeometry(const QRectF &geometry, QLegend *leg return result; } - -QRectF ChartLayout::calculateChartsGeometry(const QRectF &geometry, const QList& charts) const -{ - Q_ASSERT(geometry.isValid()); - foreach (ChartElement *chart, charts) - chart->handleGeometryChanged(geometry); - return geometry; -} - QRectF ChartLayout::calculateLegendMinimum(const QRectF &geometry, QLegend *legend) const { QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize, QSizeF(-1, -1)); diff --git a/src/chartlayout_p.h b/src/chartlayout_p.h index fa126ef..71466dc 100644 --- a/src/chartlayout_p.h +++ b/src/chartlayout_p.h @@ -31,7 +31,6 @@ class ChartTitle; class QLegend; class ChartAxis; class ChartBackground; -class ChartElement; class ChartLayout : public QGraphicsLayout { @@ -44,7 +43,6 @@ public: QMargins margins() const; void setGeometry(const QRectF &rect); - QRectF chartsGeometry() const { return m_chartsRect;} protected: QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; @@ -56,10 +54,8 @@ private: QRectF calculateBackgroundGeometry(const QRectF &geometry, ChartBackground *background) const; QRectF calculateContentGeometry(const QRectF &geometry) const; QRectF calculateTitleGeometry(const QRectF &geometry, ChartTitle *title) const; - QRectF calculateChartGeometry(const QRectF &geometry, const QList& axes) const; QRectF calculateLegendGeometry(const QRectF &geometry, QLegend *legend) const; QRectF calculateAxisGeometry(const QRectF &geometry, const QList& axes) const; - QRectF calculateChartsGeometry(const QRectF &geometry, const QList& charts) const; QRectF calculateBackgroundMinimum(const QRectF &minimum) const; QRectF calculateContentMinimum(const QRectF &minimum) const; QRectF calculateTitleMinimum(const QRectF &minimum, ChartTitle *title) const; @@ -71,7 +67,6 @@ private: QMargins m_margins; QRectF m_minChartRect; QRectF m_minAxisRect; - QRectF m_chartsRect; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index db9a014..1f16599 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -19,11 +19,11 @@ ****************************************************************************/ #include "chartpresenter_p.h" #include "qchart.h" +#include "chartitem_p.h" #include "qchart_p.h" #include "qabstractaxis.h" #include "qabstractaxis_p.h" #include "chartdataset_p.h" -#include "charttheme_p.h" #include "chartanimation_p.h" #include "qabstractseries_p.h" #include "qareaseries.h" @@ -35,11 +35,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartPresenter::ChartPresenter(QChart *chart, ChartDataSet *dataset) +ChartPresenter::ChartPresenter(QChart *chart) : QObject(chart), m_chart(chart), - m_dataset(dataset), - m_chartTheme(0), m_options(QChart::NoAnimation), m_state(ShowState), m_layout(new ChartLayout(this)), @@ -51,164 +49,97 @@ ChartPresenter::ChartPresenter(QChart *chart, ChartDataSet *dataset) ChartPresenter::~ChartPresenter() { - delete m_chartTheme; + } -void ChartPresenter::handleAxisAdded(QAbstractAxis *axis, Domain *domain) +void ChartPresenter::setGeometry(const QRectF rect) { - ChartAxis *item = axis->d_ptr->createGraphics(this); - item->setDomain(domain); - - if (m_options.testFlag(QChart::GridAxisAnimations)) - item->setAnimation(new AxisAnimation(item)); - - QObject::connect(domain, SIGNAL(updated()), item, SLOT(handleDomainUpdated())); + if(m_rect != rect) { + m_rect=rect; + foreach (ChartItem *chart, m_chartItems){ + chart->domain()->setSize(rect.size()); + chart->setPos(rect.topLeft()); + } + } +} - //initialize - domain->emitUpdated(); - m_chartTheme->decorate(axis); - axis->d_ptr->setDirty(false); - axis->d_ptr->emitUpdated(); +QRectF ChartPresenter::geometry() const +{ + return m_rect; +} - m_axisItems.insert(axis, item); +void ChartPresenter::handleAxisAdded(QAbstractAxis *axis) +{ + qDebug()<<__FUNCTION__; + axis->d_ptr->initializeGraphics(rootItem()); + axis->d_ptr->initializeAnimations(m_options); + ChartAxis *item = axis->d_ptr->axisItem(); + item->setPresenter(this); + item->setThemeManager(m_chart->d_ptr->m_themeManager); + m_axisItems<invalidate(); } void ChartPresenter::handleAxisRemoved(QAbstractAxis *axis) { - ChartAxis *item = m_axisItems.take(axis); - Q_ASSERT(item); + qDebug()<<__FUNCTION__; + ChartAxis *item = axis->d_ptr->m_item.take(); item->hide(); item->disconnect(); - QObject::disconnect(this, 0, item, 0); item->deleteLater(); + m_axisItems.removeAll(item); + m_axes.removeAll(axis); m_layout->invalidate(); } -void ChartPresenter::handleSeriesAdded(QAbstractSeries *series, Domain *domain) +void ChartPresenter::handleSeriesAdded(QAbstractSeries *series) { - ChartElement *item = series->d_ptr->createGraphics(this); - Q_ASSERT(item); - item->setDomain(domain); - - QObject::connect(domain, SIGNAL(updated()), item, SLOT(handleDomainUpdated())); - //initialize - item->handleDomainUpdated(); - - m_chartItems.insert(series, item); + qDebug()<<__FUNCTION__; + series->d_ptr->initializeGraphics(rootItem()); + series->d_ptr->initializeAnimations(m_options); + ChartItem *chart = series->d_ptr->chartItem(); + chart->setPresenter(this); + chart->setThemeManager(m_chart->d_ptr->m_themeManager); + chart->domain()->setSize(m_rect.size()); + chart->setPos(m_rect.topLeft()); + chart->handleDomainUpdated(); //this could be moved to intializeGraphics when animator is refactored + m_chartItems<invalidate(); } void ChartPresenter::handleSeriesRemoved(QAbstractSeries *series) { - ChartElement *item = m_chartItems.take(series); - Q_ASSERT(item); - item->deleteLater(); -} - -void ChartPresenter::setTheme(QChart::ChartTheme theme, bool force) -{ - if (m_chartTheme && m_chartTheme->id() == theme) - return; - delete m_chartTheme; - m_chartTheme = ChartTheme::createTheme(theme); - m_chartTheme->setForced(force); - m_chartTheme->decorate(m_chart); - m_chartTheme->decorate(m_chart->legend()); - resetAllElements(); - - // We do not want "force" to stay on. - // Bar/pie are calling decorate when adding/removing slices/bars which means - // that to preserve users colors "force" must not be on. - m_chartTheme->setForced(false); -} - -QChart::ChartTheme ChartPresenter::theme() -{ - return m_chartTheme->id(); + qDebug()<<__FUNCTION__; + ChartItem *chart = series->d_ptr->m_item.take(); + chart->hide(); + chart->disconnect(); + chart->deleteLater(); + m_chartItems.removeAll(chart); + m_series.removeAll(series); + m_layout->invalidate(); } void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) { if (m_options != options) { m_options = options; - resetAllElements(); - } -} - -void ChartPresenter::resetAllElements() -{ - QMapIterator i(m_axisItems); - while (i.hasNext()) { - i.next(); - Domain *domain = i.value()->domain(); - QAbstractAxis *axis = i.key(); - handleAxisRemoved(axis); - handleAxisAdded(axis, domain); - } - - QMapIterator j(m_chartItems); - while (j.hasNext()) { - j.next(); - Domain *domain = j.value()->domain(); - QAbstractSeries *series = j.key(); - handleSeriesRemoved(series); - handleSeriesAdded(series, domain); + foreach(QAbstractSeries* series, m_series){ + series->d_ptr->initializeAnimations(m_options); + } + foreach(QAbstractAxis* axis, m_axes){ + axis->d_ptr->initializeAnimations(m_options); + } } - - layout()->invalidate(); } -void ChartPresenter::zoomIn(qreal factor) +void ChartPresenter::setState(State state,QPointF point) { - QRectF rect = m_layout->chartsGeometry(); - rect.setWidth(rect.width() / factor); - rect.setHeight(rect.height() / factor); - rect.moveCenter(m_layout->chartsGeometry().center()); - zoomIn(rect); -} - -void ChartPresenter::zoomIn(const QRectF &rect) -{ - QRectF r = rect.normalized(); - r.translate(-m_layout->chartsGeometry().topLeft()); - if (!r.isValid()) - return; - - m_state = ZoomInState; - m_statePoint = QPointF(r.center().x() / m_layout->chartsGeometry().width(), r.center().y() / m_layout->chartsGeometry().height()); - m_dataset->zoomInDomain(r, m_layout->chartsGeometry().size()); - m_state = ShowState; -} - -void ChartPresenter::zoomOut(qreal factor) -{ - m_state = ZoomOutState; - - QRectF chartRect; - chartRect.setSize(m_layout->chartsGeometry().size()); - - QRectF rect; - rect.setSize(chartRect.size() / factor); - rect.moveCenter(chartRect.center()); - if (!rect.isValid()) - return; - m_statePoint = QPointF(rect.center().x() / m_layout->chartsGeometry().width(), rect.center().y() / m_layout->chartsGeometry().height()); - m_dataset->zoomOutDomain(rect, chartRect.size()); - m_state = ShowState; -} - -void ChartPresenter::scroll(qreal dx, qreal dy) -{ - if (dx < 0) m_state = ScrollLeftState; - if (dx > 0) m_state = ScrollRightState; - if (dy < 0) m_state = ScrollUpState; - if (dy > 0) m_state = ScrollDownState; - - m_dataset->scrollDomain(dx, dy, m_layout->chartsGeometry().size()); - m_state = ShowState; + m_state=state; + m_statePoint=point; } QChart::AnimationOptions ChartPresenter::animationOptions() const @@ -370,12 +301,12 @@ ChartBackground *ChartPresenter::backgroundElement() QList ChartPresenter::axisItems() const { - return m_axisItems.values(); + return m_axisItems; } -QList ChartPresenter::chartItems() const +QList ChartPresenter::chartItems() const { - return m_chartItems.values(); + return m_chartItems; } ChartTitle *ChartPresenter::titleElement() diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 7383b36..4000422 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -37,12 +37,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -class ChartElement; +class ChartItem; +class AxisItem; class QAbstractSeries; class ChartDataSet; class Domain; class ChartAxis; -class ChartTheme; class ChartAnimator; class ChartBackground; class ChartTitle; @@ -78,16 +78,21 @@ public: ZoomOutState }; - ChartPresenter(QChart *chart, ChartDataSet *dataset); + ChartPresenter(QChart *chart); virtual ~ChartPresenter(); - ChartTheme *chartTheme() const { return m_chartTheme; } - ChartDataSet *dataSet() const { return m_dataset; } - QGraphicsItem *rootItem() const { return m_chart; } + + void setGeometry(QRectF rect); + QRectF geometry() const; + + QGraphicsItem *rootItem(){ return m_chart; } ChartBackground *backgroundElement(); ChartTitle *titleElement(); QList axisItems() const; - QList chartItems() const; + QList chartItems() const; + + ChartItem* chartElement(QAbstractSeries* series) const; + ChartAxis* chartElement(QAbstractAxis* axis) const; QLegend *legend(); @@ -114,23 +119,15 @@ public: void setVisible(bool visible); - void setTheme(QChart::ChartTheme theme, bool force = true); - QChart::ChartTheme theme(); - void setAnimationOptions(QChart::AnimationOptions options); QChart::AnimationOptions animationOptions() const; - void zoomIn(qreal factor); - void zoomIn(const QRectF &rect); - void zoomOut(qreal factor); - void scroll(qreal dx, qreal dy); - void startAnimation(ChartAnimation *animation); + + //TODO refactor + void setState(State state,QPointF point); State state() const { return m_state; } QPointF statePoint() const { return m_statePoint; } - - void resetAllElements(); - ChartLayout *layout(); private: @@ -138,9 +135,9 @@ private: void createTitleItem(); public Q_SLOTS: - void handleSeriesAdded(QAbstractSeries *series, Domain *domain); + void handleSeriesAdded(QAbstractSeries *series); void handleSeriesRemoved(QAbstractSeries *series); - void handleAxisAdded(QAbstractAxis *axis, Domain *domain); + void handleAxisAdded(QAbstractAxis *axis); void handleAxisRemoved(QAbstractAxis *axis); private Q_SLOTS: @@ -151,10 +148,10 @@ Q_SIGNALS: private: QChart *m_chart; - ChartDataSet *m_dataset; - ChartTheme *m_chartTheme; - QMap m_chartItems; - QMap m_axisItems; + QList m_chartItems; + QList m_axisItems; + QList m_series; + QList m_axes; QChart::AnimationOptions m_options; State m_state; QPointF m_statePoint; @@ -162,6 +159,7 @@ private: ChartLayout *m_layout; ChartBackground *m_background; ChartTitle *m_title; + QRectF m_rect; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/charttheme.cpp b/src/charttheme.cpp deleted file mode 100644 index abb95b2..0000000 --- a/src/charttheme.cpp +++ /dev/null @@ -1,399 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 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 Qt Commercial Charts Add-on. -** -** $QT_BEGIN_LICENSE$ -** Licensees holding valid Qt Commercial licenses may use this file in -** accordance with the Qt Commercial 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 -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "charttheme_p.h" -#include "qchart.h" -#include "qchart_p.h" -#include "qchartview.h" -#include "qlegend.h" -#include "qabstractaxis.h" -#include - -//series -#include "qbarset.h" -#include "qabstractbarseries.h" -#include "qstackedbarseries.h" -#include "qpercentbarseries.h" -#include "qlineseries.h" -#include "qareaseries.h" -#include "qscatterseries.h" -#include "qpieseries.h" -#include "qpieslice.h" -#include "qpieslice_p.h" -#include "qsplineseries.h" - -//items -#include "chartaxis_p.h" -#include "abstractbarchartitem_p.h" -#include "stackedbarchartitem_p.h" -#include "percentbarchartitem_p.h" -#include "linechartitem_p.h" -#include "areachartitem_p.h" -#include "scatterchartitem_p.h" -#include "piechartitem_p.h" -#include "splinechartitem_p.h" - -//themes -#include "chartthemesystem_p.h" -#include "chartthemelight_p.h" -#include "chartthemebluecerulean_p.h" -#include "chartthemedark_p.h" -#include "chartthemebrownsand_p.h" -#include "chartthemebluencs_p.h" -#include "chartthemehighcontrast_p.h" -#include "chartthemeblueicy_p.h" - -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -ChartTheme::ChartTheme(QChart::ChartTheme id) : - m_id(id), - m_masterFont(QFont("arial", 14)), - m_labelFont(QFont("arial", 10)), - m_labelBrush(QColor(QRgb(0x000000))), - m_axisLinePen(QPen(QRgb(0x000000))), - m_backgroundShadesPen(Qt::NoPen), - m_backgroundShadesBrush(Qt::NoBrush), - m_backgroundShades(BackgroundShadesNone), - m_backgroundDropShadowEnabled(false), - m_gridLinePen(QPen(QRgb(0x000000))), - m_force(false) -{ - qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); -} - - -ChartTheme *ChartTheme::createTheme(QChart::ChartTheme theme) -{ - switch (theme) { - case QChart::ChartThemeLight: - return new ChartThemeLight(); - case QChart::ChartThemeBlueCerulean: - return new ChartThemeBlueCerulean(); - case QChart::ChartThemeDark: - return new ChartThemeDark(); - case QChart::ChartThemeBrownSand: - return new ChartThemeBrownSand(); - case QChart::ChartThemeBlueNcs: - return new ChartThemeBlueNcs(); - case QChart::ChartThemeHighContrast: - return new ChartThemeHighContrast(); - case QChart::ChartThemeBlueIcy: - return new ChartThemeBlueIcy(); - default: - return new ChartThemeSystem(); - } -} - -void ChartTheme::decorate(QChart *chart) -{ - QBrush brush; - - if (m_force || brush == chart->backgroundBrush()) - chart->setBackgroundBrush(m_chartBackgroundGradient); - chart->setTitleFont(m_masterFont); - chart->setTitleBrush(m_labelBrush); - chart->setDropShadowEnabled(m_backgroundDropShadowEnabled); -} - -void ChartTheme::decorate(QLegend *legend) -{ - QPen pen; - QBrush brush; - QFont font; - - if (m_force || pen == legend->pen()) - legend->setPen(m_axisLinePen); - - if (m_force || brush == legend->brush()) - legend->setBrush(m_chartBackgroundGradient); - - if (m_force || font == legend->font()) - legend->setFont(m_labelFont); - - if (m_force || brush == legend->labelBrush()) - legend->setLabelBrush(m_labelBrush); -} - -void ChartTheme::decorate(QAreaSeries *series, int index) -{ - QPen pen; - QBrush brush; - - if (m_force || pen == series->pen()) { - pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); - pen.setWidthF(2); - series->setPen(pen); - } - - if (m_force || brush == series->brush()) { - QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); - series->setBrush(brush); - } -} - - -void ChartTheme::decorate(QLineSeries *series, int index) -{ - QPen pen; - if (m_force || pen == series->pen()) { - pen.setColor(m_seriesColors.at(index % m_seriesColors.size())); - pen.setWidthF(2); - series->setPen(pen); - } -} - -void ChartTheme::decorate(QAbstractBarSeries *series, int index) -{ - QBrush brush; - QPen pen; - QList sets = series->barSets(); - - qreal takeAtPos = 0.5; - qreal step = 0.2; - if (sets.count() > 1) { - step = 1.0 / (qreal) sets.count(); - if (sets.count() % m_seriesGradients.count()) - step *= m_seriesGradients.count(); - else - step *= (m_seriesGradients.count() - 1); - } - - for (int i(0); i < sets.count(); i++) { - int colorIndex = (index + i) % m_seriesGradients.count(); - if (i > 0 && i % m_seriesGradients.count() == 0) { - // There is no dedicated base color for each sets, generate more colors - takeAtPos += step; - if (takeAtPos == 1.0) - takeAtPos += step; - takeAtPos -= (int) takeAtPos; - } - if (m_force || brush == sets.at(i)->brush()) - sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos)); - - // Pick label color from the opposite end of the gradient. - // 0.3 as a boundary seems to work well. - if (m_force || brush == sets.at(i)->labelBrush()) { - if (takeAtPos < 0.3) - sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1)); - else - sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0)); - } - - if (m_force || pen == sets.at(i)->pen()) { - QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); - sets.at(i)->setPen(c); - } - } -} - -void ChartTheme::decorate(QScatterSeries *series, int index) -{ - QPen pen; - QBrush brush; - - if (m_force || pen == series->pen()) { - pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); - pen.setWidthF(2); - series->setPen(pen); - } - - if (m_force || brush == series->brush()) { - QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); - series->setBrush(brush); - } -} - -void ChartTheme::decorate(QPieSeries *series, int index) -{ - - for (int i(0); i < series->slices().count(); i++) { - - QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); - - // Get color for a slice from a gradient linearly, beginning from the start of the gradient - qreal pos = (qreal)(i + 1) / (qreal) series->count(); - QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); - - QPieSlice *s = series->slices().at(i); - QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s); - - if (m_force || d->m_data.m_slicePen.isThemed()) - d->setPen(penColor, true); - - if (m_force || d->m_data.m_sliceBrush.isThemed()) - d->setBrush(brushColor, true); - - if (m_force || d->m_data.m_labelBrush.isThemed()) - d->setLabelBrush(m_labelBrush.color(), true); - - if (m_force || d->m_data.m_labelFont.isThemed()) - d->setLabelFont(m_labelFont, true); - } -} - -void ChartTheme::decorate(QSplineSeries *series, int index) -{ - QPen pen; - if (m_force || pen == series->pen()) { - pen.setColor(m_seriesColors.at(index % m_seriesColors.size())); - pen.setWidthF(2); - series->setPen(pen); - } -} - -void ChartTheme::decorate(QAbstractAxis *axis) -{ - QPen pen; - QBrush brush; - QFont font; - - bool axisX = axis->orientation() == Qt::Horizontal; - - if (axis->isLineVisible()) { - - if (m_force || brush == axis->labelsBrush()) - axis->setLabelsBrush(m_labelBrush); - - //TODO: introduce axis brush - if (m_force || brush == axis->titleBrush()) - axis->setTitleBrush(m_labelBrush); - - if (m_force || pen == axis->labelsPen()) - axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons - - if (m_force || pen == axis->titlePen()) - axis->setTitlePen(Qt::NoPen); // Noen for performance reasons - - if (m_force || axis->shadesVisible()) { - - if (m_force || brush == axis->shadesBrush()) - axis->setShadesBrush(m_backgroundShadesBrush); - - if (m_force || pen == axis->shadesPen()) - axis->setShadesPen(m_backgroundShadesPen); - - if (m_force && (m_backgroundShades == BackgroundShadesBoth - || (m_backgroundShades == BackgroundShadesVertical && axisX) - || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))) { - axis->setShadesVisible(true); - } - } - - if (m_force || pen == axis->linePen()) - axis->setLinePen(m_axisLinePen); - - if (m_force || pen == axis->gridLinePen()) - axis->setGridLinePen(m_gridLinePen); - - if (m_force || font == axis->labelsFont()) - axis->setLabelsFont(m_labelFont); - - //TODO: discuss with Tero - if (m_force || font == axis->titleFont()){ - QFont font(m_labelFont); - font.setBold(true); - axis->setTitleFont(font); - } - } -} - -void ChartTheme::generateSeriesGradients() -{ - // Generate gradients in HSV color space - foreach (const QColor &color, m_seriesColors) { - QLinearGradient g; - qreal h = color.hsvHueF(); - qreal s = color.hsvSaturationF(); - - // TODO: tune the algorithm to give nice results with most base colors defined in - // most themes. The rest of the gradients we can define manually in theme specific - // implementation. - QColor start = color; - start.setHsvF(h, 0.0, 1.0); - g.setColorAt(0.0, start); - - g.setColorAt(0.5, color); - - QColor end = color; - end.setHsvF(h, s, 0.25); - g.setColorAt(1.0, end); - - m_seriesGradients << g; - } -} - - -QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) -{ - Q_ASSERT(pos >= 0.0 && pos <= 1.0); - qreal r = start.redF() + ((end.redF() - start.redF()) * pos); - qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); - qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); - QColor c; - c.setRgbF(r, g, b); - return c; -} - -QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos) -{ - Q_ASSERT(pos >= 0 && pos <= 1.0); - - QGradientStops stops = gradient.stops(); - int count = stops.count(); - - // find previous stop relative to position - QGradientStop prev = stops.first(); - for (int i = 0; i < count; i++) { - QGradientStop stop = stops.at(i); - if (pos > stop.first) - prev = stop; - - // given position is actually a stop position? - if (pos == stop.first) { - //qDebug() << "stop color" << pos; - return stop.second; - } - } - - // find next stop relative to position - QGradientStop next = stops.last(); - for (int i = count - 1; i >= 0; i--) { - QGradientStop stop = stops.at(i); - if (pos < stop.first) - next = stop; - } - - //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; - - qreal range = next.first - prev.first; - qreal posDelta = pos - prev.first; - qreal relativePos = posDelta / range; - - //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; - - return colorAt(prev.second, next.second, relativePos); -} - -void ChartTheme::setForced(bool enabled) -{ - m_force = enabled; -} - -QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartthememanager.cpp b/src/chartthememanager.cpp new file mode 100644 index 0000000..989aecc --- /dev/null +++ b/src/chartthememanager.cpp @@ -0,0 +1,259 @@ +/**************************************************************************** +** +** Copyright (C) 2012 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 Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial 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 +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qabstractseries_p.h" +#include "qabstractaxis_p.h" +#include +//themes +#include "chartthemesystem_p.h" +#include "chartthemelight_p.h" +#include "chartthemebluecerulean_p.h" +#include "chartthemedark_p.h" +#include "chartthemebrownsand_p.h" +#include "chartthemebluencs_p.h" +#include "chartthemehighcontrast_p.h" +#include "chartthemeblueicy_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +ChartThemeManager::ChartThemeManager(QChart* chart) : + m_chart(chart) +{ + qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); +} + + +void ChartThemeManager::setTheme(QChart::ChartTheme theme) +{ + if(m_theme.isNull() || theme != m_theme->id()) + { + switch (theme) { + case QChart::ChartThemeLight: + m_theme.reset(new ChartThemeLight()); + break; + case QChart::ChartThemeBlueCerulean: + m_theme.reset(new ChartThemeBlueCerulean()); + break; + case QChart::ChartThemeDark: + m_theme.reset(new ChartThemeDark()); + break; + case QChart::ChartThemeBrownSand: + m_theme.reset(new ChartThemeBrownSand()); + break; + case QChart::ChartThemeBlueNcs: + m_theme.reset(new ChartThemeBlueNcs()); + break; + case QChart::ChartThemeHighContrast: + m_theme.reset(new ChartThemeHighContrast()); + break; + case QChart::ChartThemeBlueIcy: + m_theme.reset(new ChartThemeBlueIcy()); + break; + default: + m_theme.reset(new ChartThemeSystem()); + break; + } + + if(!m_theme.isNull()) + { + decorateChart(m_chart,m_theme.data(),true); + decorateLegend(m_chart->legend(),m_theme.data(),true); + foreach(QAbstractAxis* axis, m_axisList) { + axis->d_ptr->initializeTheme(m_theme.data(),true); + } + foreach(QAbstractSeries* series, m_seriesMap.keys()) { + series->d_ptr->initializeTheme(m_seriesMap[series],m_theme.data(),true); + } + + } + } +} + +void ChartThemeManager::decorateChart(QChart *chart,ChartTheme* theme,bool force) const +{ + QBrush brush; + + if (force || brush == chart->backgroundBrush()) + chart->setBackgroundBrush(theme->chartBackgroundGradient()); + + chart->setTitleFont(theme->masterFont()); + chart->setTitleBrush(theme->labelBrush()); + chart->setDropShadowEnabled(theme->isBackgroundDropShadowEnabled()); +} + +void ChartThemeManager::decorateLegend(QLegend *legend, ChartTheme* theme, bool force) const +{ + QPen pen; + QBrush brush; + QFont font; + + if (force || pen == legend->pen()) + legend->setPen(theme->axisLinePen()); + + if (force || brush == legend->brush()) + legend->setBrush(theme->chartBackgroundGradient()); + + if (force || font == legend->font()) + legend->setFont(theme->labelFont()); + + if (force || brush == legend->labelBrush()) + legend->setLabelBrush(theme->labelBrush()); +} + +int ChartThemeManager::createIndexKey(QList keys) const +{ + qSort(keys); + + int key = 0; + QList::iterator i; + i = keys.begin(); + + while (i != keys.end()) { + if (*i != key) + break; + key++; + i++; + } + + return key; +} + +int ChartThemeManager::seriesCount(QAbstractSeries::SeriesType type) const +{ + int count = 0; + QList series = m_seriesMap.keys(); + foreach(QAbstractSeries *s, series) { + if (s->type() == type) + count++; + } + return count; +} + +void ChartThemeManager::handleSeriesAdded(QAbstractSeries *series) +{ + int key = createIndexKey(m_seriesMap.values()); + m_seriesMap.insert(series,key); + series->d_ptr->initializeTheme(key,m_theme.data(),false); +} + +void ChartThemeManager::handleSeriesRemoved(QAbstractSeries *series) +{ + m_seriesMap.remove(series); +} + +void ChartThemeManager::handleAxisAdded(QAbstractAxis *axis) +{ + m_axisList.append(axis); + axis->d_ptr->initializeTheme(m_theme.data(),false); +} + +void ChartThemeManager::handleAxisRemoved(QAbstractAxis *axis) +{ + m_axisList.removeAll(axis); +} + +void ChartThemeManager::updateSeries(QAbstractSeries *series) +{ + if(m_seriesMap.contains(series)){ + series->d_ptr->initializeTheme(m_seriesMap[series],m_theme.data(),false); + } +} +QList ChartThemeManager::generateSeriesGradients(const QList& colors) +{ + QList result; + // Generate gradients in HSV color space + foreach (const QColor &color, colors) { + QLinearGradient g; + qreal h = color.hsvHueF(); + qreal s = color.hsvSaturationF(); + + // TODO: tune the algorithm to give nice results with most base colors defined in + // most themes. The rest of the gradients we can define manually in theme specific + // implementation. + QColor start = color; + start.setHsvF(h, 0.0, 1.0); + g.setColorAt(0.0, start); + + g.setColorAt(0.5, color); + + QColor end = color; + end.setHsvF(h, s, 0.25); + g.setColorAt(1.0, end); + + result << g; + } + + return result; +} + + +QColor ChartThemeManager::colorAt(const QColor &start, const QColor &end, qreal pos) +{ + Q_ASSERT(pos >= 0.0 && pos <= 1.0); + qreal r = start.redF() + ((end.redF() - start.redF()) * pos); + qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); + qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); + QColor c; + c.setRgbF(r, g, b); + return c; +} + +QColor ChartThemeManager::colorAt(const QGradient &gradient, qreal pos) +{ + Q_ASSERT(pos >= 0 && pos <= 1.0); + + QGradientStops stops = gradient.stops(); + int count = stops.count(); + + // find previous stop relative to position + QGradientStop prev = stops.first(); + for (int i = 0; i < count; i++) { + QGradientStop stop = stops.at(i); + if (pos > stop.first) + prev = stop; + + // given position is actually a stop position? + if (pos == stop.first) { + //qDebug() << "stop color" << pos; + return stop.second; + } + } + + // find next stop relative to position + QGradientStop next = stops.last(); + for (int i = count - 1; i >= 0; i--) { + QGradientStop stop = stops.at(i); + if (pos < stop.first) + next = stop; + } + + //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; + + qreal range = next.first - prev.first; + qreal posDelta = pos - prev.first; + qreal relativePos = posDelta / range; + + //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; + + return colorAt(prev.second, next.second, relativePos); +} + +#include "moc_chartthememanager_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartthememanager_p.h b/src/chartthememanager_p.h new file mode 100644 index 0000000..eefc09d --- /dev/null +++ b/src/chartthememanager_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2012 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 Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial 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 +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial Chart API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef CHARTTHEMEMANAGER_H +#define CHARTTHEMEMANAGER_H + +#include "qchartglobal.h" +#include "qchart.h" +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +class ChartTheme; + +class ChartThemeManager: public QObject +{ + Q_OBJECT +public: + enum BackgroundShadesMode { + BackgroundShadesNone = 0, + BackgroundShadesVertical, + BackgroundShadesHorizontal, + BackgroundShadesBoth + }; + +public: + explicit ChartThemeManager(QChart* chart); + void setTheme(QChart::ChartTheme theme); + ChartTheme* theme() const { return m_theme.data(); } + void decorateChart(QChart *chart, ChartTheme* theme, bool force = false) const; + void decorateLegend(QLegend *legend, ChartTheme* theme, bool force = false) const; + void updateSeries(QAbstractSeries *series); + +public: + static QList generateSeriesGradients(const QList& colors); + static QColor colorAt(const QColor &start, const QColor &end, qreal pos); + static QColor colorAt(const QGradient &gradient, qreal pos); + +private: + int createIndexKey(QList keys) const; + int seriesCount(QAbstractSeries::SeriesType type) const; + +public Q_SLOTS: + void handleSeriesAdded(QAbstractSeries *series); + void handleSeriesRemoved(QAbstractSeries *series); + void handleAxisAdded(QAbstractAxis *axis); + void handleAxisRemoved(QAbstractAxis *axis); + +protected: + QScopedPointer m_theme; + QMap m_seriesMap; + QList m_axisList; + QChart* m_chart; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // CHARTTHEME_H diff --git a/src/domain.cpp b/src/domain.cpp index 7847905..b13d3bd 100644 --- a/src/domain.cpp +++ b/src/domain.cpp @@ -20,6 +20,7 @@ #include "domain_p.h" #include "qabstractaxis_p.h" +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -28,7 +29,8 @@ Domain::Domain(QObject *parent) m_minX(0), m_maxX(0), m_minY(0), - m_maxY(0) + m_maxY(0), + m_axisSignalsBlocked(false) { } @@ -36,6 +38,20 @@ Domain::~Domain() { } +void Domain::setSize(const QSizeF& size) +{ + if(m_size!=size) + { + m_size=size; + emit updated(); + } +} + +QSizeF Domain::size() const +{ + return m_size; +} + void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) { bool axisXChanged = false; @@ -45,14 +61,14 @@ void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) m_minX = minX; m_maxX = maxX; axisXChanged = true; - emit rangeXChanged(m_minX, m_maxX); + emit rangeHorizontalChanged(m_minX, m_maxX); } if (!qFuzzyCompare(m_minY, minY) || !qFuzzyCompare(m_maxY, maxY)) { m_minY = minY; m_maxY = maxY; axisYChanged = true; - emit rangeYChanged(m_minY, m_maxY); + emit rangeVerticalChanged(m_minY, m_maxY); } if (axisXChanged || axisYChanged) @@ -103,13 +119,13 @@ qreal Domain::spanY() const bool Domain::isEmpty() const { - return qFuzzyCompare(spanX(), 0) || qFuzzyCompare(spanY(), 0); + return qFuzzyCompare(spanX(),0) || qFuzzyCompare(spanY(),0) || m_size.isEmpty() ; } -void Domain::zoomIn(const QRectF &rect, const QSizeF &size) +void Domain::zoomIn(const QRectF &rect) { - qreal dx = spanX() / size.width(); - qreal dy = spanY() / size.height(); + qreal dx = spanX() / m_size.width(); + qreal dy = spanY() / m_size.height(); qreal maxX = m_maxX; qreal minX = m_minX; @@ -124,7 +140,7 @@ void Domain::zoomIn(const QRectF &rect, const QSizeF &size) setRange(minX, maxX, minY, maxY); } -void Domain::zoomOut(const QRectF &rect, const QSizeF &size) +void Domain::zoomOut(const QRectF &rect) { qreal dx = spanX() / rect.width(); qreal dy = spanY() / rect.height(); @@ -135,17 +151,17 @@ void Domain::zoomOut(const QRectF &rect, const QSizeF &size) qreal maxY = m_maxY; minX = maxX - dx * rect.right(); - maxX = minX + dx * size.width(); + maxX = minX + dx * m_size.width(); maxY = minY + dy * rect.bottom(); - minY = maxY - dy * size.height(); + minY = maxY - dy * m_size.height(); setRange(minX, maxX, minY, maxY); } -void Domain::move(qreal dx, qreal dy, const QSizeF &size) +void Domain::move(qreal dx, qreal dy) { - qreal x = spanX() / size.width(); - qreal y = spanY() / size.height(); + qreal x = spanX() / m_size.width(); + qreal y = spanY() / m_size.height(); qreal maxX = m_maxX; qreal minX = m_minX; @@ -163,22 +179,97 @@ void Domain::move(qreal dx, qreal dy, const QSizeF &size) setRange(minX, maxX, minY, maxY); } -void Domain::emitUpdated() +QPointF Domain::calculateGeometryPoint(const QPointF &point) const { - emit updated(); + const qreal deltaX = m_size.width() / (m_maxX - m_minX); + const qreal deltaY = m_size.height() / (m_maxY - m_minY); + qreal x = (point.x() - m_minX) * deltaX; + qreal y = (point.y() - m_minY) * -deltaY + m_size.height(); + return QPointF(x, y); } -void Domain::handleAxisUpdated() +QVector Domain::calculateGeometryPoints(const QList& vector) const { - QAbstractAxisPrivate *axis = qobject_cast(sender()); - Q_ASSERT(axis); - axis->setDirty(false); - if (axis->orientation() == Qt::Horizontal) - setRangeX(axis->min(), axis->max()); - else if (axis->orientation() == Qt::Vertical) - setRangeY(axis->min(), axis->max()); + const qreal deltaX = m_size.width() / (m_maxX - m_minX); + const qreal deltaY = m_size.height() / (m_maxY - m_minY); + + QVector result; + result.resize(vector.count()); + + for (int i = 0; i < vector.count(); ++i) { + qreal x = (vector[i].x() - m_minX) * deltaX; + qreal y = (vector[i].y() - m_minY) * -deltaY + m_size.height(); + result[i].setX(x); + result[i].setY(y); + } + return result; } +QPointF Domain::calculateDomainPoint(const QPointF &point) const +{ + const qreal deltaX = m_size.width() / (m_maxX - m_minX); + const qreal deltaY = m_size.height() / (m_maxY - m_minY); + qreal x = point.x() / deltaX + m_minX; + qreal y = (point.y() - m_size.height()) / (-deltaY) + m_minY; + return QPointF(x, y); +} + +// handlers + +void Domain::handleVerticalAxisRangeChanged(qreal min, qreal max) +{ + if(!m_axisSignalsBlocked) + setRangeY(min, max); +} + +void Domain::handleHorizontalAxisRangeChanged(qreal min, qreal max) +{ + if(!m_axisSignalsBlocked) + setRangeX(min, max); +} + +void Domain::blockAxisSignals(bool block) +{ + m_axisSignalsBlocked=block; +} + +//algorithm defined by Paul S.Heckbert GraphicalGems I + +void Domain::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) +{ + qreal range = niceNumber(max - min, true); //range with ceiling + qreal step = niceNumber(range / (ticksCount - 1), false); + min = qFloor(min / step); + max = qCeil(max / step); + ticksCount = int(max - min) + 1; + min *= step; + max *= step; +} + +//nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n + +qreal Domain::niceNumber(qreal x, bool ceiling) +{ + qreal z = qPow(10, qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x + qreal q = x / z; //q<10 && q>=1; + + if (ceiling) { + if (q <= 1.0) q = 1; + else if (q <= 2.0) q = 2; + else if (q <= 5.0) q = 5; + else q = 10; + } else { + if (q < 1.5) q = 1; + else if (q < 3.0) q = 2; + else if (q < 7.0) q = 5; + else q = 10; + } + return q * z; +} + + +// operators + bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const Domain &domain1, const Domain &domain2) { return (qFuzzyCompare(domain1.m_maxX, domain2.m_maxX) && @@ -196,7 +287,7 @@ bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const Domain &domain1, const QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const Domain &domain) { - dbg.nospace() << "Domain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')'; + dbg.nospace() << "Domain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size; return dbg.maybeSpace(); } diff --git a/src/domain_p.h b/src/domain_p.h index e5ff3ef..bc07676 100644 --- a/src/domain_p.h +++ b/src/domain_p.h @@ -43,6 +43,9 @@ public: explicit Domain(QObject *object = 0); virtual ~Domain(); + void setSize(const QSizeF& size); + QSizeF size() const; + void setRange(qreal minX, qreal maxX, qreal minY, qreal maxY); void setRangeX(qreal min, qreal max); void setRangeY(qreal min, qreal max); @@ -60,28 +63,40 @@ public: qreal spanY() const; bool isEmpty() const; + void blockAxisSignals(bool block); + bool axisSignalsBlocked() const { return m_axisSignalsBlocked; } + friend bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const Domain &domain1, const Domain &domain2); friend bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const Domain &domain1, const Domain &domain2); friend QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const Domain &domain); - void zoomIn(const QRectF &rect, const QSizeF &size); - void zoomOut(const QRectF &rect, const QSizeF &size); - void move(qreal dx, qreal dy, const QSizeF &size); - void emitUpdated(); + void zoomIn(const QRectF &rect); + void zoomOut(const QRectF &rect); + void move(qreal dx, qreal dy); + + QPointF calculateGeometryPoint(const QPointF &point) const; + QPointF calculateDomainPoint(const QPointF &point) const; + QVector calculateGeometryPoints(const QList& vector) const; + + static void looseNiceNumbers(qreal &min, qreal &max, int &ticksCount); + static qreal niceNumber(qreal x, bool ceiling); Q_SIGNALS: void updated(); - void rangeXChanged(qreal min, qreal max); - void rangeYChanged(qreal min, qreal max); + void rangeHorizontalChanged(qreal min, qreal max); + void rangeVerticalChanged(qreal min, qreal max); public Q_SLOTS: - void handleAxisUpdated(); + void handleVerticalAxisRangeChanged(qreal min,qreal max); + void handleHorizontalAxisRangeChanged(qreal min,qreal max); private: qreal m_minX; qreal m_maxX; qreal m_minY; qreal m_maxY; + QSizeF m_size; + bool m_axisSignalsBlocked; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index b0e832f..ece8ff6 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -29,6 +29,7 @@ #include "qlegendmarker.h" #include "qlegendmarker_p.h" #include "legendmarkeritem_p.h" +#include "chartdataset_p.h" #include #include #include @@ -161,7 +162,7 @@ QLegend::QLegend(QChart *chart): QGraphicsWidget(chart), { setZValue(ChartPresenter::LegendZValue); setFlags(QGraphicsItem::ItemClipsChildrenToShape); - QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*))); + QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesAdded(QAbstractSeries*))); QObject::connect(chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), d_ptr.data(), SLOT(handleSeriesRemoved(QAbstractSeries*))); setLayout(d_ptr->m_layout); } @@ -249,14 +250,14 @@ QPen QLegend::pen() const void QLegend::setFont(const QFont &font) { - if (d_ptr->m_font != font) + if (d_ptr->m_font != font) { d_ptr->m_font = font; - - foreach (QLegendMarker *marker, d_ptr->markers()) { - marker->setFont(d_ptr->m_font); + foreach (QLegendMarker *marker, d_ptr->markers()) { + marker->setFont(d_ptr->m_font); + } + layout()->invalidate(); + emit fontChanged(font); } - layout()->invalidate(); - emit fontChanged(font); } QFont QLegend::font() const diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index c52df1e..3f9ff89 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -22,6 +22,7 @@ #include "qlineseries.h" #include "qlineseries_p.h" #include "chartpresenter_p.h" +#include "domain_p.h" #include #include @@ -29,9 +30,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE const qreal mouseEventMinWidth(12); -LineChartItem::LineChartItem(QLineSeries *series, ChartPresenter *presenter) - : XYChart(series, presenter), - QGraphicsItem(presenter ? presenter->rootItem() : 0), +LineChartItem::LineChartItem(QLineSeries *series,QGraphicsItem* item) + : XYChart(series,item), m_series(series), m_pointsVisible(false) { @@ -96,8 +96,6 @@ void LineChartItem::updateGeometry() m_path = stroker.createStroke(linePath); m_rect = m_path.boundingRect(); - - setPos(origin()); } void LineChartItem::handleUpdated() @@ -117,7 +115,7 @@ void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt painter->save(); painter->setPen(m_linePen); painter->setBrush(m_linePen.color()); - painter->setClipRect(clipRect()); + painter->setClipRect(QRectF(QPointF(0,0),domain()->size())); if (m_pointsVisible) { painter->drawPath(m_linePath); @@ -130,22 +128,22 @@ void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { - emit XYChart::clicked(calculateDomainPoint(event->pos())); + emit XYChart::clicked(domain()->calculateDomainPoint(event->pos())); QGraphicsItem::mousePressEvent(event); } void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { - emit XYChart::hovered(calculateDomainPoint(event->pos()), true); + emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true); event->accept(); -// QGraphicsItem::hoverEnterEvent(event); + //QGraphicsItem::hoverEnterEvent(event); } void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { - emit XYChart::hovered(calculateDomainPoint(event->pos()), false); + emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false); event->accept(); -// QGraphicsItem::hoverEnterEvent(event); + //QGraphicsItem::hoverEnterEvent(event); } #include "moc_linechartitem_p.cpp" diff --git a/src/linechart/linechartitem_p.h b/src/linechart/linechartitem_p.h index 383ed41..4e7a515 100644 --- a/src/linechart/linechartitem_p.h +++ b/src/linechart/linechartitem_p.h @@ -39,12 +39,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QLineSeries; class ChartPresenter; -class LineChartItem : public XYChart , public QGraphicsItem +class LineChartItem : public XYChart { Q_OBJECT Q_INTERFACES(QGraphicsItem) public: - explicit LineChartItem(QLineSeries *series, ChartPresenter *presenter); + explicit LineChartItem(QLineSeries *series, QGraphicsItem* item = 0); ~LineChartItem() {} //from QGraphicsItem diff --git a/src/linechart/qlineseries.cpp b/src/linechart/qlineseries.cpp index 9ddf52a..437920c 100644 --- a/src/linechart/qlineseries.cpp +++ b/src/linechart/qlineseries.cpp @@ -119,8 +119,8 @@ QLineSeries::QLineSeries(QLineSeriesPrivate &d, QObject *parent) QLineSeries::~QLineSeries() { Q_D(QLineSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } QAbstractSeries::SeriesType QLineSeries::type() const @@ -148,14 +148,25 @@ QLineSeriesPrivate::QLineSeriesPrivate(QLineSeries *q) }; -ChartElement *QLineSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QLineSeriesPrivate::initializeGraphics(QGraphicsItem *parent) { Q_Q(QLineSeries); - LineChartItem *line = new LineChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) - line->setAnimation(new XYAnimation(line)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return line; + LineChartItem *line = new LineChartItem(q,parent); + m_item.reset(line); + QAbstractSeriesPrivate::initializeGraphics(parent); +} + +void QLineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced) +{ + Q_Q(QLineSeries); + const QList colors = theme->seriesColors(); + + QPen pen; + if (forced || pen == m_pen) { + pen.setColor(colors.at(index % colors.size())); + pen.setWidthF(2); + q->setPen(pen); + } } #include "moc_qlineseries.cpp" diff --git a/src/linechart/qlineseries_p.h b/src/linechart/qlineseries_p.h index 8bfdbf8..5c2c638 100644 --- a/src/linechart/qlineseries_p.h +++ b/src/linechart/qlineseries_p.h @@ -39,7 +39,8 @@ class QLineSeriesPrivate: public QXYSeriesPrivate { public: QLineSeriesPrivate(QLineSeries *q); - ChartElement *createGraphics(ChartPresenter *presenter); + void initializeGraphics(QGraphicsItem* parent); + void initializeTheme(int index, ChartTheme* theme, bool forced = false); private: Q_DECLARE_PUBLIC(QLineSeries); diff --git a/src/piechart/piechartitem.cpp b/src/piechart/piechartitem.cpp index c071c49..91b4c80 100644 --- a/src/piechart/piechartitem.cpp +++ b/src/piechart/piechartitem.cpp @@ -32,8 +32,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter *presenter) - : ChartItem(presenter), +PieChartItem::PieChartItem(QPieSeries *series, QGraphicsItem* item) + : ChartItem(series->d_func(),item), m_series(series), m_animation(0) { @@ -70,46 +70,17 @@ ChartAnimation *PieChartItem::animation() const return m_animation; } -void PieChartItem::handleGeometryChanged(const QRectF &rect) -{ - prepareGeometryChange(); - m_rect = rect; - updateLayout(); - - // This is for delayed initialization of the slice items during startup. - // It ensures that startup animation originates from the correct position. - if (m_sliceItems.isEmpty()) - handleSlicesAdded(m_series->slices()); -} - -void PieChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) -{ - Q_UNUSED(minX); - Q_UNUSED(maxX); - Q_UNUSED(minY); - Q_UNUSED(maxY); - // does not apply to pie -} - void PieChartItem::handleDomainUpdated() { - // does not apply to pie -} - -void PieChartItem::rangeXChanged(qreal min, qreal max, int tickXCount) -{ - Q_UNUSED(min); - Q_UNUSED(max); - Q_UNUSED(tickXCount); - // does not apply to pie -} + QRectF rect(QPointF(0,0),domain()->size()); + if(m_rect!=rect){ + prepareGeometryChange(); + m_rect = rect; + updateLayout(); -void PieChartItem::rangeYChanged(qreal min, qreal max, int tickYCount) -{ - Q_UNUSED(min); - Q_UNUSED(max); - Q_UNUSED(tickYCount); - // does not apply to pie + if (m_sliceItems.isEmpty()) + handleSlicesAdded(m_series->slices()); + } } void PieChartItem::updateLayout() @@ -149,7 +120,7 @@ void PieChartItem::handleSlicesAdded(QList slices) if (!m_rect.isValid() && m_sliceItems.isEmpty()) return; - presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); + themeManager()->updateSeries(m_series); bool startupAnimation = m_sliceItems.isEmpty(); @@ -185,7 +156,7 @@ void PieChartItem::handleSlicesAdded(QList slices) void PieChartItem::handleSlicesRemoved(QList slices) { - presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); + themeManager()->updateSeries(m_series); foreach (QPieSlice *slice, slices) { diff --git a/src/piechart/piechartitem_p.h b/src/piechart/piechartitem_p.h index 7072a0f..5df4643 100644 --- a/src/piechart/piechartitem_p.h +++ b/src/piechart/piechartitem_p.h @@ -45,7 +45,7 @@ class PieChartItem : public ChartItem Q_OBJECT public: - explicit PieChartItem(QPieSeries *series, ChartPresenter *presenter); + explicit PieChartItem(QPieSeries *series, QGraphicsItem* item = 0); ~PieChartItem(); // from QGraphicsItem @@ -54,11 +54,7 @@ public: public Q_SLOTS: // from Chart - virtual void handleGeometryChanged(const QRectF &rect); - virtual void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); virtual void handleDomainUpdated(); - virtual void rangeXChanged(qreal min, qreal max, int tickXCount); - virtual void rangeYChanged(qreal min, qreal max, int tickYCount); void updateLayout(); void handleSlicesAdded(QList slices); diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 7c6f1b0..cb75504 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -834,20 +834,29 @@ void QPieSeriesPrivate::sliceHovered(bool state) emit q->hovered(slice, state); } -void QPieSeriesPrivate::scaleDomain(Domain &domain) +void QPieSeriesPrivate::initializeDomain() { - Q_UNUSED(domain); // does not apply to pie } -ChartElement *QPieSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QPieSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QPieSeries); - PieChartItem *pie = new PieChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) - pie->setAnimation(new PieAnimation(pie)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return pie; + PieChartItem *pie = new PieChartItem(q,parent); + m_item.reset(pie); + QAbstractSeriesPrivate::initializeGraphics(parent); +} + +void QPieSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options) +{ + PieChartItem *item = static_cast(m_item.data()); + Q_ASSERT(item); + if (options.testFlag(QChart::SeriesAnimations)) { + item->setAnimation(new PieAnimation(item)); + }else{ + item->setAnimation(0); + } + QAbstractSeriesPrivate::initializeAnimations(options); } QList QPieSeriesPrivate::createLegendMarkers(QLegend* legend) @@ -861,9 +870,9 @@ QList QPieSeriesPrivate::createLegendMarkers(QLegend* legend) return markers; } -void QPieSeriesPrivate::initializeAxis(QAbstractAxis *axis) +void QPieSeriesPrivate::initializeAxes() { - Q_UNUSED(axis); + } QAbstractAxis::AxisType QPieSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const @@ -872,6 +881,44 @@ QAbstractAxis::AxisType QPieSeriesPrivate::defaultAxisType(Qt::Orientation orien return QAbstractAxis::AxisTypeNoAxis; } +QAbstractAxis* QPieSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const +{ + Q_UNUSED(orientation); + return 0; +} + +void QPieSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced) +{ + Q_Q(QPieSeries); + const QList& colors = theme->seriesColors(); + const QList& gradients = theme->seriesGradients(); + + for (int i(0); i < m_slices.count(); i++) { + + QColor penColor = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0); + + // Get color for a slice from a gradient linearly, beginning from the start of the gradient + qreal pos = (qreal)(i + 1) / (qreal) m_slices.count(); + QColor brushColor = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), pos); + + QPieSlice *s = m_slices.at(i); + QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s); + + if (forced || d->m_data.m_slicePen.isThemed()) + d->setPen(penColor, true); + + if (forced || d->m_data.m_sliceBrush.isThemed()) + d->setBrush(brushColor, true); + + if (forced || d->m_data.m_labelBrush.isThemed()) + d->setLabelBrush(theme->labelBrush().color(), true); + + if (forced || d->m_data.m_labelFont.isThemed()) + d->setLabelFont(theme->labelFont(), true); + } +} + + #include "moc_qpieseries.cpp" #include "moc_qpieseries_p.cpp" diff --git a/src/piechart/qpieseries.h b/src/piechart/qpieseries.h index 857d2cf..6bfc86a 100644 --- a/src/piechart/qpieseries.h +++ b/src/piechart/qpieseries.h @@ -95,6 +95,7 @@ Q_SIGNALS: private: Q_DECLARE_PRIVATE(QPieSeries) Q_DISABLE_COPY(QPieSeries) + friend class PieChartItem; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/piechart/qpieseries_p.h b/src/piechart/qpieseries_p.h index c5c1aad..28b91e1 100644 --- a/src/piechart/qpieseries_p.h +++ b/src/piechart/qpieseries_p.h @@ -44,11 +44,16 @@ public: QPieSeriesPrivate(QPieSeries *parent); ~QPieSeriesPrivate(); - void scaleDomain(Domain &domain); - ChartElement *createGraphics(ChartPresenter *presenter); + void initializeDomain(); + void initializeAxes(); + void initializeGraphics(QGraphicsItem* parent); + void initializeAnimations(QtCommercialChart::QChart::AnimationOptions options); + void initializeTheme(int index, ChartTheme* theme, bool forced = false); + QList createLegendMarkers(QLegend *legend); - void initializeAxis(QAbstractAxis *axis); + QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const; + QAbstractAxis* createDefaultAxis(Qt::Orientation orientation) const; void updateDerivativeData(); void setSizes(qreal innerSize, qreal outerSize); @@ -78,7 +83,7 @@ private: qreal m_sum; qreal m_holeRelativeSize; -private: +public: friend class QLegendPrivate; Q_DECLARE_PUBLIC(QPieSeries) }; diff --git a/src/piechart/qpieslice_p.h b/src/piechart/qpieslice_p.h index 95a6361..3c19a05 100644 --- a/src/piechart/qpieslice_p.h +++ b/src/piechart/qpieslice_p.h @@ -65,7 +65,7 @@ Q_SIGNALS: private: friend class QPieSeries; friend class QPieSeriesPrivate; - friend class ChartTheme; + friend class ChartThemeManager; friend class PieChartItem; QPieSlice * const q_ptr; diff --git a/src/qabstractseries.cpp b/src/qabstractseries.cpp index e744f43..7d5be66 100644 --- a/src/qabstractseries.cpp +++ b/src/qabstractseries.cpp @@ -21,6 +21,9 @@ #include "qabstractseries.h" #include "qabstractseries_p.h" #include "chartdataset_p.h" +#include "qchart.h" +#include "qchart_p.h" +#include "chartitem_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -137,7 +140,7 @@ QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) : */ QAbstractSeries::~QAbstractSeries() { - if (d_ptr->m_dataset) + if (d_ptr->m_chart) qFatal("Still binded series detected !"); } @@ -222,12 +225,39 @@ void QAbstractSeries::hide() setVisible(false); } +bool QAbstractSeries::attachAxis(QAbstractAxis* axis) +{ + if(d_ptr->m_chart) { + return d_ptr->m_chart->d_ptr->m_dataset->attachAxis(this,axis); + } else { + qWarning()<<"Series not in the chart. Please addSeries to chart first."; + return false; + } +} + +bool QAbstractSeries::detachAxis(QAbstractAxis* axis) +{ + if(d_ptr->m_chart) { + return d_ptr->m_chart->d_ptr->m_dataset->detachAxis(this,axis); + } + else { + qWarning()<<"Series not in the chart. Please addSeries to chart first."; + return false; + } +} + +QList QAbstractSeries::attachedAxes() +{ + return d_ptr->m_axes; +} + /////////////////////////////////////////////////////////////////////////////////////////////////// QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries *q) : q_ptr(q), m_chart(0), - m_dataset(0), + m_item(0), + m_domain(new Domain()), m_visible(true), m_opacity(1.0) { @@ -237,6 +267,31 @@ QAbstractSeriesPrivate::~QAbstractSeriesPrivate() { } +void QAbstractSeriesPrivate::setDomain(QSharedPointer domain) +{ + Q_ASSERT(!domain.isNull()); + if(m_domain!=domain) { + if(!m_item.isNull()) QObject::disconnect(m_domain.data(), SIGNAL(updated()), m_item.data(), SLOT(handleDomainUpdated())); + m_domain = domain; + if(!m_item.isNull()) { + QObject::connect(m_domain.data(), SIGNAL(updated()),m_item.data(), SLOT(handleDomainUpdated())); + m_item->handleDomainUpdated(); + } + } +} + +void QAbstractSeriesPrivate::initializeGraphics(QGraphicsItem* parent) +{ + Q_ASSERT(!m_item.isNull()); + Q_UNUSED(parent); + QObject::connect(m_domain.data(), SIGNAL(updated()),m_item.data(), SLOT(handleDomainUpdated())); +} + +void QAbstractSeriesPrivate::initializeAnimations(QChart::AnimationOptions options) +{ + Q_UNUSED(options); +} + #include "moc_qabstractseries.cpp" #include "moc_qabstractseries_p.cpp" diff --git a/src/qabstractseries.h b/src/qabstractseries.h index 4ad1f45..55b1555 100644 --- a/src/qabstractseries.h +++ b/src/qabstractseries.h @@ -71,6 +71,10 @@ public: QChart *chart() const; + bool attachAxis(QAbstractAxis* axis); + bool detachAxis(QAbstractAxis* axis); + QList attachedAxes(); + void show(); void hide(); @@ -83,6 +87,7 @@ protected: QScopedPointer d_ptr; friend class ChartDataSet; friend class ChartPresenter; + friend class ChartThemeManager; friend class QLegendPrivate; friend class DeclarativeChart; }; diff --git a/src/qabstractseries_p.h b/src/qabstractseries_p.h index 2c28ec9..52de806 100644 --- a/src/qabstractseries_p.h +++ b/src/qabstractseries_p.h @@ -31,10 +31,13 @@ #define QABSTRACTSERIES_P_H #include "qabstractseries.h" +#include "qchart.h" +#include "domain_p.h" + +class QGraphicsItem; QTCOMMERCIALCHART_BEGIN_NAMESPACE -class Domain; class ChartPresenter; class ChartElement; class LegendMarker; @@ -42,6 +45,9 @@ class QLegend; class ChartDataSet; class QAbstractAxis; class QLegendMarker; +class ChartTheme; +class ChartAnimation; +class ChartItem; class QAbstractSeriesPrivate : public QObject { @@ -50,11 +56,23 @@ public: QAbstractSeriesPrivate(QAbstractSeries *q); ~QAbstractSeriesPrivate(); - virtual void scaleDomain(Domain &domain) = 0; - virtual ChartElement *createGraphics(ChartPresenter *presenter) = 0; + virtual void initializeDomain() = 0; + virtual void initializeAxes() = 0; + virtual void initializeTheme(int index, ChartTheme* theme, bool forced = false) = 0; + virtual void initializeGraphics(QGraphicsItem* parent) = 0; + virtual void initializeAnimations(QChart::AnimationOptions options) = 0; + virtual QList createLegendMarkers(QLegend* legend) = 0; - virtual void initializeAxis(QAbstractAxis *axis) = 0; + virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation) const = 0; + virtual QAbstractAxis* createDefaultAxis(Qt::Orientation) const = 0; + + ChartItem* chartItem() { return m_item.data(); } + + virtual void setDomain(QSharedPointer domain); + QSharedPointer domain() { return m_domain; } + + QChart* chart() { return m_chart; } Q_SIGNALS: void countChanged(); @@ -62,13 +80,17 @@ Q_SIGNALS: protected: QAbstractSeries *q_ptr; QChart *m_chart; - ChartDataSet *m_dataset; + QScopedPointer m_item; + QList m_axes; +private: + QSharedPointer m_domain; QString m_name; bool m_visible; qreal m_opacity; friend class QAbstractSeries; friend class ChartDataSet; + friend class ChartPresenter; friend class QLegendPrivate; }; diff --git a/src/qchart.cpp b/src/qchart.cpp index 8b31af0..104b77b 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -25,6 +25,9 @@ #include "chartbackground_p.h" #include "qabstractaxis.h" #include "chartlayout_p.h" +#include "charttheme_p.h" +#include "chartpresenter_p.h" +#include "chartdataset_p.h" #include #include @@ -106,13 +109,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent, wFlags), - d_ptr(new QChartPrivate()) + d_ptr(new QChartPrivate(this)) { - d_ptr->m_dataset = new ChartDataSet(this); - d_ptr->m_presenter = new ChartPresenter(this, d_ptr->m_dataset); - d_ptr->createConnections(); d_ptr->m_legend = new LegendScroller(this); - d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); + setTheme(QChart::ChartThemeLight); + //TODO: what is that ? //connect(d_ptr->m_presenter, SIGNAL(marginsChanged(QRectF)), this, SIGNAL(marginsChanged(QRectF))); setLayout(d_ptr->m_presenter->layout()); } @@ -122,10 +123,9 @@ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) */ QChart::~QChart() { - //delete first presenter , since this is a root of all the graphical items - setLayout(0); - delete d_ptr->m_presenter; - d_ptr->m_presenter = 0; + //start by deleting dataset, it will remove all series and axes + delete d_ptr->m_dataset; + d_ptr->m_dataset = 0; } /*! @@ -245,12 +245,12 @@ QBrush QChart::titleBrush() const void QChart::setTheme(QChart::ChartTheme theme) { - d_ptr->m_presenter->setTheme(theme); + d_ptr->m_themeManager->setTheme(theme); } QChart::ChartTheme QChart::theme() const { - return d_ptr->m_presenter->theme(); + return d_ptr->m_themeManager->theme()->id(); } /*! @@ -258,7 +258,7 @@ QChart::ChartTheme QChart::theme() const */ void QChart::zoomIn() { - d_ptr->m_presenter->zoomIn(2.0); + d_ptr->zoomIn(2.0); } /*! @@ -266,9 +266,7 @@ void QChart::zoomIn() */ void QChart::zoomIn(const QRectF &rect) { - if (!rect.isValid()) - return; - d_ptr->m_presenter->zoomIn(rect); + d_ptr->zoomIn(rect); } /*! @@ -276,7 +274,7 @@ void QChart::zoomIn(const QRectF &rect) */ void QChart::zoomOut() { - d_ptr->m_presenter->zoomOut(2.0); + d_ptr->zoomOut(2.0); } /*! @@ -296,9 +294,9 @@ void QChart::zoom(qreal factor) return; if (factor > 1.0) - d_ptr->m_presenter->zoomIn(factor); + d_ptr->zoomIn(factor); else - d_ptr->m_presenter->zoomOut(1.0 / factor); + d_ptr->zoomOut(1.0 / factor); } /*! @@ -307,7 +305,27 @@ void QChart::zoom(qreal factor) */ QAbstractAxis *QChart::axisX(QAbstractSeries *series) const { - return d_ptr->m_dataset->axisX(series); + if(!series && d_ptr->m_dataset->series().size()>0){ + series = d_ptr->m_dataset->series().first(); + }else{ + return 0; + } + + QList axes = series->attachedAxes(); + QAbstractAxis* bottom=0; + QAbstractAxis* top=0; + + foreach(QAbstractAxis* axis, axes){ + + if(axis->alignment()==Qt::AlignTop){ + top=axis; + } + + if(axis->alignment()==Qt::AlignBottom){ + bottom=axis; + } + } + return bottom?bottom:top; } /*! @@ -316,7 +334,42 @@ QAbstractAxis *QChart::axisX(QAbstractSeries *series) const */ QAbstractAxis *QChart::axisY(QAbstractSeries *series) const { - return d_ptr->m_dataset->axisY(series); + if(!series && d_ptr->m_dataset->series().size()>0) { + series = d_ptr->m_dataset->series().first(); + } else { + return 0; + } + + QList axes = series->attachedAxes(); + + QAbstractAxis* left=0; + QAbstractAxis* right=0; + + foreach(QAbstractAxis* axis, axes){ + + if(axis->alignment()==Qt::AlignLeft){ + left=axis; + } + + if(axis->alignment()==Qt::AlignRight){ + right=axis; + } + } + + return left?left:right; +} + + +QList QChart::axes(Qt::Orientations orientation, QAbstractSeries *series) const +{ + QList result ; + + foreach(QAbstractAxis* axis,series->attachedAxes()){ + if(orientation.testFlag(axis->orientation())) + result << axis; + } + + return result; } /*! @@ -410,7 +463,7 @@ QMargins QChart::margins() const */ QRectF QChart::plotArea() const { - return d_ptr->m_presenter->layout()->chartsGeometry(); + return d_ptr->m_presenter->geometry(); } ///*! @@ -440,7 +493,7 @@ QChart::AnimationOptions QChart::animationOptions() const */ void QChart::scroll(qreal dx, qreal dy) { - d_ptr->m_presenter->scroll(dx, dy); + d_ptr->scroll(dx,dy); } void QChart::setBackgroundVisible(bool visible) @@ -480,9 +533,18 @@ QList QChart::series() const */ void QChart::setAxisX(QAbstractAxis *axis , QAbstractSeries *series) { - if (axis->alignment() == Qt::AlignLeft || axis->alignment() == Qt::AlignRight) - return; - d_ptr->m_dataset->setAxis(series, axis, Qt::Horizontal); + QList list = axes(Qt::Horizontal,series); + + foreach(QAbstractAxis* a, list){ + if(a->alignment()==axis->alignment()){ + d_ptr->m_dataset->removeAxis(a); + delete a; + } + } + + if(!d_ptr->m_dataset->axes().contains(axis)) + d_ptr->m_dataset->addAxis(axis,Qt::AlignBottom); + d_ptr->m_dataset->attachAxis(series,axis); } /*! @@ -492,19 +554,47 @@ void QChart::setAxisX(QAbstractAxis *axis , QAbstractSeries *series) */ void QChart::setAxisY(QAbstractAxis *axis , QAbstractSeries *series) { - if (axis->alignment() == Qt::AlignTop || axis->alignment() == Qt::AlignBottom) - return; - d_ptr->m_dataset->setAxis(series, axis, Qt::Vertical); + QList list = axes(Qt::Vertical,series); + + foreach(QAbstractAxis* a, list) { + if(a->alignment()==axis->alignment()) { + d_ptr->m_dataset->removeAxis(a); + delete a; + } + } + + if(!d_ptr->m_dataset->axes().contains(axis)) + d_ptr->m_dataset->addAxis(axis,Qt::AlignLeft); + d_ptr->m_dataset->attachAxis(series,axis); +} + +void QChart::addAxis(QAbstractAxis *axis,Qt::Alignment aligment) +{ + d_ptr->m_dataset->addAxis(axis,aligment); +} + +void QChart::removeAxis(QAbstractAxis *axis) +{ + d_ptr->m_dataset->removeAxis(axis); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QChartPrivate::QChartPrivate(): +QChartPrivate::QChartPrivate(QChart *q): + q_ptr(q), m_legend(0), - m_dataset(0), - m_presenter(0) + m_dataset(new ChartDataSet(q)), + m_presenter(new ChartPresenter(q)), + m_themeManager(new ChartThemeManager(q)) { - + QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), m_presenter, SLOT(handleSeriesAdded(QAbstractSeries*))); + QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_presenter, SLOT(handleSeriesRemoved(QAbstractSeries*))); + QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*)), m_presenter, SLOT(handleAxisAdded(QAbstractAxis*))); + QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)), m_presenter, SLOT(handleAxisRemoved(QAbstractAxis*))); + QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*)), m_themeManager, SLOT(handleSeriesAdded(QAbstractSeries*))); + QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_themeManager, SLOT(handleSeriesRemoved(QAbstractSeries*))); + QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*)), m_themeManager, SLOT(handleAxisAdded(QAbstractAxis*))); + QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)), m_themeManager, SLOT(handleAxisRemoved(QAbstractAxis*))); } QChartPrivate::~QChartPrivate() @@ -512,13 +602,59 @@ QChartPrivate::~QChartPrivate() } -void QChartPrivate::createConnections() +void QChartPrivate::zoomIn(qreal factor) { - QObject::connect(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)), m_presenter, SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); - QObject::connect(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)), m_presenter, SLOT(handleSeriesRemoved(QAbstractSeries*))); - QObject::connect(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*)), m_presenter, SLOT(handleAxisAdded(QAbstractAxis*,Domain*))); - QObject::connect(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)), m_presenter, SLOT(handleAxisRemoved(QAbstractAxis*))); - //QObject::connect(m_presenter, SIGNAL(marginsChanged(QRectF)), q_ptr, SIGNAL(marginsChanged(QRectF))); + QRectF rect = m_presenter->geometry(); + rect.setWidth(rect.width() / factor); + rect.setHeight(rect.height() / factor); + rect.moveCenter(m_presenter->geometry().center()); + zoomIn(rect); +} + +void QChartPrivate::zoomIn(const QRectF &rect) +{ + if (!rect.isValid()) + return; + + QRectF r = rect.normalized(); + const QRectF geometry = m_presenter->geometry(); + r.translate(-geometry.topLeft()); + + if (!r.isValid()) + return; + + QPointF zoomPoint(r.center().x() / geometry.width(), r.center().y() / geometry.height()); + m_presenter->setState(ChartPresenter::ZoomInState,zoomPoint); + m_dataset->zoomInDomain(r); + m_presenter->setState(ChartPresenter::ShowState,QPointF()); + +} + +void QChartPrivate::zoomOut(qreal factor) +{ + const QRectF geometry = m_presenter->geometry(); + + QRectF r; + r.setSize(geometry.size() / factor); + r.moveCenter(QPointF(geometry.size().width()/2 ,geometry.size().height()/2)); + if (!r.isValid()) + return; + + QPointF zoomPoint(r.center().x() / geometry.width(), r.center().y() / geometry.height()); + m_presenter->setState(ChartPresenter::ZoomOutState,zoomPoint); + m_dataset->zoomOutDomain(r); + m_presenter->setState(ChartPresenter::ShowState,QPointF()); +} + +void QChartPrivate::scroll(qreal dx, qreal dy) +{ + if (dx < 0) m_presenter->setState(ChartPresenter::ScrollLeftState,QPointF()); + if (dx > 0) m_presenter->setState(ChartPresenter::ScrollRightState,QPointF()); + if (dy < 0) m_presenter->setState(ChartPresenter::ScrollUpState,QPointF()); + if (dy > 0) m_presenter->setState(ChartPresenter::ScrollDownState,QPointF()); + + m_dataset->scrollDomain(dx, dy); + m_presenter->setState(ChartPresenter::ShowState,QPointF()); } #include "moc_qchart.cpp" diff --git a/src/qchart.h b/src/qchart.h index 27806cc..e2d1540 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -76,11 +76,17 @@ public: void removeAllSeries(); QList series() const; + //depreciated shit//////////////////////////////////////////////////. void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = 0); void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = 0); QAbstractAxis *axisX(QAbstractSeries *series = 0) const; QAbstractAxis *axisY(QAbstractSeries *series = 0) const; + ///////////////////////////////////////////////////////////////////// + + void addAxis(QAbstractAxis *axis,Qt::Alignment aligment); + void removeAxis(QAbstractAxis *axis); + QList axes(Qt::Orientations orientation = Qt::Horizontal|Qt::Vertical, QAbstractSeries *series = 0) const; void createDefaultAxes(); @@ -107,9 +113,11 @@ public: AnimationOptions animationOptions() const; void zoomIn(); - void zoomIn(const QRectF &rect); void zoomOut(); + + void zoomIn(const QRectF &rect); void zoom(qreal factor); + void scroll(qreal dx, qreal dy); QLegend *legend() const; @@ -126,6 +134,10 @@ protected: QScopedPointer d_ptr; friend class QLegend; friend class DeclarativeChart; + friend class ChartDataSet; + friend class ChartPresenter; + friend class ChartThemeManager; + friend class QAbstractSeries; Q_DISABLE_COPY(QChart) }; diff --git a/src/qchart_p.h b/src/qchart_p.h index 6403072..c3d4f80 100644 --- a/src/qchart_p.h +++ b/src/qchart_p.h @@ -30,23 +30,32 @@ #ifndef QCHART_P_H #define QCHART_P_H -#include "qlegend.h" -#include "chartpresenter_p.h" -#include "chartdataset_p.h" +#include "qchartglobal.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; +class ChartThemeManager; +class ChartPresenter; +class QLegend; +class ChartDataSet; -struct QChartPrivate +class QChartPrivate { - QChartPrivate(); + +public: + QChartPrivate(QChart *q); ~QChartPrivate(); + QChart *q_ptr; QLegend *m_legend; ChartDataSet *m_dataset; ChartPresenter *m_presenter; + ChartThemeManager *m_themeManager; - void createConnections(); + void zoomIn(qreal factor); + void zoomOut(qreal factor); + void zoomIn(const QRectF &rect); + void scroll(qreal dx, qreal dy); }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/scatterchart/qscatterseries.cpp b/src/scatterchart/qscatterseries.cpp index 543541f..c913d8f 100644 --- a/src/scatterchart/qscatterseries.cpp +++ b/src/scatterchart/qscatterseries.cpp @@ -143,8 +143,8 @@ QScatterSeries::QScatterSeries(QObject *parent) QScatterSeries::~QScatterSeries() { Q_D(QScatterSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } QAbstractSeries::SeriesType QScatterSeries::type() const @@ -254,14 +254,32 @@ QScatterSeriesPrivate::QScatterSeriesPrivate(QScatterSeries *q) { } -ChartElement *QScatterSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QScatterSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QScatterSeries); - ScatterChartItem *scatter = new ScatterChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) - scatter->setAnimation(new XYAnimation(scatter)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return scatter; + ScatterChartItem *scatter = new ScatterChartItem(q,parent); + m_item.reset(scatter); + QAbstractSeriesPrivate::initializeGraphics(parent); +} + +void QScatterSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced) +{ + Q_Q(QScatterSeries); + QPen pen; + QBrush brush; + const QList colors = theme->seriesColors(); + const QList gradients = theme->seriesGradients(); + + if (forced || pen == m_pen) { + pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0)); + pen.setWidthF(2); + q->setPen(pen); + } + + if (forced || brush == m_brush) { + QBrush brush(colors.at(index % colors.size())); + q->setBrush(brush); + } } #include "moc_qscatterseries.cpp" diff --git a/src/scatterchart/qscatterseries_p.h b/src/scatterchart/qscatterseries_p.h index dd87682..5e5dc86 100644 --- a/src/scatterchart/qscatterseries_p.h +++ b/src/scatterchart/qscatterseries_p.h @@ -38,7 +38,8 @@ class QScatterSeriesPrivate: public QXYSeriesPrivate { public: QScatterSeriesPrivate(QScatterSeries *q); - ChartElement *createGraphics(ChartPresenter *presenter); + void initializeGraphics(QGraphicsItem* parent); + void initializeTheme(int index, ChartTheme* theme, bool forced = false); private: QScatterSeries::MarkerShape m_shape; diff --git a/src/scatterchart/scatterchartitem.cpp b/src/scatterchart/scatterchartitem.cpp index 8e8e2c1..ee3bd3d 100644 --- a/src/scatterchart/scatterchartitem.cpp +++ b/src/scatterchart/scatterchartitem.cpp @@ -22,6 +22,7 @@ #include "qscatterseries.h" #include "qscatterseries_p.h" #include "chartpresenter_p.h" +#include "domain_p.h" #include #include #include @@ -29,9 +30,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) - : XYChart(series, presenter), - QGraphicsItem(presenter ? presenter->rootItem() : 0), +ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem* item) + : XYChart(series,item), m_series(series), m_items(this), m_visible(true), @@ -93,12 +93,12 @@ void ScatterChartItem::deletePoints(int count) void ScatterChartItem::markerSelected(QGraphicsItem *marker) { - emit XYChart::clicked(calculateDomainPoint(m_markerMap[marker])); + emit XYChart::clicked(domain()->calculateDomainPoint(m_markerMap[marker])); } void ScatterChartItem::markerHovered(QGraphicsItem *marker, bool state) { - emit XYChart::hovered(calculateDomainPoint(m_markerMap[marker]), state); + emit XYChart::hovered(domain()->calculateDomainPoint(m_markerMap[marker]), state); } void ScatterChartItem::updateGeometry() @@ -123,21 +123,22 @@ void ScatterChartItem::updateGeometry() QList items = m_items.childItems(); + QRectF clipRect(QPointF(0,0),domain()->size()); + for (int i = 0; i < points.size(); i++) { QGraphicsItem *item = items.at(i); const QPointF &point = points.at(i); const QRectF &rect = item->boundingRect(); m_markerMap[item] = point; item->setPos(point.x() - rect.width() / 2, point.y() - rect.height() / 2); - if (!m_visible || !clipRect().contains(point)) + if (!m_visible || !clipRect.contains(point)) item->setVisible(false); else item->setVisible(true); } prepareGeometryChange(); - m_rect = clipRect(); - setPos(origin()); + m_rect = clipRect; } void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) diff --git a/src/scatterchart/scatterchartitem_p.h b/src/scatterchart/scatterchartitem_p.h index c64322d..9b4a38c 100644 --- a/src/scatterchart/scatterchartitem_p.h +++ b/src/scatterchart/scatterchartitem_p.h @@ -39,12 +39,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QScatterSeries; -class ScatterChartItem : public XYChart, public QGraphicsItem +class ScatterChartItem : public XYChart { Q_OBJECT Q_INTERFACES(QGraphicsItem) public: - explicit ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter); + explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem* item = 0); public: //from QGraphicsItem diff --git a/src/splinechart/qsplineseries.cpp b/src/splinechart/qsplineseries.cpp index 576dc02..83a6deb 100644 --- a/src/splinechart/qsplineseries.cpp +++ b/src/splinechart/qsplineseries.cpp @@ -114,8 +114,8 @@ QSplineSeries::QSplineSeries(QObject *parent) QSplineSeries::~QSplineSeries() { Q_D(QSplineSeries); - if (d->m_dataset) - d->m_dataset->removeSeries(this); + if (d->m_chart) + d->m_chart->removeSeries(this); } QAbstractSeries::SeriesType QSplineSeries::type() const @@ -244,14 +244,38 @@ void QSplineSeriesPrivate::updateControlPoints() } } -ChartElement *QSplineSeriesPrivate::createGraphics(ChartPresenter *presenter) +void QSplineSeriesPrivate::initializeGraphics(QGraphicsItem* parent) { Q_Q(QSplineSeries); - SplineChartItem *spline = new SplineChartItem(q, presenter); - if (presenter->animationOptions().testFlag(QChart::SeriesAnimations)) - spline->setAnimation(new SplineAnimation(spline)); - presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); - return spline; + SplineChartItem *spline = new SplineChartItem(q,parent); + m_item.reset(spline); + QAbstractSeriesPrivate::initializeGraphics(parent); +} + +void QSplineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced) +{ + Q_Q(QSplineSeries); + const QList colors = theme->seriesColors(); + + QPen pen; + if (forced || pen == m_pen) { + pen.setColor(colors.at(index % colors.size())); + pen.setWidthF(2); + q->setPen(pen); + } +} + + +void QSplineSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options) +{ + SplineChartItem *item = static_cast(m_item.data()); + Q_ASSERT(item); + if (options.testFlag(QChart::SeriesAnimations)) { + item->setAnimation(new SplineAnimation(item)); + }else{ + item->setAnimation(0); + } + QAbstractSeriesPrivate::initializeAnimations(options); } #include "moc_qsplineseries.cpp" diff --git a/src/splinechart/qsplineseries_p.h b/src/splinechart/qsplineseries_p.h index 4cd0df0..8bca724 100644 --- a/src/splinechart/qsplineseries_p.h +++ b/src/splinechart/qsplineseries_p.h @@ -39,9 +39,12 @@ class QSplineSeriesPrivate: public QLineSeriesPrivate { Q_OBJECT public: - ChartElement *createGraphics(ChartPresenter *presenter); QSplineSeriesPrivate(QSplineSeries *q); + void initializeTheme(int index, ChartTheme* theme, bool forced = false); + void initializeGraphics(QGraphicsItem* parent); + void initializeAnimations(QtCommercialChart::QChart::AnimationOptions options); + QPointF controlPoint(int index) const; public Q_SLOTS: diff --git a/src/splinechart/splinechartitem.cpp b/src/splinechart/splinechartitem.cpp index ae01535..1e09d17 100644 --- a/src/splinechart/splinechartitem.cpp +++ b/src/splinechart/splinechartitem.cpp @@ -22,14 +22,14 @@ #include "qsplineseries_p.h" #include "chartpresenter_p.h" #include "splineanimation_p.h" +#include "domain_p.h" #include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) - : XYChart(series, presenter), - QGraphicsItem(presenter ? presenter->rootItem() : 0), +SplineChartItem::SplineChartItem(QSplineSeries *series, QGraphicsItem* item) + : XYChart(series,item), m_series(series), m_pointsVisible(false), m_animation(0) @@ -101,7 +101,7 @@ void SplineChartItem::updateChart(QVector &oldPoints, QVector QPointF SplineChartItem::calculateGeometryControlPoint(int index) const { - return XYChart::calculateGeometryPoint(m_series->d_func()->controlPoint(index)); + return domain()->calculateGeometryPoint(m_series->d_func()->controlPoint(index)); } void SplineChartItem::updateGeometry() @@ -131,7 +131,7 @@ void SplineChartItem::updateGeometry() // m_path = stroker.createStroke(splinePath); m_path = splinePath; m_rect = splinePath.boundingRect(); - setPos(origin()); + } //handlers @@ -155,9 +155,9 @@ void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o Q_UNUSED(option) painter->save(); + painter->setClipRect(QRectF(QPointF(0,0),domain()->size())); painter->setPen(m_linePen); // painter->setBrush(m_linePen.color()); - painter->setClipRect(clipRect()); painter->drawPath(m_path); if (m_pointsVisible) { @@ -169,19 +169,19 @@ void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o void SplineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { - emit XYChart::clicked(calculateDomainPoint(event->pos())); + emit XYChart::clicked(domain()->calculateDomainPoint(event->pos())); QGraphicsItem::mousePressEvent(event); } void SplineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { - emit XYChart::hovered(calculateDomainPoint(event->pos()), true); + emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true); event->accept(); } void SplineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { - emit XYChart::hovered(calculateDomainPoint(event->pos()), false); + emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false); event->accept(); } diff --git a/src/splinechart/splinechartitem_p.h b/src/splinechart/splinechartitem_p.h index 87fddd6..b2aa8fb 100644 --- a/src/splinechart/splinechartitem_p.h +++ b/src/splinechart/splinechartitem_p.h @@ -37,12 +37,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class SplineAnimation; -class SplineChartItem : public XYChart, public QGraphicsItem +class SplineChartItem : public XYChart { Q_OBJECT Q_INTERFACES(QGraphicsItem) public: - SplineChartItem(QSplineSeries *series, ChartPresenter *presenter); + SplineChartItem(QSplineSeries *series, QGraphicsItem* item = 0); //from QGraphicsItem QRectF boundingRect() const; diff --git a/src/src.pro b/src/src.pro index 74e8dad..ed61387 100644 --- a/src/src.pro +++ b/src/src.pro @@ -31,13 +31,14 @@ INCLUDEPATH += ../include . SOURCES += \ $$PWD/chartdataset.cpp \ $$PWD/chartpresenter.cpp \ - $$PWD/charttheme.cpp \ + $$PWD/chartthememanager.cpp \ $$PWD/domain.cpp \ $$PWD/qchart.cpp \ $$PWD/qchartview.cpp \ $$PWD/qabstractseries.cpp \ $$PWD/chartbackground.cpp \ $$PWD/chartelement.cpp \ + $$PWD/chartitem.cpp \ $$PWD/scroller.cpp \ $$PWD/chartlayout.cpp \ $$PWD/charttitle.cpp @@ -45,7 +46,7 @@ PRIVATE_HEADERS += \ $$PWD/chartdataset_p.h \ $$PWD/chartitem_p.h \ $$PWD/chartpresenter_p.h \ - $$PWD/charttheme_p.h \ + $$PWD/chartthememanager_p.h \ $$PWD/domain_p.h \ $$PWD/chartbackground_p.h \ $$PWD/chartelement_p.h \ diff --git a/src/charttheme_p.h b/src/themes/charttheme_p.h similarity index 68% rename from src/charttheme_p.h rename to src/themes/charttheme_p.h index 08c8f6b..f60efe1 100644 --- a/src/charttheme_p.h +++ b/src/themes/charttheme_p.h @@ -30,33 +30,15 @@ #ifndef CHARTTHEME_H #define CHARTTHEME_H -#include "qchartglobal.h" -#include "qchart.h" +#include "chartthememanager_p.h" #include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class ChartItem; -class LineChartItem; -class QLineSeries; -class AbstractBarChartItem; -class QAbstractBarSeries; -class StackedBarChartItem; -class QStackedBarSeries; -class QPercentBarSeries; -class PercentBarChartItem; -class QScatterSeries; -class ScatterChartItem; -class PieChartItem; -class QPieSeries; -class SplineChartItem; -class QSplineSeries; -class AreaChartItem; -class QAreaSeries; - class ChartTheme { + public: enum BackgroundShadesMode { BackgroundShadesNone = 0, @@ -66,26 +48,23 @@ public: }; protected: - explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeLight); + explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeLight):m_id(id), + m_backgroundShades(BackgroundShadesNone), + m_backgroundDropShadowEnabled(false){}; public: - static ChartTheme *createTheme(QChart::ChartTheme theme); QChart::ChartTheme id() const { return m_id; } - void decorate(QChart *chart); - void decorate(QLegend *legend); - void decorate(QAbstractBarSeries *series, int index); - void decorate(QLineSeries *series, int index); - void decorate(QAreaSeries *series, int index); - void decorate(QScatterSeries *series, int index); - void decorate(QPieSeries *series, int index); - void decorate(QSplineSeries *series, int index); - void decorate(QAbstractAxis *axis); - void setForced(bool enabled); - bool isForced() { return m_force; } - -public: // utils - void generateSeriesGradients(); - static QColor colorAt(const QColor &start, const QColor &end, qreal pos); - static QColor colorAt(const QGradient &gradient, qreal pos); + QList seriesGradients() const { return m_seriesGradients; } + QList seriesColors() const { return m_seriesColors; } + QLinearGradient chartBackgroundGradient() const { return m_chartBackgroundGradient; } + QFont masterFont() const { return m_masterFont; } + QFont labelFont() const { return m_labelFont; } + QBrush labelBrush() const { return m_labelBrush; } + QPen axisLinePen() const { return m_axisLinePen; } + QPen backgroundShadesPen() const { return m_backgroundShadesPen; } + QBrush backgroundShadesBrush() const { return m_backgroundShadesBrush; } + BackgroundShadesMode backgroundShades() const { return m_backgroundShades; } + bool isBackgroundDropShadowEnabled() const { return m_backgroundDropShadowEnabled; } + QPen girdLinePen() const { return m_gridLinePen; } protected: QChart::ChartTheme m_id; @@ -102,7 +81,7 @@ protected: BackgroundShadesMode m_backgroundShades; bool m_backgroundDropShadowEnabled; QPen m_gridLinePen; - bool m_force; + }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/themes/chartthemebluecerulean_p.h b/src/themes/chartthemebluecerulean_p.h index d2a1516..f210e4f 100644 --- a/src/themes/chartthemebluecerulean_p.h +++ b/src/themes/chartthemebluecerulean_p.h @@ -44,7 +44,7 @@ public: m_seriesColors << QRgb(0x5cbf9b); m_seriesColors << QRgb(0x009fbf); m_seriesColors << QRgb(0xee7392); - generateSeriesGradients(); + m_seriesGradients = ChartThemeManager::generateSeriesGradients(m_seriesColors); // Background QLinearGradient backgroundGradient(0.5, 0.0, 0.5, 1.0); diff --git a/src/themes/chartthemeblueicy_p.h b/src/themes/chartthemeblueicy_p.h index 4bfcef7..e0d0a83 100644 --- a/src/themes/chartthemeblueicy_p.h +++ b/src/themes/chartthemeblueicy_p.h @@ -45,7 +45,7 @@ public: m_seriesColors << QRgb(0x0c2673); m_seriesColors << QRgb(0x5f3dba); m_seriesColors << QRgb(0x2fa3b4); - generateSeriesGradients(); + m_seriesGradients = ChartThemeManager::generateSeriesGradients(m_seriesColors); // Background QLinearGradient backgroundGradient(0.5, 0.0, 0.5, 1.0); diff --git a/src/themes/chartthemebluencs_p.h b/src/themes/chartthemebluencs_p.h index c42c031..a52b5ea 100644 --- a/src/themes/chartthemebluencs_p.h +++ b/src/themes/chartthemebluencs_p.h @@ -45,7 +45,7 @@ public: m_seriesColors << QRgb(0x88d41e); m_seriesColors << QRgb(0xff8e1a); m_seriesColors << QRgb(0x398ca3); - generateSeriesGradients(); + m_seriesGradients = ChartThemeManager::generateSeriesGradients(m_seriesColors); // Background QLinearGradient backgroundGradient; diff --git a/src/themes/chartthemebrownsand_p.h b/src/themes/chartthemebrownsand_p.h index c3154b4..42002bc 100644 --- a/src/themes/chartthemebrownsand_p.h +++ b/src/themes/chartthemebrownsand_p.h @@ -45,7 +45,7 @@ public: m_seriesColors << QRgb(0xc35660); m_seriesColors << QRgb(0x536780); m_seriesColors << QRgb(0x494345); - generateSeriesGradients(); + m_seriesGradients = ChartThemeManager::generateSeriesGradients(m_seriesColors); // Background QLinearGradient backgroundGradient; diff --git a/src/themes/chartthemedark_p.h b/src/themes/chartthemedark_p.h index 9473cdb..e1da420 100644 --- a/src/themes/chartthemedark_p.h +++ b/src/themes/chartthemedark_p.h @@ -45,7 +45,7 @@ public: m_seriesColors << QRgb(0xeb8817); m_seriesColors << QRgb(0x7b7f8c); m_seriesColors << QRgb(0xbf593e); - generateSeriesGradients(); + m_seriesGradients = ChartThemeManager::generateSeriesGradients(m_seriesColors); // Background QLinearGradient backgroundGradient(0.5, 0.0, 0.5, 1.0); diff --git a/src/themes/chartthemehighcontrast_p.h b/src/themes/chartthemehighcontrast_p.h index 2433856..826d209 100644 --- a/src/themes/chartthemehighcontrast_p.h +++ b/src/themes/chartthemehighcontrast_p.h @@ -45,7 +45,7 @@ public: m_seriesColors << QRgb(0xffab03); m_seriesColors << QRgb(0x038e9b); m_seriesColors << QRgb(0xff4a41); - generateSeriesGradients(); + m_seriesGradients = ChartThemeManager::generateSeriesGradients(m_seriesColors); // Background QLinearGradient backgroundGradient(0.5, 0.0, 0.5, 1.0); diff --git a/src/themes/chartthemelight_p.h b/src/themes/chartthemelight_p.h index 85c4cb1..55df34e 100644 --- a/src/themes/chartthemelight_p.h +++ b/src/themes/chartthemelight_p.h @@ -45,7 +45,7 @@ public: m_seriesColors << QRgb(0xf6a625); m_seriesColors << QRgb(0x6d5fd5); m_seriesColors << QRgb(0xbf593e); - generateSeriesGradients(); + m_seriesGradients = ChartThemeManager::generateSeriesGradients(m_seriesColors); // Background QLinearGradient backgroundGradient; diff --git a/src/themes/chartthemesystem_p.h b/src/themes/chartthemesystem_p.h index c624590..12a552b 100644 --- a/src/themes/chartthemesystem_p.h +++ b/src/themes/chartthemesystem_p.h @@ -106,7 +106,7 @@ public: m_seriesColors << QRgb(0x92ca66); m_seriesColors << QRgb(0xeba85f); m_seriesColors << QRgb(0xfc5751); - generateSeriesGradients(); + m_seriesGradients = ChartThemeManager::generateSeriesGradients(m_seriesColors); // Background QLinearGradient backgroundGradient; @@ -152,7 +152,7 @@ public: m_seriesColors << QRgb(0x92ca66); m_seriesColors << QRgb(0xeba85f); m_seriesColors << QRgb(0xfc5751); - generateSeriesGradients(); + m_seriesGradients = ChartThemeManager::generateSeriesGradients(m_seriesColors); // Background QLinearGradient backgroundGradient; diff --git a/src/themes/themes.pri b/src/themes/themes.pri index a32ce57..53af482 100644 --- a/src/themes/themes.pri +++ b/src/themes/themes.pri @@ -2,6 +2,7 @@ INCLUDEPATH += $$PWD DEPENDPATH += $$PWD THEMES += \ + $$PWD/charttheme_p.h \ $$PWD/chartthemesystem_p.h \ $$PWD/chartthemelight_p.h \ $$PWD/chartthemebluecerulean_p.h \ diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index 421c26a..7950904 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -22,6 +22,7 @@ #include "qxyseries_p.h" #include "domain_p.h" #include "qvalueaxis.h" +#include "xychart_p.h" #include "qxylegendmarker.h" @@ -418,7 +419,7 @@ QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) { } -void QXYSeriesPrivate::scaleDomain(Domain &domain) +void QXYSeriesPrivate::initializeDomain() { qreal minX(0); qreal minY(0); @@ -445,7 +446,7 @@ void QXYSeriesPrivate::scaleDomain(Domain &domain) } } - domain.setRange(minX, maxX, minY, maxY); + domain()->setRange(minX, maxX, minY, maxY); } QList QXYSeriesPrivate::createLegendMarkers(QLegend* legend) @@ -455,9 +456,9 @@ QList QXYSeriesPrivate::createLegendMarkers(QLegend* legend) return list << new QXYLegendMarker(q,legend); } -void QXYSeriesPrivate::initializeAxis(QAbstractAxis *axis) +void QXYSeriesPrivate::initializeAxes() { - Q_UNUSED(axis); + } QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const @@ -466,6 +467,24 @@ QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisType(Qt::Orientation orient return QAbstractAxis::AxisTypeValue; } +QAbstractAxis* QXYSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const +{ + Q_UNUSED(orientation); + return 0; +} + +void QXYSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::AnimationOptions options) +{ + XYChart *item = static_cast(m_item.data()); + Q_ASSERT(item); + if (options.testFlag(QChart::SeriesAnimations)) { + item->setAnimation(new XYAnimation(item)); + }else{ + item->setAnimation(0); + } + QAbstractSeriesPrivate::initializeAnimations(options); +} + #include "moc_qxyseries.cpp" #include "moc_qxyseries_p.cpp" diff --git a/src/xychart/qxyseries_p.h b/src/xychart/qxyseries_p.h index 876cffa..789873b 100644 --- a/src/xychart/qxyseries_p.h +++ b/src/xychart/qxyseries_p.h @@ -44,11 +44,14 @@ class QXYSeriesPrivate: public QAbstractSeriesPrivate public: QXYSeriesPrivate(QXYSeries *q); - void scaleDomain(Domain &domain); + void initializeDomain(); + void initializeAxes(); + void initializeAnimations(QtCommercialChart::QChart::AnimationOptions options); + QList createLegendMarkers(QLegend* legend); - void initializeAxis(QAbstractAxis *axis); QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const; + QAbstractAxis* createDefaultAxis(Qt::Orientation orientation) const; Q_SIGNALS: void updated(); diff --git a/src/xychart/xychart.cpp b/src/xychart/xychart.cpp index f7356b1..72b7ea6 100644 --- a/src/xychart/xychart.cpp +++ b/src/xychart/xychart.cpp @@ -32,12 +32,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE //TODO: optimize : remove points which are not visible -XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter) - : ChartElement(presenter), - m_minX(0), - m_maxX(0), - m_minY(0), - m_maxY(0), +XYChart::XYChart(QXYSeries *series,QGraphicsItem* item): + ChartItem(series->d_func(),item), m_series(series), m_animation(0), m_dirty(true) @@ -55,11 +51,6 @@ void XYChart::setGeometryPoints(const QVector& points) m_points = points; } -void XYChart::setClipRect(const QRectF &rect) -{ - m_clipRect = rect; -} - void XYChart::setAnimation(XYAnimation *animation) { m_animation = animation; @@ -70,51 +61,6 @@ void XYChart::setDirty(bool dirty) m_dirty = dirty; } -QPointF XYChart::calculateGeometryPoint(const QPointF &point) const -{ - const qreal deltaX = m_size.width() / (m_maxX - m_minX); - const qreal deltaY = m_size.height() / (m_maxY - m_minY); - qreal x = (point.x() - m_minX) * deltaX; - qreal y = (point.y() - m_minY) * -deltaY + m_size.height(); - return QPointF(x, y); -} - -QPointF XYChart::calculateGeometryPoint(int index) const -{ - const qreal deltaX = m_size.width() / (m_maxX - m_minX); - const qreal deltaY = m_size.height() / (m_maxY - m_minY); - const QList& vector = m_series->points(); - qreal x = (vector[index].x() - m_minX) * deltaX; - qreal y = (vector[index].y() - m_minY) * -deltaY + m_size.height(); - return QPointF(x, y); -} - -QVector XYChart::calculateGeometryPoints() const -{ - const qreal deltaX = m_size.width() / (m_maxX - m_minX); - const qreal deltaY = m_size.height() / (m_maxY - m_minY); - - QVector result; - result.resize(m_series->count()); - const QList& vector = m_series->points(); - for (int i = 0; i < m_series->count(); ++i) { - qreal x = (vector[i].x() - m_minX) * deltaX; - qreal y = (vector[i].y() - m_minY) * -deltaY + m_size.height(); - result[i].setX(x); - result[i].setY(y); - } - return result; -} - -QPointF XYChart::calculateDomainPoint(const QPointF &point) const -{ - const qreal deltaX = m_size.width() / (m_maxX - m_minX); - const qreal deltaY = m_size.height() / (m_maxY - m_minY); - qreal x = point.x() / deltaX + m_minX; - qreal y = (point.y() - m_size.height()) / (-deltaY) + m_minY; - return QPointF(x, y); -} - void XYChart::updateChart(QVector &oldPoints, QVector &newPoints, int index) { @@ -139,10 +85,10 @@ void XYChart::handlePointAdded(int index) QVector points; if (m_dirty) { - points = calculateGeometryPoints(); + points = domain()->calculateGeometryPoints(m_series->points()); } else { points = m_points; - QPointF point = calculateGeometryPoint(index); + QPointF point = domain()->calculateGeometryPoint(m_series->points()[index]); points.insert(index, point); } @@ -157,7 +103,7 @@ void XYChart::handlePointRemoved(int index) QVector points; if (m_dirty) { - points = calculateGeometryPoints(); + points = domain()->calculateGeometryPoints(m_series->points()); } else { points = m_points; points.remove(index); @@ -174,9 +120,9 @@ void XYChart::handlePointReplaced(int index) QVector points; if (m_dirty) { - points = calculateGeometryPoints(); + points = domain()->calculateGeometryPoints(m_series->points()); } else { - QPointF point = calculateGeometryPoint(index); + QPointF point = domain()->calculateGeometryPoint(m_series->points()[index]); points = m_points; points.replace(index, point); } @@ -187,40 +133,20 @@ void XYChart::handlePointReplaced(int index) void XYChart::handlePointsReplaced() { // All the points were replaced -> recalculate - QVector points = calculateGeometryPoints(); + QVector points = domain()->calculateGeometryPoints(m_series->points()); updateChart(m_points, points, -1); } void XYChart::handleDomainUpdated() { - m_minX = domain()->minX(); - m_maxX = domain()->maxX(); - m_minY = domain()->minY(); - m_maxY = domain()->maxY(); if (isEmpty()) return; - - QVector points = calculateGeometryPoints(); - - updateChart(m_points, points); -} - -void XYChart::handleGeometryChanged(const QRectF &rect) -{ - Q_ASSERT(rect.isValid()); - m_size = rect.size(); - m_clipRect = rect.translated(-rect.topLeft()); - m_origin = rect.topLeft(); - - if (isEmpty()) return; - - QVector points = calculateGeometryPoints(); - + QVector points = domain()->calculateGeometryPoints(m_series->points()); updateChart(m_points, points); } bool XYChart::isEmpty() { - return !m_clipRect.isValid() || qFuzzyCompare(m_maxX, m_minX) || qFuzzyCompare(m_maxY, m_minY) || m_series->points().isEmpty(); + return domain()->isEmpty() || m_series->points().isEmpty(); } #include "moc_xychart_p.cpp" diff --git a/src/xychart/xychart_p.h b/src/xychart/xychart_p.h index 2cb780d..a194207 100644 --- a/src/xychart/xychart_p.h +++ b/src/xychart/xychart_p.h @@ -41,22 +41,16 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class ChartPresenter; class QXYSeries; -class XYChart : public ChartElement +class XYChart : public ChartItem { Q_OBJECT public: - explicit XYChart(QXYSeries *series, ChartPresenter *presenter); + explicit XYChart(QXYSeries *series,QGraphicsItem* item = 0); ~XYChart() {} void setGeometryPoints(const QVector& points); QVector geometryPoints() const { return m_points; } - void setClipRect(const QRectF &rect); - QRectF clipRect() const { return m_clipRect; } - - QSizeF size() const { return m_size; } - QPointF origin() const { return m_origin; } - void setAnimation(XYAnimation *animation); ChartAnimation *animation() const { return m_animation; } virtual void updateGeometry() = 0; @@ -70,7 +64,6 @@ public Q_SLOTS: void handlePointReplaced(int index); void handlePointsReplaced(); void handleDomainUpdated(); - void handleGeometryChanged(const QRectF &size); Q_SIGNALS: void clicked(const QPointF &point); @@ -78,23 +71,12 @@ Q_SIGNALS: protected: virtual void updateChart(QVector &oldPoints, QVector &newPoints, int index = -1); - QPointF calculateGeometryPoint(const QPointF &point) const; - QPointF calculateGeometryPoint(int index) const; - QPointF calculateDomainPoint(const QPointF &point) const; - QVector calculateGeometryPoints() const; private: inline bool isEmpty(); protected: - qreal m_minX; - qreal m_maxX; - qreal m_minY; - qreal m_maxY; QXYSeries *m_series; - QSizeF m_size; - QPointF m_origin; - QRectF m_clipRect; QVector m_points; XYAnimation *m_animation; bool m_dirty; diff --git a/tests/auto/chartdataset/tst_chartdataset.cpp b/tests/auto/chartdataset/tst_chartdataset.cpp index f7d3234..d418128 100644 --- a/tests/auto/chartdataset/tst_chartdataset.cpp +++ b/tests/auto/chartdataset/tst_chartdataset.cpp @@ -43,6 +43,8 @@ QTEST_MAIN(tst_ChartDataSet) #include #include #include +#include +#include #include #include #include @@ -62,6 +64,8 @@ Q_DECLARE_METATYPE(QAbstractAxis *) Q_DECLARE_METATYPE(QAbstractSeries *) Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(Qt::Alignment) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(QLineSeries *) class tst_ChartDataSet: public QObject { @@ -79,26 +83,20 @@ private Q_SLOTS: void chartdataset(); void addSeries_data(); void addSeries(); - void setAxisX_data(); - void setAxisX(); - void setAxisY_data(); - void setAxisY(); void removeSeries_data(); void removeSeries(); void removeAllSeries_data(); void removeAllSeries(); - void seriesCount_data(); - void seriesCount(); - void seriesIndex_data(); - void seriesIndex(); - void domain_data(); - void domain(); - void zoomInDomain_data(); - void zoomInDomain(); - void zoomOutDomain_data(); - void zoomOutDomain(); - void scrollDomain_data(); - void scrollDomain(); + void addAxis_data(); + void addAxis(); + void removeAxis_data(); + void removeAxis(); + void removeAllAxes_data(); + void removeAllAxes(); + void attachAxis_data(); + void attachAxis(); + void detachAxis_data(); + void detachAxis(); private: ChartDataSet* m_dataset; @@ -117,7 +115,7 @@ void tst_ChartDataSet::cleanupTestCase() void tst_ChartDataSet::init() { - m_dataset = new ChartDataSet(); + m_dataset = new ChartDataSet(0); } @@ -136,14 +134,9 @@ void tst_ChartDataSet::chartdataset_data() void tst_ChartDataSet::chartdataset() { - QVERIFY(m_dataset->axisX(0) == 0); - QVERIFY(m_dataset->axisY(0) == 0); - QLineSeries* series = new QLineSeries(this); - QCOMPARE(m_dataset->seriesIndex(series),-1); - QVERIFY(m_dataset->domain(series) == 0); - QVERIFY(m_dataset->axisX(series) == 0); - QVERIFY(m_dataset->axisY(series) == 0); - m_dataset->createDefaultAxes(); + QVERIFY(m_dataset->axes().isEmpty()); + QVERIFY(m_dataset->series().isEmpty()); + m_dataset->createDefaultAxes(); } @@ -172,143 +165,21 @@ void tst_ChartDataSet::addSeries_data() void tst_ChartDataSet::addSeries() { - QFETCH(QAbstractSeries*, series); + QVERIFY(m_dataset->series().isEmpty()); - QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); + QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); + QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); m_dataset->addSeries(series); - m_dataset->createDefaultAxes(); - if(series->type()==QAbstractSeries::SeriesTypePie){ - TRY_COMPARE(spy0.count(), 0); - }else{ - TRY_COMPARE(spy0.count(), 2); - } - TRY_COMPARE(spy1.count(), 0); - TRY_COMPARE(spy2.count(), 1); - TRY_COMPARE(spy3.count(), 0); -} - - -void tst_ChartDataSet::setAxisX_data() -{ - QTest::addColumn >("seriesList"); - QTest::addColumn >("axisList"); - QTest::addColumn("axisCount"); - - QAbstractSeries* line = new QLineSeries(this); - QAbstractSeries* area = new QAreaSeries(static_cast(line)); - QAbstractSeries* scatter = new QScatterSeries(this); - QAbstractSeries* spline = new QSplineSeries(this); - /*QAbstractSeries* pie = */new QPieSeries(this); - /*QAbstractSeries* bar = */new QBarSeries(this); - /*QAbstractSeries* percent = */new QPercentBarSeries(this); - /*QAbstractSeries* stacked = */new QStackedBarSeries(this); - - QTest::newRow("line,spline,scatter: axis 0 axis1 axis 2") - << (QList() << line << spline << scatter) - << (QList() << new QValueAxis(this) << new QValueAxis(this) << new QValueAxis(this)) << 3; - - QTest::newRow("area: axis 0") << (QList() << area) - << (QList() << new QValueAxis(this)) << 1; - - QList axes0; - axes0 << new QValueAxis(this) << new QValueAxis(this); - axes0 << axes0.last(); - QTest::newRow("line,spline,scatter: axis 0 axis1 axis 1") - << (QList() << line << spline << scatter) - << axes0 << 2; - //TODO: add more test cases -} - -void tst_ChartDataSet::setAxisX() -{ - QFETCH(QList, seriesList); - QFETCH(QList, axisList); - QFETCH(int, axisCount); - - Q_ASSERT(seriesList.count() == axisList.count()); - - QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); - QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); - QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); - - foreach (QAbstractSeries* series, seriesList){ - m_dataset->addSeries(series); - } + QCOMPARE(m_dataset->series().count(),1); TRY_COMPARE(spy0.count(), 0); TRY_COMPARE(spy1.count(), 0); - TRY_COMPARE(spy2.count(), seriesList.count()); - TRY_COMPARE(spy3.count(), 0); - - QSignalSpy spy4(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); - QSignalSpy spy5(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy6(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); - QSignalSpy spy7(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); - - for(int i=0 ; i < seriesList.count(); i++){ - m_dataset->setAxis(seriesList.at(i),axisList.at(i),Qt::Horizontal); - } - - TRY_COMPARE(spy4.count(), axisCount); - TRY_COMPARE(spy5.count(), 0); - TRY_COMPARE(spy6.count(), 0); - TRY_COMPARE(spy7.count(), 0); - - for(int i=0 ; i < seriesList.count(); i++){ - QVERIFY(m_dataset->axisX(seriesList.at(i)) == axisList.at(i)); - } -} - -void tst_ChartDataSet::setAxisY_data() -{ - setAxisX_data(); -} - -void tst_ChartDataSet::setAxisY() -{ - QFETCH(QList, seriesList); - QFETCH(QList, axisList); - QFETCH(int, axisCount); - - Q_ASSERT(seriesList.count() == axisList.count()); - - QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); - QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); - QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); - - foreach (QAbstractSeries* series, seriesList){ - m_dataset->addSeries(series); - } - - TRY_COMPARE(spy0.count(), 0); - TRY_COMPARE(spy1.count(), 0); - TRY_COMPARE(spy2.count(), seriesList.count()); + TRY_COMPARE(spy2.count(), 1); TRY_COMPARE(spy3.count(), 0); - - QSignalSpy spy4(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); - QSignalSpy spy5(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy6(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); - QSignalSpy spy7(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); - - for(int i=0 ; i < seriesList.count(); i++){ - m_dataset->setAxis(seriesList.at(i),axisList.at(i),Qt::Vertical); - } - - TRY_COMPARE(spy4.count(), axisCount); - TRY_COMPARE(spy5.count(), 0); - TRY_COMPARE(spy6.count(), 0); - TRY_COMPARE(spy7.count(), 0); - - for(int i=0 ; i < seriesList.count(); i++){ - QVERIFY(m_dataset->axisY(seriesList.at(i)) == axisList.at(i)); - } } void tst_ChartDataSet::removeSeries_data() @@ -319,24 +190,19 @@ void tst_ChartDataSet::removeSeries_data() void tst_ChartDataSet::removeSeries() { QFETCH(QAbstractSeries*, series); - + QVERIFY(m_dataset->series().isEmpty()); m_dataset->addSeries(series); - m_dataset->createDefaultAxes(); - QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); + QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); + QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); m_dataset->removeSeries(series); + QCOMPARE(m_dataset->series().count(),0); TRY_COMPARE(spy0.count(), 0); - if (series->type() == QAbstractSeries::SeriesTypePie) { - TRY_COMPARE(spy1.count(), 0); - } - else { - TRY_COMPARE(spy1.count(), 2); - } + TRY_COMPARE(spy1.count(), 0); TRY_COMPARE(spy2.count(), 0); TRY_COMPARE(spy3.count(), 1); } @@ -345,298 +211,270 @@ void tst_ChartDataSet::removeAllSeries_data() { QTest::addColumn >("seriesList"); QTest::addColumn >("axisList"); - QTest::addColumn("axisCount"); - - QTest::newRow("line,spline,scatter: axis 0 axis1 axis 2") - << (QList() << new QLineSeries(this) << new QSplineSeries(this) - << new QScatterSeries(this)) - << (QList() << new QValueAxis(this) << new QValueAxis(this) - << new QValueAxis(this)) << 3; - //TODO: + + QList series; + QList axis; + + series << new QLineSeries(this) << new QSplineSeries(this) << new QScatterSeries(this); + axis << new QValueAxis(this) << new QValueAxis(this) << new QValueAxis(this); + + QTest::newRow("3 series , 3 axis") << series << axis; } void tst_ChartDataSet::removeAllSeries() { QFETCH(QList, seriesList); QFETCH(QList, axisList); - QFETCH(int, axisCount); + + QCOMPARE(m_dataset->series().count(),0); + QCOMPARE(m_dataset->axes().count(),0); foreach (QAbstractSeries* series, seriesList) { m_dataset->addSeries(series); } + foreach (QAbstractAxis* axis, axisList) { + m_dataset->addAxis(axis,Qt::AlignBottom); + } + for (int i = 0; i < seriesList.count(); i++) { - m_dataset->setAxis(seriesList.at(i), axisList.at(i),Qt::Horizontal); + m_dataset->attachAxis(seriesList.at(i),axisList.at(i)); } - QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); + QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); + QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); m_dataset->removeAllSeries(); TRY_COMPARE(spy0.count(), 0); - TRY_COMPARE(spy1.count(), axisCount); + TRY_COMPARE(spy1.count(), 0); TRY_COMPARE(spy2.count(), 0); TRY_COMPARE(spy3.count(), seriesList.count()); -} + QCOMPARE(m_dataset->series().count(),0); + QCOMPARE(m_dataset->axes().count(),axisList.count()); +} -void tst_ChartDataSet::seriesCount_data() +void tst_ChartDataSet::addAxis_data() { - QTest::addColumn >("seriesList"); - QTest::addColumn("seriesCount"); - - QTest::newRow("line,line, line, spline 3") << (QList() << new QLineSeries(this) << new QLineSeries(this) << new QLineSeries(this) << new QSplineSeries(this) ) << 3; - QTest::newRow("scatter,scatter, line, line 2") << (QList() << new QScatterSeries(this) << new QScatterSeries(this) << new QLineSeries(this) << new QLineSeries(this) ) << 2; + QTest::addColumn("axis"); + QAbstractAxis* value = new QValueAxis(this); + QAbstractAxis* category = new QCategoryAxis(this); + QAbstractAxis* barcategory = new QBarCategoryAxis(this); + QAbstractAxis* datetime = new QDateTimeAxis(this); + + QTest::newRow("value") << value; + QTest::newRow("category") << category; + QTest::newRow("barcategory") << barcategory; + QTest::newRow("datetime") << datetime; } -void tst_ChartDataSet::seriesCount() +void tst_ChartDataSet::addAxis() { - QFETCH(QList, seriesList); - QFETCH(int, seriesCount); + QFETCH(QAbstractAxis*, axis); + QVERIFY(m_dataset->axes().isEmpty()); - foreach (QAbstractSeries* series, seriesList){ - m_dataset->addSeries(series); - } - - QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); + QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); + QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); - QCOMPARE(m_dataset->seriesCount(seriesList.at(0)->type()),seriesCount); - TRY_COMPARE(spy0.count(), 0); + m_dataset->addAxis(axis,Qt::AlignBottom); + + QCOMPARE(m_dataset->axes().count(),1); + TRY_COMPARE(spy0.count(), 1); TRY_COMPARE(spy1.count(), 0); TRY_COMPARE(spy2.count(), 0); TRY_COMPARE(spy3.count(), 0); } -void tst_ChartDataSet::seriesIndex_data() +void tst_ChartDataSet::removeAxis_data() { - QTest::addColumn >("seriesList"); - - QTest::newRow("line,line, line, spline") << (QList() << new QLineSeries(this) << new QLineSeries(this) << new QLineSeries(this) << new QSplineSeries(this) ); - QTest::newRow("scatter,scatter, line, line") << (QList() << new QScatterSeries(this) << new QScatterSeries(this) << new QLineSeries(this) << new QLineSeries(this) ); + addAxis_data(); } -void tst_ChartDataSet::seriesIndex() +void tst_ChartDataSet::removeAxis() { + QFETCH(QAbstractAxis*, axis); + QVERIFY(m_dataset->series().isEmpty()); + m_dataset->addAxis(axis,Qt::AlignBottom); - QFETCH(QList, seriesList); - - foreach (QAbstractSeries* series, seriesList) { - m_dataset->addSeries(series); - } - - QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); + QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); + QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); - for (int i = 0; i < seriesList.count(); i++) { - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i); - } + m_dataset->removeAxis(axis); + QCOMPARE(m_dataset->series().count(),0); TRY_COMPARE(spy0.count(), 0); - TRY_COMPARE(spy1.count(), 0); + TRY_COMPARE(spy1.count(), 1); TRY_COMPARE(spy2.count(), 0); TRY_COMPARE(spy3.count(), 0); +} - foreach (QAbstractSeries* series, seriesList) { - m_dataset->removeSeries(series); - } - - for (int i = 0; i < seriesList.count(); i++) { - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), -1); - } - - foreach (QAbstractSeries* series, seriesList) { - m_dataset->addSeries(series); - } - - for (int i = 0; i < seriesList.count(); i++) { - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i); - } +void tst_ChartDataSet::removeAllAxes_data() +{ + QTest::addColumn >("seriesList"); + QTest::addColumn >("axisList"); - m_dataset->removeSeries(seriesList.at(1)); + QList series; + QList axis; - for (int i = 0; i < seriesList.count(); i++) { - if (i != 1) - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i); - else - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), -1); - } + series << new QLineSeries(this) << new QSplineSeries(this) << new QScatterSeries(this); + axis << new QValueAxis(this) << new QValueAxis(this) << new QValueAxis(this); - m_dataset->addSeries(seriesList.at(1)); + QTest::newRow("3 series , 3 axis") << series << axis; +} - for (int i = 0; i < seriesList.count(); i++) { - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i); - } +void tst_ChartDataSet::removeAllAxes() +{ + QFETCH(QList, seriesList); + QFETCH(QList, axisList); - m_dataset->removeSeries(seriesList.at(2)); + QCOMPARE(m_dataset->series().count(),0); + QCOMPARE(m_dataset->axes().count(),0); - for (int i = 0; i < seriesList.count(); i++) { - if (i != 2) - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i); - else - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), -1); + foreach (QAbstractSeries* series, seriesList) { + m_dataset->addSeries(series); } - m_dataset->removeSeries(seriesList.at(0)); - - for (int i = 0; i < seriesList.count(); i++) { - if (i != 2 && i != 0) - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i); - else - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), -1); + foreach (QAbstractAxis* axis, axisList) { + m_dataset->addAxis(axis,Qt::AlignBottom); } - m_dataset->addSeries(seriesList.at(2)); - m_dataset->addSeries(seriesList.at(0)); - for (int i = 0; i < seriesList.count(); i++) { - if (i == 2) - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), 0); - else if (i == 0) - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), 2); - else - QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i); + m_dataset->attachAxis(seriesList.at(i),axisList.at(i)); } -} - -void tst_ChartDataSet::domain_data() -{ - addSeries_data(); -} - -void tst_ChartDataSet::domain() -{ - QFETCH(QAbstractSeries*, series); - - QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*))); + QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); - QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*))); + QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); - m_dataset->addSeries(series); - QVERIFY(m_dataset->domain(series)); - + m_dataset->removeAllAxes(); TRY_COMPARE(spy0.count(), 0); - TRY_COMPARE(spy1.count(), 0); - TRY_COMPARE(spy2.count(), 1); - - QList arguments = spy2.takeFirst(); - Domain *domain = (Domain *) arguments.at(1).value(); - QVERIFY(m_dataset->domain(series) == domain); - + TRY_COMPARE(spy1.count(), axisList.count()); + TRY_COMPARE(spy2.count(), 0); TRY_COMPARE(spy3.count(), 0); + QCOMPARE(m_dataset->series().count(),seriesList.count()); + QCOMPARE(m_dataset->axes().count(),0); } -void tst_ChartDataSet::zoomInDomain_data() -{ - QTest::addColumn("sameAxis"); - QTest::addColumn >("seriesList"); - QTest::newRow("sameAxis: line,line, line, spline") << true << (QList() << new QLineSeries(this) << new QLineSeries(this) << new QLineSeries(this) << new QSplineSeries(this) ); - QTest::newRow("separeateAxis: line,line, line, spline") << false << (QList() << new QLineSeries(this) << new QLineSeries(this) << new QLineSeries(this) << new QSplineSeries(this) ); -} - -void tst_ChartDataSet::zoomInDomain() +void tst_ChartDataSet::attachAxis_data() { - QFETCH(bool, sameAxis); - QFETCH(QList, seriesList); - - foreach (QAbstractSeries* series, seriesList) { - m_dataset->addSeries(series); - } - - if(sameAxis) m_dataset->createDefaultAxes(); - - QList spyList; - - foreach (QAbstractSeries* series, seriesList) { - spyList << new QSignalSpy(m_dataset->domain(series),SIGNAL(updated())); - } + QTest::addColumn >("series"); + QTest::addColumn >("axis"); + QTest::addColumn >("alignment"); + QTest::addColumn("attachSeries"); + QTest::addColumn("attachAxis"); + QTest::addColumn("success"); - m_dataset->zoomInDomain(QRect(0, 0, 100, 100), QSize(1000, 1000)); + QList series; + QList axes; + QList alignment; - foreach (QSignalSpy* spy, spyList) { - TRY_COMPARE(spy->count(), 1); - } + QAbstractSeries* line = new QLineSeries(this); + QAbstractSeries* area = new QAreaSeries(static_cast(line)); + QAbstractSeries* scatter = new QScatterSeries(this); + QAbstractSeries* spline = new QSplineSeries(this); + QAbstractSeries* pie = new QPieSeries(this); + QAbstractSeries* bar = new QBarSeries(this); + QAbstractSeries* percent = new QPercentBarSeries(this); + QAbstractSeries* stacked = new QStackedBarSeries(this); - qDeleteAll(spyList); -} + QAbstractAxis* value1 = new QValueAxis(this); + QAbstractAxis* value2 = new QValueAxis(this); + QAbstractAxis* category = new QCategoryAxis(this); + QAbstractAxis* barcategory = new QBarCategoryAxis(this); + QAbstractAxis* datetime = new QDateTimeAxis(this); + series << line << 0; + axes << value1 << value2; + alignment << Qt::AlignBottom << Qt::AlignLeft; + QTest::newRow("line + two axes") << series << axes << alignment << line << value2 << true; -void tst_ChartDataSet::zoomOutDomain_data() -{ - zoomInDomain_data(); } -void tst_ChartDataSet::zoomOutDomain() +void tst_ChartDataSet::attachAxis() { - QFETCH(bool, sameAxis); - QFETCH(QList, seriesList); - - foreach (QAbstractSeries* series, seriesList) { - m_dataset->addSeries(series); - } + QFETCH(QList, series); + QFETCH(QList, axis); + QFETCH(QList, alignment); + QFETCH(QAbstractSeries*, attachSeries); + QFETCH(QAbstractAxis*, attachAxis); + QFETCH(bool, success); - if (sameAxis) - m_dataset->createDefaultAxes(); + Q_ASSERT(series.count() == axis.count()); + Q_ASSERT(series.count() == alignment.count()); - QList spyList; + QVERIFY(m_dataset->series().isEmpty()); + QVERIFY(m_dataset->axes().isEmpty()); - foreach (QAbstractSeries* series, seriesList) { - spyList << new QSignalSpy(m_dataset->domain(series), SIGNAL(updated())); + for(int i = 0 ; i < series.count() ; i++){ + if(series[i]) m_dataset->addSeries(series[i]); + if(axis[i]) m_dataset->addAxis(axis[i],alignment[i]); + if(series[i] && axis[i]) m_dataset->attachAxis(series[i],axis[i]); } - m_dataset->zoomOutDomain(QRect(0, 0, 100, 100), QSize(1000, 1000)); + QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); + QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); + QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); + QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); - foreach (QSignalSpy* spy, spyList) { - TRY_COMPARE(spy->count(), 1); - } + QCOMPARE(m_dataset->attachAxis(attachSeries,attachAxis),success); - qDeleteAll (spyList); } -void tst_ChartDataSet::scrollDomain_data() +void tst_ChartDataSet::detachAxis_data() { - zoomInDomain_data(); -} + QTest::addColumn >("series"); + QTest::addColumn >("axis"); + QTest::addColumn("detachSeries"); + QTest::addColumn("detachAxis"); + QTest::addColumn("success"); -void tst_ChartDataSet::scrollDomain() -{ - QFETCH(bool, sameAxis); - QFETCH(QList, seriesList); + QList series; + QList axes; - foreach (QAbstractSeries* series, seriesList) { - m_dataset->addSeries(series); - } + QAbstractSeries* line = new QLineSeries(this); + QAbstractAxis* value = new QValueAxis(this); - if (sameAxis) - m_dataset->createDefaultAxes(); + series << line; + axes << value; + QTest::newRow("line + axis") << series << axes << line << value << true; +} - QList spyList; +void tst_ChartDataSet::detachAxis() +{ + QFETCH(QList, series); + QFETCH(QList, axis); + QFETCH(QAbstractSeries*, detachSeries); + QFETCH(QAbstractAxis*, detachAxis); + QFETCH(bool, success); - foreach (QAbstractSeries* series, seriesList) { - spyList - << new QSignalSpy(m_dataset->domain(series), - SIGNAL(updated())); - } + Q_ASSERT(series.count() == axis.count()); - m_dataset->scrollDomain(10, 10, QSize(1000, 1000)); + QVERIFY(m_dataset->series().isEmpty()); + QVERIFY(m_dataset->axes().isEmpty()); - foreach (QSignalSpy* spy, spyList) { - TRY_COMPARE(spy->count(), 1); + for(int i = 0; i < series.count(); i++) { + if(series[i]) m_dataset->addSeries(series[i]); + if(axis[i]) m_dataset->addAxis(axis[i],Qt::AlignBottom); + if(series[i] && axis[i]) m_dataset->attachAxis(series[i],axis[i]); } - qDeleteAll(spyList); + QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*))); + QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); + QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*))); + QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*))); + + QCOMPARE(m_dataset->detachAxis(detachSeries,detachAxis),success); } QTEST_MAIN(tst_ChartDataSet) diff --git a/tests/auto/domain/tst_domain.cpp b/tests/auto/domain/tst_domain.cpp index 3e40463..4980731 100644 --- a/tests/auto/domain/tst_domain.cpp +++ b/tests/auto/domain/tst_domain.cpp @@ -49,19 +49,19 @@ QTCOMMERCIALCHART_USE_NAMESPACE Q_DECLARE_METATYPE(Domain*) Q_DECLARE_METATYPE(QSizeF) +Q_DECLARE_METATYPE(QMargins) class AxisMock: public QAbstractAxisPrivate { Q_OBJECT public: - AxisMock(Qt::Orientation orientation):QAbstractAxisPrivate(0){ setOrientation(orientation);}; - ChartAxis* createGraphics(ChartPresenter* presenter) + AxisMock(Qt::Alignment alignment):QAbstractAxisPrivate(0){ setAlignment(alignment);}; + void initializeGraphics(QGraphicsItem* item) { - Q_UNUSED(presenter); - return 0; + Q_UNUSED(item); }; - void intializeDomain(Domain* domain) + void initializeDomain(Domain* domain) { Q_UNUSED(domain); }; @@ -81,6 +81,13 @@ public: Q_UNUSED(max); }; + void setRange(qreal min, qreal max) + { + m_min=min; + m_max=max; + emit rangeChanged(min,max); + }; + int count () const { return m_count; } void handleDomainUpdated(){}; @@ -102,10 +109,10 @@ public Q_SLOTS: private Q_SLOTS: void domain(); - void handleAxisUpdatedX_data(); - void handleAxisUpdatedX(); - void handleAxisUpdatedY_data(); - void handleAxisUpdatedY(); + void handleHorizontalAxisRangeChanged_data(); + void handleHorizontalAxisRangeChanged(); + void handleVerticalAxisRangeChanged_data(); + void handleVerticalAxisRangeChanged(); void isEmpty_data(); void isEmpty(); void maxX_data(); @@ -128,8 +135,10 @@ private Q_SLOTS: void spanX(); void spanY_data(); void spanY(); - void zoom_data(); - void zoom(); + void zoomIn_data(); + void zoomIn(); + void zoomOut_data(); + void zoomOut(); void move_data(); void move(); }; @@ -161,7 +170,7 @@ void tst_Domain::domain() QCOMPARE(domain.minY(), 0.0); } -void tst_Domain::handleAxisUpdatedX_data() +void tst_Domain::handleHorizontalAxisRangeChanged_data() { QTest::addColumn("min"); QTest::addColumn("max"); @@ -170,7 +179,7 @@ void tst_Domain::handleAxisUpdatedX_data() QTest::newRow("-1 0") << -1.0 << 0.0; } -void tst_Domain::handleAxisUpdatedX() +void tst_Domain::handleHorizontalAxisRangeChanged() { QFETCH(qreal, min); QFETCH(qreal, max); @@ -178,14 +187,12 @@ void tst_Domain::handleAxisUpdatedX() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); - AxisMock axis(Qt::Horizontal); - QObject::connect(&axis,SIGNAL(updated()),&domain,SLOT(handleAxisUpdated())); - axis.m_min=min; - axis.m_max=max; - axis.emitUpdated(); + AxisMock axis(Qt::AlignBottom); + QObject::connect(&axis,SIGNAL(rangeChanged(qreal,qreal)),&domain,SLOT(handleHorizontalAxisRangeChanged(qreal,qreal))); + axis.setRange(min,max); QVERIFY(qFuzzyCompare(domain.minX(), min)); QVERIFY(qFuzzyCompare(domain.maxX(), max)); @@ -200,7 +207,7 @@ void tst_Domain::handleAxisUpdatedX() } -void tst_Domain::handleAxisUpdatedY_data() +void tst_Domain::handleVerticalAxisRangeChanged_data() { QTest::addColumn("min"); QTest::addColumn("max"); @@ -209,7 +216,7 @@ void tst_Domain::handleAxisUpdatedY_data() QTest::newRow("-1 0") << -1.0 << 0.0; } -void tst_Domain::handleAxisUpdatedY() +void tst_Domain::handleVerticalAxisRangeChanged() { QFETCH(qreal, min); QFETCH(qreal, max); @@ -217,14 +224,12 @@ void tst_Domain::handleAxisUpdatedY() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); - AxisMock axis(Qt::Vertical); - QObject::connect(&axis, SIGNAL(updated()), &domain, SLOT(handleAxisUpdated())); - axis.m_min = min; - axis.m_max = max; - axis.emitUpdated(); + AxisMock axis(Qt::AlignLeft); + QObject::connect(&axis, SIGNAL(rangeChanged(qreal,qreal)), &domain, SLOT(handleVerticalAxisRangeChanged(qreal,qreal))); + axis.setRange(min,max); QVERIFY(qFuzzyCompare(domain.minY(), min)); QVERIFY(qFuzzyCompare(domain.maxY(), max)); @@ -244,11 +249,13 @@ void tst_Domain::isEmpty_data() QTest::addColumn("maxX"); QTest::addColumn("minY"); QTest::addColumn("maxY"); + QTest::addColumn("size"); QTest::addColumn("isEmpty"); - QTest::newRow("0 0 0 0") << 0.0 << 0.0 << 0.0 << 0.0 << true; - QTest::newRow("0 1 0 0") << 0.0 << 1.0 << 0.0 << 0.0 << true; - QTest::newRow("0 0 0 1") << 0.0 << 1.0 << 0.0 << 0.0 << true; - QTest::newRow("0 1 0 1") << 0.0 << 1.0 << 0.0 << 1.0 << false; + QTest::newRow("0 0 0 0") << 0.0 << 0.0 << 0.0 << 0.0 << QSizeF(1,1) << true; + QTest::newRow("0 1 0 0") << 0.0 << 1.0 << 0.0 << 0.0 << QSizeF(1,1) << true; + QTest::newRow("0 0 0 1") << 0.0 << 1.0 << 0.0 << 0.0 << QSizeF(1,1) << true; + QTest::newRow("0 1 0 1") << 0.0 << 1.0 << 0.0 << 1.0 << QSizeF(1,1) << false; + QTest::newRow("0 1 0 1") << 0.0 << 1.0 << 0.0 << 1.0 << QSizeF(-11,1) << true; } void tst_Domain::isEmpty() @@ -257,10 +264,12 @@ void tst_Domain::isEmpty() QFETCH(qreal, maxX); QFETCH(qreal, minY); QFETCH(qreal, maxY); + QFETCH(QSizeF, size); QFETCH(bool, isEmpty); Domain domain; domain.setRange(minX, maxX, minY, maxY); + domain.setSize(size); QCOMPARE(domain.isEmpty(), isEmpty); } @@ -283,8 +292,8 @@ void tst_Domain::maxX() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); domain.setMaxX(maxX1); QCOMPARE(domain.maxX(), maxX1); @@ -316,8 +325,8 @@ void tst_Domain::maxY() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); domain.setMaxY(maxY1); QCOMPARE(domain.maxY(), maxY1); @@ -348,8 +357,8 @@ void tst_Domain::minX() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); domain.setMinX(minX1); QCOMPARE(domain.minX(), minX1); @@ -380,8 +389,8 @@ void tst_Domain::minY() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); domain.setMinY(minY1); QCOMPARE(domain.minY(), minY1); @@ -430,8 +439,8 @@ void tst_Domain::operatorEquals() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); QCOMPARE(*domain1==*domain2, equals); QCOMPARE(*domain1!=*domain2, notEquals); @@ -462,8 +471,8 @@ void tst_Domain::setRange() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); domain.setRange(minX, maxX, minY, maxY); @@ -495,8 +504,8 @@ void tst_Domain::setRangeX() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); domain.setRangeX(min, max); @@ -529,8 +538,8 @@ void tst_Domain::setRangeY() Domain domain; QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); domain.setRangeY(min, max); @@ -566,8 +575,8 @@ void tst_Domain::spanX() domain.setRangeX(minX, maxX); QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); QCOMPARE(domain.spanX(), spanX); @@ -596,8 +605,8 @@ void tst_Domain::spanY() domain.setRangeY(minY, maxY); QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); QCOMPARE(domain.spanY(), spanY); @@ -606,93 +615,123 @@ void tst_Domain::spanY() TRY_COMPARE(spy2.count(), 0); } -void tst_Domain::zoom_data() +void tst_Domain::zoomIn_data() { - QTest::addColumn("rect0"); - QTest::addColumn("size0"); - QTest::addColumn("rect1"); - QTest::addColumn("size1"); - QTest::addColumn("rect2"); - QTest::addColumn("size2"); - QTest::newRow("first") << QRectF(10, 10, 100, 100) << QSizeF(1000, 1000) - << QRectF(20, 20, 100, 100) << QSizeF(1000, 1000) << QRectF(50, 50, 100, 100) - << QSizeF(1000, 1000); - QTest::newRow("scound") << QRectF(10, 10, 50, 50) << QSizeF(1000, 1000) - << QRectF(20, 20, 100, 100) << QSizeF(1000, 1000) << QRectF(50, 50, 100, 100) - << QSizeF(1000, 1000); - QTest::newRow("third") << QRectF(10, 10, 10, 10) << QSizeF(100, 100) << QRectF(20, 20, 20, 20) - << QSizeF(100, 100) << QRectF(50, 50, 50, 50) << QSizeF(100, 100); + QTest::addColumn("range"); + QTest::addColumn("size"); + QTest::addColumn("zoom"); + QTest::addColumn("result"); + + QTest::newRow("first") << QMargins(0,0,1000,1000) << QSizeF(1000, 1000) << + QMargins(100, 100, 900, 900) << QMargins(100,100,900,900); + QTest::newRow("second") << QMargins(0,0,2000,2000) << QSizeF(1000, 1000) << + QMargins(100, 100, 900, 900) << QMargins(200,200,1800,1800); } -void tst_Domain::zoom() +void tst_Domain::zoomIn() { - QFETCH(QRectF, rect0); - QFETCH(QSizeF, size0); - QFETCH(QRectF, rect1); - QFETCH(QSizeF, size1); - QFETCH(QRectF, rect2); - QFETCH(QSizeF, size2); + QFETCH(QMargins, range); + QFETCH(QSizeF, size); + QFETCH(QMargins, zoom); + QFETCH(QMargins, result); Domain domain; + domain.setRange(range.left(), range.right(), range.top(),range.bottom()); + domain.setSize(size); + + QSignalSpy spy0(&domain, SIGNAL(updated())); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); + + domain.zoomIn(QRectF(zoom.left(),zoom.top(),zoom.right()-zoom.left(),zoom.bottom()-zoom.top())); + + QCOMPARE(domain.minX(),qreal(result.left())); + QCOMPARE(domain.maxX(),qreal(result.right())); + QCOMPARE(domain.minY(),qreal(result.top())); + QCOMPARE(domain.maxY(),qreal(result.bottom())); + + TRY_COMPARE(spy0.count(), 1); + TRY_COMPARE(spy1.count(), 1); + TRY_COMPARE(spy2.count(), 1); +} + +void tst_Domain::zoomOut_data() +{ + QTest::addColumn("range"); + QTest::addColumn("size"); + QTest::addColumn("zoom"); + QTest::addColumn("result"); + + QTest::newRow("first") << QMargins(100,100,900,900) << QSizeF(1000, 1000) << + QMargins(100, 100, 900, 900) << QMargins(0,0,1000,1000); + QTest::newRow("second") << QMargins(200,200,1800,1800) << QSizeF(1000, 1000) << + QMargins(100, 100, 900, 900) << QMargins(0,0,2000,2000); +} + +void tst_Domain::zoomOut() +{ + QFETCH(QMargins, range); + QFETCH(QSizeF, size); + QFETCH(QMargins, zoom); + QFETCH(QMargins, result); - domain.setRange(0, 1000, 0, 1000); + Domain domain; + domain.setRange(range.left(), range.right(), range.top(),range.bottom()); + domain.setSize(size); QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); - - Domain domain0; - domain0.setRange(domain.minX(), domain.maxX(), domain.minY(), domain.maxY()); - domain.zoomIn(rect0, size0); - Domain domain1; - domain1.setRange(domain.minX(), domain.maxX(), domain.minY(), domain.maxY()); - domain.zoomIn(rect1, size1); - Domain domain2; - domain2.setRange(domain.minX(), domain.maxX(), domain.minY(), domain.maxY()); - domain.zoomIn(rect2, size2); - domain.zoomOut(rect2, size2); - QCOMPARE(domain == domain2, true); - domain.zoomOut(rect1, size1); - QCOMPARE(domain == domain1, true); - domain.zoomOut(rect0, size0); - QCOMPARE(domain == domain0, true); - TRY_COMPARE(spy0.count(), 6); - TRY_COMPARE(spy1.count(), 6); - TRY_COMPARE(spy2.count(), 6); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); + + domain.zoomOut(QRectF(zoom.left(),zoom.top(),zoom.right()-zoom.left(),zoom.bottom()-zoom.top())); + + QCOMPARE(domain.minX(),qreal(result.left())); + QCOMPARE(domain.maxX(),qreal(result.right())); + QCOMPARE(domain.minY(),qreal(result.top())); + QCOMPARE(domain.maxY(),qreal(result.bottom())); + + TRY_COMPARE(spy0.count(), 1); + TRY_COMPARE(spy1.count(), 1); + TRY_COMPARE(spy2.count(), 1); } void tst_Domain::move_data() { + QTest::addColumn("range"); + QTest::addColumn("size"); QTest::addColumn("dx"); QTest::addColumn("dy"); - QTest::addColumn("size"); - QTest::newRow("dx 100, dy 0, size 1000x1000") << 100 << 0 << QSizeF(1000, 1000); - QTest::newRow("dx 0, dy 100, size 1000x1000") << 0 << 100 << QSizeF(1000, 1000); - QTest::newRow("dx -100, dy 0, size 1000x1000") << -100 << 0 << QSizeF(1000, 1000); - QTest::newRow("dx 0, dy -100, size 1000x1000") << 0 << -100 << QSizeF(1000, 1000); - QTest::newRow("dx 100, dy 100, size 1000x1000") << 100 << 100 << QSizeF(1000, 1000); - QTest::newRow("dx 100, dy 50, size 1000x1000") << 100 << 50 << QSizeF(1000, 1000); + QTest::addColumn("result"); + + QTest::newRow("first") << QMargins(0,0,1000,1000) << QSizeF(1000, 1000) << + 10 << 10 << QMargins(10,10,1010,1010); + QTest::newRow("second") << QMargins(0,0,1000,1000) << QSizeF(1000, 1000) << + -10 << -10 << QMargins(-10,-10,990,990); } void tst_Domain::move() { + QFETCH(QMargins, range); + QFETCH(QSizeF, size); QFETCH(int, dx); QFETCH(int, dy); - QFETCH(QSizeF, size); - Domain domain; + QFETCH(QMargins, result); - domain.setRange(0, size.width(), 0, size.height()); + Domain domain; + domain.setRange(range.left(), range.right(), range.top(),range.bottom()); + domain.setSize(size); QSignalSpy spy0(&domain, SIGNAL(updated())); - QSignalSpy spy1(&domain, SIGNAL(rangeXChanged(qreal,qreal))); - QSignalSpy spy2(&domain, SIGNAL(rangeYChanged(qreal,qreal))); + QSignalSpy spy1(&domain, SIGNAL(rangeHorizontalChanged(qreal,qreal))); + QSignalSpy spy2(&domain, SIGNAL(rangeVerticalChanged(qreal,qreal))); - domain.move(dx, dy, size); + domain.move(dx, dy); - Domain result; - result.setRange(dx, size.width() + dx, dy, size.height() + dy); + QCOMPARE(domain.minX(),qreal(result.left())); + QCOMPARE(domain.maxX(),qreal(result.right())); + QCOMPARE(domain.minY(),qreal(result.top())); + QCOMPARE(domain.maxY(),qreal(result.bottom())); - QCOMPARE(domain == result, true); TRY_COMPARE(spy0.count(), 1); TRY_COMPARE(spy1.count(), (dx != 0 ? 1 : 0)); TRY_COMPARE(spy2.count(), (dy != 0 ? 1 : 0)); diff --git a/tests/auto/qabstractaxis/tst_qabstractaxis.cpp b/tests/auto/qabstractaxis/tst_qabstractaxis.cpp index 6807ed4..a3c618d 100644 --- a/tests/auto/qabstractaxis/tst_qabstractaxis.cpp +++ b/tests/auto/qabstractaxis/tst_qabstractaxis.cpp @@ -106,7 +106,7 @@ void tst_QAbstractAxis::axisPen() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -159,7 +159,7 @@ void tst_QAbstractAxis::gridLinePen() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -201,7 +201,7 @@ void tst_QAbstractAxis::lineVisible() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -243,7 +243,7 @@ void tst_QAbstractAxis::gridLineVisible() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -286,7 +286,7 @@ void tst_QAbstractAxis::visible() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -327,7 +327,7 @@ void tst_QAbstractAxis::labelsAngle() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -370,7 +370,7 @@ void tst_QAbstractAxis::labelsBrush() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -421,7 +421,7 @@ void tst_QAbstractAxis::labelsFont() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -463,7 +463,7 @@ void tst_QAbstractAxis::labelsPen() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -505,7 +505,7 @@ void tst_QAbstractAxis::labelsVisible() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -545,7 +545,7 @@ void tst_QAbstractAxis::orientation() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -646,7 +646,7 @@ void tst_QAbstractAxis::shadesBrush() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -697,7 +697,7 @@ void tst_QAbstractAxis::shadesPen() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -739,7 +739,7 @@ void tst_QAbstractAxis::shadesVisible() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -778,7 +778,7 @@ void tst_QAbstractAxis::show() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); @@ -812,7 +812,7 @@ void tst_QAbstractAxis::hide() QSignalSpy spy0(m_axis, SIGNAL(lineVisibleChanged(bool))); QSignalSpy spy1(m_axis, SIGNAL(colorChanged(QColor))); - QSignalSpy spy2(m_axis, SIGNAL(gridVisibleChanged(bool))); + QSignalSpy spy2(m_axis, SIGNAL(gridLineVisibleChanged(bool))); QSignalSpy spy3(m_axis, SIGNAL(labelsColorChanged(QColor))); QSignalSpy spy4(m_axis, SIGNAL(labelsVisibleChanged(bool))); QSignalSpy spy5(m_axis, SIGNAL(shadesBorderColorChanged(QColor))); diff --git a/tests/auto/qvalueaxis/tst_qvalueaxis.cpp b/tests/auto/qvalueaxis/tst_qvalueaxis.cpp index 2030e86..6d31039 100644 --- a/tests/auto/qvalueaxis/tst_qvalueaxis.cpp +++ b/tests/auto/qvalueaxis/tst_qvalueaxis.cpp @@ -47,8 +47,8 @@ private slots: void min(); void min_animation_data(); void min_animation(); - void niceNumbersEnabled_data(); - void niceNumbersEnabled(); + void applyNiceNumbers_data(); + void applyNiceNumbers(); void range_raw_data(); void range_raw(); void range_data(); @@ -215,7 +215,7 @@ void tst_QValueAxis::min_animation() min(); } -void tst_QValueAxis::niceNumbersEnabled_data() +void tst_QValueAxis::applyNiceNumbers_data() { QTest::addColumn("niceNumbersEnabled"); QTest::addColumn("min"); @@ -232,7 +232,7 @@ void tst_QValueAxis::niceNumbersEnabled_data() QTest::newRow("true 5, 93.5 , 5") << true << (qreal)5.0 << (qreal)93.5 << 5 << (qreal)0.0 << (qreal)100.0 << 6; } -void tst_QValueAxis::niceNumbersEnabled() +void tst_QValueAxis::applyNiceNumbers() { QFETCH(bool, niceNumbersEnabled); QFETCH(qreal, min); @@ -252,8 +252,7 @@ void tst_QValueAxis::niceNumbersEnabled() QSignalSpy spy1(m_valuesaxis, SIGNAL(minChanged(qreal))); QSignalSpy spy2(m_valuesaxis, SIGNAL(rangeChanged(qreal,qreal))); - m_valuesaxis->setNiceNumbersEnabled(niceNumbersEnabled); - QCOMPARE(m_valuesaxis->niceNumbersEnabled(), niceNumbersEnabled); + if(niceNumbersEnabled) m_valuesaxis->applyNiceNumbers(); if(!qFuzzyCompare(expectedMin, min)) QCOMPARE(spy1.count(), 1);