doubleclicking slider sets 100% zoom
[presencevnc] / src / mainwindow.cpp
index d30fbc6..d52cc85 100644 (file)
 #include "keymenu.h"
 #include "mainwindow.h"
 #include "preferences.h"
+#include "scrollarea.h"
 #include "vncview.h"
 
-#ifdef Q_WS_HILDON
+#ifdef Q_WS_MAEMO_5
 #include <QtMaemo5>
 #include <QX11Info>
 
 MainWindow::MainWindow(QString url, int quality):
        QMainWindow(0),
        vnc_view(0),
-       scroll_area(new ScrollArea(0))
+       scroll_area(new ScrollArea(0)),
+       key_menu(new KeyMenu(this))
 {
        setWindowTitle("Presence VNC");
-
-       migrateConfiguration();
-       QSettings settings;
-
-#ifdef Q_WS_HILDON
+       setContextMenuPolicy(Qt::NoContextMenu);
+#ifdef Q_WS_MAEMO_5
        setAttribute(Qt::WA_Maemo5StackedWindow);
 #endif
 
+       migrateConfiguration();
+
        //set up toolbar
        toolbar = new QToolBar(0);
        toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
@@ -53,36 +54,38 @@ MainWindow::MainWindow(QString url, int quality):
        toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
        toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
        toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
-#ifdef Q_WS_HILDON
+#ifdef Q_WS_MAEMO_5
        toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn()));
        toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel()));
 #endif
 
-       //move fullscreen button to the right
-       QWidget *spacer = new QWidget();
-       spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
-       toolbar->addWidget(spacer);
+       QSettings settings;
+       zoom_slider = new QSlider(Qt::Horizontal, 0);
+       zoom_slider->setRange(0, 100);
+       connect(zoom_slider, SIGNAL(valueChanged(int)),
+               this, SLOT(setZoomLevel(int)));
+       connect(zoom_slider, SIGNAL(sliderReleased()),
+               this, SLOT(zoomSliderReleased()));
+       zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
+       toolbar->addWidget(zoom_slider);
 
        toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/general_fullsize.png"), "", this, SLOT(toggleFullscreen()));
        addToolBar(toolbar);
        toolbar->setVisible(settings.value("show_toolbar", true).toBool());
+       toolbar->setEnabled(false);
 
        //set up menu
        QAction *connect_action = new QAction(tr("Connect"), this);
        disconnect_action = new QAction(tr("Disconnect"), this);
-       scaling = new QAction(tr("Fit to Screen"), this);
-       scaling->setCheckable(true);
-       scaling->setChecked(settings.value("rescale", true).toBool());
        show_toolbar = new QAction(tr("Show Toolbar"), this);
        show_toolbar->setCheckable(true);
        show_toolbar->setChecked(settings.value("show_toolbar", true).toBool());
        QAction *pref_action = new QAction(tr("Preferences"), this);
        QAction *about_action = new QAction(tr("About"), this);
 
-#ifdef Q_WS_HILDON
+#ifdef Q_WS_MAEMO_5
        menuBar()->addAction(connect_action);
        menuBar()->addAction(disconnect_action);
-       menuBar()->addAction(scaling);
        menuBar()->addAction(show_toolbar);
        menuBar()->addAction(pref_action);
        menuBar()->addAction(about_action);
@@ -91,12 +94,11 @@ MainWindow::MainWindow(QString url, int quality):
        session_menu->addAction(connect_action);
        session_menu->addAction(disconnect_action);
        session_menu->addSeparator();
-       //session_menu->addAction(pref_action);
-       //session_menu->addSeparator();
+       session_menu->addAction(pref_action);
+       session_menu->addSeparator();
        session_menu->addAction(tr("&Quit"), this, SLOT(close()));
 
        QMenu* view_menu = menuBar()->addMenu(tr("&View"));
-       view_menu->addAction(scaling);
        view_menu->addAction(show_toolbar);
 
        QMenu* help_menu = menuBar()->addMenu(tr("&Help"));
