Created
June 23, 2014 17:13
-
-
Save soardex/7e3298f1ea781f184836 to your computer and use it in GitHub Desktop.
QT 4 Python simple sample that maps coordinates to json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import random | |
import json | |
import uuid | |
from PyQt4 import Qt, QtGui, QtCore | |
from spritemapper_ui import Ui_Dialog | |
DEBUG = False | |
TEMPLATE_SPRITE = u""" | |
{ | |
"metadata": { | |
"type": "sprite" | |
}, | |
"images": [ | |
%(images)s | |
], | |
"sprites": [ | |
%(sprites)s | |
] | |
} | |
""" | |
TEMPLATE_SUB_SPRITE = u""" | |
{ | |
"uuid": "%(uuid)s", | |
"name": "%(name)s", | |
"dimension": [%(dimension)s], | |
"upper-left": [%(ul)s], | |
"lower-right": [%(lr)s], | |
"image": [ | |
"%(imageid)s" | |
] | |
} | |
""" | |
TEMPLATE_SUB_IMAGE = u""" | |
{ | |
"uuid": "%s", | |
"url": "%s" | |
} | |
""" | |
class SpriteMapperDialog(QtGui.QDialog): | |
def __init__(self): | |
QtGui.QDialog.__init__(self) | |
self.initSets() | |
def initSets(self): | |
self.ui = Ui_Dialog() | |
self.ui.setupUi(self) | |
self.ui.btnExit.clicked.connect(self.handleBtnExit) | |
self.ui.btnLoadImage.clicked.connect(self.handleBtnLoadImage) | |
self.ui.btnMap.clicked.connect(self.handleBtnMap) | |
self.ui.btnUL.clicked.connect(self.handleBtnUpper) | |
self.ui.btnLR.clicked.connect(self.handleBtnLower) | |
self.scene = QtGui.QGraphicsScene() | |
self.ui.viewImage.setScene(self.scene) | |
self.ui.viewImage.viewport().installEventFilter(self) | |
self.ui.viewImage.setMouseTracking(True) | |
self.targetViewportPos = QtCore.QPointF() | |
self.targetScenePos = QtCore.QPointF() | |
self.zoomFactor = 1.0015 | |
self.upperPointRecord = False | |
self.lowerPointRecord = False | |
self.upperPointSet = False | |
self.lowerPointSet = False | |
## NOTE: create dictionary | |
self.records = {} | |
self.records['width'] = -1 | |
self.records['height'] = -1 | |
engineId = str(uuid.uuid4()) | |
print 'Generated Engine Id: %s' % engineId.upper() | |
def eventFilter(self, object, event): | |
if object == self.ui.viewImage.viewport() and \ | |
event.type() == QtCore.QEvent.MouseButtonPress: | |
position = event.pos() | |
try: | |
if self.upperPointRecord: | |
a = [position.x(), position.y()] | |
self.records['upper'] = a | |
self.upperPointSet = True | |
self.upperPointRecord = False | |
print ('LOG: upper point has been set') | |
elif self.lowerPointRecord: | |
a = [position.x(), position.y()] | |
self.records['lower'] = a | |
self.lowerPointSet = True | |
self.lowerPointRecord = False | |
print ('LOG: lower point has been set') | |
else: | |
pass | |
except: | |
print ('ERROR: no vertex coordinate has been recorded') | |
return True | |
elif object == self.ui.viewImage.viewport() and \ | |
event.type() == QtCore.QEvent.MouseMove: | |
mouse = event | |
delta = self.targetViewportPos - mouse.pos() | |
if abs(delta.x()) > 5 or abs(delta.y()) > 5: | |
self.targetViewportPos = mouse.pos() | |
self.targetScenePos = self.ui.viewImage.mapToScene(mouse.pos()) | |
return True | |
elif object == self.ui.viewImage.viewport() and \ | |
event.type() == QtCore.QEvent.Wheel: | |
if event.orientation() == 0x02: | |
angle = event.delta(); | |
factor = pow(self.zoomFactor, angle) | |
self.smoothZoom(factor) | |
return True | |
if DEBUG: | |
try: | |
position = event.pos() | |
print ('mouse_pos', position) | |
except: | |
print ('ERROR: mouse is out of bounds') | |
return False | |
def smoothZoom(self, factor): | |
self.ui.viewImage.scale(factor, factor) | |
self.ui.viewImage.centerOn(self.targetScenePos) | |
deltaViewportPos = self.targetViewportPos - QtCore.QPointF( | |
self.ui.viewImage.viewport().width() / 2.0, | |
self.ui.viewImage.viewport().height() / 2.0) | |
viewportCenter = self.ui.viewImage.mapFromScene(self.targetScenePos) - deltaViewportPos | |
self.ui.viewImage.centerOn(self.ui.viewImage.mapToScene(viewportCenter.toPoint())) | |
def handleBtnExit(self): | |
print ('Terminating Sprite Mapper...') | |
QtCore.QCoreApplication.instance().quit() | |
def handleBtnUpper(self): | |
if not self.upperPointRecord: | |
self.upperPointRecord = True | |
self.lowerPointRecord = False | |
def handleBtnLower(self): | |
if not self.lowerPointRecord: | |
self.lowerPointRecord = True | |
self.upperPointRecord = False | |
def handleBtnLoadImage(self): | |
url = QtGui.QFileDialog.getOpenFileName(self, 'Open Image...', '') | |
try: | |
image = QtGui.QPixmap(url) | |
self.records['url'] = str(url) | |
self.records['width'] = image.width() | |
self.records['height'] = image.height() | |
self.scene.addPixmap(image) | |
self.scene.update() | |
except: | |
print ('ERROR: error parsing file') | |
def handleBtnMap(self): | |
if self.upperPointSet and self.lowerPointSet: | |
dimension = str(self.records['width']) + ',' + str(self.records['height']) | |
upper = ','.join(str(i) for i in self.records['upper']) | |
lower = ','.join(str(i) for i in self.records['lower']) | |
sprid = str(uuid.uuid4()).upper() | |
imgid = str(uuid.uuid4()).upper() | |
images = TEMPLATE_SUB_IMAGE % (imgid, str(self.records['url'])) | |
sprites = TEMPLATE_SUB_SPRITE % { | |
"uuid": sprid, | |
"name": "regularSprite", | |
"dimension": dimension, | |
"ul": upper, | |
"lr": lower, | |
"imageid": imgid | |
} | |
data = TEMPLATE_SPRITE % { | |
"images": images, | |
"sprites": sprites | |
} | |
print data | |
self.upperPointSet = False | |
self.lowerPointSet = False | |
def main(): | |
app = QtGui.QApplication(sys.argv) | |
win = SpriteMapperDialog() | |
win.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() | |
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>Dialog</class> | |
<widget class="QDialog" name="Dialog"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>640</width> | |
<height>480</height> | |
</rect> | |
</property> | |
<property name="minimumSize"> | |
<size> | |
<width>640</width> | |
<height>480</height> | |
</size> | |
</property> | |
<property name="maximumSize"> | |
<size> | |
<width>640</width> | |
<height>480</height> | |
</size> | |
</property> | |
<property name="windowTitle"> | |
<string>Sprite Mapper</string> | |
</property> | |
<widget class="QGraphicsView" name="viewImage"> | |
<property name="geometry"> | |
<rect> | |
<x>10</x> | |
<y>10</y> | |
<width>621</width> | |
<height>431</height> | |
</rect> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="btnMap"> | |
<property name="geometry"> | |
<rect> | |
<x>10</x> | |
<y>450</y> | |
<width>61</width> | |
<height>23</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Map</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="btnUL"> | |
<property name="geometry"> | |
<rect> | |
<x>90</x> | |
<y>450</y> | |
<width>131</width> | |
<height>23</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Upper Left Point</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="btnLR"> | |
<property name="geometry"> | |
<rect> | |
<x>220</x> | |
<y>450</y> | |
<width>151</width> | |
<height>23</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Lower Left Point</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="btnLoadImage"> | |
<property name="geometry"> | |
<rect> | |
<x>390</x> | |
<y>450</y> | |
<width>141</width> | |
<height>23</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Load Image</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="btnExit"> | |
<property name="geometry"> | |
<rect> | |
<x>540</x> | |
<y>450</y> | |
<width>91</width> | |
<height>23</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Exit</string> | |
</property> | |
</widget> | |
</widget> | |
<resources/> | |
<connections/> | |
</ui> | |
# -*- coding: utf-8 -*- | |
# Form implementation generated from reading ui file 'spritemapper.ui' | |
# | |
# Created: Mon Jun 02 20:21:45 2014 | |
# by: PyQt4 UI code generator 4.11 | |
# | |
# WARNING! All changes made in this file will be lost! | |
from PyQt4 import QtCore, QtGui | |
try: | |
_fromUtf8 = QtCore.QString.fromUtf8 | |
except AttributeError: | |
def _fromUtf8(s): | |
return s | |
try: | |
_encoding = QtGui.QApplication.UnicodeUTF8 | |
def _translate(context, text, disambig): | |
return QtGui.QApplication.translate(context, text, disambig, _encoding) | |
except AttributeError: | |
def _translate(context, text, disambig): | |
return QtGui.QApplication.translate(context, text, disambig) | |
class Ui_Dialog(object): | |
def setupUi(self, Dialog): | |
Dialog.setObjectName(_fromUtf8("Dialog")) | |
Dialog.resize(640, 480) | |
Dialog.setMinimumSize(QtCore.QSize(640, 480)) | |
Dialog.setMaximumSize(QtCore.QSize(640, 480)) | |
self.viewImage = QtGui.QGraphicsView(Dialog) | |
self.viewImage.setGeometry(QtCore.QRect(10, 10, 621, 431)) | |
self.viewImage.setObjectName(_fromUtf8("viewImage")) | |
self.btnMap = QtGui.QPushButton(Dialog) | |
self.btnMap.setGeometry(QtCore.QRect(10, 450, 61, 23)) | |
self.btnMap.setObjectName(_fromUtf8("btnMap")) | |
self.btnUL = QtGui.QPushButton(Dialog) | |
self.btnUL.setGeometry(QtCore.QRect(90, 450, 131, 23)) | |
self.btnUL.setObjectName(_fromUtf8("btnUL")) | |
self.btnLR = QtGui.QPushButton(Dialog) | |
self.btnLR.setGeometry(QtCore.QRect(220, 450, 151, 23)) | |
self.btnLR.setObjectName(_fromUtf8("btnLR")) | |
self.btnLoadImage = QtGui.QPushButton(Dialog) | |
self.btnLoadImage.setGeometry(QtCore.QRect(390, 450, 141, 23)) | |
self.btnLoadImage.setObjectName(_fromUtf8("btnLoadImage")) | |
self.btnExit = QtGui.QPushButton(Dialog) | |
self.btnExit.setGeometry(QtCore.QRect(540, 450, 91, 23)) | |
self.btnExit.setObjectName(_fromUtf8("btnExit")) | |
self.retranslateUi(Dialog) | |
QtCore.QMetaObject.connectSlotsByName(Dialog) | |
def retranslateUi(self, Dialog): | |
Dialog.setWindowTitle(_translate("Dialog", "Sprite Mapper", None)) | |
self.btnMap.setText(_translate("Dialog", "Map", None)) | |
self.btnUL.setText(_translate("Dialog", "Upper Left Point", None)) | |
self.btnLR.setText(_translate("Dialog", "Lower Left Point", None)) | |
self.btnLoadImage.setText(_translate("Dialog", "Load Image", None)) | |
self.btnExit.setText(_translate("Dialog", "Exit", None)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment