248 lines
7.0 KiB
C++
248 lines
7.0 KiB
C++
/*******************************************************************************
|
|
* *
|
|
* Société de Transports de Montréal. *
|
|
* 2013 *
|
|
* *
|
|
* Projet Zones Tests *
|
|
* *
|
|
* *
|
|
* *
|
|
*******************************************************************************/
|
|
/*
|
|
Description:
|
|
Widget Bouton dans lequel est affiché du texte. Une image peut aussi être
|
|
affichée à l'intérieur du bouton.
|
|
*/
|
|
|
|
/* ************************************************************************** */
|
|
/* Revision:
|
|
### 20130306 JFM
|
|
Verision d'origine.
|
|
|
|
### YYYYMMDD Description du besoin ou du bug
|
|
Description du changement.
|
|
*/
|
|
|
|
/* ************************************************************************** */
|
|
|
|
#include "TextButtonWidget.h"
|
|
#include <QPainter>
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
CTextButtonWidget::CTextButtonWidget(QString Text, int TextButtonData,int ButtonHeight,int ButtonWidth,int FontSize, QPixmap *ButtonImage)
|
|
{
|
|
|
|
if(ButtonImage)
|
|
{
|
|
mButtonImagePtr = ButtonImage;
|
|
}
|
|
else
|
|
mButtonImagePtr = 0;
|
|
|
|
mIsPressed = false;
|
|
mWidgetText = Text;
|
|
mTextButtonData = TextButtonData;
|
|
mButtonHeight = ButtonHeight;
|
|
mBackgroundColor = QColor(245, 245, 255, 220);
|
|
|
|
if(FontSize == 0)
|
|
mFontSize = mButtonHeight -10;
|
|
else
|
|
mFontSize = FontSize;
|
|
|
|
//Fit widget size with text
|
|
|
|
mTextFont.setPixelSize(mFontSize);
|
|
mTextFont.setBold(true);
|
|
|
|
int IconOffset = 0;
|
|
QFontMetrics FontMetrics(mTextFont);
|
|
if(ButtonWidth == 0)
|
|
{
|
|
if(mButtonImagePtr != 0)
|
|
IconOffset = mButtonHeight + 5;
|
|
//mButtonWidth = FontMetrics.width(mWidgetText)+IconOffset + 10;
|
|
mButtonWidth = FontMetrics.boundingRect(mWidgetText).width()+IconOffset + 10;
|
|
}
|
|
else
|
|
mButtonWidth = ButtonWidth;
|
|
|
|
resize(mButtonWidth,mButtonHeight);
|
|
|
|
// setGeometry(0,0,TEXT_BUTTON_WIDGET_WIDTH,TEXT_BUTTON_WIDGET_HEIGHT);
|
|
setGeometry(0,0,rect().width(),rect().height());
|
|
mIsSelected = false;
|
|
mWidgetOrigin.setX(0);
|
|
mWidgetOrigin.setY(0);
|
|
|
|
mTimeLine = new QTimeLine(200,this);
|
|
mTimeLine->setCurveShape(QTimeLine::SineCurve);
|
|
connect(mTimeLine, SIGNAL(valueChanged(qreal)), this, SLOT(TimeLineEvent(qreal)));
|
|
|
|
mButtonImagePtr = ButtonImage;
|
|
|
|
//QRect WidgetRect(mWidgetOrigin.x(),mWidgetOrigin.y(),TEXT_BUTTON_WIDGET_WIDTH,TEXT_BUTTON_WIDGET_HEIGHT);
|
|
QRect WidgetRect(mWidgetOrigin.x(),mWidgetOrigin.y(),rect().width(),rect().height());
|
|
|
|
// QRectF textRect = WidgetRect.adjusted(5, 2, -2, -2);
|
|
// int flags = Qt::AlignVCenter | Qt::AlignLeft;
|
|
|
|
}
|
|
|
|
void CTextButtonWidget::CheckScrolling()
|
|
{
|
|
int IconOffset = 0;
|
|
if(mButtonImagePtr != 0)
|
|
IconOffset = rect().height();
|
|
|
|
QFontMetrics FontMetrics(mTextFont);
|
|
if(FontMetrics.boundingRect(mWidgetText).width()+IconOffset > rect().width())
|
|
{
|
|
mScrollingRequired = true;
|
|
mWidgetText.append(" ");
|
|
}
|
|
else
|
|
mScrollingRequired = false;
|
|
}
|
|
|
|
void CTextButtonWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
{
|
|
PaintButtonWidget(painter,option,widget);
|
|
}
|
|
void CTextButtonWidget::PaintButtonWidget(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
|
|
{
|
|
Q_UNUSED(widget)
|
|
Q_UNUSED(option)
|
|
// painter->setOpacity(1);
|
|
if(mIsPressed)
|
|
painter->setPen(QPen(Qt::red, 2));
|
|
else
|
|
painter->setPen(QPen(Qt::black, 2));
|
|
// if(mIsAnimating)
|
|
// painter->setBrush(QColor(0, 50, 255, 220));
|
|
// else
|
|
//painter->setBrush(QColor(245, 245, 255, 220));
|
|
painter->setBrush(mBackgroundColor);
|
|
//QRect WidgetRect(mWidgetOrigin.x(),mWidgetOrigin.y(),TEXT_BUTTON_WIDGET_WIDTH,TEXT_BUTTON_WIDGET_HEIGHT);
|
|
QRect WidgetRect(mWidgetOrigin.x(),mWidgetOrigin.y(),rect().width(),rect().height());
|
|
|
|
//painter->drawRoundedRect(WidgetRect,10,10);
|
|
painter->drawRect(WidgetRect);
|
|
|
|
QRectF textRect;
|
|
if(mButtonImagePtr)
|
|
textRect = WidgetRect.adjusted(rect().height() + 5, 2, -2, -2); //if there is an image, it will be square so text must begin at 5 + height of button (height determines size of image)
|
|
else
|
|
textRect = WidgetRect.adjusted(5, 2, -2, -2);
|
|
|
|
int flags = Qt::AlignVCenter | Qt::AlignLeft;
|
|
|
|
// QFont font;
|
|
// font.setPixelSize(mButtonHeight/2 - 10);
|
|
// font.setBold(true);
|
|
painter->setPen(QPen(Qt::black,1));
|
|
painter->setFont(mTextFont);
|
|
painter->drawText(textRect, flags, mWidgetText);
|
|
|
|
if(mButtonImagePtr)
|
|
{
|
|
painter->drawPixmap(WidgetRect.left(),WidgetRect.top(),mButtonImagePtr->scaled(rect().height(),rect().height()));
|
|
}
|
|
}
|
|
|
|
void CTextButtonWidget::SetButtonImage(QPixmap *ButtonImage)
|
|
{
|
|
if(ButtonImage != NULL)
|
|
{
|
|
mButtonImagePtr = ButtonImage;
|
|
update(rect());
|
|
}
|
|
}
|
|
|
|
void CTextButtonWidget::SetButtonText(QString ButtonText)
|
|
{
|
|
mWidgetText = ButtonText;
|
|
update(rect());
|
|
}
|
|
|
|
void CTextButtonWidget::SetButtonPos(int x, int y)
|
|
{
|
|
mWidgetOrigin.setX(x);
|
|
mWidgetOrigin.setY(y);
|
|
}
|
|
|
|
void CTextButtonWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
Q_UNUSED(event)
|
|
// qDebug("Mouse pressed Text Button Widget");
|
|
// mIsSelected = true;
|
|
// event->ignore();
|
|
// update();
|
|
|
|
// qDebug("Mouse pressed Text Button Widget");
|
|
mIsPressed = true;
|
|
update(rect());
|
|
emit TxtButtonPressed(this);
|
|
// QGraphicsWidget::mousePressEvent(event);
|
|
}
|
|
|
|
void CTextButtonWidget::SetBackgroundColor(QColor color)
|
|
{
|
|
mBackgroundColor = color;
|
|
update(rect());
|
|
}
|
|
|
|
void CTextButtonWidget::mouseReleaseEvent( QGraphicsSceneMouseEvent * event)
|
|
{
|
|
Q_UNUSED(event)
|
|
///* qDebug("Mouse released Text Button Widget");
|
|
// mIsSelected = false;
|
|
// update();
|
|
// if(isVisible())
|
|
// emit clicked(this);
|
|
// event->ignore();*/
|
|
// //qDebug("Start Timeline");
|
|
// mIsAnimating = true;
|
|
// mTimeLine->start();
|
|
// qDebug("Mouse released Text Button Widget");
|
|
mIsPressed = false;
|
|
update(rect());
|
|
emit TxtButtonReleased(this);
|
|
|
|
// qDebug("Text Button Widget clicked");
|
|
emit TxtButtonClicked(this);
|
|
|
|
// QGraphicsWidget::mouseReleaseEvent(event);
|
|
}
|
|
|
|
//void CTextButtonWidget::ButtonClicked()
|
|
//{
|
|
// mIsSelected = false;
|
|
// qDebug("A button is clicked");
|
|
//}
|
|
|
|
void CTextButtonWidget::TimeLineEvent(qreal value)
|
|
{
|
|
Q_UNUSED(value)
|
|
//// ClickTransparency = 255*value;
|
|
|
|
// update();
|
|
|
|
// if(value == 0)
|
|
// {
|
|
// mIsAnimating = false;
|
|
// emit clicked(this);
|
|
// }
|
|
}
|
|
|
|
void CTextButtonWidget::TickScrollingText()
|
|
{
|
|
if(mScrollingRequired && isVisible())
|
|
{
|
|
QChar Character = mWidgetText.at(mSongNameIndex);
|
|
mWidgetText.remove(mSongNameIndex,1);
|
|
mWidgetText.append(Character);
|
|
update();
|
|
}
|
|
}
|