| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2010 Ricardo Villalba <rvm@escomposlinux.org>
|
|---|
| 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 "floatingwidget.h"
|
|---|
| 20 | #include <QToolBar>
|
|---|
| 21 | #include <QTimer>
|
|---|
| 22 | #include <QHBoxLayout>
|
|---|
| 23 |
|
|---|
| 24 | FloatingWidget::FloatingWidget( QWidget * parent )
|
|---|
| 25 | : QWidget( parent, Qt::Window | Qt::FramelessWindowHint |
|
|---|
| 26 | Qt::WindowStaysOnTopHint )
|
|---|
| 27 | {
|
|---|
| 28 | setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
|
|---|
| 29 |
|
|---|
| 30 | tb = new QToolBar;
|
|---|
| 31 | tb->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
|
|---|
| 32 |
|
|---|
| 33 | QHBoxLayout *layout = new QHBoxLayout;
|
|---|
| 34 | layout->setSpacing(2);
|
|---|
| 35 | layout->setMargin(2);
|
|---|
| 36 | layout->addWidget(tb);
|
|---|
| 37 |
|
|---|
| 38 | setLayout(layout);
|
|---|
| 39 |
|
|---|
| 40 | _margin = 0;
|
|---|
| 41 | _animated = false;
|
|---|
| 42 | animation_timer = new QTimer(this);
|
|---|
| 43 | animation_timer->setInterval(2);
|
|---|
| 44 | connect( animation_timer, SIGNAL(timeout()), this, SLOT(animate()) );
|
|---|
| 45 |
|
|---|
| 46 | connect( &auto_hide_timer, SIGNAL(timeout()),
|
|---|
| 47 | this, SLOT(checkUnderMouse()) );
|
|---|
| 48 | setAutoHide(true);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | FloatingWidget::~FloatingWidget() {
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | #ifndef Q_OS_WIN
|
|---|
| 55 | void FloatingWidget::setBypassWindowManager(bool b) {
|
|---|
| 56 | if (b) {
|
|---|
| 57 | setWindowFlags(windowFlags() | Qt::X11BypassWindowManagerHint);
|
|---|
| 58 | }
|
|---|
| 59 | else {
|
|---|
| 60 | setWindowFlags(windowFlags() & ~Qt::X11BypassWindowManagerHint);
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | #endif
|
|---|
| 64 |
|
|---|
| 65 | void FloatingWidget::setAutoHide(bool b) {
|
|---|
| 66 | auto_hide = b;
|
|---|
| 67 |
|
|---|
| 68 | if (b)
|
|---|
| 69 | auto_hide_timer.start(5000);
|
|---|
| 70 | else
|
|---|
| 71 | auto_hide_timer.stop();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | void FloatingWidget::showOver(QWidget * widget, int size, Place place) {
|
|---|
| 76 | qDebug("FloatingWidget::showOver");
|
|---|
| 77 |
|
|---|
| 78 | int w = widget->width() * size / 100;
|
|---|
| 79 | int h = height();
|
|---|
| 80 | resize( w, h );
|
|---|
| 81 |
|
|---|
| 82 | //qDebug("widget x: %d, y: %d, h: %d, w: %d", widget->x(), widget->y(), widget->width(), widget->height());
|
|---|
| 83 |
|
|---|
| 84 | int x = (widget->width() - width() ) / 2;
|
|---|
| 85 | int y;
|
|---|
| 86 | if (place == Top)
|
|---|
| 87 | y = 0 + _margin;
|
|---|
| 88 | else
|
|---|
| 89 | y = widget->height() - height() - _margin;
|
|---|
| 90 |
|
|---|
| 91 | QPoint p = widget->mapToGlobal(QPoint(x, y));
|
|---|
| 92 |
|
|---|
| 93 | //qDebug("FloatingWidget::showOver: x: %d, y: %d, w: %d, h: %d", x, y, w, h);
|
|---|
| 94 | //qDebug("FloatingWidget::showOver: global x: %d global y: %d", p.x(), p.y());
|
|---|
| 95 | move(p);
|
|---|
| 96 |
|
|---|
| 97 | if (isAnimated()) {
|
|---|
| 98 | Movement m = Upward;
|
|---|
| 99 | if (place == Top) m = Downward;
|
|---|
| 100 | showAnimated(p, m);
|
|---|
| 101 | } else {
|
|---|
| 102 | show();
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void FloatingWidget::showAnimated(QPoint final_position, Movement movement) {
|
|---|
| 107 | current_movement = movement;
|
|---|
| 108 | final_y = final_position.y();
|
|---|
| 109 |
|
|---|
| 110 | if (movement == Upward) {
|
|---|
| 111 | current_y = final_position.y() + height();
|
|---|
| 112 | } else {
|
|---|
| 113 | current_y = final_position.y() - height();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | move(x(), current_y);
|
|---|
| 117 | show();
|
|---|
| 118 |
|
|---|
| 119 | animation_timer->start();
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | void FloatingWidget::animate() {
|
|---|
| 123 | if (current_y == final_y) {
|
|---|
| 124 | animation_timer->stop();
|
|---|
| 125 | } else {
|
|---|
| 126 | if (current_movement == Upward) current_y--; else current_y++;
|
|---|
| 127 | move(x(), current_y);
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | void FloatingWidget::checkUnderMouse() {
|
|---|
| 132 | if (auto_hide) {
|
|---|
| 133 | //qDebug("FloatingWidget::checkUnderMouse");
|
|---|
| 134 | if ((isVisible()) && (!underMouse())) hide();
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | #include "moc_floatingwidget.cpp"
|
|---|