From e59660aaa64a66517d2cbb9c0b4cd546cf622f38 2012-02-22 11:06:57 From: Marek Rosa Date: 2012-02-22 11:06:57 Subject: [PATCH] Merge branch 'master' of https://git.it.local/repos/QtCommercialDevel-13049/charts --- diff --git a/example/stackedbarchart/main.cpp b/example/stackedbarchart/main.cpp index c593b92..cf6294c 100644 --- a/example/stackedbarchart/main.cpp +++ b/example/stackedbarchart/main.cpp @@ -26,15 +26,15 @@ int main(int argc, char *argv[]) // Create some test data to chart *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; - *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2; - *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5; - *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; +// *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2; +// *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5; +// *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6; series0->addBarSet(set0); - series0->addBarSet(set1); - series0->addBarSet(set2); - series0->addBarSet(set3); +// series0->addBarSet(set1); +// series0->addBarSet(set2); +// series0->addBarSet(set3); series0->addBarSet(set4); ChartWidget* chartWidget = new ChartWidget(&window); diff --git a/src/axisitem.cpp b/src/axisitem.cpp index ceaa778..aa56fba 100644 --- a/src/axisitem.cpp +++ b/src/axisitem.cpp @@ -16,11 +16,13 @@ m_shadesEnabled(true), m_grid(parent), m_shades(parent), m_labels(parent), -m_origin(0,0) +m_axis(parent) { //initial initialization + m_axis.setZValue(ChartPresenter::AxisZValue); m_shades.setZValue(ChartPresenter::ShadesZValue); m_grid.setZValue(ChartPresenter::GridZValue); + setFlags(QGraphicsItem::ItemHasNoContents); } AxisItem::~AxisItem() @@ -34,10 +36,12 @@ QRectF AxisItem::boundingRect() const void AxisItem::createItems(int count) { + m_axis.addToGroup(new QGraphicsLineItem(this)); for (int i = 0; i < count; ++i) { m_grid.addToGroup(new QGraphicsLineItem(this)); m_labels.addToGroup(new QGraphicsSimpleTextItem(this)); if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this)); + m_axis.addToGroup(new QGraphicsLineItem(this)); } } @@ -55,6 +59,10 @@ void AxisItem::clear() delete item; } + foreach(QGraphicsItem* item , m_axis.childItems()) { + delete item; + } + m_thicksList.clear(); } @@ -66,10 +74,12 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void AxisItem::updateItem(int count) { + if(count ==0) return; QList lines = m_grid.childItems(); QList labels = m_labels.childItems(); QList shades = m_shades.childItems(); + QList axis = m_axis.childItems(); switch (m_type) { @@ -77,7 +87,8 @@ void AxisItem::updateItem(int count) { const qreal deltaX = m_rect.width() / (count-1); - m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); for (int i = 0; i < count; ++i) { int x = i * deltaX + m_rect.left(); @@ -88,11 +99,12 @@ void AxisItem::updateItem(int count) QPointF center = labelItem->boundingRect().center(); labelItem->setTransformOriginPoint(center.x(), center.y()); labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding); - if(i%2){ QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height()); } + lineItem = static_cast(axis.at(i+1)); + lineItem->setLine(x,m_rect.bottom(),x,m_rect.bottom()+5); } } break; @@ -101,7 +113,8 @@ void AxisItem::updateItem(int count) { const qreal deltaY = m_rect.height()/ (count-1); - m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); + QGraphicsLineItem *lineItem = static_cast(axis.at(0)); + lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); for (int i = 0; i < count; ++i) { int y = i * -deltaY + m_rect.bottom(); @@ -116,6 +129,8 @@ void AxisItem::updateItem(int count) QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY); } + lineItem = static_cast(axis.at(i+1)); + lineItem->setLine(m_rect.left()-5,y,m_rect.left(),y); } } break; @@ -170,14 +185,14 @@ void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels) { m_thicksList=labels; QList items = m_labels.childItems(); - if(items.size()!=m_thicksList.size()){ + //if(items.size()!=m_thicksList.size()){ clear(); m_thicksList=labels; createItems(m_thicksList.size()); updateItem(m_thicksList.size()); items = m_labels.childItems(); handleAxisUpdate(axis); - } + // } Q_ASSERT(items.size()==m_thicksList.size()); @@ -283,7 +298,9 @@ void AxisItem::setShadesPen(const QPen& pen) void AxisItem::setAxisPen(const QPen& pen) { - m_axis.setPen(pen); + foreach(QGraphicsItem* item , m_axis.childItems()) { + static_cast(item)->setPen(pen); + } } void AxisItem::setGridPen(const QPen& pen) diff --git a/src/axisitem_p.h b/src/axisitem_p.h index cd28482..db42b61 100644 --- a/src/axisitem_p.h +++ b/src/axisitem_p.h @@ -66,9 +66,8 @@ private: QGraphicsItemGroup m_grid; QGraphicsItemGroup m_shades; QGraphicsItemGroup m_labels; - QGraphicsLineItem m_axis; + QGraphicsItemGroup m_axis; QStringList m_thicksList; - QPointF m_origin; }; diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index 615314b..941c5be 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -63,7 +63,7 @@ void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidg QRectF Bar::boundingRect() const { - QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); + QRectF r(mXpos, mYpos, mWidth, mHeight); return r; } diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index 55826af..463dc47 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -51,10 +51,9 @@ QBarSet* BarChartModel::nextSet(bool getFirst) return set; } - -QBarSet& BarChartModel::setAt(int index) +QBarSet* BarChartModel::setAt(int index) { - return *mDataModel.at(index); + return mDataModel.at(index); } int BarChartModel::countSets() diff --git a/src/barchart/barchartmodel_p.h b/src/barchart/barchartmodel_p.h index 26f3009..40b821d 100644 --- a/src/barchart/barchartmodel_p.h +++ b/src/barchart/barchartmodel_p.h @@ -23,8 +23,7 @@ public: void addBarSet(QBarSet *set); void removeBarSet(QBarSet *set); QBarSet* nextSet(bool getFirst); - - QBarSet& setAt(int index); // Internal + QBarSet *setAt(int index); int countSets(); // Number of sets in model int countCategories(); // Number of categories diff --git a/src/barchart/barlabel.cpp b/src/barchart/barlabel.cpp index 3437334..6f8c635 100644 --- a/src/barchart/barlabel.cpp +++ b/src/barchart/barlabel.cpp @@ -29,7 +29,7 @@ void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QRectF BarLabel::boundingRect() const { - QRectF r(mXpos, mYpos, mXpos + mSize.width(), mYpos + mSize.height()); + QRectF r(mXpos, mYpos, mSize.width(), mSize.height()); return r; } diff --git a/src/barchart/barpresenter.cpp b/src/barchart/barpresenter.cpp index 3323d2a..592f997 100644 --- a/src/barchart/barpresenter.cpp +++ b/src/barchart/barpresenter.cpp @@ -52,7 +52,7 @@ void BarPresenter::layoutChanged() // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); - bar->setBrush(mModel.setAt(set).brush()); + bar->setBrush(mModel.setAt(set)->brush()); bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight); itemIndex++; xPos += mBarDefaultWidth; diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 4eba484..75d5782 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -63,13 +63,13 @@ void BarPresenterBase::dataChanged() mFloatingValues.clear(); // Create new graphic items for bars - for (int s=0; sbelongsToSet(set)) { - mFloatingValues.at(i)->setVisible(set->isFloatingValuesVisible()); - } - } -} - //handlers void BarPresenterBase::handleModelChanged(int index) diff --git a/src/barchart/barpresenterbase.h b/src/barchart/barpresenterbase.h index ecbe5fa..01c9ace 100644 --- a/src/barchart/barpresenterbase.h +++ b/src/barchart/barpresenterbase.h @@ -41,9 +41,6 @@ public: virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes -public Q_SLOTS: - void setFloatingValues(QBarSet *set); - protected slots: void handleModelChanged(int index); void handleDomainChanged(const Domain& domain); diff --git a/src/barchart/barvalue.cpp b/src/barchart/barvalue.cpp index cfa2063..3673227 100644 --- a/src/barchart/barvalue.cpp +++ b/src/barchart/barvalue.cpp @@ -5,10 +5,10 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE BarValue::BarValue(QBarSet &set, QGraphicsItem *parent) - : QGraphicsItem(parent) + : QGraphicsObject(parent) ,mBarSet(set) { -// setVisible(false); + setVisible(false); } void BarValue::setValueString(QString str) @@ -56,9 +56,14 @@ void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QRectF BarValue::boundingRect() const { - QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight); + QRectF r(mXpos, mYpos, mWidth, mHeight); return r; } +void BarValue::toggleVisible() +{ + setVisible(!isVisible()); +} +#include "moc_barvalue_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barvalue_p.h b/src/barchart/barvalue_p.h index eaba88b..e705c14 100644 --- a/src/barchart/barvalue_p.h +++ b/src/barchart/barvalue_p.h @@ -2,7 +2,7 @@ #define BARVALUE_P_H #include "qchartglobal.h" -#include +#include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -12,8 +12,9 @@ class QBarSet; // Visual class for floating bar values // TODO: fonts, colors etc. // By default these are not visible. -class BarValue : public QGraphicsItem // TODO: public QGraphicsObject for signals? +class BarValue : public QGraphicsObject { + Q_OBJECT public: BarValue(QBarSet &set, QGraphicsItem *parent = 0); @@ -32,6 +33,9 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QRectF boundingRect() const; +public Q_SLOTS: + void toggleVisible(); + private: QBarSet& mBarSet; diff --git a/src/barchart/percentbarpresenter.cpp b/src/barchart/percentbarpresenter.cpp index dd90d73..7169a17 100644 --- a/src/barchart/percentbarpresenter.cpp +++ b/src/barchart/percentbarpresenter.cpp @@ -50,7 +50,7 @@ void PercentBarPresenter::layoutChanged() // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); - bar->setBrush(mModel.setAt(set).brush()); + bar->setBrush(mModel.setAt(set)->brush()); bar->setPos(xPos, yPos-barHeight); itemIndex++; yPos -= barHeight; diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index fc0107a..1548c2d 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -65,24 +65,11 @@ bool QBarSet::isFloatingValuesVisible() void QBarSet::barClicked() { - qDebug() << "QBarset::barClicked"; + qDebug() << "QBarset::barClicked" << this; // Some bar of this set has been clicked // TODO: What happens then? emit clicked(); // Notify that set has been clicked } -void QBarSet::toggleFloatingValuesVisible() -{ - qDebug() << "QBarset::toggleFloatingValuesVisible"; - // TODO: toggle vs explicit set? - if (mFloatingValuesVisible) { - mFloatingValuesVisible=false; - } else { - mFloatingValuesVisible=true; - } - emit setFloatingValuesVisible(this); -} - - #include "moc_qbarset.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 9a7e82a..889881a 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -31,7 +31,6 @@ public: Q_SIGNALS: void clicked(); - void setFloatingValuesVisible(QBarSet* set); /* void hoverEnter(); void hoverLeave(); @@ -39,7 +38,6 @@ Q_SIGNALS: public Q_SLOTS: void barClicked(); - void toggleFloatingValuesVisible(); private: diff --git a/src/barchart/stackedbarpresenter.cpp b/src/barchart/stackedbarpresenter.cpp index 67c0c7b..e5dafa0 100644 --- a/src/barchart/stackedbarpresenter.cpp +++ b/src/barchart/stackedbarpresenter.cpp @@ -55,7 +55,7 @@ void StackedBarPresenter::layoutChanged() Bar* bar = mBars.at(itemIndex); bar->resize(mBarDefaultWidth, barHeight); - bar->setBrush(mModel.setAt(set).brush()); + bar->setBrush(mModel.setAt(set)->brush()); bar->setPos(xPos, yPos-barHeight); itemIndex++; yPos -= barHeight; diff --git a/src/qchart.cpp b/src/qchart.cpp index 457af65..522e2b6 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -104,6 +104,7 @@ void QChart::createChartBackgroundItem() { if(!m_backgroundItem) { m_backgroundItem = new QGraphicsRectItem(this); + m_backgroundItem->setPen(Qt::NoPen); m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); } }