fix compile warning
[presencevnc] / src / vncview.cpp
index 24119a2..8502748 100644 (file)
@@ -45,21 +45,21 @@ const int DOUBLE_TAP_UP_TIME = 500;
 
 
 VncView::VncView(QWidget *parent, const KUrl &url, RemoteView::Quality quality, int listen_port)
-        : RemoteView(parent),
-        m_initDone(false),
-        m_buttonMask(0),
-        cursor_x(0),
-        cursor_y(0),
-        m_quitFlag(false),
-        m_firstPasswordTry(true),
-        m_dontSendClipboard(false),
-        m_horizontalFactor(1.0),
-        m_verticalFactor(1.0),
-        m_forceLocalCursor(false),
-        quality(quality),
-        listen_port(listen_port),
-        transformation_mode(Qt::FastTransformation),
-        display_off(false)
+    : RemoteView(parent),
+      m_initDone(false),
+      m_buttonMask(0),
+      cursor_x(0),
+      cursor_y(0),
+      m_quitFlag(false),
+      m_firstPasswordTry(true),
+      m_dontSendClipboard(false),
+      m_horizontalFactor(1.0),
+      m_verticalFactor(1.0),
+      m_forceLocalCursor(false),
+      quality(quality),
+      listen_port(listen_port),
+      transformation_mode(Qt::FastTransformation),
+      display_off(false)
 {
     m_url = url;
     m_host = url.host();
@@ -104,13 +104,13 @@ bool VncView::eventFilter(QObject *obj, QEvent *event)
 
             event->ignore();
             return true;
-               }
+        }
     }
 
     return RemoteView::eventFilter(obj, event);
 }
 
-QSize VncView::framebufferSize()
+QSize VncView::framebufferSize() const
 {
     return m_frame.size();
 }
@@ -136,7 +136,7 @@ void VncView::startQuitting()
 
     m_quitFlag = true;
 
-       vncThread.stop();
+    vncThread.stop();
 
     const bool quitSuccess = vncThread.wait(700);
     if(!quitSuccess) {
@@ -150,7 +150,7 @@ void VncView::startQuitting()
     setStatus(Disconnected);
 }
 
-bool VncView::isQuitting()
+bool VncView::isQuitting() const
 {
     return m_quitFlag;
 }
@@ -159,7 +159,7 @@ bool VncView::start()
 {
     vncThread.setHost(m_host);
     vncThread.setPort(m_port);
-       vncThread.setListenPort(listen_port); //if port is != 0, thread will listen for connections
+    vncThread.setListenPort(listen_port); //if port is != 0, thread will listen for connections
     vncThread.setQuality(quality);
 
     // set local cursor on by default because low quality mostly means slow internet connection
@@ -254,19 +254,21 @@ void VncView::outputErrorMessage(const QString &message)
         return;
     }
 
-    startQuitting();
-
     emit errorMessage(i18n("VNC failure"), message);
+
+    startQuitting();
 }
 
 void VncView::updateImage(int x, int y, int w, int h)
 {
+#ifdef Q_WS_MAEMO_5
     //don't update if window is out of focus / display is off
     if(!QApplication::focusWidget() || display_off) {
         return;
     }
+#endif
 
-     //kDebug(5011) << "got update" << width() << height();
+    //kDebug(5011) << "got update" << width() << height();
 
     m_x = x;
     m_y = y;
@@ -287,8 +289,7 @@ void VncView::updateImage(int x, int y, int w, int h)
     m_frame = vncThread.image();
 
     if (!m_initDone) { //TODO this seems an odd place for initialization
-        setAttribute(Qt::WA_StaticContents);
-        setAttribute(Qt::WA_OpaquePaintEvent);
+        setAttribute(Qt::WA_NoSystemBackground);
         installEventFilter(this);
 
         setCursor(((m_dotCursorState == CursorOn) || m_forceLocalCursor) ? localDotCursor() : Qt::BlankCursor);
@@ -297,15 +298,15 @@ void VncView::updateImage(int x, int y, int w, int h)
         setFocusPolicy(Qt::WheelFocus);
         setStatus(Connected);
         emit connected();
-        
+
         resize(width(), height());
-        
+
         m_initDone = true;
     }
 
-       static QSize old_frame_size = QSize();
+    static QSize old_frame_size = QSize();
     if ((y == 0 && x == 0) && (m_frame.size() != old_frame_size)) {
-           old_frame_size = m_frame.size();
+        old_frame_size = m_frame.size();
         kDebug(5011) << "Updating framebuffer size";
         setZoomLevel();
         useFastTransformations(false);
@@ -313,7 +314,7 @@ void VncView::updateImage(int x, int y, int w, int h)
         emit framebufferSizeChanged(m_frame.width(), m_frame.height());
     }
 
-    repaint(qRound(m_x * m_horizontalFactor), qRound(m_y * m_verticalFactor), qRound(m_w * m_horizontalFactor), qRound(m_h * m_verticalFactor));
+    update(qRound(m_x * m_horizontalFactor), qRound(m_y * m_verticalFactor), qRound(m_w * m_horizontalFactor), qRound(m_h * m_verticalFactor));
 }
 
 void VncView::setViewOnly(bool viewOnly)
