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 setAttribute(Qt::WA_Maemo5StackedWindow);
23 toolbar = new QToolBar(0);
24 //toolbar->setAttribute(Qt::WA_InputMethodEnabled, true);
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("IM", toolbar, SLOT(setFocus())); //doesn't work
31 toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
33 toolbar->setVisible(settings.value("show_toolbar", true).toBool());
36 QMenuBar *menu = new QMenuBar(this);
37 QAction *connect_action = new QAction("Connect", this);
38 disconnect_action = new QAction("Disconnect", this);
39 // menu->addAction(connect_action);
40 // menu->addAction(disconnect_action);
41 scaling = new QAction("Fit to Screen", this);
42 scaling->setCheckable(true);
43 scaling->setChecked(settings.value("rescale", true).toBool());
44 menu->addAction(scaling);
45 QAction *show_toolbar = new QAction("Show Toolbar", this);
46 show_toolbar->setCheckable(true);
47 show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
48 menu->addAction(show_toolbar);
49 QAction *pref_action = new QAction("Preferences", this);
50 menu->addAction(pref_action);
51 QAction *about_action = new QAction("About", this);
52 menu->addAction(about_action);
54 //menu->setAttribute(Qt::WA_Maemo5StackedWindow);
57 connect(about_action, SIGNAL(triggered()),
59 connect(pref_action, SIGNAL(triggered()),
60 this, SLOT(showPreferences()));
61 connect(connect_action, SIGNAL(triggered()),
62 this, SLOT(connectDialog()));
63 connect(disconnect_action, SIGNAL(triggered()),
64 this, SLOT(disconnectFromHost()));
65 connect(show_toolbar, SIGNAL(toggled(bool)),
66 toolbar, SLOT(setVisible(bool)));
67 connect(show_toolbar, SIGNAL(toggled(bool)),
68 this, SLOT(forceResizeDelayed()));
70 setCentralWidget(scroll_area);
75 connect(QApplication::desktop(), SIGNAL(resized(int)),
76 this, SLOT(forceResize()));
79 disconnect_action->setEnabled(false);
80 toolbar->setEnabled(false);
83 vnc_view = new VncView(this, url, RemoteView::Quality(quality));
84 connect(scaling, SIGNAL(toggled(bool)),
85 vnc_view, SLOT(enableScaling(bool)));
86 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
87 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
88 scroll_area->setWidget(vnc_view);
90 vnc_view->enableScaling(scaling->isChecked());
93 if(!vnc_view) //not connected
94 QTimer::singleShot(100, this, SLOT(close()));
97 void MainWindow::grabZoomKeys(bool grab)
99 unsigned long val = (grab)?1:0;
100 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
102 qWarning("Couldn't get zoom key atom");
105 XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER,
106 32, PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
109 void MainWindow::closeEvent(QCloseEvent*) {
113 settings.setValue("show_toolbar", toolbar->isVisible());
114 settings.setValue("rescale", scaling->isChecked());
119 disconnectFromHost();
122 void MainWindow::about() {
123 QMessageBox::about(this, tr("About Presence VNC"),
124 tr("<center><h1>Presence VNC 0.3</h1>\
125 A touchscreen friendly VNC client\
126 <small><p>©2010 Christian Pulvermacher <pulvermacher@gmx.de></p>\
127 <p>Based on KRDC, © 2007-2008 Urs Wolfer</small></center>\
128 <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>"));
131 void MainWindow::connectDialog()
134 QString url = QInputDialog::getText(this, "Connect to Host", "VNC Server:", QLineEdit::Normal, settings.value("last_hostname", "").toString());
135 if(url.isEmpty()) { //dialog dismissed or nothing entered
138 settings.setValue("last_hostname", url);
139 url = "vnc://" + url;
141 disconnectFromHost();
143 vnc_view = new VncView(this, url, RemoteView::Quality(2)); //TODO: get quality in dialog
144 scroll_area->setWidget(vnc_view);
146 connect(scaling, SIGNAL(toggled(bool)),
147 vnc_view, SLOT(enableScaling(bool)));
148 connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
149 this, SLOT(statusChanged(RemoteView::RemoteStatus)));
151 vnc_view->enableScaling(scaling->isChecked());
152 disconnect_action->setEnabled(true);
153 toolbar->setEnabled(true);
156 void MainWindow::disconnectFromHost()
161 //TODO: crashes when deleting vnc_view - no idea why
162 vnc_view->startQuitting();
163 scroll_area->setWidget(0);
165 vnc_view->disconnect(); //remove all connections
168 disconnect_action->setEnabled(false);
169 toolbar->setEnabled(false);
172 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
174 static RemoteView::RemoteStatus old_status = RemoteView::Disconnected;
177 case RemoteView::Connecting:
178 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
180 case RemoteView::Connected:
181 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
183 case RemoteView::Disconnecting:
184 if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
185 QMaemo5InformationBox::information(this, "Connection lost");
189 case RemoteView::Disconnected:
190 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
191 if(old_status == RemoteView::Disconnecting) {
192 scroll_area->setWidget(0); //remove widget
200 //when rescaling is enabled, this resizes the widget to use available screen space
201 //necessary when rotating, showing fullscreen, etc.
202 void MainWindow::forceResize()
204 if(vnc_view and scaling->isChecked()) {
205 vnc_view->resize(scroll_area->size());
209 void MainWindow::forceResizeDelayed()
211 QTimer::singleShot(500, this, SLOT(forceResize()));
214 void MainWindow::toggleFullscreen()
216 setWindowState(windowState() ^ Qt::WindowFullScreen);
217 forceResizeDelayed();
220 void MainWindow::showModifierMenu()
222 static QMenu *mod_menu = new QMenu(tr("Modifiers"), this);
223 static QAction *win = mod_menu->addAction(tr("Win"));
224 static QAction *alt = mod_menu->addAction(tr("Alt"));
225 win->setCheckable(true);
226 alt->setCheckable(true);
228 //show menu at top-left corner of toolbar
229 QAction *chosen = mod_menu->exec(toolbar->mapToGlobal(QPoint(0,0)));
232 } else if(chosen == alt) {
233 vnc_view->sendKey(Qt::Key_Alt);
234 } else if(chosen == win) {
235 vnc_view->sendKey(Qt::Key_Meta);
237 std::cout << "unhandled action?\n";
241 void MainWindow::showPreferences()
243 Preferences *p = new Preferences(this);
250 void MainWindow::reloadSettings()
253 int rotation = settings.value("screen_rotation", 0).toInt();
254 setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
255 setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
256 setAttribute(Qt::WA_Maemo5PortraitOrientation, rotation == 2);
259 vnc_view->reloadSettings();