resize vnc_view when available space changes, rotation still missing
[presencevnc] / src / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "vncview.h"
3
4 #include <QtMaemo5>
5 #include <QX11Info>
6
7 #include <X11/Xlib.h>
8 #include <X11/Xatom.h>
9
10 #include <iostream>
11
12 MainWindow::MainWindow(QString url, int quality):
13         QMainWindow(0),
14         vnc_view(0),
15         scroll_area(new QScrollArea(0))
16 {
17         setWindowTitle("Presence VNC");
18 //      swipe_start = QPoint(0,0);
19         setAttribute(Qt::WA_Maemo5StackedWindow);
20
21         //set up toolbar
22         toolbar = new QToolBar(0);
23         toolbar->addAction("Mod"); //TODO
24         toolbar->addAction("Tab", this, SLOT(sendTab()));
25         toolbar->addAction("Esc", this, SLOT(sendEsc()));
26         toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
27         toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
28         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
29         addToolBar(toolbar);
30
31         //set up menu
32         QMenuBar *menu = new QMenuBar(this);
33         QAction *connect_action = new QAction("Connect", this);
34         disconnect_action = new QAction("Disconnect", this);
35         menu->addAction(connect_action);
36         menu->addAction(disconnect_action);
37         scaling = new QAction("Rescale Remote Screen", this);
38         scaling->setCheckable(true);
39         scaling->setChecked(true);
40         menu->addAction(scaling);
41         QAction *show_toolbar = new QAction("Show Toolbar", this);
42         show_toolbar->setCheckable(true);
43         show_toolbar->setChecked(true);
44         menu->addAction(show_toolbar);
45         QAction *about_action = new QAction("About", this);
46         menu->addAction(about_action);
47
48         //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
49         //menu->hide();
50
51         connect(about_action, SIGNAL(triggered()),
52                 this, SLOT(about()));
53         connect(connect_action, SIGNAL(triggered()),
54                 this, SLOT(connectDialog()));
55         connect(disconnect_action, SIGNAL(triggered()),
56                 this, SLOT(disconnectFromHost()));
57         connect(show_toolbar, SIGNAL(toggled(bool)),
58                 toolbar, SLOT(setVisible(bool)));
59         connect(show_toolbar, SIGNAL(toggled(bool)),
60                 this, SLOT(forceResizeDelayed()));
61
62         setCentralWidget(scroll_area);
63
64         grabZoomKeys(true);
65         setAttribute(Qt::WA_Maemo5AutoOrientation, true);
66         //setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
67
68         if(url.isNull()) {
69                 disconnect_action->setEnabled(false);
70                 toolbar->setEnabled(false);
71                 connectDialog();
72         } else {
73                 vnc_view = new VncView(0, url, RemoteView::Quality(quality));
74                 connect(scaling, SIGNAL(toggled(bool)),
75                         vnc_view, SLOT(enableScaling(bool)));
76                 connect(scaling, SIGNAL(toggled(bool)),
77                         scroll_area, SLOT(setWidgetResizable(bool)));
78                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
79                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
80                 scroll_area->setWidget(vnc_view);
81                 vnc_view->start();
82         }
83 }
84
85 void MainWindow::grabZoomKeys(bool grab)
86 {
87         unsigned long val = (grab)?1:0;
88         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
89         if(!atom) {
90                 qWarning("Couldn't get zoom key atom");
91                 return;
92         }
93         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
94                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
95 }
96
97 void MainWindow::closeEvent(QCloseEvent*) {
98         hide();
99         grabZoomKeys(false);
100         disconnectFromHost();
101 }
102
103 void MainWindow::about() {
104         QMessageBox::about(this, tr("About Presence VNC"),
105                 tr("<center><h1>Presence VNC 0.1 alpha</h1>\
106 A touch screen friendly VNC client\
107 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt</p>\
108 <p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</small></center>\
109 <p>This program is free software; License: <a href=\"http://www.gnu.org/licenses/gpl-2.0.html\">GNU GPL 2</a> or later.</p>"));
110 }
111
112 /* swipe not used, and doesn't work without scaling anyway :/
113 virtual bool event(QEvent *event) {
114         if(event->type() == QEvent::MouseMove) {
115                 QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
116                 if(!swipe_start.isNull()) {
117                         QPoint diff = swipe_start - ev->pos();
118                         const int swipe_dist = 60;
119                         if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
120                                 menu->show();
121                                 swipe_start = QPoint(0,0);
122                         }
123                 } else if((width() - ev->x()) < 10) {
124                         swipe_start = ev->pos();
125                 }
126                 std::cout << "mousex: " << width() - ev->x() << "\n";
127                 //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
128                 return true;
129         } else if(event->type() == QEvent::MouseButtonRelease) {
130                 swipe_start = QPoint(0,0);
131                 return true;
132         } else {
133 //                      std::cout << "event " << event->type() << "\n";
134                 return QScrollArea::event(event);
135         }
136 }
137 */
138
139 void MainWindow::connectDialog()
140 {
141         QSettings settings;
142         QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
143         if(url.isEmpty()) { //dialog dismissed or nothing entered
144                 return;
145         }
146         settings.setValue("last_hostname", url);
147         url = "vnc://" + url;
148
149         disconnectFromHost();
150
151         vnc_view = new VncView(0, url, RemoteView::Quality(2)); //TODO: get quality in dialog
152         scroll_area->setWidget(vnc_view);
153
154         connect(scaling, SIGNAL(toggled(bool)),
155                 vnc_view, SLOT(enableScaling(bool)));
156         connect(scaling, SIGNAL(toggled(bool)),
157                 scroll_area, SLOT(setWidgetResizable(bool)));
158         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
159                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
160         vnc_view->start();
161         disconnect_action->setEnabled(true);
162         toolbar->setEnabled(true);
163 }
164
165 void MainWindow::disconnectFromHost()
166 {
167         if(!vnc_view)
168                 return;
169
170         vnc_view->startQuitting();
171         scroll_area->setWidget(0);
172
173         vnc_view->disconnect(); //remove all connections
174         delete vnc_view;
175         vnc_view = 0;
176         disconnect_action->setEnabled(false);
177         toolbar->setEnabled(false);
178 }
179
180 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
181 {
182         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
183
184         switch(status) {
185         case RemoteView::Connecting:
186                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
187                 break;
188         case RemoteView::Connected:
189                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
190                 break;
191         case RemoteView::Disconnecting:
192                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
193                         QMaemo5InformationBox::information(this, "Connection lost");
194                 }
195                 break;
196         case RemoteView::Disconnected:
197                 if(old_status == RemoteView::Disconnecting) {
198                         scroll_area->setWidget(0); //remove widget
199                 }
200                 break;
201         }
202
203         old_status = status;
204 }
205
206 //when rescaling is enabled, this resizes the widget to use available screen space
207 //necessary when rotating, showing fullscreen, etc.
208 void MainWindow::forceResize()
209 {
210         if(vnc_view and scaling->isChecked()) {
211                 vnc_view->resize(scroll_area->size());
212         }
213
214
215 void MainWindow::forceResizeDelayed()
216 {
217         QTimer::singleShot(500, this, SLOT(forceResize()));
218 }
219
220 void MainWindow::toggleFullscreen()
221 {
222         setWindowState(windowState() ^ Qt::WindowFullScreen); 
223         forceResizeDelayed();
224 }