bar.cpp
96 lines
| 1.8 KiB
| text/x-c
|
CppLexer
sauimone
|
r118 | #include "bar_p.h" | ||
sauimone
|
r56 | #include <QDebug> | ||
Michal Klocek
|
r59 | #include <QPainter> | ||
sauimone
|
r283 | #include <QGraphicsSceneEvent> | ||
sauimone
|
r56 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
sauimone
|
r425 | Bar::Bar(QString category, QGraphicsItem *parent) | ||
Tero Ahola
|
r518 | : QGraphicsObject(parent), | ||
mXpos(0), | ||||
mYpos(0), | ||||
mWidth(0), | ||||
Jani Honkonen
|
r609 | mHeight(0), | ||
mCategory(category) | ||||
sauimone
|
r56 | { | ||
sauimone
|
r412 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | ||
sauimone
|
r280 | setAcceptHoverEvents(true); | ||
sauimone
|
r56 | } | ||
sauimone
|
r247 | void Bar::setSize(const QSizeF& size) | ||
{ | ||||
mWidth = size.width(); | ||||
mHeight = size.height(); | ||||
} | ||||
sauimone
|
r113 | void Bar::resize( qreal w, qreal h ) | ||
sauimone
|
r56 | { | ||
mWidth = w; | ||||
mHeight = h; | ||||
} | ||||
void Bar::setPos(qreal x, qreal y) | ||||
{ | ||||
mXpos = x; | ||||
mYpos = y; | ||||
} | ||||
sauimone
|
r183 | void Bar::setPen(QPen pen) | ||
{ | ||||
mPen = pen; | ||||
} | ||||
void Bar::setBrush(QBrush brush) | ||||
{ | ||||
mBrush = brush; | ||||
} | ||||
sauimone
|
r56 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
Tero Ahola
|
r611 | Q_UNUSED(option) | ||
Q_UNUSED(widget) | ||||
sauimone
|
r82 | if (0 == mHeight) { | ||
return; | ||||
} | ||||
sauimone
|
r183 | painter->setBrush(mBrush); | ||
sauimone
|
r113 | |||
// This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. | ||||
int x0 = mXpos; | ||||
int x1 = (mXpos + mWidth); | ||||
int w = x1-x0; | ||||
int y0 = mYpos; | ||||
int y1 = (mYpos + mHeight); | ||||
int h = y1-y0; | ||||
painter->drawRect(x0, y0 ,w ,h); | ||||
sauimone
|
r56 | } | ||
QRectF Bar::boundingRect() const | ||||
{ | ||||
sauimone
|
r273 | QRectF r(mXpos, mYpos, mWidth, mHeight); | ||
sauimone
|
r56 | return r; | ||
} | ||||
sauimone
|
r412 | void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event) | ||
sauimone
|
r256 | { | ||
sauimone
|
r412 | if (event->button() == Qt::LeftButton) { | ||
sauimone
|
r425 | emit clicked(mCategory); | ||
sauimone
|
r412 | } else if (event->button() == Qt::RightButton) { | ||
sauimone
|
r425 | emit rightClicked(mCategory); | ||
sauimone
|
r412 | } | ||
sauimone
|
r256 | } | ||
sauimone
|
r280 | void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event) | ||
sauimone
|
r276 | { | ||
sauimone
|
r283 | emit hoverEntered(event->lastScreenPos()); | ||
sauimone
|
r276 | } | ||
sauimone
|
r283 | void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
sauimone
|
r276 | { | ||
sauimone
|
r283 | emit hoverLeaved(); | ||
sauimone
|
r276 | } | ||
sauimone
|
r256 | #include "moc_bar_p.cpp" | ||
sauimone
|
r239 | |||
sauimone
|
r56 | QTCOMMERCIALCHART_END_NAMESPACE | ||