legendmarker.cpp
121 lines
| 2.9 KiB
| text/x-c
|
CppLexer
/ src / legendmarker.cpp
|
r547 | #include "qchartglobal.h" | ||
#include "legendmarker_p.h" | ||||
#include <QPainter> | ||||
#include <QGraphicsSceneEvent> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent) | ||||
: QGraphicsObject(parent) | ||||
|
r565 | ,mBoundingRect(0,0,1,1) | ||
|
r585 | ,mMarkerBoundingRect(0,0,1,1) | ||
|
r547 | ,mSeries(series) | ||
|
r565 | ,mBarset(0) | ||
,mPieslice(0) | ||||
,mType(LegendMarkerTypeSeries) | ||||
|
r585 | ,mTextItem(new QGraphicsSimpleTextItem(this)) | ||
|
r565 | { | ||
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||||
} | ||||
|
r576 | LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent) | ||
|
r565 | : QGraphicsObject(parent) | ||
,mBoundingRect(0,0,1,1) | ||||
|
r585 | ,mMarkerBoundingRect(0,0,1,1) | ||
|
r576 | ,mSeries(series) | ||
|
r565 | ,mBarset(barset) | ||
,mPieslice(0) | ||||
,mType(LegendMarkerTypeBarset) | ||||
|
r585 | ,mTextItem(new QGraphicsSimpleTextItem(this)) | ||
|
r565 | { | ||
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||||
} | ||||
|
r576 | LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent) | ||
|
r565 | : QGraphicsObject(parent) | ||
|
r547 | ,mBoundingRect(0,0,1,1) | ||
|
r585 | ,mMarkerBoundingRect(0,0,1,1) | ||
|
r576 | ,mSeries(series) | ||
|
r565 | ,mBarset(0) | ||
,mPieslice(pieslice) | ||||
,mType(LegendMarkerTypePieslice) | ||||
|
r585 | ,mTextItem(new QGraphicsSimpleTextItem(this)) | ||
|
r547 | { | ||
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||||
} | ||||
void LegendMarker::setBoundingRect(const QRectF rect) | ||||
{ | ||||
mBoundingRect = rect; | ||||
|
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 ); | ||||
|
r547 | } | ||
void LegendMarker::setBrush(const QBrush brush) | ||||
{ | ||||
mBrush = brush; | ||||
} | ||||
|
r565 | QBrush LegendMarker::brush() const | ||
{ | ||||
return mBrush; | ||||
} | ||||
|
r547 | void LegendMarker::setName(const QString name) | ||
{ | ||||
|
r585 | mTextItem.setText(name); | ||
|
r547 | } | ||
QString LegendMarker::name() const | ||||
{ | ||||
|
r585 | return mTextItem.text(); | ||
|
r547 | } | ||
|
r576 | QSeries* LegendMarker::series() const | ||
{ | ||||
return mSeries; | ||||
} | ||||
|
r547 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
painter->setBrush(mBrush); | ||||
|
r585 | painter->drawRect(mMarkerBoundingRect); | ||
|
r547 | } | ||
QRectF LegendMarker::boundingRect() const | ||||
{ | ||||
return mBoundingRect; | ||||
} | ||||
void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||||
{ | ||||
|
r565 | switch (mType) | ||
{ | ||||
case LegendMarkerTypeSeries: { | ||||
|
r567 | emit clicked(mSeries,event->button()); | ||
|
r565 | break; | ||
|
r569 | } | ||
|
r565 | case LegendMarkerTypeBarset: { | ||
|
r567 | emit clicked(mBarset,event->button()); | ||
|
r565 | break; | ||
|
r569 | } | ||
|
r565 | case LegendMarkerTypePieslice: { | ||
|
r567 | emit clicked(mPieslice,event->button()); | ||
|
r565 | break; | ||
} | ||||
default: { | ||||
break; | ||||
|
r569 | } | ||
|
r565 | } | ||
|
r547 | } | ||
#include "moc_legendmarker_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||