QtGraphicsButton.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6727 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 08:03:50 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  CLASS QtGraphicsButton - IMPLEMENTATION
00049 //
00050 //=============================================================================
00051 
00052 //== INCLUDES =================================================================
00053 
00054 #include <QPainter>
00055 #include <QGraphicsSceneMouseEvent>
00056 
00057 #include "QtGraphicsButton.hh"
00058 
00059 //== IMPLEMENTATION ==========================================================
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   // scale image to button dimensions
00076   QImage i = _image.scaled (width_, height_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).convertToFormat (QImage::Format_ARGB32);
00077 
00078   checkedPix_ = QPixmap::fromImage (i);
00079 
00080   // generate black/white and transparent images for the other button states
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   // reset state if button was hidden
00181   if (_change == QGraphicsItem::ItemVisibleHasChanged)
00182   {
00183     pressed_ = false;
00184     over_ = false;
00185   }
00186   return QGraphicsItem::itemChange (_change, _value);
00187 }
00188 
00189 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .