3 Copyright (C) 2010 Christian Pulvermacher
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.
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.
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.
19 #include "connectdialog.h"
20 #include "fullscreenexitbutton.h"
22 #include "mainwindow.h"
23 #include "preferences.h"
24 #include "scrollarea.h"
32 #include <X11/Xatom.h>
36 MainWindow::MainWindow(QString url, int quality, bool view_only):
39 scroll_area(new ScrollArea(0)),
40 input_toolbuttons(new QActionGroup(this)),
41 key_menu(new KeyMenu(this))
43 setWindowTitle("Presence VNC");
45 setContextMenuPolicy(Qt::NoContextMenu);
46 setAttribute(Qt::WA_Maemo5StackedWindow);
49 migrateConfiguration();
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())));
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())));
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);
73 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
75 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
76 toolbar->setEnabled(false);
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);
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);
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()));
102 QMenu* view_menu = menuBar()->addMenu(tr("&View"));
103 view_menu->addAction(show_toolbar);
105 QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
106 help_menu->addAction(about_action);
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()));
122 setCentralWidget(scroll_area);
124 FullScreenExitButton* fullscreen_exit_button = new FullScreenExitButton(this);
125 connect(fullscreen_exit_button, SIGNAL(clicked()),
126 this, SLOT(toggleFullscreen()));
132 disconnect_action->setEnabled(false);
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);
144 void MainWindow::grabZoomKeys(bool grab)
147 unsigned long val = (grab)?1:0;
148 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
150 qWarning("Couldn't get zoom key atom");
153 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
154 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
158 void MainWindow::closeEvent(QCloseEvent*) {
162 settings.setValue("show_toolbar", show_toolbar->isChecked());
163 settings.setValue("zoomlevel", zoom_slider->value());
168 disconnectFromHost();
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=\"http://presencevnc.garage.maemo.org/\">http://presencevnc.garage.maemo.org/</a></p></center>\
176 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de><br />\
177 Based on KRDC, © 2007-2008 Urs Wolfer<br />\
178 and LibVNCServer, © 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>"));
182 void MainWindow::showConnectDialog()
184 ConnectDialog *connect_dialog = new ConnectDialog(this);
185 connect(connect_dialog, SIGNAL(connectToHost(QString, int, int, bool)),
186 this, SLOT(connectToHost(QString, int, int, bool)));
187 connect_dialog->exec();
190 void MainWindow::connectToHost(QString url, int quality, int listen_port, bool view_only)
192 disconnectFromHost();
194 vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
195 vnc_view->setViewOnly(view_only);
197 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
198 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
199 scroll_area->setWidget(vnc_view);
202 disconnect_action->setEnabled(true);
206 key_menu = new KeyMenu(this);
209 void MainWindow::disconnectFromHost()
214 disconnect_action->setEnabled(false);
215 toolbar->setEnabled(false);
216 scroll_area->setWidget(0);
222 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
224 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
227 case RemoteView::Connecting:
229 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
232 case RemoteView::Connected:
234 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
236 toolbar->setEnabled(true);
238 //disable key input buttons in view only mode
239 input_toolbuttons->setEnabled(!vnc_view->viewOnly());
241 vnc_view->setZoomLevel(zoom_slider->value());
242 vnc_view->useFastTransformations(false);
245 case RemoteView::Disconnecting:
246 if(old_status == RemoteView::Disconnected) //Disconnecting also occurs while connecting, so check last state
249 if(disconnect_action->isEnabled()) //don't show when manually disconnecting
250 scroll_area->showMessage(tr("Connection lost"));
253 scroll_area->setWidget(0);
255 disconnect_action->setEnabled(false);
256 toolbar->setEnabled(false);
258 //exit fullscreen mode
259 if(windowState() & Qt::WindowFullScreen)
260 setWindowState(windowState() ^ Qt::WindowFullScreen);
262 case RemoteView::Disconnected:
264 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
266 if(old_status == RemoteView::Disconnecting) {
267 scroll_area->setWidget(0); //remove widget
270 default: //avoid compiler warnings
277 //updates available screen space for current zoom level
278 //necessary when rotating, showing fullscreen, etc.
279 void MainWindow::updateScreenSpace()
282 vnc_view->setZoomLevel();
286 void MainWindow::updateScreenSpaceDelayed()
288 QTimer::singleShot(500, this, SLOT(updateScreenSpace()));
291 void MainWindow::toggleFullscreen()
293 bool in_fullscreen = windowState() & Qt::WindowFullScreen;
295 //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
296 toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
299 //menu bar is invisible by default on maemo
300 menuBar()->setVisible(in_fullscreen);
303 setWindowState(windowState() ^ Qt::WindowFullScreen);
304 updateScreenSpaceDelayed();
307 void MainWindow::showKeyMenu()
310 vnc_view->sendKeySequence(key_menu->getKeySequence());
313 void MainWindow::showPreferences()
315 Preferences *p = new Preferences(this);
322 void MainWindow::reloadSettings()
325 zoom_to_cursor = settings.value("zoom_to_cursor", true).toBool();
328 int rotation = settings.value("screen_rotation", 0).toInt();
329 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
330 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
331 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
335 vnc_view->reloadSettings();
338 void MainWindow::resizeEvent(QResizeEvent *event)
340 QMainWindow::resizeEvent(event);
344 vnc_view->setZoomLevel(zoom_slider->value());
347 //hide zoom slider in portrait mode
348 zoom_slider->setVisible(height() < width());
352 void MainWindow::showInputPanel()
355 //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
356 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
357 vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
359 QEvent event(QEvent::RequestSoftwareInputPanel);
360 QApplication::sendEvent(vnc_view, &event);
364 void MainWindow::setZoomLevel(int level)
369 const qreal old_factor = vnc_view->zoomFactor();
370 QPoint center = vnc_view->visibleRegion().boundingRect().center();
372 vnc_view->setZoomLevel(level);
374 const qreal new_factor = vnc_view->zoomFactor();
376 //scroll to center, if zoom level actually changed
377 if(old_factor != new_factor) {
379 center = new_factor * vnc_view->cursorPosition();
380 else //zoom to center of visible region
381 center = center * (double(new_factor)/old_factor);
383 scroll_area->ensureVisible(center.x(), center.y(),
384 vnc_view->visibleRegion().boundingRect().width()/2,
385 vnc_view->visibleRegion().boundingRect().height()/2);
387 vnc_view->useFastTransformations(zoom_slider->isSliderDown());
390 scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*new_factor)));
394 void MainWindow::zoomSliderReleased()
397 if(!time.isNull() and time.elapsed() < 500) //double clicked
398 zoom_slider->setValue(95); //100%
402 //stopped zooming, reenable high quality
403 vnc_view->useFastTransformations(false);
406 void MainWindow::sendTab() { vnc_view->sendKey(Qt::Key_Tab); }
407 void MainWindow::sendEsc() { vnc_view->sendKey(Qt::Key_Escape); }
408 void MainWindow::sendPgUp() { vnc_view->sendKey(Qt::Key_PageUp); }
409 void MainWindow::sendPgDn() { vnc_view->sendKey(Qt::Key_PageDown); }
410 void MainWindow::sendReturn() { vnc_view->sendKey(Qt::Key_Return); }