##// END OF EJS Templates
Added support for adding and removing data with model. Updated the example
Added support for adding and removing data with model. Updated the example

File last commit:

r540:7d8a0757e05d
r545:366c5163e81a
Show More
qlegend.cpp
140 lines | 3.4 KiB | text/x-c | CppLexer
sauimone
framework for legend
r524 #include "qchartglobal.h"
#include "qlegend.h"
#include "qseries.h"
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 #include <QPainter>
#include <QPen>
sauimone
framework for legend
r524
QTCOMMERCIALCHART_BEGIN_NAMESPACE
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529
// TODO: this to legendmarker_p.h header
class LegendMarker : public QGraphicsItem
{
public:
LegendMarker(QGraphicsItem *parent = 0) : QGraphicsItem(parent)
,mBoundingRect(0,0,1,1)
{}
void setBoundingRect(const QRectF rect) { mBoundingRect = rect; }
void setBrush(const QBrush brush) { mBrush = brush; }
void setName(const QString name) { mName = name; }
QString name() const { return mName; }
QColor color() const { return mBrush.color(); }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
{
painter->setBrush(mBrush);
painter->drawRect(mBoundingRect);
}
QRectF boundingRect() const { return mBoundingRect; }
private:
QRectF mBoundingRect;
QBrush mBrush;
QString mName;
};
sauimone
framework for legend
r524 QLegend::QLegend(QGraphicsItem *parent)
: QGraphicsObject(parent)
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 ,mBoundingRect(0,0,1,1)
sauimone
background to legend, theme applies
r540 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
sauimone
framework for legend
r524 {
}
void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
sauimone
background to legend, theme applies
r540 painter->setBrush(mBackgroundBrush);
painter->drawRect(mBoundingRect);
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 foreach(LegendMarker* m, mMarkers) {
QRectF r = m->boundingRect();
painter->setPen(m->color());
sauimone
background to legend, theme applies
r540 painter->drawText(r.x() + 20, r.y() + r.height(), m->name());
sauimone
framework for legend
r524 }
}
QRectF QLegend::boundingRect() const
{
return mBoundingRect;
}
sauimone
background to legend, theme applies
r540 void QLegend::setBackgroundBrush(const QBrush& brush)
{
mBackgroundBrush = brush;
}
QBrush QLegend::backgroundBrush() const
{
return mBackgroundBrush;
}
sauimone
framework for legend
r524 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
{
mSeriesList.append(series);
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 dataChanged();
layoutChanged();
sauimone
framework for legend
r524 }
void QLegend::handleSeriesRemoved(QSeries* series)
{
mSeriesList.removeOne(series);
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 dataChanged();
layoutChanged();
}
void QLegend::handleGeometryChanged(const QRectF& size)
{
mBoundingRect = size;
layoutChanged();
sauimone
framework for legend
r524 }
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 void QLegend::dataChanged()
{
foreach (QGraphicsItem* i, childItems()) {
delete i;
}
mMarkers.clear();
foreach (QSeries* s, mSeriesList) {
for (int i=0; i<s->legendEntries().count(); i++) {
LegendMarker *marker = new LegendMarker(this);
marker->setBrush(s->legendEntries().at(i).mBrush);
marker->setName(s->legendEntries().at(i).mName);
mMarkers.append(marker);
sauimone
background to legend, theme applies
r540 // childItems().append(marker);
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 }
}
}
void QLegend::layoutChanged()
{
// Calculate layout for markers and text
if (mMarkers.count() <= 0) {
// Nothing to do
return;
}
// TODO: marker defined by series.
QSizeF markerSize(10,10);
// TODO: better layout, this is just concept.
// Leave some space around markers like this: | x x x x |
sauimone
background to legend, theme applies
r540 qreal steps = mMarkers.count();
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529
sauimone
background to legend, theme applies
r540 qreal xStep = mBoundingRect.width() / steps;
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 qreal yStep = mBoundingRect.height() / steps;
sauimone
background to legend, theme applies
r540 qreal x = mBoundingRect.x() + 5;
qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 foreach (LegendMarker* m, mMarkers) {
sauimone
background to legend, theme applies
r540 qDebug() << "marker x:" << x;
qDebug() << "marker y:" << y;
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height()));
sauimone
background to legend, theme applies
r540 x += xStep;
sauimone
First version of legend. Simple markers and serie names. Using drilldown as example for now.
r529 }
}
sauimone
framework for legend
r524
#include "moc_qlegend.cpp"
QTCOMMERCIALCHART_END_NAMESPACE