1 #include "mainwindow.h"
2 #include "preferences.h"
13 MainWindow::MainWindow(QString url, int quality):
16 scroll_area(new QScrollArea(0))
18 setWindowTitle("Presence VNC");
19 // swipe_start = QPoint(0,0);
20 setAttribute(Qt::WA_Maemo5StackedWindow);
24 toolbar = new QToolBar(0);
25 toolbar->setAttribute(Qt::WA_InputMethodEnabled, true);
26 toolbar->addAction("Mod", this, SLOT(showModifierMenu()));
27 toolbar->addAction("Tab", this, SLOT(sendTab()));
28 toolbar->addAction("Esc", this, SLOT(sendEsc()));
29 toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
30 toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
31 // toolbar->addAction("IM", toolbar, SLOT(setFocus())); //doesn't work
32 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
34 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
37 QMenuBar *menu = new QMenuBar(this);
38 QAction *connect_action = new QAction("Connect", this);
39 disconnect_action = new QAction("Disconnect", this);
40 // menu->addAction(connect_action);
41 // menu->addAction(disconnect_action);
42 scaling = new QAction("Fit to Screen", this);
43 scaling->setCheckable(true);
44 scaling->setChecked(settings.value("rescale", true).toBool());
45 menu->addAction(scaling);
46 QAction *show_toolbar = new QAction("Show Toolbar", this);
47 show_toolbar->setCheckable(true);
48 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
49 menu->addAction(show_toolbar);
50 QAction *pref_action = new QAction("Preferences", this);
51 menu->addAction(pref_action);
52 QAction *about_action = new QAction("About", this);
53 menu->addAction(about_action);
55 //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
58 connect(about_action, SIGNAL(triggered()),
60 connect(pref_action, SIGNAL(triggered()),
61 this, SLOT(showPreferences()));
62 connect(connect_action, SIGNAL(triggered()),
63 this, SLOT(connectDialog()));
64 connect(disconnect_action, SIGNAL(triggered()),
65 this, SLOT(disconnectFromHost()));
66 connect(show_toolbar, SIGNAL(toggled(bool)),
67 toolbar, SLOT(setVisible(bool)));
68 connect(show_toolbar, SIGNAL(toggled(bool)),
69 this, SLOT(forceResizeDelayed()));
71 setCentralWidget(scroll_area);
76 connect(QApplication::desktop(), SIGNAL(resized(int)),
77 this, SLOT(forceResize()));
80 disconnect_action->setEnabled(false);
81 toolbar->setEnabled(false);
84 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
85 connect(scaling, SIGNAL(toggled(bool)),
86 vnc_view, SLOT(enableScaling(bool)));
87 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
88 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
89 scroll_area->setWidget(vnc_view);
91 vnc_view->enableScaling(scaling->isChecked());
94 if(!vnc_view) //not connected
95 QTimer::singleShot(100, this, SLOT(close()));
98 void MainWindow::grabZoomKeys(bool grab)
100 unsigned long val = (grab)?1:0;
101 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
103 qWarning("Couldn't get zoom key atom");
106 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
107 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
110 void MainWindow::closeEvent(QCloseEvent*) {
114 settings.setValue("show_toolbar", toolbar->isVisible());
115 settings.setValue("rescale", scaling->isChecked());
120 disconnectFromHost();
123 void MainWindow::about() {
124 QMessageBox::about(this, tr("About Presence VNC"),
125 tr("<center><h1>Presence VNC 0.1</h1>\
126 A touchscreen friendly VNC client\
127 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
128 <p>Based on KRDC, © 2007-2008 Urs Wolfer</small></center>\
129 <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>"));
132 /* swipe not used, and doesn't work without scaling anyway :/
133 virtual bool event(QEvent *event) {
134 if(event->type() == QEvent::MouseMove) {
135 QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
136 if(!swipe_start.isNull()) {
137 QPoint diff = swipe_start - ev->pos();
138 const int swipe_dist = 60;
139 if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
141 swipe_start = QPoint(0,0);
143 } else if((width() - ev->x()) < 10) {
144 swipe_start = ev->pos();
146 std::cout << "mousex: " << width() - ev->x() << "\n";
147 //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
149 } else if(event->type() == QEvent::MouseButtonRelease) {
150 swipe_start = QPoint(0,0);
153 // std::cout << "event " << event->type() << "\n";
154 return QScrollArea::event(event);
159 void MainWindow::connectDialog()
162 QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
163 if(url.isEmpty()) { //dialog dismissed or nothing entered
166 settings.setValue("last_hostname", url);
167 url = "vnc://" + url;
169 disconnectFromHost();
171 vnc_view = new VncView(this, url, RemoteView::Quality(2)); //TODO: get quality in dialog
172 scroll_area->setWidget(vnc_view);
174 connect(scaling, SIGNAL(toggled(bool)),
175 vnc_view, SLOT(enableScaling(bool)));
176 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
177 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
179 vnc_view->enableScaling(scaling->isChecked());
180 disconnect_action->setEnabled(true);
181 toolbar->setEnabled(true);
184 void MainWindow::disconnectFromHost()
189 //TODO: crashes when deleting vnc_view - no idea why
190 //vnc_view->startQuitting();
191 //scroll_area->setWidget(0);
193 // vnc_view->disconnect(); //remove all connections
196 disconnect_action->setEnabled(false);
197 toolbar->setEnabled(false);
200 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
202 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
205 case RemoteView::Connecting:
206 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
208 case RemoteView::Connected:
209 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
211 case RemoteView::Disconnecting:
212 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
213 QMaemo5InformationBox::information(this, "Connection lost");
217 case RemoteView::Disconnected:
218 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
219 if(old_status == RemoteView::Disconnecting) {
220 scroll_area->setWidget(0); //remove widget
228 //when rescaling is enabled, this resizes the widget to use available screen space
229 //necessary when rotating, showing fullscreen, etc.
230 void MainWindow::forceResize()
232 if(vnc_view and scaling->isChecked()) {
233 vnc_view->resize(scroll_area->size());
237 void MainWindow::forceResizeDelayed()
239 QTimer::singleShot(500, this, SLOT(forceResize()));
242 void MainWindow::toggleFullscreen()
244 setWindowState(windowState() ^ Qt::WindowFullScreen);
245 forceResizeDelayed();
248 void MainWindow::showModifierMenu()
250 static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
251 static QAction *win = mod_menu->addAction(tr("Win"));
252 static QAction *alt = mod_menu->addAction(tr("Alt"));
253 win->setCheckable(true);
254 alt->setCheckable(true);
256 //show menu at top-left corner of toolbar
257 QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
260 } else if(chosen == alt) {
261 vnc_view->sendKey(Qt::Key_Alt);
262 } else if(chosen == win) {
263 vnc_view->sendKey(Qt::Key_Meta);
265 std::cout << "unhandled action?\n";
269 void MainWindow::showPreferences()
271 Preferences *p = new Preferences(this);
278 void MainWindow::reloadSettings()
281 int rotation = settings.value("screen_rotation", 0).toInt();
282 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
283 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
284 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
287 vnc_view->reloadSettings();