@@ -114,7 +116,7 @@ MainWindow::MainWindow(QString url, int quality):
        connect(show_toolbar, SIGNAL(toggled(bool)),
                toolbar, SLOT(setVisible(bool)));
        connect(show_toolbar, SIGNAL(toggled(bool)),
-               this, SLOT(forceResizeDelayed()));
+               this, SLOT(updateScreenSpaceDelayed()));
 
        setCentralWidget(scroll_area);
        new FullScreenExitButton(this);
@@ -122,29 +124,21 @@ MainWindow::MainWindow(QString url, int quality):
        grabZoomKeys(true);
        reloadSettings();
 
-       //TODO: can i replace this with resizeEvent?
-       connect(QApplication::desktop(), SIGNAL(resized(int)),
-               this, SLOT(forceResize()));
-
        if(url.isNull()) {
                disconnect_action->setEnabled(false);
-               toolbar->setEnabled(false);
                showConnectDialog();
        } else {
                vnc_view = new VncView(this, url, RemoteView::Quality(quality));
-               connect(scaling, SIGNAL(toggled(bool)),
-                       vnc_view, SLOT(enableScaling(bool)));
                connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
                        this, SLOT(statusChanged(RemoteView::RemoteStatus)));
                scroll_area->setWidget(vnc_view);
                vnc_view->start();
