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);
122 new FullScreenExitButton(this);
128 disconnect_action->setEnabled(false);
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);
139 void MainWindow::grabZoomKeys(bool grab)
142 unsigned long val = (grab)?1:0;
143 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
145 qWarning("Couldn't get zoom key atom");
148 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
149 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
153 void MainWindow::closeEvent(QCloseEvent*) {
157 settings.setValue("show_toolbar", show_toolbar->isChecked());
158 settings.setValue("zoomlevel", zoom_slider->value());
163 disconnectFromHost();
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>©2010 Christian Pulvermacher <pulvermacher@gmx.de><br />\
172 Based on KRDC, © 2007-2008 Urs Wolfer<br />\
173 and LibVNCServer, © 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>"));
177 void MainWindow::showConnectDialog()
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();
185 void MainWindow::connectToHost(QString url, int quality, int listen_port)
187 disconnectFromHost();
189 vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
191 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
192 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
193 scroll_area->setWidget(vnc_view);
196 disconnect_action->setEnabled(true);
200 key_menu = new KeyMenu(this);
203 void MainWindow::disconnectFromHost()
208 disconnect_action->setEnabled(false);
209 toolbar->setEnabled(false);
210 scroll_area->setWidget(0);
216 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
218 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
221 case RemoteView::Connecting:
223 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
226 case RemoteView::Connected:
228 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
230 toolbar->setEnabled(true);
232 vnc_view->setZoomLevel(zoom_slider->value());
233 vnc_view->useFastTransformations(false);
236 case RemoteView::Disconnecting:
237 if(old_status == RemoteView::Disconnected) //Disconnecting also occurs while connecting, so check last state
240 if(disconnect_action->isEnabled()) //don't show when manually disconnecting
241 scroll_area->showMessage(tr("Connection lost"));
244 scroll_area->setWidget(0);
246 disconnect_action->setEnabled(false);
247 toolbar->setEnabled(false);
249 //exit fullscreen mode
250 if(windowState() & Qt::WindowFullScreen)
251 setWindowState(windowState() ^ Qt::WindowFullScreen);
253 case RemoteView::Disconnected:
255 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
257 if(old_status == RemoteView::Disconnecting) {
258 scroll_area->setWidget(0); //remove widget
261 default: //avoid compiler warnings
268 //updates available screen space for current zoom level
269 //necessary when rotating, showing fullscreen, etc.
270 void MainWindow::updateScreenSpace()
273 vnc_view->setZoomLevel();
277 void MainWindow::updateScreenSpaceDelayed()
279 QTimer::singleShot(500, this, SLOT(updateScreenSpace()));
282 void MainWindow::toggleFullscreen()
284 bool in_fullscreen = windowState() & Qt::WindowFullScreen;
286 //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
287 toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
290 //menu bar is invisible by default on maemo
291 menuBar()->setVisible(in_fullscreen);
294 setWindowState(windowState() ^ Qt::WindowFullScreen);
295 updateScreenSpaceDelayed();
298 void MainWindow::showKeyMenu()
301 vnc_view->sendKeySequence(key_menu->getKeySequence());
304 void MainWindow::showPreferences()
306 Preferences *p = new Preferences(this);
313 void MainWindow::reloadSettings()
317 int rotation = settings.value("screen_rotation", 0).toInt();
318 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
319 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
320 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
324 vnc_view->reloadSettings();
327 void MainWindow::resizeEvent(QResizeEvent *event)
329 QMainWindow::resizeEvent(event);
333 vnc_view->setZoomLevel(zoom_slider->value());
336 //hide zoom slider in portrait mode
337 zoom_slider->setVisible(height() < width());
341 void MainWindow::showInputPanel()
344 //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
345 vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
346 vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
348 QEvent event(QEvent::RequestSoftwareInputPanel);
349 QApplication::sendEvent(vnc_view, &event);
353 void MainWindow::setZoomLevel(int level)
358 const qreal old_factor = vnc_view->getZoomFactor();
359 QPoint center = vnc_view->visibleRegion().boundingRect().center();
361 vnc_view->setZoomLevel(level);
363 const qreal new_factor = vnc_view->getZoomFactor();
365 //scroll to center, if zoom level actually changed
366 if(old_factor != new_factor) {
367 center = center * (double(new_factor)/old_factor);
368 scroll_area->ensureVisible(center.x(), center.y(),
369 vnc_view->visibleRegion().boundingRect().width()/2,
370 vnc_view->visibleRegion().boundingRect().height()/2);
372 vnc_view->useFastTransformations(zoom_slider->isSliderDown());
375 scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*new_factor)));
379 void MainWindow::zoomSliderReleased()
382 if(!time.isNull() and time.elapsed() < 500) //double clicked
383 zoom_slider->setValue(95); //100%
387 //stopped zooming, reenable high quality
388 vnc_view->useFastTransformations(false);