zoom into center of visible area
[presencevnc] / src / mainwindow.cpp
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 #include "connectdialog.h"
20 #include "fullscreenexitbutton.h"
21 #include "keymenu.h"
22 #include "mainwindow.h"
23 #include "preferences.h"
24 #include "vncview.h"
25
26 #ifdef Q_WS_MAEMO_5
27 #include <QtMaemo5>
28 #include <QX11Info>
29
30 #include <X11/Xlib.h>
31 #include <X11/Xatom.h>
32 #endif
33
34
35 MainWindow::MainWindow(QString url, int quality):
36         QMainWindow(0),
37         vnc_view(0),
38         scroll_area(new ScrollArea(0)),
39         key_menu(0)
40 {
41         setWindowTitle("Presence VNC");
42
43         migrateConfiguration();
44         QSettings settings;
45
46 #ifdef Q_WS_MAEMO_5
47         setAttribute(Qt::WA_Maemo5StackedWindow);
48 #endif
49
50         //set up toolbar
51         toolbar = new QToolBar(0);
52         toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
53         toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
54         toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
55         toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
56         toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
57 #ifdef Q_WS_MAEMO_5
58         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
59         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
60 #endif
61
62         /*
63         //move remaining buttons to the right
64         QWidget *spacer = new QWidget();
65         spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
66         toolbar->addWidget(spacer);
67         */
68
69         zoom_slider = new QSlider(Qt::Horizontal, 0);
70         zoom_slider->setRange(0, 100);
71         connect(zoom_slider, SIGNAL(valueChanged(int)),
72                 this, SLOT(setZoomLevel(int)));
73         zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
74         toolbar->addWidget(zoom_slider);
75
76         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
77         addToolBar(toolbar);
78         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
79
80         //set up menu
81         QAction *connect_action = new QAction(tr("Connect"), this);
82         disconnect_action = new QAction(tr("Disconnect"), this);
83         show_toolbar = new QAction(tr("Show Toolbar"), this);
84         show_toolbar->setCheckable(true);
85         show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
86         QAction *pref_action = new QAction(tr("Preferences"), this);
87         QAction *about_action = new QAction(tr("About"), this);
88
89 #ifdef Q_WS_MAEMO_5
90         menuBar()->addAction(connect_action);
91         menuBar()->addAction(disconnect_action);
92         menuBar()->addAction(show_toolbar);
93         menuBar()->addAction(pref_action);
94         menuBar()->addAction(about_action);
95 #else
96         QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
97         session_menu->addAction(connect_action);
98         session_menu->addAction(disconnect_action);
99         session_menu->addSeparator();
100         session_menu->addAction(pref_action);
101         session_menu->addSeparator();
102         session_menu->addAction(tr("&Quit"), this, SLOT(close()));
103
104         QMenu* view_menu = menuBar()->addMenu(tr("&View"));
105         view_menu->addAction(show_toolbar);
106
107         QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
108         help_menu->addAction(about_action);
109 #endif
110
111         connect(about_action, SIGNAL(triggered()),
112                 this, SLOT(about()));
113         connect(pref_action, SIGNAL(triggered()),
114                 this, SLOT(showPreferences()));
115         connect(connect_action, SIGNAL(triggered()),
116                 this, SLOT(showConnectDialog()));
117         connect(disconnect_action, SIGNAL(triggered()),
118                 this, SLOT(disconnectFromHost()));
119         connect(show_toolbar, SIGNAL(toggled(bool)),
120                 toolbar, SLOT(setVisible(bool)));
121         connect(show_toolbar, SIGNAL(toggled(bool)),
122                 this, SLOT(forceResizeDelayed()));
123
124         setCentralWidget(scroll_area);
125         new FullScreenExitButton(this);
126
127         grabZoomKeys(true);
128         reloadSettings();
129
130         if(url.isNull()) {
131                 disconnect_action->setEnabled(false);
132                 toolbar->setEnabled(false);
133                 showConnectDialog();
134         } else {
135                 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
136                 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
137                         this, SLOT(statusChanged(RemoteView::RemoteStatus)));
138                 scroll_area->setWidget(vnc_view);
139                 vnc_view->start();
140                 key_menu = new KeyMenu(this);
141         }
142 }
143
144 void MainWindow::grabZoomKeys(bool grab)
145 {
146 #ifdef Q_WS_MAEMO_5
147         unsigned long val = (grab)?1:0;
148         Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
149         if(!atom) {
150                 qWarning("Couldn't get zoom key atom");
151                 return;
152         }
153         XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
154                 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
155 #endif
156 }
157
158 void MainWindow::closeEvent(QCloseEvent*) {
159         grabZoomKeys(false);
160
161         QSettings settings;
162         settings.setValue("show_toolbar", show_toolbar->isChecked());
163         settings.setValue("zoomlevel", zoom_slider->value());
164         settings.sync();
165
166         hide();
167
168         disconnectFromHost();
169 }
170
171 void MainWindow::about() {
172         QMessageBox::about(this, tr("About Presence VNC"),
173                 tr("<center><h1>Presence VNC 0.6</h1>\
174 <p>A touchscreen friendly VNC client</p>\
175 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
176 <small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;<br />\
177 Based on KRDC, &copy; 2007-2008 Urs Wolfer<br />\
178 and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
179 <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></small>"));
180 }
181
182 void MainWindow::showConnectDialog()
183 {
184         ConnectDialog *connect_dialog = new ConnectDialog(this);
185         connect(connect_dialog, SIGNAL(connectToHost(QString, int, int)),
186                 this, SLOT(connectToHost(QString, int, int)));
187         connect_dialog->exec();
188 }
189
190 void MainWindow::connectToHost(QString url, int quality, int listen_port)
191 {
192         disconnectFromHost();
193
194         vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
195
196         connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
197                 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
198         scroll_area->setWidget(vnc_view);
199         vnc_view->start();
200         disconnect_action->setEnabled(true);
201         toolbar->setEnabled(true);
202
203         if(key_menu) //reset
204                 delete key_menu;
205         key_menu = new KeyMenu(this);
206 }
207
208 void MainWindow::disconnectFromHost()
209 {
210         if(!vnc_view)
211                 return;
212
213         scroll_area->setWidget(0);
214
215         delete vnc_view;
216         vnc_view = 0;
217         disconnect_action->setEnabled(false);
218         toolbar->setEnabled(false);
219 }
220
221 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
222 {
223         static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
224
225         switch(status) {
226         case RemoteView::Connecting:
227 #ifdef Q_WS_MAEMO_5
228                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
229 #endif
230                 break;
231         case RemoteView::Connected:
232 #ifdef Q_WS_MAEMO_5
233                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
234 #endif
235                 vnc_view->setZoomLevel(zoom_slider->value());
236                 vnc_view->forceFullRepaint();
237                 break;
238         case RemoteView::Disconnecting:
239                 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
240 #ifdef Q_WS_MAEMO_5
241                         QMaemo5InformationBox::information(this, tr("Connection lost"));
242 #endif
243                         
244                         //clean up
245                         scroll_area->setWidget(0);
246                         vnc_view = 0;
247                         disconnect_action->setEnabled(false);
248                         toolbar->setEnabled(false);
249
250                         //exit fullscreen mode
251                         if(windowState() & Qt::WindowFullScreen)
252                                 setWindowState(windowState() ^ Qt::WindowFullScreen);
253                 }
254                 break;
255         case RemoteView::Disconnected:
256 #ifdef Q_WS_MAEMO_5
257                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
258 #endif
259                 if(old_status == RemoteView::Disconnecting) {
260                         scroll_area->setWidget(0); //remove widget
261                 }
262                 break;
263         default: //avoid compiler warnings
264                 break;
265         }
266
267         old_status = status;
268 }
269
270 //resizes the widget to use available screen space
271 //necessary when rotating, showing fullscreen, etc.
272 void MainWindow::forceResize()
273 {
274         if(vnc_view) {
275                 vnc_view->resize(scroll_area->size());
276         }
277
278
279 void MainWindow::forceResizeDelayed()
280 {
281         QTimer::singleShot(500, this, SLOT(forceResize()));
282 }
283
284 void MainWindow::toggleFullscreen()
285 {
286         bool in_fullscreen = windowState() & Qt::WindowFullScreen;
287
288         //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
289         toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
290
291 #ifndef Q_WS_MAEMO_5
292         //menu bar is invisible by default on maemo
293         menuBar()->setVisible(in_fullscreen);
294 #endif
295
296         setWindowState(windowState() ^ Qt::WindowFullScreen); 
297         forceResizeDelayed();
298 }
299
300 void MainWindow::showKeyMenu()
301 {
302         key_menu->exec();
303         vnc_view->sendKeySequence(key_menu->getKeySequence());
304 }
305
306 void MainWindow::showPreferences()
307 {
308         Preferences *p = new Preferences(this);
309         p->exec();
310         delete p;
311
312         reloadSettings();
313 }
314
315 void MainWindow::reloadSettings()
316 {
317         QSettings settings;
318 #ifdef Q_WS_MAEMO_5
319         int rotation = settings.value("screen_rotation", 0).toInt();
320         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
321         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
322         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
323 #endif
324
325         if(vnc_view)
326                 vnc_view->reloadSettings();
327 }
328
329 void MainWindow::resizeEvent(QResizeEvent *event)
330 {
331         QMainWindow::resizeEvent(event);
332
333         forceResize();
334         if(vnc_view)
335                 vnc_view->setZoomLevel(zoom_slider->value());
336         
337 #ifdef Q_WS_MAEMO_5
338         //hide zoom slider in portrait mode
339         zoom_slider->setVisible(height() < width());
340 #endif
341 }
342
343 void MainWindow::showInputPanel()
344 {
345 #ifdef Q_WS_MAEMO_5
346         //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
347         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
348         vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
349
350         QEvent event(QEvent::RequestSoftwareInputPanel);
351         QApplication::sendEvent(vnc_view, &event);
352 #endif
353 }
354
355 void MainWindow::setZoomLevel(int level)
356 {
357         if(!vnc_view)
358                 return;
359         
360         int old_width = vnc_view->width();
361         QPoint center = vnc_view->visibleRegion().boundingRect().center();
362
363         vnc_view->setZoomLevel(level);
364
365         int new_width = vnc_view->width();
366         center = center * (double(new_width)/old_width);
367
368         //scroll to center
369         scroll_area->ensureVisible(center.x(), center.y(),
370                 scroll_area->width()/2, scroll_area->height()/2);
371 }