update version
[colorflood] / colorflood / src / fullscreenexitbutton.hpp
1 /*
2   Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; version 2 of the License.
7
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   GNU General Public License for more details.
12 */
13
14 #ifndef _FULLSCREENEXITBUTTON_HPP
15 #define _FULLSCREENEXITBUTTON_HPP
16
17 #include <QToolButton>
18 #include <QEvent>
19
20 class FullScreenExitButton : public QToolButton
21 {
22     Q_OBJECT;
23
24 public:
25     inline explicit FullScreenExitButton(QWidget *parent);
26
27 protected:
28     inline bool eventFilter(QObject *obj, QEvent *ev);
29 };
30
31 FullScreenExitButton::FullScreenExitButton(QWidget *parent)
32     : QToolButton(parent)
33 {
34     Q_ASSERT(parent);
35
36     // set the fullsize icon from Maemo's theme
37     setIcon(QIcon::fromTheme(QLatin1String("general_fullsize")));
38
39     // ensure that our size is fixed to our ideal size
40     setFixedSize(sizeHint());
41
42     // set the background to 0.5 alpha
43     QPalette pal = palette();
44     QColor backgroundColor = pal.color(backgroundRole());
45     backgroundColor.setAlpha(128);
46     pal.setColor(backgroundRole(), backgroundColor);
47     setPalette(pal);
48
49     // ensure that we're painting our background
50     setAutoFillBackground(true);
51
52     // when we're clicked, tell the parent to exit fullscreen
53     connect(this, SIGNAL(clicked()), parent, SLOT(showNormal()));
54
55     // install an event filter to listen for the parent's events
56     parent->installEventFilter(this);
57 }
58
59 bool FullScreenExitButton::eventFilter(QObject *obj, QEvent *ev)
60 {
61     if (obj != parent())
62         return QToolButton::eventFilter(obj, ev);
63
64     QWidget *parent = parentWidget();
65     bool isFullScreen = parent->windowState() & Qt::WindowFullScreen;
66
67     switch (ev->type()) {
68     case QEvent::WindowStateChange:
69         setVisible(isFullScreen);
70         if (isFullScreen)
71             raise();
72         // fall through
73     case QEvent::Resize:
74         if (isVisible())
75             move(parent->width() - width(),
76                  parent->height() - height());
77         break;
78     default:
79         break;
80     }
81
82     return QToolButton::eventFilter(obj, ev);
83 }
84
85 #endif // !_FULLSCREENEXITBUTTON_HPP