@@ -323,11 +324,11 @@ void VncView::setViewOnly(bool viewOnly)
     m_dontSendClipboard = viewOnly;
 
     if (viewOnly) {
-               showDotCursor(CursorOff);
+        showDotCursor(CursorOff);
         setCursor(Qt::ArrowCursor);
     } else {
         setCursor(m_dotCursorState == CursorOn ? localDotCursor() : Qt::BlankCursor);
-       }
+    }
 }
 
 void VncView::showDotCursor(DotCursorState state)
@@ -361,11 +362,11 @@ void VncView::setZoomLevel(int level)
         magnification = (level)/90.0*(1.0 - fit_screen_magnification) + fit_screen_magnification;
     }
 
-    if(magnification < 0                       //remote display smaller than local?
-            or magnification != magnification) //nan
+    //remote display smaller than local (or NAN)?
+    if(magnification < 0 or magnification != magnification)
         magnification = 1.0;
 
-    m_verticalFactor = m_horizontalFactor =    magnification;
+    m_verticalFactor = m_horizontalFactor = magnification;
     resize(m_frame.width()*magnification, m_frame.height()*magnification);
 }
 
@@ -387,23 +388,29 @@ void VncView::paintEvent(QPaintEvent *event)
 
     event->accept();
 
-    const QRect update_rect = event->rect();
+    //split update region into smaller non-intersecting rectangles and only paint those
     QPainter painter(this);
-    if (update_rect != rect()) {
-        // kDebug(5011) << "Partial repaint";
+    foreach(QRect update_rect, event->region().rects()) {
+        if(m_horizontalFactor == 2.0 and m_verticalFactor == 2.0) {
+            //grow client side updates to multiples of 2 to avoid artifacts
+            update_rect.adjust(
+                    -update_rect.x()%2,
+                    -update_rect.y()%2,
+                    0, 0);
+            update_rect.adjust(0, 0,
+                    (update_rect.x()+update_rect.width())%2,
+                    (update_rect.y()+update_rect.height())%2);
+        }
         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();
+        //kDebug(5011) << "Partial repaint, widget x,y,w,h:" << update_rect.x() << update_rect.y() << update_rect.width() << update_rect.height() << "orig: " << sx << sy << sw << sh;
 
-        painter.drawImage(rect(),
-                m_frame.scaled(size(), Qt::IgnoreAspectRatio, transformation_mode));
+        painter.drawImage(update_rect,
+                          m_frame.copy(sx, sy, sw, sh)
+                          .scaled(update_rect.size(), Qt::IgnoreAspectRatio, transformation_mode));
     }
 
     //draw local cursor ourselves, normal mouse pointer doesn't deal with scrolling
@@ -540,11 +547,12 @@ void VncView::mouseEventHandler(QMouseEvent *e)
     if(((m_dotCursorState == CursorOn) || m_forceLocalCursor)
             and (cursor_x != old_cursor_x or cursor_y != old_cursor_y)) {
         //clear last position
-        repaint(old_cursor_x*m_horizontalFactor - CURSOR_SIZE/2, old_cursor_y*m_verticalFactor - CURSOR_SIZE/2, CURSOR_SIZE, CURSOR_SIZE);
+        update(old_cursor_x*m_horizontalFactor - CURSOR_SIZE/2, old_cursor_y*m_verticalFactor - CURSOR_SIZE/2, CURSOR_SIZE, CURSOR_SIZE);
         //and refresh new one
-        repaint(cursor_x*m_horizontalFactor - CURSOR_SIZE/2, cursor_y*m_verticalFactor - CURSOR_SIZE/2, CURSOR_SIZE, CURSOR_SIZE);
+        update(cursor_x*m_horizontalFactor - CURSOR_SIZE/2, cursor_y*m_verticalFactor - CURSOR_SIZE/2, CURSOR_SIZE, CURSOR_SIZE);
 
-        old_cursor_x = cursor_x; old_cursor_y = cursor_y;
+        old_cursor_x = cursor_x;
+        old_cursor_y = cursor_y;
     }
 }
 
