/******************************************************************************* * * * Société de Transports de Montréal. * * 2013 * * * * Projet Zones Tests * * * * * * * *******************************************************************************/ /* Description: Widget graphique Bouton qui consiste en une image sur laquelle l'on peut cliquer. */ /* ************************************************************************** */ /* Revision: ### 20130204 JFM Verision d'origine. ### YYYYMMDD Description du besoin ou du bug Description du changement. */ /* ************************************************************************** */ #include "PushButton.h" #include #include #include #include #include #include #include CPushButton::CPushButton(QGraphicsItem *Parent,QString NormalFileName, bool UseTextOverlay,int ButtonData) { Q_UNUSED(ButtonData) setParentItem(Parent); setAcceptHoverEvents(true); m_Normal = new QGraphicsPixmapItem(QPixmap(NormalFileName)); ClickTransparency = 1; //Inform QGraphicsItem of the geometry setGeometry(m_Normal->boundingRect()); ispressed = false; isFlashingValid = false; mDisplayFlashIcon = false; isreleasing = false; UseOverlay = UseTextOverlay; ButtonTextOverlay = ""; timeLine = new QTimeLine(PRESS_EFFECT_FADE_TIMEOUT); timeLine->setCurveShape(QTimeLine::EaseInCurve); connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(TimeLine(qreal))); } CPushButton::~CPushButton() { delete m_Normal; delete timeLine; } void CPushButton::paint(QPainter *Painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { // Painter->setCompositionMode(QPainter::CompositionMode_DestinationOver); /*if(isDown() || isChecked()) { m_Normal->paint(Painter,option,widget); qDebug("Paint Called, button down"); } else if(underMouse()) { m_Hovered->paint(Painter,option,widget); qDebug("Paint Called, hovered"); } else { m_Pressed->paint(Painter,option,widget); qDebug("Paint Called, normal"); }*/ if(ispressed) { //QRadialGradient gradient(50, 50, 50, 50, 50); QRadialGradient gradient(m_Normal->boundingRect().center(),m_Normal->boundingRect().width()/2); gradient.setColorAt(0, QColor::fromRgbF(1, 1, 1,1)); gradient.setColorAt(1, QColor::fromRgbF(1, 1, 1,0)); QBrush brush(gradient); // m_Pressed->paint(Painter,option,widget); m_Normal->paint(Painter,option,widget); Painter->setBrush(brush); Painter->setPen(Qt::NoPen); //Painter->drawEllipse(m_Normal->boundingRect()); Painter->drawEllipse(m_Normal->boundingRect()); //qDebug("Paint Called, pressed"); } /* else if(ishovered) { m_Hovered->paint(Painter,option,widget); // qDebug("Paint Called, hovered"); }*/ else { if(mDisplayFlashIcon) { m_Flashing->paint(Painter,option,widget); } else { //draw button icon m_Normal->paint(Painter,option,widget); //draw text overlay QRectF textRect = m_Normal->boundingRect();//.adjusted(15, 10, -10, -15); int flags = Qt::AlignHCenter | Qt::AlignVCenter; QFont font; font.setPixelSize(16); font.setBold(true); Painter->setPen(Qt::red); Painter->setFont(font); Painter->drawText(textRect, flags, ButtonTextOverlay); //if necessary, draw halo animation if(isreleasing) { //QRadialGradient gradient(50, 50, 50, 50, 50); QRadialGradient gradient(m_Normal->boundingRect().center(),m_Normal->boundingRect().width()/2); gradient.setColorAt(0, QColor::fromRgbF(1, 1, 1,ClickTransparency)); gradient.setColorAt(1, QColor::fromRgbF(1, 1, 1, 0)); QBrush brush(gradient); // m_Pressed->paint(Painter,option,widget); m_Normal->paint(Painter,option,widget); Painter->setBrush(brush); Painter->setPen(Qt::NoPen); //Painter->drawEllipse(m_Normal->boundingRect()); Painter->drawEllipse(m_Normal->boundingRect()); } } // qDebug("Paint Called, normal"); } } void CPushButton::hoverEnterEvent ( QGraphicsSceneHoverEvent * event ) { event->accept(); update(); // qDebug("Hover Enter Called"); } void CPushButton::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) { event->accept(); update(); // qDebug("Hover Leave Called"); } void CPushButton::mousePressEvent ( QGraphicsSceneMouseEvent * event ) { Q_UNUSED(event) // ispressed = true; update(); emit pressed(this); // qDebug("Mouse Press Event"); //event->ignore(); } void CPushButton::mouseReleaseEvent( QGraphicsSceneMouseEvent * event) { Q_UNUSED(event) ispressed = false; emit released(this); isreleasing = true; timeLine->start(); // qDebug("Button Mouse Release Event"); } void CPushButton::StartFlashing() { mFlashTimer->start(mFlashTimeout); } void CPushButton::StopFlashing() { mFlashTimer->stop(); mDisplayFlashIcon = false; } void CPushButton::setFlashingData(QString FlashFileName,int FlashDelay) { QPixmap temp(FlashFileName); QBitmap temp2 = temp.createMaskFromColor(QColor(255,255,255),Qt::MaskOutColor); // temp.setMask(temp2); m_Flashing = new QGraphicsPixmapItem(temp); if(!m_Flashing) { isFlashingValid = false; return; } isFlashingValid = true; mFlashTimeout = FlashDelay; mFlashTimer = new QTimer(this); connect(mFlashTimer,SIGNAL(timeout()),this,SLOT(FlashButton())); } void CPushButton::FlashButton() { mDisplayFlashIcon = !mDisplayFlashIcon; update(); } void CPushButton::TimeLine(qreal value) { ClickTransparency = 1-value; update(); if(value == 1) { isreleasing = false; emit clicked(this); } } void CPushButton::SetTextOverlay(QString Text) { ButtonTextOverlay = Text.left(MAX_OVERLAY_TEXT_LENGTH); update(); } void CPushButton::resizeEvent(QGraphicsSceneResizeEvent *event) { m_Normal->setPixmap(m_Normal->pixmap().scaled(event->newSize().width(),event->newSize().height())); this->resize(event->newSize().width(),event->newSize().height()); }