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("Rescale Remote 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(0, 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());
95 void MainWindow::grabZoomKeys(bool grab)
97 unsigned long val = (grab)?1:0;
98 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
100 qWarning("Couldn't get zoom key atom");
103 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
104 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
107 void MainWindow::closeEvent(QCloseEvent*) {
111 settings.setValue("show_toolbar", toolbar->isVisible());
112 settings.setValue("rescale", scaling->isChecked());
117 disconnectFromHost();
120 void MainWindow::about() {
121 QMessageBox::about(this, tr("About Presence VNC"),
122 tr("<center><h1>Presence VNC 0.1 beta</h1>\
123 A touch screen friendly VNC client\
124 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
125 <p>Based on KRDC, © 2007-2008 Urs Wolfer</small></center>\
126 <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>"));
129 /* swipe not used, and doesn't work without scaling anyway :/
130 virtual bool event(QEvent *event) {
131 if(event->type() == QEvent::MouseMove) {
132 QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
133 if(!swipe_start.isNull()) {
134 QPoint diff = swipe_start - ev->pos();
135 const int swipe_dist = 60;
136 if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
138 swipe_start = QPoint(0,0);
140 } else if((width() - ev->x()) < 10) {
141 swipe_start = ev->pos();
143 std::cout << "mousex: " << width() - ev->x() << "\n";
144 //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
146 } else if(event->type() == QEvent::MouseButtonRelease) {
147 swipe_start = QPoint(0,0);
150 // std::cout << "event " << event->type() << "\n";
151 return QScrollArea::event(event);
156 void MainWindow::connectDialog()
159 QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
160 if(url.isEmpty()) { //dialog dismissed or nothing entered
163 settings.setValue("last_hostname", url);
164 url = "vnc://" + url;
166 disconnectFromHost();
168 vnc_view = new VncView(0, url, RemoteView::Quality(2)); //TODO: get quality in dialog
169 scroll_area->setWidget(vnc_view);
171 connect(scaling, SIGNAL(toggled(bool)),
172 vnc_view, SLOT(enableScaling(bool)));
173 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
174 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
176 vnc_view->enableScaling(scaling->isChecked());
177 disconnect_action->setEnabled(true);
178 toolbar->setEnabled(true);
181 void MainWindow::disconnectFromHost()
186 vnc_view->startQuitting();
187 scroll_area->setWidget(0);
189 vnc_view->disconnect(); //remove all connections
192 disconnect_action->setEnabled(false);
193 toolbar->setEnabled(false);
196 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
198 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
201 case RemoteView::Connecting:
202 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
204 case RemoteView::Connected:
205 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
207 case RemoteView::Disconnecting:
208 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
209 QMaemo5InformationBox::information(this, "Connection lost");
212 case RemoteView::Disconnected:
213 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
214 if(old_status == RemoteView::Disconnecting) {
215 scroll_area->setWidget(0); //remove widget
223 //when rescaling is enabled, this resizes the widget to use available screen space
224 //necessary when rotating, showing fullscreen, etc.
225 void MainWindow::forceResize()
227 if(vnc_view and scaling->isChecked()) {
228 vnc_view->resize(scroll_area->size());
232 void MainWindow::forceResizeDelayed()
234 QTimer::singleShot(500, this, SLOT(forceResize()));
237 void MainWindow::toggleFullscreen()
239 setWindowState(windowState() ^ Qt::WindowFullScreen);
240 forceResizeDelayed();
243 void MainWindow::showModifierMenu()
245 static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
246 static QAction *win = mod_menu->addAction(tr("Win"));
247 static QAction *alt = mod_menu->addAction(tr("Alt"));
248 win->setCheckable(true);
249 alt->setCheckable(true);
251 //show menu at top-left corner of toolbar
252 QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
255 } else if(chosen == alt) {
256 vnc_view->sendKey(Qt::Key_Alt);
257 } else if(chosen == win) {
258 vnc_view->sendKey(Qt::Key_Meta);
260 std::cout << "unhandled action?\n";
264 void MainWindow::showPreferences()
266 Preferences *p = new Preferences(this);
273 void MainWindow::reloadSettings()
276 int rotation = settings.value("screen_rotation", 0).toInt();
277 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
278 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
279 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
282 vnc_view->reloadSettings();