standardise capitalisation in UI
[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, bool view_only):
37         QMainWindow(0),
38         vnc_view(0),
39         scroll_area(new ScrollArea(0)),
40         input_toolbuttons(new QActionGroup(this)),
41         key_menu(new KeyMenu(this))
42 {
43         setWindowTitle("Presence VNC");
44 #ifdef Q_WS_MAEMO_5
45         setContextMenuPolicy(Qt::NoContextMenu);
46         setAttribute(Qt::WA_Maemo5StackedWindow);
47 #endif
48
49         migrateConfiguration();
50
51         //set up toolbar
52         toolbar = new QToolBar(0);
53         input_toolbuttons->addAction(toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu()))); //"..." button
54         input_toolbuttons->addAction(toolbar->addAction(tr("Tab"), this, SLOT(sendTab())));
55         input_toolbuttons->addAction(toolbar->addAction(tr("Esc"), this, SLOT(sendEsc())));
56         input_toolbuttons->addAction(toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp())));
57         input_toolbuttons->addAction(toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn())));
58 #ifdef Q_WS_MAEMO_5
59         input_toolbuttons->addAction(toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn())));
60         input_toolbuttons->addAction(toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel())));
61 #endif
62
63         QSettings settings;
64         zoom_slider = new QSlider(Qt::Horizontal, 0);
65         zoom_slider->setRange(0, 100);
66         connect(zoom_slider, SIGNAL(valueChanged(int)),
67                 this, SLOT(setZoomLevel(int)));
68         connect(zoom_slider, SIGNAL(sliderReleased()),
69                 this, SLOT(zoomSliderReleased()));
70         zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
71         toolbar->addWidget(zoom_slider);
72
73         toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
74         addToolBar(toolbar);
75         toolbar->setVisible(settings.value("show_toolbar", true).toBool());
76         toolbar->setEnabled(false);
77
78         //set up menu
79         QAction *connect_action = new QAction(tr("Connect"), this);
80         disconnect_action = new QAction(tr("Disconnect"), this);
81         show_toolbar = new QAction(tr("Show toolbar"), this);
82         show_toolbar->setCheckable(true);
83         show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
84         QAction *pref_action = new QAction(tr("Preferences"), this);
85         QAction *about_action = new QAction(tr("About"), this);
86
87 #ifdef Q_WS_MAEMO_5
88         menuBar()->addAction(connect_action);
89         menuBar()->addAction(disconnect_action);
90         menuBar()->addAction(show_toolbar);
91         menuBar()->addAction(pref_action);
92         menuBar()->addAction(about_action);
93 #else
94         QMenu* session_menu = menuBar()->addMenu(tr("&Session"));
95         session_menu->addAction(connect_action);
96         session_menu->addAction(disconnect_action);
97         session_menu->addSeparator();
98         session_menu->addAction(pref_action);
99         session_menu->addSeparator();
100         session_menu->addAction(tr("&Quit"), this, SLOT(close()));
101
102         QMenu* view_menu = menuBar()->addMenu(tr("&View"));
103         view_menu->addAction(show_toolbar);
104
105         QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
106         help_menu->addAction(about_action);
107 #endif
108
109         connect(about_action, SIGNAL(triggered()),
110                 this, SLOT(about()));
111         connect(pref_action, SIGNAL(triggered()),
112                 this, SLOT(showPreferences()));
113         connect(connect_action, SIGNAL(triggered()),
114                 this, SLOT(showConnectDialog()));
115         connect(disconnect_action, SIGNAL(triggered()),
116                 this, SLOT(disconnectFromHost()));
117         connect(show_toolbar, SIGNAL(toggled(bool)),
118                 toolbar, SLOT(setVisible(bool)));
119         connect(show_toolbar, SIGNAL(toggled(bool)),
120                 this, SLOT(updateScreenSpaceDelayed()));
121
122         setCentralWidget(scroll_area);
123
124         FullScreenExitButton* fullscreen_exit_button = new FullScreenExitButton(this);
125         connect(fullscreen_exit_button, SIGNAL(clicked()),
126                 this, SLOT(toggleFullscreen()));
127
128         grabZoomKeys(true);
129         reloadSettings();
130
131         if(url.isEmpty()) {
132                 disconnect_action->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->setViewOnly(view_only);
140                 vnc_view->start();
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.8</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
201         disconnect_action->setEnabled(true);
202
203         //reset key menu
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         disconnect_action->setEnabled(false);
214         toolbar->setEnabled(false);
215         scroll_area->setWidget(0);
216
217         delete vnc_view;
218         vnc_view = 0;
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                 toolbar->setEnabled(true);
236
237                 //disable key input buttons in view only mode
238                 input_toolbuttons->setEnabled(!vnc_view->viewOnly());
239
240                 vnc_view->setZoomLevel(zoom_slider->value());
241                 vnc_view->useFastTransformations(false);
242                 vnc_view->repaint();
243                 break;
244         case RemoteView::Disconnecting:
245                 if(old_status == RemoteView::Disconnected) //Disconnecting also occurs while connecting, so check last state
246                         break;
247
248                 if(disconnect_action->isEnabled()) //don't show when manually disconnecting
249                         scroll_area->showMessage(tr("Connection lost"));
250                 
251                 //clean up
252                 scroll_area->setWidget(0);
253                 vnc_view = 0;
254                 disconnect_action->setEnabled(false);
255                 toolbar->setEnabled(false);
256
257                 //exit fullscreen mode
258                 if(windowState() & Qt::WindowFullScreen)
259                         setWindowState(windowState() ^ Qt::WindowFullScreen);
260                 break;
261         case RemoteView::Disconnected:
262 #ifdef Q_WS_MAEMO_5
263                 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
264 #endif
265                 if(old_status == RemoteView::Disconnecting) {
266                         scroll_area->setWidget(0); //remove widget
267                 }
268                 break;
269         default: //avoid compiler warnings
270                 break;
271         }
272
273         old_status = status;
274 }
275
276 //updates available screen space for current zoom level
277 //necessary when rotating, showing fullscreen, etc.
278 void MainWindow::updateScreenSpace()
279 {
280         if(vnc_view) {
281                 vnc_view->setZoomLevel();
282         }
283
284
285 void MainWindow::updateScreenSpaceDelayed()
286 {
287         QTimer::singleShot(500, this, SLOT(updateScreenSpace()));
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         updateScreenSpaceDelayed();
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         zoom_to_cursor = settings.value("zoom_to_cursor", true).toBool();
325         
326 #ifdef Q_WS_MAEMO_5
327         int rotation = settings.value("screen_rotation", 0).toInt();
328         setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
329         setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
330         setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
331 #endif
332
333         if(vnc_view)
334                 vnc_view->reloadSettings();
335 }
336
337 void MainWindow::resizeEvent(QResizeEvent *event)
338 {
339         QMainWindow::resizeEvent(event);
340
341         updateScreenSpace();
342         if(vnc_view)
343                 vnc_view->setZoomLevel(zoom_slider->value());
344         
345 #ifdef Q_WS_MAEMO_5
346         //hide zoom slider in portrait mode
347         zoom_slider->setVisible(height() < width());
348 #endif
349 }
350
351 void MainWindow::showInputPanel()
352 {
353 #ifdef Q_WS_MAEMO_5
354         //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
355         vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
356         vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
357
358         QEvent event(QEvent::RequestSoftwareInputPanel);
359         QApplication::sendEvent(vnc_view, &event);
360 #endif
361 }
362
363 void MainWindow::setZoomLevel(int level)
364 {
365         if(!vnc_view)
366                 return;
367         
368         const qreal old_factor = vnc_view->zoomFactor();
369         QPoint center = vnc_view->visibleRegion().boundingRect().center();
370
371         vnc_view->setZoomLevel(level);
372
373         const qreal new_factor = vnc_view->zoomFactor();
374
375         //scroll to center, if zoom level actually changed
376         if(old_factor != new_factor) {
377                 if(zoom_to_cursor)
378                         center = new_factor * vnc_view->cursorPosition();
379                 else //zoom to center of visible region
380                         center = center * (double(new_factor)/old_factor);
381
382                 scroll_area->ensureVisible(center.x(), center.y(),
383                         vnc_view->visibleRegion().boundingRect().width()/2,
384                         vnc_view->visibleRegion().boundingRect().height()/2);
385
386                 vnc_view->useFastTransformations(zoom_slider->isSliderDown());
387                 vnc_view->update();
388
389                 scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*new_factor)));
390         }
391 }
392
393 void MainWindow::zoomSliderReleased()
394 {
395         static QTime time;
396         if(!time.isNull() and time.elapsed() < 500) //double clicked
397                 zoom_slider->setValue(95); //100%
398         
399         time.restart();
400
401         //stopped zooming, reenable high quality
402         vnc_view->useFastTransformations(false);
403 }
404
405 void MainWindow::sendTab() { vnc_view->sendKey(Qt::Key_Tab); }
406 void MainWindow::sendEsc() { vnc_view->sendKey(Qt::Key_Escape); }
407 void MainWindow::sendPgUp() { vnc_view->sendKey(Qt::Key_PageUp); }
408 void MainWindow::sendPgDn() { vnc_view->sendKey(Qt::Key_PageDown); }
409 void MainWindow::sendReturn() { vnc_view->sendKey(Qt::Key_Return); }