legendmarker.cpp
144 lines
| 3.3 KiB
| text/x-c
|
CppLexer
/ src / legendmarker.cpp
sauimone
|
r547 | #include "qchartglobal.h" | ||
#include "legendmarker_p.h" | ||||
sauimone
|
r587 | #include <qpieslice.h> | ||
#include <qbarset.h> | ||||
sauimone
|
r547 | #include <QPainter> | ||
#include <QGraphicsSceneEvent> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent) | ||||
: QGraphicsObject(parent) | ||||
sauimone
|
r565 | ,mBoundingRect(0,0,1,1) | ||
sauimone
|
r585 | ,mMarkerBoundingRect(0,0,1,1) | ||
sauimone
|
r547 | ,mSeries(series) | ||
sauimone
|
r565 | ,mBarset(0) | ||
,mPieslice(0) | ||||
,mType(LegendMarkerTypeSeries) | ||||
sauimone
|
r585 | ,mTextItem(new QGraphicsSimpleTextItem(this)) | ||
sauimone
|
r565 | { | ||
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||||
} | ||||
sauimone
|
r576 | LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent) | ||
sauimone
|
r565 | : QGraphicsObject(parent) | ||
,mBoundingRect(0,0,1,1) | ||||
sauimone
|
r585 | ,mMarkerBoundingRect(0,0,1,1) | ||
sauimone
|
r576 | ,mSeries(series) | ||
sauimone
|
r565 | ,mBarset(barset) | ||
,mPieslice(0) | ||||
,mType(LegendMarkerTypeBarset) | ||||
sauimone
|
r585 | ,mTextItem(new QGraphicsSimpleTextItem(this)) | ||
sauimone
|
r565 | { | ||
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||||
} | ||||
sauimone
|
r576 | LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent) | ||
sauimone
|
r565 | : QGraphicsObject(parent) | ||
sauimone
|
r547 | ,mBoundingRect(0,0,1,1) | ||
sauimone
|
r585 | ,mMarkerBoundingRect(0,0,1,1) | ||
sauimone
|
r576 | ,mSeries(series) | ||
sauimone
|
r565 | ,mBarset(0) | ||
,mPieslice(pieslice) | ||||
,mType(LegendMarkerTypePieslice) | ||||
sauimone
|
r585 | ,mTextItem(new QGraphicsSimpleTextItem(this)) | ||
sauimone
|
r547 | { | ||
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||||
} | ||||
void LegendMarker::setBoundingRect(const QRectF rect) | ||||
{ | ||||
mBoundingRect = rect; | ||||
sauimone
|
r585 | // Calculate Marker pos | ||
// TODO: remove hard coding. 5 is marigin around marker | ||||
QSizeF markerSize(10,10); | ||||
qreal x = mBoundingRect.x() + 5; | ||||
qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2; | ||||
mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height()); | ||||
mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y ); | ||||
sauimone
|
r547 | } | ||
void LegendMarker::setBrush(const QBrush brush) | ||||
{ | ||||
mBrush = brush; | ||||
} | ||||
sauimone
|
r565 | QBrush LegendMarker::brush() const | ||
{ | ||||
return mBrush; | ||||
} | ||||
sauimone
|
r547 | void LegendMarker::setName(const QString name) | ||
{ | ||||
sauimone
|
r585 | mTextItem.setText(name); | ||
sauimone
|
r547 | } | ||
QString LegendMarker::name() const | ||||
{ | ||||
sauimone
|
r585 | return mTextItem.text(); | ||
sauimone
|
r547 | } | ||
sauimone
|
r576 | QSeries* LegendMarker::series() const | ||
{ | ||||
return mSeries; | ||||
} | ||||
sauimone
|
r547 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
painter->setBrush(mBrush); | ||||
sauimone
|
r585 | painter->drawRect(mMarkerBoundingRect); | ||
sauimone
|
r547 | } | ||
QRectF LegendMarker::boundingRect() const | ||||
{ | ||||
return mBoundingRect; | ||||
} | ||||
void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||||
{ | ||||
sauimone
|
r565 | switch (mType) | ||
{ | ||||
case LegendMarkerTypeSeries: { | ||||
sauimone
|
r567 | emit clicked(mSeries,event->button()); | ||
sauimone
|
r565 | break; | ||
sauimone
|
r569 | } | ||
sauimone
|
r565 | case LegendMarkerTypeBarset: { | ||
sauimone
|
r567 | emit clicked(mBarset,event->button()); | ||
sauimone
|
r565 | break; | ||
sauimone
|
r569 | } | ||
sauimone
|
r565 | case LegendMarkerTypePieslice: { | ||
sauimone
|
r567 | emit clicked(mPieslice,event->button()); | ||
sauimone
|
r565 | break; | ||
} | ||||
default: { | ||||
break; | ||||
sauimone
|
r569 | } | ||
sauimone
|
r565 | } | ||
sauimone
|
r547 | } | ||
sauimone
|
r587 | void LegendMarker::changed() | ||
{ | ||||
switch (mType) | ||||
{ | ||||
case LegendMarkerTypeSeries: { | ||||
// TODO: | ||||
break; | ||||
} | ||||
case LegendMarkerTypeBarset: { | ||||
setBrush(mBarset->brush()); | ||||
setName(mBarset->name()); | ||||
break; | ||||
} | ||||
case LegendMarkerTypePieslice: { | ||||
setBrush(mPieslice->sliceBrush()); | ||||
setName(mPieslice->label()); | ||||
break; | ||||
} | ||||
} | ||||
} | ||||
sauimone
|
r547 | #include "moc_legendmarker_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||