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