@@ -570,9 +578,10 @@ void VncView::keyEventHandler(QKeyEvent *e)
         return;
     }
 
-// parts of this code are based on http://italc.sourcearchive.com/documentation/1.0.9.1/vncview_8cpp-source.html
+    // parts of this code are based on http://italc.sourcearchive.com/documentation/1.0.9.1/vncview_8cpp-source.html
     rfbKeySym k = e->nativeVirtualKey();
 
+
     // we do not handle Key_Backtab separately as the Shift-modifier
     // is already enabled
     if (e->key() == Qt::Key_Backtab) {
@@ -585,9 +594,9 @@ void VncView::keyEventHandler(QKeyEvent *e)
     //don't send ISO_Level3_Shift (would break things like Win+0-9)
     //also enable IM so symbol key works
     if(k == 0xfe03) {
-           setAttribute(Qt::WA_InputMethodEnabled, pressed);
-           e->ignore();
-           return;
+        setAttribute(Qt::WA_InputMethodEnabled, pressed);
+        e->ignore();
+        return;
     }
 #endif
 
@@ -609,7 +618,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() << "', nativeVirtualKey: " << k;
         vncThread.keyEvent(k, pressed);
     } else {
         kDebug(5011) << "nativeVirtualKey() for '" << e->text() << "' failed.";
@@ -748,15 +757,12 @@ void VncView::sendKey(Qt::Key key)
         k = 0xff67;
         break;
     case Qt::Key_Meta:
-    case Qt::MetaModifier:
         k = XK_Super_L;
         break;
     case Qt::Key_Alt:
-    case Qt::AltModifier:
         k = XK_Alt_L;
         break;
     case Qt::Key_Control:
-    case Qt::ControlModifier:
         k = XK_Control_L;
         break;
     default:
@@ -783,7 +789,7 @@ void VncView::sendKeySequence(QKeySequence keys)
     Q_ASSERT(keys.count() <= 1); //we can only handle a single combination
 
     //to get at individual key presses, we split 'keys' into its components
-    QList<int> key_list;
+    QList<Qt::Key> key_list;
     int pos = 0;
     while(true) {
         QString k = keys.toString().section('+', pos, pos);
@@ -798,18 +804,18 @@ void VncView::sendKeySequence(QKeySequence keys)
         } else if(k == "Meta") {
             key_list.append(Qt::Key_Meta);
         } else {
-            key_list.append(QKeySequence(k)[0]);
+            key_list.append((Qt::Key)QKeySequence(k)[0]);
         }
 
         pos++;
     }
 
     for(int i = 0; i < key_list.count(); i++)
-        sendKey(Qt::Key(key_list.at(i)));
+        sendKey(key_list.at(i));
 
     //release modifiers (everything before final key)
     for(int i = key_list.count()-2; i >= 0; i--)
-        sendKey(Qt::Key(key_list.at(i)));
+        sendKey(key_list.at(i));
 }
 
 void VncView::reloadSettings()
@@ -820,8 +826,7 @@ void VncView::reloadSettings()
     disable_tapping = settings.value("disable_tapping", false).toBool();
 
     bool always_show_local_cursor = settings.value("always_show_local_cursor", false).toBool();
-    if(always_show_local_cursor)
-        showDotCursor(CursorOn);
+    showDotCursor(always_show_local_cursor?CursorOn:CursorOff);
 
     enableScaling(true);
 }