Merge branch 'master' of https://vcs.maemo.org/git/presencevnc
[presencevnc] / src / scrollarea.h
1 /*
2     Presence VNC
3     Copyright (C) 2010 Christian Pulvermacher
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 along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #ifndef SCROLLAREA_H
20 #define SCROLLAREA_H
21
22 #include "fullscreenexitbutton.h"
23
24 #include <QScrollArea>
25 #include <QLabel>
26 #include <QTimer>
27
28 //fixes tearing during scrolling and can display messages
29 class ScrollArea : public QScrollArea
30 {
31     Q_OBJECT
32 public:
33     explicit ScrollArea(QWidget *parent):
34         QScrollArea(parent),
35         message(this),
36         fullscreen_exit_button(parent) //owned by parent!
37     {
38         message.setVisible(false);
39         message.setAlignment(Qt::AlignCenter);
40         message.setWordWrap(true);
41
42         QPalette pal = message.palette();
43         QColor backgroundColor = pal.color(backgroundRole());
44         backgroundColor.setAlpha(128);
45         pal.setColor(backgroundRole(), backgroundColor);
46         message.setPalette(pal);
47         message.setAutoFillBackground(true);
48
49         message_timer.setSingleShot(true);
50         message_timer.setInterval(4000);
51         connect(&message_timer, SIGNAL(timeout()),
52                 &message, SLOT(hide()));
53
54         connect(&fullscreen_exit_button, SIGNAL(clicked()),
55             this, SIGNAL(fullscreenButtonClicked()));
56
57         button_reenable_timer.setSingleShot(true);
58         button_reenable_timer.setInterval(500);
59         connect(&button_reenable_timer, SIGNAL(timeout()),
60                 &fullscreen_exit_button, SLOT(reenable()));
61
62 #ifdef Q_WS_MAEMO_5
63         // disable overshooting because it somehow causes repaint events for the whole widget (slow)
64         QAbstractKineticScroller *scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
65         if (scroller)
66             scroller->setOvershootPolicy(QAbstractKineticScroller::OvershootAlwaysOff);
67 #endif
68     }
69
70 public slots:
71     void showMessage(const QString &s) {
72         message.setText(s);
73         message.show();
74         message_timer.start();
75     }
76
77     //provided for convenience
78     void showMessage(QString /*title*/, QString msg) { showMessage(msg); }
79
80 signals:
81     void fullscreenButtonClicked();
82 protected:
83     virtual void resizeEvent(QResizeEvent* ev) {
84         QScrollArea::resizeEvent(ev);
85         message.setFixedWidth(width());
86     }
87     virtual void scrollContentsBy(int dx, int dy) {
88         QScrollArea::scrollContentsBy(dx, dy);
89         if(widget()) {
90             //overlay-widgets slow down scrolling
91             message.hide();
92             fullscreen_exit_button.hide();
93             fullscreen_exit_button.setEnabled(false);
94             button_reenable_timer.start();
95         }
96     }
97 private:
98     QLabel message;
99     QTimer message_timer;
100     FullScreenExitButton fullscreen_exit_button;
101     QTimer button_reenable_timer;
102 };
103 #endif