summaryrefslogtreecommitdiffstats
path: root/dragmove
diff options
context:
space:
mode:
authorDavid Boddie <dboddie@trolltech.com>2009-03-25 13:56:54 +0100
committerDavid Boddie <dboddie@trolltech.com>2009-03-25 13:56:54 +0100
commit58105320f1ea6e374653e8b3ee51fdd7f54ddbba (patch)
tree132c59c8935cd0a2fe7215240db44b09c9ba0f74 /dragmove
parentacbf0c709a98fe42dbde53c7d09859b08e2d88e9 (diff)
Added Python versions of the Google Suggest and Drag Charm examples.
Diffstat (limited to 'dragmove')
-rw-r--r--dragmove/dragmovecharm.py99
1 files changed, 99 insertions, 0 deletions
diff --git a/dragmove/dragmovecharm.py b/dragmove/dragmovecharm.py
new file mode 100644
index 0000000..81bfc49
--- /dev/null
+++ b/dragmove/dragmovecharm.py
@@ -0,0 +1,99 @@
+#############################################################################
+##
+## Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+## Contact: Qt Software Information (qt-info@nokia.com)
+##
+## This file is part of the Graphics Dojo project on Qt Labs.
+##
+## This file may be used under the terms of the GNU General Public
+## License version 2.0 or 3.0 as published by the Free Software Foundation
+## and appearing in the file LICENSE.GPL included in the packaging of
+## this file. Please review the following information to ensure GNU
+## General Public Licensing requirements will be met:
+## http://www.fsf.org/licensing/licenses/info/GPLv2.html and
+## http://www.gnu.org/copyleft/gpl.html.
+##
+## If you are unsure which license is appropriate for your use, please
+## contact the sales department at qt-sales@nokia.com.
+##
+## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+##
+#############################################################################
+
+from PyQt4.QtCore import QEvent, QObject, QPoint, Qt
+from PyQt4.QtGui import QMouseEvent, QWidget
+
+class DragMoveData:
+ def __init__(self):
+ self.isMoving = False
+ self.startDrag = QPoint()
+
+class DragMoveCharm(QObject):
+
+ def __init__(self, parent = None):
+
+ QObject.__init__(self, parent)
+ self.dragMoveData = {}
+
+ def activateOn(self, widget):
+
+ if widget in self.dragMoveData:
+ return
+
+ data = DragMoveData()
+ data.startDrag = QPoint(0, 0)
+ data.isMoving = False
+ self.dragMoveData[widget] = data
+
+ widget.installEventFilter(self)
+
+ def deactivateFrom(self, widget):
+
+ del self.dragMoveData[widget]
+ self.dragMoveData.remove(widget)
+ widget.removeEventFilter(self)
+
+ def eventFilter(self, object, event):
+
+ if not isinstance(object, QWidget):
+ return False
+
+ widget = object
+
+ type = event.type()
+ if type != QEvent.MouseButtonPress and \
+ type != QEvent.MouseButtonRelease and \
+ type != QEvent.MouseMove:
+ return False
+
+ if isinstance(event, QMouseEvent):
+ if event.modifiers() != Qt.NoModifier:
+ return False
+ button = event.button()
+ mouseEvent = event
+
+ try:
+ data = self.dragMoveData[widget]
+ except KeyError:
+ return False
+
+ consumed = False
+
+ if type == QEvent.MouseButtonPress and button == Qt.LeftButton:
+ data.startDrag = QPoint(mouseEvent.globalPos())
+ data.isMoving = True
+ event.accept()
+ consumed = True
+
+ if type == QEvent.MouseButtonRelease:
+ data.startDrag = QPoint(0, 0)
+ data.isMoving = False
+
+ if type == QEvent.MouseMove and data.isMoving:
+ pos = mouseEvent.globalPos()
+ widget.move(widget.pos() + pos - data.startDrag)
+ data.startDrag = QPoint(pos)
+ consumed = True
+
+ return consumed