disable overshooting during scrolling for performance; hide messages when scrolling
[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 <QScrollArea>
23 #include <QLabel>
24 #include <QTimer>
25
26 //fixes tearing during scrolling and can display messages
27 class ScrollArea : public QScrollArea
28 {
29 public:
30     explicit ScrollArea(QWidget *parent):
31         QScrollArea(parent) {
32         message.setParent(this);
33         message.setVisible(false);
34         message.setAlignment(Qt::AlignCenter);
35         message.setWordWrap(true);
36
37         QPalette pal = message.palette();
38         QColor backgroundColor = pal.color(backgroundRole());
39         backgroundColor.setAlpha(128);
40         pal.setColor(backgroundRole(), backgroundColor);
41         message.setPalette(pal);
42         message.setAutoFillBackground(true);
43
44         message_timer.setSingleShot(true);
45         message_timer.setInterval(2500);
46         connect(&message_timer, SIGNAL(timeout()),
47                 &message, SLOT(hide()));
48
49 #ifdef Q_WS_MAEMO_5
50         // disable overshooting because it somehow causes repaint events for the whole widget (slow)
51         QAbstractKineticScroller *scroller = property("kineticScroller").value<QAbstractKineticScroller *>();
52         if (scroller)
53             scroller->setOvershootPolicy(QAbstractKineticScroller::OvershootAlwaysOff);
54 #endif
55     }
56
57     void showMessage(const QString &s) {
58         message.setText(s);
59         message.show();
60         message_timer.start();
61     }
62 protected:
63     virtual void resizeEvent(QResizeEvent* ev) {
64         QScrollArea::resizeEvent(ev);
65         message.setFixedWidth(width());
66     }
67     virtual void scrollContentsBy(int dx, int dy) {
68         QScrollArea::scrollContentsBy(dx, dy);
69         if(widget())
70             message.hide(); //overlay-widget slows down scrolling
71     }
72 private:
73     QLabel message;
74     QTimer message_timer;
75 };
76 #endif