1 /*  smplayer, GUI front-end for mplayer.
2     Copyright (C) 2006-2021 Ricardo Villalba <ricardo@smplayer.info>
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 
MyButton(QWidget * parent)26 MyButton::MyButton(QWidget *parent) :
27     QAbstractButton(parent), mouseHover(false), state(false), action(0)
28 {
29 
30 }
31 
32 
paintEvent(QPaintEvent * e)33 void MyButton::paintEvent(QPaintEvent *e)
34 {
35     Q_UNUSED(e);
36 
37     QPixmap pix;
38     if(isEnabled() && ( isDown() || isChecked()))
39     {
40         pix = icon.pixmap(MyIcon::MouseDown, state ? MyIcon::On : MyIcon::Off);
41     }
42     else if(isEnabled() && mouseHover)
43     {
44         pix = icon.pixmap(MyIcon::MouseOver, state ? MyIcon::On : MyIcon::Off);
45     }
46     else if(isEnabled())
47     {
48         pix = icon.pixmap(MyIcon::Normal, state ? MyIcon::On : MyIcon::Off);
49     }
50     else
51     {
52         pix = icon.pixmap(MyIcon::Disabled, state ? MyIcon::On : MyIcon::Off);
53     }
54     QPainter p(this);
55     if(!pix.isNull())
56         p.drawPixmap(0,0,pix);
57 }
58 
59 
enterEvent(QEvent *)60 void MyButton::enterEvent(QEvent *)
61 {
62     mouseHover = true;
63     update();
64 }
65 
leaveEvent(QEvent *)66 void MyButton::leaveEvent(QEvent *)
67 {
68     mouseHover = false;
69     update();
70 }
71 
setAction(MyAction * pAction)72 void MyButton::setAction(MyAction *pAction)
73 {
74     action = pAction;
75     if(action)
76     {
77         setEnabled(action->isEnabled());
78         action->installEventFilter(this);
79         connect(this, SIGNAL(clicked()), action, SLOT(trigger()));
80         if( action->isCheckable())
81         {
82             toggleImage();
83             connect(action, SIGNAL(toggled(bool)), this, SLOT(toggleImage()));
84         }
85     }
86 }
87 
eventFilter(QObject * watched,QEvent * event)88 bool MyButton::eventFilter(QObject *watched, QEvent *event)
89 {
90     if(watched == action)
91     {
92         if(event->type() == QEvent::ActionChanged)
93         {
94             setEnabled(action->isEnabled());
95         }
96     }
97     return false;
98 }
99 
toggleImage()100 void MyButton::toggleImage()
101 {
102     if(isCheckable()) setChecked(action->isChecked());
103     else setState(action->isChecked());
104     update();
105 }
106 
107 #include "moc_mybutton.cpp"
108