61 #include <QGraphicsSceneMouseEvent> 63 #include "QtGraphicsButton.hh" 68 QGraphicsItem (_parent),
79 height_ = _image.height ();
82 QImage i = _image.scaled (
width_, height_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).convertToFormat (QImage::Format_ARGB32);
84 checkedPix_ = QPixmap::fromImage (i);
87 QImage normal (
width_, height_, QImage::Format_ARGB32);
88 QImage over (
width_, height_, QImage::Format_ARGB32);
90 for (
int x = 0; x <
width_; x++)
91 for (
int y = 0; y < height_; y++)
93 QRgb pix = i.pixel (x, y);
94 over.setPixel (x, y, qRgba (qRed (pix), qGreen (pix), qBlue (pix), qAlpha (pix) * 0.5));
95 normal.setPixel (x, y, qRgba (qGray (pix), qGray (pix), qGray (pix), qAlpha (pix) * 0.3));
97 overPix_ = QPixmap::fromImage (over);
100 setAcceptHoverEvents (
true);
107 return QRectF (QPointF(0, 0), QSizeF (
width_, height_));
114 _painter->setClipping (
false);
115 if (pressed_ || checked_)
116 _painter->drawPixmap (0, 0, checkedPix_);
118 _painter->drawPixmap (0, 0, overPix_);
154 void QtGraphicsButton::hoverLeaveEvent (QGraphicsSceneHoverEvent *)
162 void QtGraphicsButton::mousePressEvent ( QGraphicsSceneMouseEvent *_event)
167 checked_ = !checked_;
175 void QtGraphicsButton::mouseReleaseEvent (QGraphicsSceneMouseEvent *_event)
184 QVariant QtGraphicsButton::itemChange (GraphicsItemChange _change,
const QVariant &_value)
187 if (_change == QGraphicsItem::ItemVisibleHasChanged)
192 return QGraphicsItem::itemChange (_change, _value);
bool checkable_
button state
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *_event)
event tracking
virtual QRectF boundingRect() const
returns the bounding rect
QtGraphicsButton(const QImage &_image, QGraphicsItem *_parent=0, int _width=-1, int _height=-1)
virtual void paint(QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget=0)
paints the button
bool isChecked() const
returns button checked state
void pressed()
signals a button press
void setCheckable(bool _value)
makes the button checkable
QPixmap normalPix_
pixmaps for different button states
void setChecked(bool _value)
sets button checked state