cleanup
[presencevnc] / src / vncview.cpp
index 3237a03..aa021c7 100644 (file)
 
 #include "vncview.h"
 
-#include <QMessageBox>
-#include <QInputDialog>
-#define KMessageBox QMessageBox
-#define error(parent, message, caption) \
-critical(parent, caption, message)
-
-#include <QApplication>
-#include <QBitmap>
-#include <QCheckBox>
-#include <QDialog>
-#include <QImage>
-#include <QHBoxLayout>
-#include <QVBoxLayout>
-#include <QPainter>
-#include <QMouseEvent>
-#include <QPushButton>
-#include <QEvent>
-#include <QSettings>
-#include <QTime>
-#include <QTimer>
+#include <QtGui>
 
 
 // Definition of key modifier mask constants
@@ -55,6 +36,7 @@ critical(parent, caption, message)
 //local cursor width/height in px, should be an odd number
 const int CURSOR_SIZE = 7;
 
+//in miliseconds
 const int TAP_PRESS_TIME = 180;
 const int DOUBLE_TAP_UP_TIME = 500;
 
@@ -65,16 +47,15 @@ VncView::VncView(QWidget *parent, const KUrl &url, RemoteView::Quality quality,
         m_buttonMask(0),
        cursor_x(0),
        cursor_y(0),
-        m_repaint(false),
         m_quitFlag(false),
         m_firstPasswordTry(true),
         m_dontSendClipboard(false),
         m_horizontalFactor(1.0),
         m_verticalFactor(1.0),
         m_forceLocalCursor(false),
-       force_full_repaint(false),
        quality(quality),
-       listen_port(listen_port)
+       listen_port(listen_port),
+       transformation_mode(Qt::FastTransformation)
 {
     m_url = url;
     m_host = url.host();
@@ -106,12 +87,6 @@ VncView::~VncView()
     startQuitting();
 }
 
-void VncView::forceFullRepaint()
-{
-       force_full_repaint = true;
-       repaint();
-}
-
 bool VncView::eventFilter(QObject *obj, QEvent *event)
 {
     if (m_viewOnly) {
@@ -121,9 +96,13 @@ bool VncView::eventFilter(QObject *obj, QEvent *event)
                 event->type() == QEvent::MouseButtonPress ||
                 event->type() == QEvent::MouseButtonRelease ||
                 event->type() == QEvent::Wheel ||
-                event->type() == QEvent::MouseMove)
+                event->type() == QEvent::MouseMove) {
+
+                       event->ignore();
             return true;
+               }
     }
+
     return RemoteView::eventFilter(obj, event);
 }
 
@@ -149,13 +128,10 @@ void VncView::startQuitting()
 
     kDebug(5011) << "about to quit";
 
-    //const bool connected = status() == RemoteView::Connected;
-
     setStatus(Disconnecting);
 
     m_quitFlag = true;
 
-       //if(connected) //remove if things work without it
        vncThread.stop();
 
     const bool quitSuccess = vncThread.wait(700);
@@ -216,8 +192,9 @@ void VncView::requestPassword()
 
        QSettings settings;
        settings.beginGroup("hosts");
-       QString password = settings.value(QString("%1/password").arg(m_host), "").toString();
+
        //check for saved password
+       QString password = settings.value(QString("%1/password").arg(m_host), "").toString();
        if(m_firstPasswordTry and !password.isEmpty()) {
                kDebug(5011) << "Trying saved password";
                m_firstPasswordTry = false;
@@ -243,7 +220,7 @@ void VncView::requestPassword()
        QHBoxLayout layout1;
        QVBoxLayout layout2;
        layout2.addWidget(&passwordbox);
-       if(!m_host.isEmpty()) //don't save incomming connections
+       if(!m_host.isEmpty()) //don't save incoming connections
                layout2.addWidget(&save_password);
        layout1.addLayout(&layout2);
        layout1.addWidget(&ok_button);
@@ -314,7 +291,6 @@ void VncView::updateImage(int x, int y, int w, int h)
         setMouseTracking(true); // get mouse events even when there is no mousebutton pressed
         setFocusPolicy(Qt::WheelFocus);
         setStatus(Connected);
-//         emit framebufferSizeChanged(m_frame.width(), m_frame.height());
         emit connected();
         
                resize(width(), height());
@@ -328,12 +304,12 @@ void VncView::updateImage(int x, int y, int w, int h)
            old_frame_size = m_frame.size();
         kDebug(5011) << "Updating framebuffer size";
                setZoomLevel();
+               useFastTransformations(false);
+
         emit framebufferSizeChanged(m_frame.width(), m_frame.height());
     }
 
