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