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