-    m_repaint = true;
     repaint(qRound(m_x * m_horizontalFactor), qRound(m_y * m_verticalFactor), qRound(m_w * m_horizontalFactor), qRound(m_h * m_verticalFactor));
-    m_repaint = false;
 }
 
 void VncView::setViewOnly(bool viewOnly)
@@ -397,48 +373,31 @@ void VncView::setCut(const QString &text)
 
 void VncView::paintEvent(QPaintEvent *event)
 {
-     //kDebug(5011) << "paint event: x: " << m_x << ", y: " << m_y << ", w: " << m_w << ", h: " << m_h;
     if (m_frame.isNull() || m_frame.format() == QImage::Format_Invalid) {
-        kDebug(5011) << "no valid image to paint";
+        //no valid image to paint
         RemoteView::paintEvent(event);
         return;
     }
 
     event->accept();
 
+       const QRect update_rect = event->rect();
     QPainter painter(this);
+       if (update_rect != rect()) {
+               // kDebug(5011) << "Partial repaint";
+               const int sx = qRound(update_rect.x()/m_horizontalFactor);
+               const int sy = qRound(update_rect.y()/m_verticalFactor);
+               const int sw = qRound(update_rect.width()/m_horizontalFactor);
+               const int sh = qRound(update_rect.height()/m_verticalFactor);
+
+               painter.drawImage(update_rect, 
+                         m_frame.copy(sx, sy, sw, sh)
+                         .scaled(update_rect.size(), Qt::IgnoreAspectRatio, transformation_mode));
+       } else {
+               //kDebug(5011) << "Full repaint" << width() << height() << m_frame.width() << m_frame.height();
 
-       Qt::TransformationMode transformation_mode = Qt::SmoothTransformation;
-       if( m_horizontalFactor >= 1.0 )
-               transformation_mode = Qt::FastTransformation;
-
-       //TODO: seems to have no purpose, remove
-    if (false and m_repaint and !force_full_repaint) {
-//         kDebug(5011) << "normal repaint";
-        painter.drawImage(QRect(qRound(m_x*m_horizontalFactor), qRound(m_y*m_verticalFactor),
-                                qRound(m_w*m_horizontalFactor), qRound(m_h*m_verticalFactor)), 
-                          m_frame.copy(m_x, m_y, m_w, m_h).scaled(qRound(m_w*m_horizontalFactor), 
-                                                                  qRound(m_h*m_verticalFactor),
-                                                                  Qt::IgnoreAspectRatio, transformation_mode));
-    } else {
-         //kDebug(5011) << "resize repaint";
-        const QRect rect = event->rect();
-        if (!force_full_repaint and (rect.width() != width() || rect.height() != height())) {
-          //   kDebug(5011) << "Partial repaint";
-            const int sx = rect.x()/m_horizontalFactor;
-            const int sy = rect.y()/m_verticalFactor;
-            const int sw = rect.width()/m_horizontalFactor;
-            const int sh = rect.height()/m_verticalFactor;
-            painter.drawImage(rect, 
-                              m_frame.copy(sx, sy, sw, sh).scaled(rect.width(), rect.height(),
-                                                                  Qt::IgnoreAspectRatio, transformation_mode));
-        } else {
-                       kDebug(5011) << "Full repaint" << width() << height() << m_frame.width() << m_frame.height();
-            painter.drawImage(QRect(0, 0, width(), height()), 
-                              m_frame.scaled(m_frame.width() * m_horizontalFactor, m_frame.height() * m_verticalFactor,
-                                             Qt::IgnoreAspectRatio, transformation_mode));
-                       force_full_repaint = false;
-        }
+               painter.drawImage(rect(),
+                       m_frame.scaled(size(), Qt::IgnoreAspectRatio, transformation_mode));
     }
 
        //draw local cursor ourselves, normal mouse pointer doesn't deal with scrolling
@@ -461,32 +420,29 @@ void VncView::resizeEvent(QResizeEvent *event)
 
 bool VncView::event(QEvent *event)
 {
-    switch (event->type()) {
-    case QEvent::KeyPress:
-    case QEvent::KeyRelease:
-//         kDebug(5011) << "keyEvent";
-        keyEventHandler(static_cast<QKeyEvent*>(event));
-        return true;
-        break;
-    case QEvent::MouseButtonDblClick:
-    case QEvent::MouseButtonPress:
-    case QEvent::MouseButtonRelease:
-    case QEvent::MouseMove:
-//         kDebug(5011) << "mouseEvent";
-        mouseEventHandler(static_cast<QMouseEvent*>(event));
-        return true;
-        break;
-    case QEvent::Wheel:
-//         kDebug(5011) << "wheelEvent";
-        wheelEventHandler(static_cast<QWheelEvent*>(event));
-        return true;
-        break;
-    case QEvent::WindowActivate: //input panel may have been closed, prevent IM from interfering with hardware keyboard
-       setAttribute(Qt::WA_InputMethodEnabled, false);
-       //fall through
-    default:
-        return RemoteView::event(event);
-    }
+       switch (event->type()) {
+       case QEvent::KeyPress:
+       case QEvent::KeyRelease:
+               keyEventHandler(static_cast<QKeyEvent*>(event));
+               return true;
+
+       case QEvent::MouseButtonDblClick:
+       case QEvent::MouseButtonPress:
+       case QEvent::MouseButtonRelease:
+       case QEvent::MouseMove:
+               mouseEventHandler(static_cast<QMouseEvent*>(event));
+               return true;
+
+       case QEvent::Wheel:
+               wheelEventHandler(static_cast<QWheelEvent*>(event));
+               return true;
+
+       case QEvent::WindowActivate: //input panel may have been closed, prevent IM from interfering with hardware keyboard
+               setAttribute(Qt::WA_InputMethodEnabled, false);
+               //fall through
+       default:
+               return RemoteView::event(event);
+       }
 }
 
 //call with e == 0 to flush held events
@@ -646,7 +602,7 @@ void VncView::keyEventHandler(QKeyEvent *e)
        else if(e->key() == Qt::Key_F7)
                current_zoom = right_zoom;
        else if (k) {
-       //      kDebug(5011) << "got '" << e->text() << "'.";
+               // kDebug(5011) << "got '" << e->text() << "'.";
                vncThread.keyEvent(k, pressed);
        } else {
                kDebug(5011) << "nativeVirtualKey() for '" << e->text() << "' failed.";
@@ -874,7 +830,7 @@ void VncView::inputMethodEvent(QInputMethodEvent *event)
        //kDebug(5011) << event->commitString() << "|" << event->preeditString() << "|" << event->replacementLength() << "|" << event->replacementStart();
        QString letters = event->commitString();
        for(int i = 0; i < letters.length(); i++) {
-               char k = letters.at(i).toLatin1(); //works with all 'normal' keys, not umlauts.
+               char k = letters.at(i).toLatin1();
                if(!k) {
                        kDebug(5011) << "unhandled key";
                        continue;
@@ -884,5 +840,12 @@ void VncView::inputMethodEvent(QInputMethodEvent *event)
        }
 }
 
-
-#include "moc_vncview.cpp"
+void VncView::useFastTransformations(bool enabled)
+{
+       if(enabled or zoomFactor() >= 1.0) {
+               transformation_mode = Qt::FastTransformation;
+       } else {
+               transformation_mode = Qt::SmoothTransformation;
+               update();
+       }
+}