1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
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; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "autohidewidget.h"
|
---|
20 | #include <QTimer>
|
---|
21 | #include <QEvent>
|
---|
22 | #include <QVBoxLayout>
|
---|
23 | #include <QMouseEvent>
|
---|
24 | #include <QDebug>
|
---|
25 |
|
---|
26 | #if QT_VERSION >= 0x040600
|
---|
27 | #include <QPropertyAnimation>
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | AutohideWidget::AutohideWidget(QWidget * parent)
|
---|
31 | : QWidget(parent)
|
---|
32 | , turned_on(false)
|
---|
33 | , auto_hide(false)
|
---|
34 | , use_animation(false)
|
---|
35 | , spacing(0)
|
---|
36 | , perc_width(100)
|
---|
37 | , activation_area(Anywhere)
|
---|
38 | , internal_widget(0)
|
---|
39 | , timer(0)
|
---|
40 | #if QT_VERSION >= 0x040600
|
---|
41 | , animation(0)
|
---|
42 | #endif
|
---|
43 | {
|
---|
44 | setBackgroundRole(QPalette::Base);
|
---|
45 | setAutoFillBackground(true);
|
---|
46 | setLayoutDirection(Qt::LeftToRight);
|
---|
47 |
|
---|
48 | parent->installEventFilter(this);
|
---|
49 | installFilter(parent);
|
---|
50 |
|
---|
51 | timer = new QTimer(this);
|
---|
52 | connect(timer, SIGNAL(timeout()), this, SLOT(checkUnderMouse()));
|
---|
53 | timer->setInterval(3000);
|
---|
54 |
|
---|
55 | QVBoxLayout *layout = new QVBoxLayout;
|
---|
56 | layout->setSpacing(0);
|
---|
57 | layout->setMargin(0);
|
---|
58 | setLayout(layout);
|
---|
59 | }
|
---|
60 |
|
---|
61 | AutohideWidget::~AutohideWidget() {
|
---|
62 | #if QT_VERSION >= 0x040600
|
---|
63 | if (animation) delete animation;
|
---|
64 | #endif
|
---|
65 | }
|
---|
66 |
|
---|
67 | void AutohideWidget::setInternalWidget(QWidget * w) {
|
---|
68 | layout()->addWidget(w);
|
---|
69 | internal_widget = w;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void AutohideWidget::setHideDelay(int ms) {
|
---|
73 | timer->setInterval(ms);
|
---|
74 | }
|
---|
75 |
|
---|
76 | int AutohideWidget::hideDelay() {
|
---|
77 | return timer->interval();
|
---|
78 | }
|
---|
79 |
|
---|
80 | void AutohideWidget::installFilter(QObject *o) {
|
---|
81 | QObjectList children = o->children();
|
---|
82 | for (int n=0; n < children.count(); n++) {
|
---|
83 | if (children[n]->isWidgetType()) {
|
---|
84 | qDebug() << "AutohideWidget::installFilter: child name:" << children[n]->objectName();
|
---|
85 | QWidget *w = static_cast<QWidget *>(children[n]);
|
---|
86 | w->setMouseTracking(true);
|
---|
87 | w->installEventFilter(this);
|
---|
88 | installFilter(children[n]);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | void AutohideWidget::activate() {
|
---|
94 | turned_on = true;
|
---|
95 | timer->start();
|
---|
96 | }
|
---|
97 |
|
---|
98 | void AutohideWidget::deactivate() {
|
---|
99 | turned_on = false;
|
---|
100 | timer->stop();
|
---|
101 | hide();
|
---|
102 | }
|
---|
103 |
|
---|
104 | void AutohideWidget::show() {
|
---|
105 | qDebug() << "AutohideWidget::show";
|
---|
106 | resizeAndMove();
|
---|
107 |
|
---|
108 | if (use_animation) {
|
---|
109 | showAnimated();
|
---|
110 | } else {
|
---|
111 | QWidget::show();
|
---|
112 | }
|
---|
113 |
|
---|
114 | // Restart timer
|
---|
115 | if (timer->isActive()) timer->start();
|
---|
116 | }
|
---|
117 |
|
---|
118 | void AutohideWidget::setAutoHide(bool b) {
|
---|
119 | auto_hide = b;
|
---|
120 | }
|
---|
121 |
|
---|
122 | void AutohideWidget::checkUnderMouse() {
|
---|
123 | if (auto_hide) {
|
---|
124 | //qDebug("AutohideWidget::checkUnderMouse");
|
---|
125 | if ((isVisible()) && (!underMouse())) hide();
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | void AutohideWidget::resizeAndMove() {
|
---|
130 | QWidget * widget = parentWidget();
|
---|
131 | int w = widget->width() * perc_width / 100;
|
---|
132 | int h = height();
|
---|
133 | resize(w, h);
|
---|
134 |
|
---|
135 | int x = (widget->width() - width() ) / 2;
|
---|
136 | int y = widget->height() - height() - spacing;
|
---|
137 | move(x, y);
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool AutohideWidget::eventFilter(QObject * obj, QEvent * event) {
|
---|
141 | if (turned_on) {
|
---|
142 | //qDebug() << "AutohideWidget::eventFilter: obj:" << obj << "type:" << event->type();
|
---|
143 | if (event->type() == QEvent::MouseMove) {
|
---|
144 | //qDebug() << "AutohideWidget::eventFilter: mouse move" << obj;
|
---|
145 | if (!isVisible()) {
|
---|
146 | if (activation_area == Anywhere) {
|
---|
147 | show();
|
---|
148 | } else {
|
---|
149 | QMouseEvent * mouse_event = dynamic_cast<QMouseEvent*>(event);
|
---|
150 | QWidget * parent = parentWidget();
|
---|
151 | QPoint p = parent->mapFromGlobal(mouse_event->globalPos());
|
---|
152 | //qDebug() << "AutohideWidget::eventFilter: y:" << p.y();
|
---|
153 | if (p.y() > (parent->height() - height() - spacing)) {
|
---|
154 | show();
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | return QWidget::eventFilter(obj, event);
|
---|
162 | }
|
---|
163 |
|
---|
164 | void AutohideWidget::showAnimated() {
|
---|
165 | #if QT_VERSION >= 0x040600
|
---|
166 | if (!animation) {
|
---|
167 | animation = new QPropertyAnimation(this, "pos");
|
---|
168 | }
|
---|
169 |
|
---|
170 | QPoint initial_position = QPoint(pos().x(), parentWidget()->size().height());
|
---|
171 | QPoint final_position = pos();
|
---|
172 | move(initial_position);
|
---|
173 |
|
---|
174 | QWidget::show();
|
---|
175 |
|
---|
176 | animation->setDuration(300);
|
---|
177 | animation->setEasingCurve(QEasingCurve::OutBounce);
|
---|
178 | animation->setEndValue(final_position);
|
---|
179 | animation->setStartValue(initial_position);
|
---|
180 | animation->start();
|
---|
181 | #else
|
---|
182 | QWidget::show();
|
---|
183 | #endif
|
---|
184 | }
|
---|
185 |
|
---|
186 | #include "moc_autohidewidget.cpp"
|
---|
187 |
|
---|