add --viewonly comand line option
[presencevnc] / src / mainwindow.cpp
index 555326a..d0d8787 100644 (file)
 #endif
 
 
-MainWindow::MainWindow(QString url, int quality):
+MainWindow::MainWindow(QString url, int quality, bool view_only):
        QMainWindow(0),
        vnc_view(0),
        scroll_area(new ScrollArea(0)),
+       input_toolbuttons(new QActionGroup(this)),
        key_menu(new KeyMenu(this))
 {
        setWindowTitle("Presence VNC");
-       setContextMenuPolicy(Qt::NoContextMenu);
 #ifdef Q_WS_MAEMO_5
+       setContextMenuPolicy(Qt::NoContextMenu);
        setAttribute(Qt::WA_Maemo5StackedWindow);
 #endif
 
@@ -49,14 +50,14 @@ MainWindow::MainWindow(QString url, int quality):
 
        //set up toolbar
        toolbar = new QToolBar(0);
-       toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu())); //"..." button
-       toolbar->addAction(tr("Tab"), this, SLOT(sendTab()));
-       toolbar->addAction(tr("Esc"), this, SLOT(sendEsc()));
-       toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp()));
-       toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn()));
+       input_toolbuttons->addAction(toolbar->addAction(QChar(0x2026), this, SLOT(showKeyMenu()))); //"..." button
+       input_toolbuttons->addAction(toolbar->addAction(tr("Tab"), this, SLOT(sendTab())));
+       input_toolbuttons->addAction(toolbar->addAction(tr("Esc"), this, SLOT(sendEsc())));
+       input_toolbuttons->addAction(toolbar->addAction(tr("PgUp"), this, SLOT(sendPgUp())));
+       input_toolbuttons->addAction(toolbar->addAction(tr("PgDn"), this, SLOT(sendPgDn())));
 #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()));
+       input_toolbuttons->addAction(toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/chat_enter.png"), "", this, SLOT(sendReturn())));
+       input_toolbuttons->addAction(toolbar->addAction(QIcon("/usr/share/icons/hicolor/48x48/hildon/control_keyboard.png"), "", this, SLOT(showInputPanel())));
 #endif
 
        QSettings settings;
@@ -65,7 +66,7 @@ MainWindow::MainWindow(QString url, int quality):
        connect(zoom_slider, SIGNAL(valueChanged(int)),
                this, SLOT(setZoomLevel(int)));
        connect(zoom_slider, SIGNAL(sliderReleased()),
-               this, SLOT(forceRepaint()));
+               this, SLOT(zoomSliderReleased()));
        zoom_slider->setValue(settings.value("zoomlevel", 95).toInt());
        toolbar->addWidget(zoom_slider);
 
@@ -119,12 +120,15 @@ MainWindow::MainWindow(QString url, int quality):
                this, SLOT(updateScreenSpaceDelayed()));
 
        setCentralWidget(scroll_area);
-       new FullScreenExitButton(this);
+
+       FullScreenExitButton* fullscreen_exit_button = new FullScreenExitButton(this);
+       connect(fullscreen_exit_button, SIGNAL(clicked()),
+               this, SLOT(toggleFullscreen()));
 
        grabZoomKeys(true);
        reloadSettings();
 
-       if(url.isNull()) {
+       if(url.isEmpty()) {
                disconnect_action->setEnabled(false);
                showConnectDialog();
        } else {
@@ -132,6 +136,7 @@ MainWindow::MainWindow(QString url, int quality):
                connect(vnc_view, SIGNAL(statusChanged(RemoteView::RemoteStatus)),
                        this, SLOT(statusChanged(RemoteView::RemoteStatus)));
                scroll_area->setWidget(vnc_view);
+               vnc_view->setViewOnly(view_only);
                vnc_view->start();
        }
 }
@@ -165,7 +170,7 @@ void MainWindow::closeEvent(QCloseEvent*) {
 
 void MainWindow::about() {
        QMessageBox::about(this, tr("About Presence VNC"),
-               tr("<center><h1>Presence VNC 0.6</h1>\
+               tr("<center><h1>Presence VNC 0.8</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;<br />\
@@ -229,8 +234,12 @@ void MainWindow::statusChanged(RemoteView::RemoteStatus status)
 #endif
                toolbar->setEnabled(true);
 
+               //disable key input buttons in view only mode
+               input_toolbuttons->setEnabled(!vnc_view->viewOnly());
+
                vnc_view->setZoomLevel(zoom_slider->value());
-               vnc_view->forceFullRepaint();
+               vnc_view->useFastTransformations(false);
+               vnc_view->repaint();
                break;
        case RemoteView::Disconnecting:
                if(old_status == RemoteView::Disconnected) //Disconnecting also occurs while connecting, so check last state
@@ -264,12 +273,6 @@ void MainWindow::statusChanged(RemoteView::RemoteStatus status)
        old_status = status;
 }
 
-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::updateScreenSpace()
@@ -317,8 +320,10 @@ void MainWindow::showPreferences()
 
 void MainWindow::reloadSettings()
 {
-#ifdef Q_WS_MAEMO_5
        QSettings settings;
+       zoom_to_cursor = settings.value("zoom_to_cursor", true).toBool();
+       
+#ifdef Q_WS_MAEMO_5
        int rotation = settings.value("screen_rotation", 0).toInt();
        setAttribute(Qt::WA_Maemo5AutoOrientation, rotation == 0);
        setAttribute(Qt::WA_Maemo5LandscapeOrientation, rotation == 1);
@@ -360,22 +365,45 @@ void MainWindow::setZoomLevel(int level)
        if(!vnc_view)
                return;
        
-       //TODO: use getZoomFactor() instead
-       int old_width = vnc_view->width();
+       const qreal old_factor = vnc_view->zoomFactor();
        QPoint center = vnc_view->visibleRegion().boundingRect().center();
 
        vnc_view->setZoomLevel(level);
 
-       int new_width = vnc_view->width();
+       const qreal new_factor = vnc_view->zoomFactor();
 
        //scroll to center, if zoom level actually changed
-       if(new_width != old_width) {
-               center = center * (double(new_width)/old_width);
+       if(old_factor != new_factor) {
+               if(zoom_to_cursor)
+                       center = new_factor * vnc_view->cursorPosition();
+               else //zoom to center of visible region
+                       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->useFastTransformations(zoom_slider->isSliderDown());
                vnc_view->update();
 
-               scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*vnc_view->getZoomFactor())));
+               scroll_area->showMessage(tr("Zoom: %1\%").arg(qRound(100*new_factor)));
        }
 }
+
+void MainWindow::zoomSliderReleased()
+{
+       static QTime time;
+       if(!time.isNull() and time.elapsed() < 500) //double clicked
+               zoom_slider->setValue(95); //100%
+       
+       time.restart();
+
+       //stopped zooming, reenable high quality
+       vnc_view->useFastTransformations(false);
+}
+
+void MainWindow::sendTab() { vnc_view->sendKey(Qt::Key_Tab); }
+void MainWindow::sendEsc() { vnc_view->sendKey(Qt::Key_Escape); }
+void MainWindow::sendPgUp() { vnc_view->sendKey(Qt::Key_PageUp); }
+void MainWindow::sendPgDn() { vnc_view->sendKey(Qt::Key_PageDown); }
+void MainWindow::sendReturn() { vnc_view->sendKey(Qt::Key_Return); }