bar.cpp
84 lines
| 1.5 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 | ||||
Tero Ahola
|
r103 | Bar::Bar(QGraphicsItem *parent) | ||
sauimone
|
r256 | : QGraphicsObject(parent) | ||
sauimone
|
r56 | { | ||
sauimone
|
r256 | setAcceptedMouseButtons(Qt::LeftButton); | ||
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) | ||
{ | ||||
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
|
r256 | void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) | ||
{ | ||||
emit clicked(); | ||||
} | ||||
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 | ||