[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    extragear/utils/kpager
From:       Pino Toscano <pino () kde ! org>
Date:       2015-11-17 22:50:42
Message-ID: E1Zyp5G-0002u3-Ne () scm ! kde ! org
[Download RAW message or body]

SVN commit 1444431 by pino:

PagerWindowDrag: switch to QDataStream

ease the encoding/decoding of the window data with QByteArray using
QDataStream, instead of manual printf/scanf; implement a small template
helper to deal with proper type of WId


 M  +31 -7     windowdrag.cpp  


--- trunk/extragear/utils/kpager/windowdrag.cpp #1444430:1444431
@@ -25,16 +25,40 @@
 *************************************************************************/
 
 #include <QMimeData>
+#include <QDataStream>
 
-#include <stdio.h>
-
 #include "windowdrag.h"
 
+/*
+ * Use template specialization to detect the equivalent Qt type
+ * to use in QDataStream, with no need to ad-hoc casts.
+ */
+template <typename T> struct TypeForWIdT;
+template <>           struct TypeForWIdT<unsigned long> { typedef quint32 Type; };
+template <>           struct TypeForWIdT<unsigned long long> { typedef quint64 Type; };
+typedef TypeForWIdT<WId>::Type TypeForWId;
+
+static QDataStream& operator<<(QDataStream& s, WId w)
+{
+    const TypeForWId t = w;
+    s << t;
+    return s;
+}
+
+static QDataStream& operator>>(QDataStream& s, WId &w)
+{
+    TypeForWId t;
+    s >> t;
+    w = t;
+    return s;
+}
+
+
 void PagerWindowDrag::populateMimeData(QMimeData *mimeData, WId w, int deltax, int deltay, int origdesk)
 {
-    QString tmp;
-    tmp.sprintf("%d %d %d %d", static_cast<int>(w), deltax, deltay, origdesk);
-    QByteArray data = tmp.toLatin1();
+    QByteArray data;
+    QDataStream stream(&data, QIODevice::WriteOnly);
+    stream << w << deltax << deltay << origdesk;
 
     mimeData->setData("application/x-kpager", data);
 }
@@ -49,8 +73,8 @@
     QByteArray data = mimeData->data("application/x-kpager");
     if (data.size())
 	{
-	    char *tmp=data.data();
-	    sscanf(tmp,"%lu %d %d %d", &w, &deltax, &deltay, &origdesk);
+	    QDataStream stream(&data, QIODevice::ReadOnly);
+	    stream >> w >> deltax >> deltay >> origdesk;
 	    return true;
 	}
     return false;
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic