QtGraphicsButton.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include <QPainter>
00055 #include <QGraphicsSceneMouseEvent>
00056
00057 #include "QtGraphicsButton.hh"
00058
00059
00060
00061 QtGraphicsButton::QtGraphicsButton (const QImage &_image, QGraphicsItem *_parent, int _width, int _height) :
00062 QGraphicsItem (_parent),
00063 checkable_(false),
00064 checked_(false),
00065 pressed_(false),
00066 over_(false),
00067 width_(_width),
00068 height_(_height)
00069 {
00070 if (width_ <= 0)
00071 width_ = _image.width ();
00072 if (height_ <= 0)
00073 height_ = _image.height ();
00074
00075
00076 QImage i = _image.scaled (width_, height_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).convertToFormat (QImage::Format_ARGB32);
00077
00078 checkedPix_ = QPixmap::fromImage (i);
00079
00080
00081 QImage normal (width_, height_, QImage::Format_ARGB32);
00082 QImage over (width_, height_, QImage::Format_ARGB32);
00083
00084 for (int x = 0; x < width_; x++)
00085 for (int y = 0; y < height_; y++)
00086 {
00087 QRgb pix = i.pixel (x, y);
00088 over.setPixel (x, y, qRgba (qRed (pix), qGreen (pix), qBlue (pix), qAlpha (pix) * 0.5));
00089 normal.setPixel (x, y, qRgba (qGray (pix), qGray (pix), qGray (pix), qAlpha (pix) * 0.3));
00090 }
00091 overPix_ = QPixmap::fromImage (over);
00092 normalPix_ = QPixmap::fromImage (normal);
00093
00094 setAcceptHoverEvents (true);
00095 }
00096
00097
00098
00099 QRectF QtGraphicsButton::boundingRect () const
00100 {
00101 return QRectF (QPointF(0, 0), QSizeF (width_, height_));
00102 }
00103
00104
00105
00106 void QtGraphicsButton::paint (QPainter *_painter, const QStyleOptionGraphicsItem *, QWidget *)
00107 {
00108 _painter->setClipping (false);
00109 if (pressed_ || checked_)
00110 _painter->drawPixmap (0, 0, checkedPix_);
00111 else if (over_)
00112 _painter->drawPixmap (0, 0, overPix_);
00113 else
00114 _painter->drawPixmap (0, 0, normalPix_);
00115 }
00116
00117
00118
00119 void QtGraphicsButton::setCheckable (bool _value)
00120 {
00121 checkable_ = _value;
00122 }
00123
00124
00125
00126 void QtGraphicsButton::setChecked (bool _value)
00127 {
00128 checked_ = _value;
00129 }
00130
00131
00132
00133 bool QtGraphicsButton::isChecked () const
00134 {
00135 return checked_;
00136 }
00137
00138
00139
00140 void QtGraphicsButton::hoverEnterEvent (QGraphicsSceneHoverEvent *)
00141 {
00142 over_ = true;
00143 update ();
00144 }
00145
00146
00147
00148 void QtGraphicsButton::hoverLeaveEvent (QGraphicsSceneHoverEvent *)
00149 {
00150 over_ = false;
00151 update ();
00152 }
00153
00154
00155
00156 void QtGraphicsButton::mousePressEvent ( QGraphicsSceneMouseEvent *_event)
00157 {
00158 _event->accept ();
00159 pressed_ = true;
00160 if (checkable_)
00161 checked_ = !checked_;
00162 update ();
00163
00164 emit pressed ();
00165 }
00166
00167
00168
00169 void QtGraphicsButton::mouseReleaseEvent (QGraphicsSceneMouseEvent *_event)
00170 {
00171 _event->accept ();
00172 pressed_ = false;
00173 update ();
00174 }
00175
00176
00177
00178 QVariant QtGraphicsButton::itemChange (GraphicsItemChange _change, const QVariant &_value)
00179 {
00180
00181 if (_change == QGraphicsItem::ItemVisibleHasChanged)
00182 {
00183 pressed_ = false;
00184 over_ = false;
00185 }
00186 return QGraphicsItem::itemChange (_change, _value);
00187 }
00188
00189