1 #include "mainwindow.h"
12 MainWindow::MainWindow(QString url, int quality):
15 scroll_area(new QScrollArea(0))
17 setWindowTitle("Presence VNC");
18 // swipe_start = QPoint(0,0);
19 setAttribute(Qt::WA_Maemo5StackedWindow);
22 toolbar = new QToolBar(0);
23 toolbar->addAction("Mod"); //TODO
24 toolbar->addAction("Tab", this, SLOT(sendTab()));
25 toolbar->addAction("Esc", this, SLOT(sendEsc()));
26 toolbar->addAction("PgUp", this, SLOT(sendPgUp()));
27 toolbar->addAction("PgDn", this, SLOT(sendPgDn()));
28 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
32 QMenuBar *menu = new QMenuBar(this);
33 QAction *connect_action = new QAction("Connect", this);
34 disconnect_action = new QAction("Disconnect", this);
35 menu->addAction(connect_action);
36 menu->addAction(disconnect_action);
37 scaling = new QAction("Rescale Remote Screen", this);
38 scaling->setCheckable(true);
39 scaling->setChecked(true);
40 menu->addAction(scaling);
41 QAction *show_toolbar = new QAction("Show Toolbar", this);
42 show_toolbar->setCheckable(true);
43 show_toolbar->setChecked(true);
44 menu->addAction(show_toolbar);
45 QAction *about_action = new QAction("About", this);
46 menu->addAction(about_action);
48 //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
51 connect(about_action, SIGNAL(triggered()),
53 connect(connect_action, SIGNAL(triggered()),
54 this, SLOT(connectDialog()));
55 connect(disconnect_action, SIGNAL(triggered()),
56 this, SLOT(disconnectFromHost()));
57 connect(show_toolbar, SIGNAL(toggled(bool)),
58 toolbar, SLOT(setVisible(bool)));
59 connect(show_toolbar, SIGNAL(toggled(bool)),
60 this, SLOT(forceResizeDelayed()));
62 setCentralWidget(scroll_area);
65 setAttribute(Qt::WA_Maemo5AutoOrientation, true);
66 //setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
69 disconnect_action->setEnabled(false);
70 toolbar->setEnabled(false);
73 vnc_view = new VncView(0, url, RemoteView::Quality(quality));
74 connect(scaling, SIGNAL(toggled(bool)),
75 vnc_view, SLOT(enableScaling(bool)));
76 connect(scaling, SIGNAL(toggled(bool)),
77 scroll_area, SLOT(setWidgetResizable(bool)));
78 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
79 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
80 scroll_area->setWidget(vnc_view);
85 void MainWindow::grabZoomKeys(bool grab)
87 unsigned long val = (grab)?1:0;
88 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
90 qWarning("Couldn't get zoom key atom");
93 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
94 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
97 void MainWindow::closeEvent(QCloseEvent*) {
100 disconnectFromHost();
103 void MainWindow::about() {
104 QMessageBox::about(this, tr("About Presence VNC"),
105 tr("<center><h1>Presence VNC 0.1 alpha</h1>\
106 A touch screen friendly VNC client\
107 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
108 <p>Based on KRDC, © 2007-2008 Urs Wolfer</small></center>\
109 <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>"));
112 /* swipe not used, and doesn't work without scaling anyway :/
113 virtual bool event(QEvent *event) {
114 if(event->type() == QEvent::MouseMove) {
115 QMouseEvent *ev = dynamic_cast<QMouseEvent* >(event);
116 if(!swipe_start.isNull()) {
117 QPoint diff = swipe_start - ev->pos();
118 const int swipe_dist = 60;
119 if(diff.x() > swipe_dist and diff.y() < swipe_dist and diff.y() > -swipe_dist) { //
121 swipe_start = QPoint(0,0);
123 } else if((width() - ev->x()) < 10) {
124 swipe_start = ev->pos();
126 std::cout << "mousex: " << width() - ev->x() << "\n";
127 //TODO: make scrolling over border result in wheel events? i get weird (out of range) mouse events when that happens
129 } else if(event->type() == QEvent::MouseButtonRelease) {
130 swipe_start = QPoint(0,0);
133 // std::cout << "event " << event->type() << "\n";
134 return QScrollArea::event(event);
139 void MainWindow::connectDialog()
142 QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
143 if(url.isEmpty()) { //dialog dismissed or nothing entered
146 settings.setValue("last_hostname", url);
147 url = "vnc://" + url;
149 disconnectFromHost();
151 vnc_view = new VncView(0, url, RemoteView::Quality(2)); //TODO: get quality in dialog
152 scroll_area->setWidget(vnc_view);
154 connect(scaling, SIGNAL(toggled(bool)),
155 vnc_view, SLOT(enableScaling(bool)));
156 connect(scaling, SIGNAL(toggled(bool)),
157 scroll_area, SLOT(setWidgetResizable(bool)));
158 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
159 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
161 disconnect_action->setEnabled(true);
162 toolbar->setEnabled(true);
165 void MainWindow::disconnectFromHost()
170 vnc_view->startQuitting();
171 scroll_area->setWidget(0);
173 vnc_view->disconnect(); //remove all connections
176 disconnect_action->setEnabled(false);
177 toolbar->setEnabled(false);
180 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
182 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
185 case RemoteView::Connecting:
186 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
188 case RemoteView::Connected:
189 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
191 case RemoteView::Disconnecting:
192 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
193 QMaemo5InformationBox::information(this, "Connection lost");
196 case RemoteView::Disconnected:
197 if(old_status == RemoteView::Disconnecting) {
198 scroll_area->setWidget(0); //remove widget
206 //when rescaling is enabled, this resizes the widget to use available screen space
207 //necessary when rotating, showing fullscreen, etc.
208 void MainWindow::forceResize()
210 if(vnc_view and scaling->isChecked()) {
211 vnc_view->resize(scroll_area->size());
215 void MainWindow::forceResizeDelayed()
217 QTimer::singleShot(500, this, SLOT(forceResize()));
220 void MainWindow::toggleFullscreen()
222 setWindowState(windowState() ^ Qt::WindowFullScreen);
223 forceResizeDelayed();