-               vnc_view->enableScaling(scaling->isChecked());
        }
 }
 
 void MainWindow::grabZoomKeys(bool grab)
 {
-#ifdef Q_WS_HILDON
+#ifdef Q_WS_MAEMO_5
        unsigned long val = (grab)?1:0;
        Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
        if(!atom) {
@@ -161,7 +155,7 @@ void MainWindow::closeEvent(QCloseEvent*) {
 
        QSettings settings;
        settings.setValue("show_toolbar", show_toolbar->isChecked());
-       settings.setValue("rescale", scaling->isChecked());
+       settings.setValue("zoomlevel", zoom_slider->value());
        settings.sync();
 
        hide();
@@ -171,38 +165,39 @@ void MainWindow::closeEvent(QCloseEvent*) {
 
 void MainWindow::about() {
        QMessageBox::about(this, tr("About Presence VNC"),
-               tr("<center><h1>Presence VNC 0.5</h1>\
+               tr("<center><h1>Presence VNC 0.7</h1>\
 <p>A touchscreen friendly VNC client</p>\
 <p><a href=\"https://garage.maemo.org/projects/presencevnc/\">https://garage.maemo.org/projects/presencevnc</a></p></center>\
-<small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;</p>\
-<p>Based on KRDC, &copy; 2007-2008 Urs Wolfer</p>\
-<p>and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
+<small><p>&copy;2010 Christian Pulvermacher &lt;pulvermacher@gmx.de&gt;<br />\
+Based on KRDC, &copy; 2007-2008 Urs Wolfer<br />\
+and LibVNCServer, &copy; 2001-2003 Johannes E. Schindelin</p>\
 <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></small>"));
 }
 
 void MainWindow::showConnectDialog()
 {
        ConnectDialog *connect_dialog = new ConnectDialog(this);
-       connect(connect_dialog, SIGNAL(connectToHost(QString)),
-               this, SLOT(connectToHost(QString)));
+       connect(connect_dialog, SIGNAL(connectToHost(QString, int, int)),
+               this, SLOT(connectToHost(QString, int, int)));
        connect_dialog->exec();
 }
 
-void MainWindow::connectToHost(QString url)
+void MainWindow::connectToHost(QString url, int quality, int listen_port)
 {
        disconnectFromHost();
 
-       vnc_view = new VncView(this, url, RemoteView::Quality(2));
+       vnc_view = new VncView(this, url, RemoteView::Quality(quality), listen_port);
 
-       connect(scaling, SIGNAL(toggled(bool)),
-               vnc_view, SLOT(enableScaling(bool)));
        connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
                this, SLOT(statusChanged(RemoteView::RemoteStatus)));
        scroll_area->setWidget(vnc_view);
        vnc_view->start();
-       vnc_view->enableScaling(scaling->isChecked());
+
        disconnect_action->setEnabled(true);
-       toolbar->setEnabled(true);
+
+       //reset key menu
+       delete key_menu;
+       key_menu = new KeyMenu(this);
 }
 
 void MainWindow::disconnectFromHost()
@@ -210,13 +205,12 @@ void MainWindow::disconnectFromHost()
        if(!vnc_view)
                return;
 
+       disconnect_action->setEnabled(false);
+       toolbar->setEnabled(false);
        scroll_area->setWidget(0);
 
-       vnc_view->disconnect(); //remove all signal-slot connections
        delete vnc_view;
        vnc_view = 0;
-       disconnect_action->setEnabled(false);
-       toolbar->setEnabled(false);
 }
 
 void MainWindow::statusChanged(RemoteView::RemoteStatus status)
@@ -225,80 +219,90 @@ void MainWindow::statusChanged(RemoteView::RemoteStatus status)
 
        switch(status) {
        case RemoteView::Connecting:
-#ifdef Q_WS_HILDON
+#ifdef Q_WS_MAEMO_5
                setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
 #endif
                break;
        case RemoteView::Connected:
-#ifdef Q_WS_HILDON
+#ifdef Q_WS_MAEMO_5
                setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
 #endif
-               if(!scaling->isChecked()) {
-                       //ugly hack to force a refresh (forceFullRepaint() doesn't repaint?? -> vnc_view hidden???)
-                       vnc_view->resize(scroll_area->size());
-                       vnc_view->enableScaling(false);
-               }
+               toolbar->setEnabled(true);
+
+               vnc_view->setZoomLevel(zoom_slider->value());
+               vnc_view->forceFullRepaint();
                break;
        case RemoteView::Disconnecting:
-               if(old_status != RemoteView::Disconnected) { //Disconnecting also occurs while connecting, so check last state
-#ifdef Q_WS_HILDON
-                       QMaemo5InformationBox::information(this, tr("Connection lost"));
-#endif
-                       
-                       //clean up
-                       scroll_area->setWidget(0);
-                       vnc_view = 0;
-                       disconnect_action->setEnabled(false);
-                       toolbar->setEnabled(false);
-
-                       //exit fullscreen mode
-                       if(windowState() & Qt::WindowFullScreen)
-                               setWindowState(windowState() ^ Qt::WindowFullScreen);
-               }
+               if(old_status == RemoteView::Disconnected) //Disconnecting also occurs while connecting, so check last state
+                       break;
+
+               if(disconnect_action->isEnabled()) //don't show when manually disconnecting
+                       scroll_area->showMessage(tr("Connection lost"));
+               
+               //clean up
+               scroll_area->setWidget(0);
+               vnc_view = 0;
+               disconnect_action->setEnabled(false);
+               toolbar->setEnabled(false);
+
+               //exit fullscreen mode
+               if(windowState() & Qt::WindowFullScreen)
+                       setWindowState(windowState() ^ Qt::WindowFullScreen);
                break;
        case RemoteView::Disconnected:
-#ifdef Q_WS_HILDON
+#ifdef Q_WS_MAEMO_5
                setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
 #endif
                if(old_status == RemoteView::Disconnecting) {
                        scroll_area->setWidget(0); //remove widget
                }
                break;
+       default: //avoid compiler warnings
+               break;
        }
 
        old_status = status;
 }
 
-//when rescaling is enabled, this resizes the widget to use available screen space
+void MainWindow::forceRepaint()
+{
+       if(vnc_view)
+               vnc_view->forceFullRepaint();
+}
+
+//updates available screen space for current zoom level
 //necessary when rotating, showing fullscreen, etc.
-void MainWindow::forceResize()
+void MainWindow::updateScreenSpace()
 {
-       if(vnc_view and scaling->isChecked()) {
-               vnc_view->resize(scroll_area->size());
+       if(vnc_view) {
+               vnc_view->setZoomLevel();
        }
 } 
 
-void MainWindow::forceResizeDelayed()
+void MainWindow::updateScreenSpaceDelayed()
 {
-       QTimer::singleShot(500, this, SLOT(forceResize()));
+       QTimer::singleShot(500, this, SLOT(updateScreenSpace()));
 }
 
 void MainWindow::toggleFullscreen()
 {
        bool in_fullscreen = windowState() & Qt::WindowFullScreen;
+
        //hide menu/toolbar in fullscreen (new state is !in_fullscreen)
        toolbar->setVisible(show_toolbar->isChecked() and in_fullscreen);
+
+#ifndef Q_WS_MAEMO_5
+       //menu bar is invisible by default on maemo
        menuBar()->setVisible(in_fullscreen);
+#endif
 
        setWindowState(windowState() ^ Qt::WindowFullScreen); 
-       forceResizeDelayed();
+       updateScreenSpaceDelayed();
 }
 
 void MainWindow::showKeyMenu()
 {
-       static KeyMenu *key_menu = new KeyMenu(this);
        key_menu->exec();
-
        vnc_view->sendKeySequence(key_menu->getKeySequence());
 }
 
@@ -313,8 +317,8 @@ void MainWindow::showPreferences()
 
 void MainWindow::reloadSettings()
 {
+#ifdef Q_WS_MAEMO_5
        QSettings settings;
-#ifdef Q_WS_HILDON
        int rotation = settings.value("screen_rotation", 0).toInt();
        setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
        setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
@@ -329,14 +333,59 @@ void MainWindow::resizeEvent(QResizeEvent *event)
 {
        QMainWindow::resizeEvent(event);
 
-       forceResize();
+       updateScreenSpace();
+       if(vnc_view)
+               vnc_view->setZoomLevel(zoom_slider->value());
+       
+#ifdef Q_WS_MAEMO_5
+       //hide zoom slider in portrait mode
+       zoom_slider->setVisible(height() < width());
+#endif
 }
 
 void MainWindow::showInputPanel()
 {
-#ifdef Q_WS_HILDON
+#ifdef Q_WS_MAEMO_5
+       //TODO: when hardware keyboard is open, this will only cause the IM to mess up 'real' key events
        vnc_view->setAttribute(Qt::WA_InputMethodEnabled, true);
+       vnc_view->setInputMethodHints(Qt::ImhNoAutoUppercase); //without this, IM starts with caps lock
+
        QEvent event(QEvent::RequestSoftwareInputPanel);
        QApplication::sendEvent(vnc_view, &event);
 #endif
 }
+
+void MainWindow::setZoomLevel(int level)
+{
+       if(!vnc_view)
+               return;
+       
+       const qreal old_factor = vnc_view->getZoomFactor();
+       QPoint center = vnc_view->visibleRegion().boundingRect().center();
+
+       vnc_view->setZoomLevel(level);
+
+       const qreal new_factor = vnc_view->getZoomFactor();
+
+       //scroll to center, if zoom level actually changed
+       if(old_factor != new_factor) {
+               center = center * (double(new_factor)/old_factor);
+               scroll_area->ensureVisible(center.x(), center.y(),
+                       vnc_view->visibleRegion().boundingRect().width()/2,
+                       vnc_view->visibleRegion().boundingRect().height()/2);
+               vnc_view->update();
+
+               scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*new_factor)));
+       }
+}
+
+void MainWindow::zoomSliderReleased()
+{
+       static QTime time;
+       if(!time.isNull() and time.elapsed() < 700) //double clicked
+               zoom_slider->setValue(95); //100%
+       else 
+               forceRepaint();
+       
+       time.restart();
+}