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):
39 scroll_area(new ScrollArea(0)),
40 key_menu(new KeyMenu(this))
42 setWindowTitle("Presence VNC");
43 setContextMenuPolicy(Qt::NoContextMenu);
45 setAttribute(Qt::WA_Maemo5StackedWindow);
48 migrateConfiguration();
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()));
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()));
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);
72 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
74 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
75 toolbar->setEnabled(false);
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);
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);
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()));
101 QMenu* view_menu = menuBar()->addMenu(tr("&View"));
102 view_menu->addAction(show_toolbar);
104 QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
105 help_menu->addAction(about_action);
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()));
121 setCentralWidget(scroll_area);
123 FullScreenExitButton* fullscreen_exit_button = new FullScreenExitButton(this);
124 connect(fullscreen_exit_button, SIGNAL(clicked()),
125 this, SLOT(toggleFullscreen()));
131 disconnect_action->setEnabled(false);
134 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
135 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
136 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
137 scroll_area->setWidget(vnc_view);
142 void MainWindow::grabZoomKeys(bool grab)
145 unsigned long val = (grab)?1:0;
146 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
148 qWarning("Couldn't get zoom key atom");
151 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
152 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
156 void MainWindow::closeEvent(QCloseEvent*) {
160 settings.setValue("show_toolbar", show_toolbar->isChecked());
161 settings.setValue("zoomlevel", zoom_slider->value());
166 disconnectFromHost();
169 void MainWindow::about() {
170 QMessageBox::about(this, tr("About Presence VNC"),
171 tr("<center><h1>Presence VNC 0.8</h1>\
172 <p>A touchscreen friendly VNC client</p>\
173 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
174 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de><br />\
175 Based on KRDC, © 2007-2008 Urs Wolfer<br />\
176 and LibVNCServer, © 2001-2003 Johannes E. Schindelin</p>\
177 <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 void MainWindow::showConnectDialog()
182 ConnectDialog *connect_dialog = new ConnectDialog(this);
183 connect(connect_dialog, SIGNAL(connectToHost(QString, int, int)),
184 this, SLOT(connectToHost(QString, int, int)));
185 connect_dialog->exec();
188 void MainWindow::connectToHost(QString url, int quality, int listen_port)
190 disconnectFromHost();
192 vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
194 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
195 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
196 scroll_area->setWidget(vnc_view);
199 disconnect_action->setEnabled(true);
203 key_menu = new KeyMenu(this);
206 void MainWindow::disconnectFromHost()
211 disconnect_action->setEnabled(false);
212 toolbar->setEnabled(false);
213 scroll_area->setWidget(0);
219 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
221 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
224 case RemoteView::Connecting:
226 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
229 case RemoteView::Connected:
231 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
233 toolbar->setEnabled(true);
235 vnc_view->setZoomLevel(zoom_slider->value());
236 vnc_view->useFastTransformations(false);
239 case RemoteView::Disconnecting:
240 if(old_status == RemoteView::Disconnected) //Disconnecting also occurs while connecting, so check last state
243 if(disconnect_action->isEnabled()) //don't show when manually disconnecting
244 scroll_area->showMessage(tr("Connection lost"));
247 scroll_area->setWidget(0);
249 disconnect_action->setEnabled(false);
250 toolbar->setEnabled(false);
252 //exit fullscreen mode
253 if(windowState() & Qt::WindowFullScreen)
254 setWindowState(windowState() ^ Qt::WindowFullScreen);
256 case RemoteView::Disconnected:
258 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
260 if(old_status == RemoteView::Disconnecting) {
261 scroll_area->setWidget(0); //remove widget
264 default: //avoid compiler warnings
271 //updates available screen space for current zoom level
272 //necessary when rotating, showing fullscreen, etc.
273 void MainWindow::updateScreenSpace()
276 vnc_view->setZoomLevel();
280 void MainWindow::updateScreenSpaceDelayed()
282 QTimer::singleShot(500, this, SLOT(updateScreenSpace()));
285 void MainWindow::toggleFullscreen()
287 bool in_fullscreen = windowState() & Qt::WindowFullScreen;
289 //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
290 toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
293 //menu bar is invisible by default on maemo
294 menuBar()->setVisible(in_fullscreen);
297 setWindowState(windowState() ^ Qt::WindowFullScreen);
298 updateScreenSpaceDelayed();
301 void MainWindow::showKeyMenu()
304 vnc_view->sendKeySequence(key_menu->getKeySequence());
307 void MainWindow::showPreferences()
309 Preferences *p = new Preferences(this);
316 void MainWindow::reloadSettings()
319 zoom_to_cursor = settings.value("zoom_to_cursor", true).toBool();
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);
329 vnc_view->reloadSettings();
332 void MainWindow::resizeEvent(QResizeEvent *event)
334 QMainWindow::resizeEvent(event);
338 vnc_view->setZoomLevel(zoom_slider->value());
341 //hide zoom slider in portrait mode
342 zoom_slider->setVisible(height() < width());
346 void MainWindow::showInputPanel()
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
353 QEvent event(QEvent::RequestSoftwareInputPanel);
354 QApplication::sendEvent(vnc_view, &event);
358 void MainWindow::setZoomLevel(int level)
363 const qreal old_factor = vnc_view->zoomFactor();
364 QPoint center = vnc_view->visibleRegion().boundingRect().center();
366 vnc_view->setZoomLevel(level);
368 const qreal new_factor = vnc_view->zoomFactor();
370 //scroll to center, if zoom level actually changed
371 if(old_factor != new_factor) {
373 center = new_factor * vnc_view->cursorPosition();
374 else //zoom to center of visible region
375 center = center * (double(new_factor)/old_factor);
377 scroll_area->ensureVisible(center.x(), center.y(),
378 vnc_view->visibleRegion().boundingRect().width()/2,
379 vnc_view->visibleRegion().boundingRect().height()/2);
381 vnc_view->useFastTransformations(zoom_slider->isSliderDown());
384 scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*new_factor)));
388 void MainWindow::zoomSliderReleased()
391 if(!time.isNull() and time.elapsed() < 500) //double clicked
392 zoom_slider->setValue(95); //100%
396 //stopped zooming, reenable high quality
397 vnc_view->useFastTransformations(false);
400 void MainWindow::sendTab() { vnc_view->sendKey(Qt::Key_Tab); }
401 void MainWindow::sendEsc() { vnc_view->sendKey(Qt::Key_Escape); }
402 void MainWindow::sendPgUp() { vnc_view->sendKey(Qt::Key_PageUp); }
403 void MainWindow::sendPgDn() { vnc_view->sendKey(Qt::Key_PageDown); }
404 void MainWindow::sendReturn() { vnc_view->sendKey(Qt::Key_Return); }