[137] | 1 | /* smplayer, GUI front-end for mplayer.
|
---|
[188] | 2 | Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
[137] | 3 | umplayer, Copyright (C) 2010 Ori Rejwan
|
---|
| 4 |
|
---|
| 5 | This program is free software; you can redistribute it and/or modify
|
---|
| 6 | it under the terms of the GNU General Public License as published by
|
---|
| 7 | the Free Software Foundation; either version 2 of the License, or
|
---|
| 8 | (at your option) any later version.
|
---|
| 9 |
|
---|
| 10 | This program is distributed in the hope that it will be useful,
|
---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | GNU General Public License for more details.
|
---|
| 14 |
|
---|
| 15 | You should have received a copy of the GNU General Public License
|
---|
| 16 | along with this program; if not, write to the Free Software
|
---|
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | #include "mybutton.h"
|
---|
| 21 | #include <QPaintEvent>
|
---|
| 22 | #include <QPainter>
|
---|
| 23 | #include <QDebug>
|
---|
| 24 | #include "myaction.h"
|
---|
| 25 |
|
---|
| 26 | MyButton::MyButton(QWidget *parent) :
|
---|
| 27 | QAbstractButton(parent), mouseHover(false), state(false), action(0)
|
---|
| 28 | {
|
---|
| 29 |
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | void MyButton::paintEvent(QPaintEvent *e)
|
---|
| 34 | {
|
---|
| 35 | QPixmap pix;
|
---|
| 36 | if(isEnabled() && ( isDown() || isChecked()))
|
---|
| 37 | {
|
---|
| 38 | pix = icon.pixmap(MyIcon::MouseDown, state ? MyIcon::On : MyIcon::Off);
|
---|
| 39 | }
|
---|
| 40 | else if(isEnabled() && mouseHover)
|
---|
| 41 | {
|
---|
| 42 | pix = icon.pixmap(MyIcon::MouseOver, state ? MyIcon::On : MyIcon::Off);
|
---|
| 43 | }
|
---|
| 44 | else if(isEnabled())
|
---|
| 45 | {
|
---|
| 46 | pix = icon.pixmap(MyIcon::Normal, state ? MyIcon::On : MyIcon::Off);
|
---|
| 47 | }
|
---|
| 48 | else
|
---|
| 49 | {
|
---|
| 50 | pix = icon.pixmap(MyIcon::Disabled, state ? MyIcon::On : MyIcon::Off);
|
---|
| 51 | }
|
---|
| 52 | QPainter p(this);
|
---|
| 53 | if(!pix.isNull())
|
---|
| 54 | p.drawPixmap(0,0,pix);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | void MyButton::enterEvent(QEvent *)
|
---|
| 59 | {
|
---|
| 60 | mouseHover = true;
|
---|
| 61 | update();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | void MyButton::leaveEvent(QEvent *)
|
---|
| 65 | {
|
---|
| 66 | mouseHover = false;
|
---|
| 67 | update();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void MyButton::setAction(MyAction *pAction)
|
---|
| 71 | {
|
---|
| 72 | action = pAction;
|
---|
| 73 | if(action)
|
---|
| 74 | {
|
---|
| 75 | setEnabled(action->isEnabled());
|
---|
| 76 | action->installEventFilter(this);
|
---|
| 77 | connect(this, SIGNAL(clicked()), action, SLOT(trigger()));
|
---|
| 78 | if( action->isCheckable())
|
---|
| 79 | {
|
---|
| 80 | toggleImage();
|
---|
| 81 | connect(action, SIGNAL(toggled(bool)), this, SLOT(toggleImage()));
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | bool MyButton::eventFilter(QObject *watched, QEvent *event)
|
---|
| 87 | {
|
---|
| 88 | if(watched == action)
|
---|
| 89 | {
|
---|
| 90 | if(event->type() == QEvent::ActionChanged)
|
---|
| 91 | {
|
---|
| 92 | setEnabled(action->isEnabled());
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | return false;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void MyButton::toggleImage()
|
---|
| 99 | {
|
---|
| 100 | if(isCheckable()) setChecked(action->isChecked());
|
---|
| 101 | else setState(action->isChecked());
|
---|
| 102 | update();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | #include "moc_mybutton.cpp"
|
---|