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->addAction("Mod", this, SLOT(showModifierMenu()));
26 toolbar->addAction("Tab", this, SLOT(sendTab()));
27 toolbar->addAction("Esc", this, SLOT(sendEsc()));
28 toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
29 toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
30 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
32 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
35 QMenuBar *menu = new QMenuBar(this);
36 QAction *connect_action = new QAction("Connect", this);
37 disconnect_action = new QAction("Disconnect", this);
38 menu->addAction(connect_action);
39 menu->addAction(disconnect_action);
40 scaling = new QAction("Rescale Remote Screen", this);
41 scaling->setCheckable(true);
42 scaling->setChecked(settings.value("rescale", true).toBool());
43 menu->addAction(scaling);
44 QAction *show_toolbar = new QAction("Show Toolbar", this);
45 show_toolbar->setCheckable(true);
46 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
47 menu->addAction(show_toolbar);
48 QAction *pref_action = new QAction("Preferences", this);
49 menu->addAction(pref_action);
50 QAction *about_action = new QAction("About", this);
51 menu->addAction(about_action);
53 //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
56 connect(about_action, SIGNAL(triggered()),
58 connect(pref_action, SIGNAL(triggered()),
59 this, SLOT(showPreferences()));
60 connect(connect_action, SIGNAL(triggered()),
61 this, SLOT(connectDialog()));
62 connect(disconnect_action, SIGNAL(triggered()),
63 this, SLOT(disconnectFromHost()));
64 connect(show_toolbar, SIGNAL(toggled(bool)),
65 toolbar, SLOT(setVisible(bool)));
66 connect(show_toolbar, SIGNAL(toggled(bool)),
67 this, SLOT(forceResizeDelayed()));
69 setCentralWidget(scroll_area);
74 connect(QApplication::desktop(), SIGNAL(resized(int)),
75 this, SLOT(forceResize()));
78 disconnect_action->setEnabled(false);
79 toolbar->setEnabled(false);
82 vnc_view = new VncView(0, url, RemoteView::Quality(quality));
83 connect(scaling, SIGNAL(toggled(bool)),
84 vnc_view, SLOT(enableScaling(bool)));
85 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
86 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
87 scroll_area->setWidget(vnc_view);
89 vnc_view->enableScaling(scaling->isChecked());
93 void MainWindow::grabZoomKeys(bool grab)
95 unsigned long val = (grab)?1:0;
96 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
98 qWarning("Couldn't get zoom key atom");
101 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
102 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
105 void MainWindow::closeEvent(QCloseEvent*) {
109 settings.setValue("show_toolbar", toolbar->isVisible());
110 settings.setValue("rescale", scaling->isChecked());
115 disconnectFromHost();
118 void MainWindow::about() {
119 QMessageBox::about(this, tr("About Presence VNC"),
120 tr("<center><h1>Presence VNC 0.1 beta</h1>\
121 A touch screen friendly VNC client\
122 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
123 <p>Based on KRDC, © 2007-2008 Urs Wolfer</small></center>\
124 <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>"));
127 /* swipe not used, and doesn't work without scaling anyway :/
128 virtual bool event(QEvent *event) {
129 if(event->type() == QEvent::MouseMove) {
130 QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
131 if(!swipe_start.isNull()) {
132 QPoint diff = swipe_start - ev->pos();
133 const int swipe_dist = 60;
134 if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
136 swipe_start = QPoint(0,0);
138 } else if((width() - ev->x()) < 10) {
139 swipe_start = ev->pos();
141 std::cout << "mousex: " << width() - ev->x() << "\n";
142 //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
144 } else if(event->type() == QEvent::MouseButtonRelease) {
145 swipe_start = QPoint(0,0);
148 // std::cout << "event " << event->type() << "\n";
149 return QScrollArea::event(event);
154 void MainWindow::connectDialog()
157 QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
158 if(url.isEmpty()) { //dialog dismissed or nothing entered
161 settings.setValue("last_hostname", url);
162 url = "vnc://" + url;
164 disconnectFromHost();
166 vnc_view = new VncView(0, url, RemoteView::Quality(2)); //TODO: get quality in dialog
167 scroll_area->setWidget(vnc_view);
169 connect(scaling, SIGNAL(toggled(bool)),
170 vnc_view, SLOT(enableScaling(bool)));
171 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
172 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
174 vnc_view->enableScaling(scaling->isChecked());
175 disconnect_action->setEnabled(true);
176 toolbar->setEnabled(true);
179 void MainWindow::disconnectFromHost()
184 vnc_view->startQuitting();
185 scroll_area->setWidget(0);
187 vnc_view->disconnect(); //remove all connections
190 disconnect_action->setEnabled(false);
191 toolbar->setEnabled(false);
194 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
196 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
199 case RemoteView::Connecting:
200 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
202 case RemoteView::Connected:
203 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
205 case RemoteView::Disconnecting:
206 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
207 QMaemo5InformationBox::information(this, "Connection lost");
210 case RemoteView::Disconnected:
211 if(old_status == RemoteView::Disconnecting) {
212 scroll_area->setWidget(0); //remove widget
220 //when rescaling is enabled, this resizes the widget to use available screen space
221 //necessary when rotating, showing fullscreen, etc.
222 void MainWindow::forceResize()
224 if(vnc_view and scaling->isChecked()) {
225 vnc_view->resize(scroll_area->size());
229 void MainWindow::forceResizeDelayed()
231 QTimer::singleShot(500, this, SLOT(forceResize()));
234 void MainWindow::toggleFullscreen()
236 setWindowState(windowState() ^ Qt::WindowFullScreen);
237 forceResizeDelayed();
240 void MainWindow::showModifierMenu()
242 static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
243 static QAction *win = mod_menu->addAction(tr("Win"));
244 static QAction *alt = mod_menu->addAction(tr("Alt"));
245 win->setCheckable(true);
246 alt->setCheckable(true);
248 //show menu at top-left corner of toolbar
249 QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
252 } else if(chosen == alt) {
253 vnc_view->sendKey(Qt::Key_Alt);
254 } else if(chosen == win) {
255 vnc_view->sendKey(Qt::Key_Meta);
257 std::cout << "unhandled action?\n";
261 void MainWindow::showPreferences()
263 Preferences *p = new Preferences(this);
270 void MainWindow::loadPreferences()
273 int rotation = settings.value("screen_rotation", 0).toInt();
274 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
275 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
276 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);