Improve performance of partial repaints
[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
50     void showMessage(const QString &s) {
51         message.setText(s);
52         message.show();
53         message_timer.start();
54     }
55 protected:
56     virtual void resizeEvent(QResizeEvent* ev) {
57         QScrollArea::resizeEvent(ev);
58         message.setFixedWidth(width());
59     }
60     virtual void scrollContentsBy(int dx, int dy) {
61         QScrollArea::scrollContentsBy(dx, dy);
62         if(widget()) {
63             const QRegion visible_region_new = widget()->visibleRegion();
64             const QRegion visible_region_old = visible_region_new.translated(-dx, -dy);
65
66             //now update only the region that became visible
67             widget()->update(visible_region_new - visible_region_old);
68         }
69     }
70 private:
71     QLabel message;
72     QTimer message_timer;
73 };
74 #endif