legendmarker.cpp
165 lines
| 3.7 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
|
r616 | #include <qxyseries.h> | ||
sauimone
|
r547 | #include <QPainter> | ||
#include <QGraphicsSceneEvent> | ||||
sauimone
|
r626 | #include <QGraphicsSimpleTextItem> | ||
sauimone
|
r547 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent) | ||||
: QGraphicsObject(parent) | ||||
sauimone
|
r626 | ,mPos(0,0) | ||
,mSize(0,0) | ||||
,mBoundingRect(0,0,0,0) | ||||
,mMarkerBoundingRect(0,0,0,0) | ||||
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) | ||
sauimone
|
r626 | ,mPos(0,0) | ||
,mSize(0,0) | ||||
,mBoundingRect(0,0,0,0) | ||||
,mMarkerBoundingRect(0,0,0,0) | ||||
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
|
r626 | ,mPos(0,0) | ||
,mSize(0,0) | ||||
,mBoundingRect(0,0,0,0) | ||||
,mMarkerBoundingRect(0,0,0,0) | ||||
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); | ||||
} | ||||
sauimone
|
r626 | void LegendMarker::setPos(qreal x, qreal y) | ||
sauimone
|
r547 | { | ||
sauimone
|
r626 | mPos = QPointF(x,y); | ||
layoutChanged(); | ||||
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
|
r626 | layoutChanged(); | ||
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) | ||
{ | ||||
Tero Ahola
|
r611 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
sauimone
|
r547 | painter->setBrush(mBrush); | ||
sauimone
|
r585 | painter->drawRect(mMarkerBoundingRect); | ||
sauimone
|
r547 | } | ||
QRectF LegendMarker::boundingRect() const | ||||
{ | ||||
return mBoundingRect; | ||||
} | ||||
sauimone
|
r626 | void LegendMarker::layoutChanged() | ||
{ | ||||
QSizeF markerSize(10,10); | ||||
qreal margin = 2; | ||||
mSize.setHeight(markerSize.height() + 2 * margin); | ||||
mSize.setWidth(mTextItem.boundingRect().width() + markerSize.width() + 3 * margin); | ||||
mBoundingRect = QRectF(mPos.x(),mPos.y(),mSize.width(),mSize.height()); | ||||
mMarkerBoundingRect = QRectF(mPos.x() + margin, mPos.y() + margin, markerSize.width(),markerSize.height()); | ||||
mTextItem.setPos(mPos.x() + markerSize.width() + 2 * margin, mPos.y() + margin ); | ||||
} | ||||
sauimone
|
r547 | 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: { | ||||
sauimone
|
r616 | QXYSeries* s = static_cast<QXYSeries*> (mSeries); | ||
setBrush(s->brush()); | ||||
setName(s->name()); | ||||
sauimone
|
r587 | 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 | ||||