class HeroShipView: """ Represents the Graphical view of the Hero's ship """ def __init__(self, ship_model): """ Initialize the ship view """ self.ship_model = ship_model self.scaled_ship = None def loadShipImage(self, width, height): """ Load the ship image """ self.unscaled_ship = QImage("hero_ship.png") matrix = QMatrix() matrix = matrix.rotate(180) self.unscaled_ship = self.unscaled_ship.transformed(matrix) xScaledSize = width*self.ship_model.rectangle.width/100 yScaledSize = height*self.ship_model.rectangle.height/100 self.scaled_ship = self.unscaled_ship.scaled(xScaledSize, yScaledSize) def draw(self, painter, window): """ Draw the image """ if self.scaled_ship is None: self.loadShipImage(window.contentsRect().width(), window.contentsRect().height()) painter.drawImage(self.ship_model.rectangle.x*window.contentsRect().width()/100, self.ship_model.rectangle.y*window.contentsRect().height()/100, self.scaled_ship)
def rotateImage(self,filepath): print "ROTATING FILE: " + filepath img = QImage(filepath) rot = QTransform() rot = rot.rotate(90) img = img.transformed(rot) img.save(filepath) self.imageRotated.emit(filepath)
def sendLocation(self, jid, latitude, longitude, rotate): latitude = latitude[:10] longitude = longitude[:10] self._d("Capturing preview...") QPixmap.grabWindow(QApplication.desktop().winId()).save(WAConstants.CACHE_PATH+"/tempimg.png", "PNG") img = QImage(WAConstants.CACHE_PATH+"/tempimg.png") if rotate == "true": rot = QTransform() rot = rot.rotate(90) img = img.transformed(rot) if img.height() > img.width(): result = img.scaledToWidth(320,Qt.SmoothTransformation); result = result.copy(result.width()/2-50,result.height()/2-50,100,100); elif img.height() < img.width(): result = img.scaledToHeight(320,Qt.SmoothTransformation); result = result.copy(result.width()/2-50,result.height()/2-50,100,100); #result = img.scaled(96, 96, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation); result.save( WAConstants.CACHE_PATH+"/tempimg2.jpg", "JPG" ); f = open(WAConstants.CACHE_PATH+"/tempimg2.jpg", 'r') stream = base64.b64encode(f.read()) f.close() os.remove(WAConstants.CACHE_PATH+"/tempimg.png") os.remove(WAConstants.CACHE_PATH+"/tempimg2.jpg") fmsg = WAXMPP.message_store.createMessage(jid); mediaItem = WAXMPP.message_store.store.Media.create() mediaItem.mediatype_id = WAConstants.MEDIA_TYPE_LOCATION mediaItem.remote_url = None mediaItem.preview = stream mediaItem.local_path ="%s,%s"%(latitude,longitude) mediaItem.transfer_status = 2 fmsg.content = QtCore.QCoreApplication.translate("WAEventHandler", "Location") fmsg.Media = mediaItem if fmsg.Conversation.type == "group": contact = WAXMPP.message_store.store.Contact.getOrCreateContactByJid(self.conn.jid) fmsg.setContact(contact); fmsg.setData({"status":0,"content":fmsg.content,"type":1}) WAXMPP.message_store.pushMessage(jid,fmsg) resultId = self.interfaceHandler.call("message_locationSend", (jid, latitude, longitude, stream)) k = Key(jid, True, resultId) fmsg.key = k.toString() fmsg.save()
def firstPage(self): for i in range( self.mainLayout.count() ): item = self.mainLayout.itemAt(0) item.widget().setParent( None ) title = QLabel( "<p style='color:rgb( 137,129,120 )'>Welcome to the PingoTools Installer</p>" ) title.setFixedHeight( 50 ) titleFont = QFont() titleFont.setPixelSize( 18 ) titleFont.setBold( True ) titleFont.setFamily( "Helvetica [Cronyx]" ) title.setAlignment( QtCore.Qt.AlignCenter ) title.setFont( titleFont ) description = QLabel() description.setAlignment( QtCore.Qt.AlignCenter ) buttonsWidget = QWidget(); buttonsWidget.setMaximumHeight( 50 ) buttonsLayout = QHBoxLayout( buttonsWidget ) emptyArea = QLabel() buttonNext = QPushButton( 'Next > ' ) buttonCancel = QPushButton( 'Cancel' ) buttonsLayout.addWidget( emptyArea ) buttonsLayout.addWidget( buttonNext ); buttonNext.setFixedWidth( 100 ) buttonsLayout.addWidget( buttonCancel ); buttonCancel.setFixedWidth( 100 ) self.mainLayout.addWidget( title ) self.mainLayout.addWidget( description ) self.mainLayout.addWidget( buttonsWidget ) origWidth = 500 frontImage = QImage() frontImage.load( os.path.dirname( __file__ ) + '/images/pingoTools_main.jpg' ) trValue = QTransform().scale( float(origWidth)/frontImage.width(), float(origWidth)/frontImage.width() ) transformedImage = frontImage.transformed( trValue ) pixmap = QPixmap.fromImage( transformedImage ) description.setPixmap( pixmap ) description.setGeometry( 0,0, transformedImage.width() , transformedImage.height() ) description.paintEvent(QPaintEvent(QtCore.QRect( 0,0,self.width(), self.height() ))) QtCore.QObject.connect( buttonNext, QtCore.SIGNAL( 'clicked()' ), self.secondPage ) QtCore.QObject.connect( buttonCancel, QtCore.SIGNAL( 'clicked()' ), self.cmd_cancel )
def _getPictureForSending(self, jid, filepath): print "Preparing picture " + filepath + " for " + jid image = filepath.replace("file://","") rotation = 0 ret = {} im = Image.open(image) try: info = im._getexif() for tag, value in info.items(): decoded = TAGS.get(tag, value) ret[decoded] = value if ret['Orientation'] == 6: rotation = 90 except: rotation = 0 user_img = QImage(image) if rotation == 90: rot = QTransform() rot = rot.rotate(90) user_img = user_img.transformed(rot) if user_img.height() > user_img.width(): preimg = user_img.scaledToWidth(480, Qt.SmoothTransformation) preimg = preimg.copy( 0, preimg.height()/2-240, 480, 480); elif user_img.height() < user_img.width(): preimg = user_img.scaledToHeight(480, Qt.SmoothTransformation) preimg = preimg.copy( preimg.width()/2-240, 0, 480, 480); else: preimg = user_img.scaled(480, 480, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation) preimg.save(WAConstants.CACHE_PATH+"/temp.jpg", "JPG") ''''f = open(WAConstants.CACHE_PATH+"/temp.jpg", 'r') stream = f.read() stream = bytearray(stream) f.close() ''' return WAConstants.CACHE_PATH+"/temp.jpg"
def __init__(self, *args, **kwargs): super(Label_descriptionImage, self).__init__(*args, **kwargs) self.setAlignment(QtCore.Qt.AlignCenter) if Label_descriptionImage.descriptionImagePath: frontImage = QImage() frontImage.load(Label_descriptionImage.descriptionImagePath) origWidth = frontImage.width() trValue = QTransform().scale( float(origWidth) / frontImage.width(), float(origWidth) / frontImage.width()) transformedImage = frontImage.transformed(trValue) pixmap = QPixmap.fromImage(transformedImage) self.setPixmap(pixmap) self.setGeometry(0, 0, transformedImage.width(), transformedImage.height()) self.paintEvent( QPaintEvent(QtCore.QRect(0, 0, self.width(), self.height())))
class ImageBase(QLabel): def __init__(self, *args, **kwargs): self.transInfo = ImageBaseTranslateInfo() super(ImageBase, self).__init__(*args, **kwargs) self.installEventFilter(self) self.image = QImage() self.imageTransformed = QImage() self.pixmap = QPixmap() self.label = QLabel(self) self.imagePath = "" self.aspect = 1 def loadImage(self, filePath): if self.imagePath == filePath: return None self.imagePath = filePath self.aspect = 1 if self.image.load(filePath): pass self.resize() def resize(self): trValue = QTransform().scale(self.transInfo.scaleX(), self.transInfo.scaleY()) trValue *= QTransform().rotate(self.transInfo.rotValue) imageTransformed = self.image.transformed(trValue) imageWidth = imageTransformed.width() imageHeight = imageTransformed.height() self.pixmap = QPixmap.fromImage(imageTransformed) self.label.setPixmap(self.pixmap) marginLeft = (self.width() - imageWidth) / 2.0 marginTop = (self.height() - imageHeight) / 2.0 self.label.setGeometry(marginLeft + self.transInfo.x, marginTop + self.transInfo.y, imageWidth, imageHeight) def flip(self, pressX): offsetX = pressX - self.width() / 2 self.transInfo.rotValue *= -1 self.transInfo.x = (self.transInfo.x - offsetX) * -1 + offsetX self.transInfo.scaleMultX *= -1 self.resize() def show(self): self.label.show() def eventFilter(self, Obj, event): if event.type() == QtCore.QEvent.Resize: self.resize() elif event.type() == QtCore.QEvent.MouseButtonPress: pressX = event.x() - self.width() / 2 pressY = event.y() - self.height() / 2 self.transInfo.buttonPress(event.button(), pressX, pressY) elif event.type() == QtCore.QEvent.MouseButtonRelease: if self.transInfo.x == self.transInfo.bx and self.transInfo.y == self.transInfo.by: pass self.transInfo.buttonRelease() Window_global.saveInfo() elif event.type() == QtCore.QEvent.MouseMove: pressX = event.x() - self.width() / 2 pressY = event.y() - self.height() / 2 self.transInfo.drag(pressX, pressY) self.resize() elif event.type() == QtCore.QEvent.MouseButtonDblClick: self.flip(event.x()) elif event.type() == QtCore.QEvent.Wheel: pressX = event.x() - self.width() / 2 pressY = event.y() - self.height() / 2 self.transInfo.buttonPress(QtCore.Qt.RightButton, pressX, pressY) self.transInfo.drag(pressX + event.delta() / 2, pressY) self.transInfo.buttonRelease() self.resize() Window_global.saveInfo() return True
class PaintArea(QWidget): tools = { 'pen': True, 'line': False, 'rect': False, 'roundRect': False, 'circle': False, 'eraser': False, } def __init__(self, parent=None): super(PaintArea, self).__init__(parent) self.autoFillBackground = True self.myPenWidth = 3 self.myPenColor = QColor(100, 100, 100, 255) self.size_w = 500 self.size_h = 500 imageSize = QtCore.QSize(self.size_w, self.size_h) self.image = QImage(imageSize, QImage.Format_RGB32) self.imagePreview = QImage(imageSize, QImage.Format_ARGB32) self.lastPoint = QtCore.QPoint() self.setMinimumSize(imageSize) self.setMaximumSize(imageSize) def saveImage(self, fileName, fileFormat='png'): visibleImage = self.image self.resizeImage(visibleImage, self.size()) if visibleImage.save(fileName, fileFormat): self.modified = False return True else: return False def setPenColor(self, newColor): self.myPenColor = newColor def setPenWidth(self, newWidth): self.myPenWidth = newWidth def clearImage(self): self.image.fill(qRgb(255, 255, 255)) self.modified = True self.update() def clearImagePreview(self): self.imagePreview.fill(QtCore.Qt.transparent) def mousePressEvent(self, event): if event.button() == QtCore.Qt.LeftButton: self.firstPoint_x = event.x() self.firstPoint_y = event.y() self.scribbling = True def mouseMoveEvent(self, event): if (event.buttons() & QtCore.Qt.LeftButton) and self.scribbling: if self.tools['pen'] or self.tools['eraser']: self.draw(event.x(), event.y()) else: self.flag = False self.drawPreview(event.x(), event.y()) def mouseReleaseEvent(self, event): if event.button() == QtCore.Qt.LeftButton and self.scribbling: self.flag = True self.draw(event.x(), event.y()) self.scribbling = False def setTool(self, st): for i in self.tools: self.tools[i] = False self.tools[st] = True def paintEvent(self, event): painter = QPainter(self) painter.drawImage(event.rect(), self.image) painter.drawImage(event.rect(), self.imagePreview) self.clearImagePreview() def draw(self, endPoint_x, endPoint_y): painter = QPainter(self.image) painter.setPen(QPen( self.myPenColor, self.myPenWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin)) painter.setClipping(True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setRenderHint(QPainter.HighQualityAntialiasing, True) painter.setRenderHint(QPainter.Antialiasing, True) painter.setCompositionMode(QPainter.CompositionMode()) if self.tools['circle']: x1 = self.firstPoint_x y1 = self.firstPoint_y x2 = endPoint_x y2 = endPoint_y painter.drawEllipse(x1, y1, (x2 - x1), (y2 - y1)) if self.tools['eraser']: painter.setPen(QPen(QtCore.Qt.white, 10, QtCore.Qt.SolidLine)) painter.drawLine( self.firstPoint_x, self.firstPoint_y, endPoint_x, endPoint_y) self.firstPoint_x = endPoint_x self.firstPoint_y = endPoint_y if self.tools['pen']: painter.drawLine( self.firstPoint_x, self.firstPoint_y, endPoint_x, endPoint_y) self.firstPoint_x = endPoint_x self.firstPoint_y = endPoint_y if self.tools['line'] and self.flag: painter.drawLine( self.firstPoint_x, self.firstPoint_y, endPoint_x, endPoint_y) if self.tools['rect']: dx = endPoint_x - self.firstPoint_x dy = endPoint_y - self.firstPoint_y painter.drawRect(self.firstPoint_x, self.firstPoint_y, dx, dy) if self.tools['roundRect']: x1 = self.firstPoint_x y1 = self.firstPoint_y dx = endPoint_x - self.firstPoint_x dy = endPoint_y - self.firstPoint_y if x1 > endPoint_x and y1 > endPoint_y: painter.drawRoundedRect( endPoint_x, endPoint_y, -dx, -dy, 20, 20, 0) else: painter.drawRoundedRect(x1, y1, dx, dy, 20., 20.) self.modified = True self.update() def drawPreview(self, endPoint_x, endPoint_y): painter = QPainter(self.imagePreview) painter.setPen(QPen( self.myPenColor, self.myPenWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin)) painter.setClipping(True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setRenderHint(QPainter.HighQualityAntialiasing, True) painter.setRenderHint(QPainter.Antialiasing, True) painter.setOpacity(0.5) if self.tools['circle']: x1 = self.firstPoint_x y1 = self.firstPoint_y x2 = endPoint_x y2 = endPoint_y painter.drawEllipse(x1, y1, (x2 - x1), (y2 - y1)) if self.tools['line']: painter.drawLine( self.firstPoint_x, self.firstPoint_y, endPoint_x, endPoint_y) if self.tools['rect']: painter.drawRect( self.firstPoint_x, self.firstPoint_y, endPoint_x - self.firstPoint_x, endPoint_y - self.firstPoint_y) if self.tools['roundRect']: x1 = self.firstPoint_x y1 = self.firstPoint_y dx = endPoint_x - self.firstPoint_x dy = endPoint_y - self.firstPoint_y if x1 > endPoint_x and y1 > endPoint_y: painter.drawRoundedRect( endPoint_x, endPoint_y, -dx, -dy, 20, 20, 0) else: painter.drawRoundedRect(x1, y1, dx, dy, 20., 20.) self.update() def rotate(self): myTransform = QTransform() myTransform.rotate(90) self.image = self.image.transformed(myTransform) self.update() def mirror_h(self): self.image = self.image.mirrored(False, True) self.update() def mirror_w(self): self.image = self.image.mirrored(True, False) self.update() def resizeImage(self, image, newSize): if image.size() == newSize: return newImage = QImage(newSize, QImage.Format_RGB32) newImage.fill(qRgb(255, 255, 255)) painter = QPainter(newImage) painter.drawImage(QtCore.QPoint(0, 0), image) self.image = newImage def isModified(self): return self.modified def penColor(self): return self.myPenColor def penWidth(self): return self.myPenWidth
class ImageBase(QLabel): def __init__(self, *args, **kwargs): self.transInfo = ImageBaseTranslateInfo() super(ImageBase, self).__init__(*args, **kwargs) self.installEventFilter(self) self.image = QImage() self.pixmap = QPixmap() self.label = QLabel(self) self.imagePath = "" self.aspect = 1 self.imageClean = True def clearImage(self): self.label.clear() self.imageClean = True def loadImage(self, filePath ): self.imageClean = False if self.imagePath == filePath: return None self.imagePath = filePath self.aspect = 1 if self.image.load(filePath): pass self.transInfo.setDefault() widgetSize = min( self.parentWidget().width(), self.parentWidget().height() ) * 0.9 imageWidth = self.image.width() self.transInfo.scale = widgetSize / float( imageWidth ) self.resize() def resize(self): if self.imageClean: return None trValue = QTransform().scale( self.transInfo.scaleX(), self.transInfo.scaleY() ) trValue *= QTransform().rotate( self.transInfo.rotValue ) imageTransformed = self.image.transformed(trValue) imageWidth = imageTransformed.width() imageHeight = imageTransformed.height() self.pixmap = QPixmap.fromImage( imageTransformed ) self.label.setPixmap( self.pixmap ) marginLeft = (self.width() - imageWidth)/2.0 marginTop = (self.height() - imageHeight)/2.0 self.label.setGeometry( marginLeft + self.transInfo.x,marginTop + self.transInfo.y, imageWidth, imageHeight ) self.label.paintEvent(QPaintEvent(QtCore.QRect( 0,0,self.width(), self.height() ))) def flip(self, pressX ): offsetX = pressX - self.width()/2 self.transInfo.rotValue *= -1 self.transInfo.x = (self.transInfo.x - offsetX)*-1 + offsetX self.transInfo.scaleMultX *= -1 self.resize() def show( self ): self.label.show() def eventFilter( self, Obj, event ): if event.type() == QtCore.QEvent.Resize: self.resize() elif event.type() == QtCore.QEvent.MouseButtonPress: pressX = event.x()-self.width()/2 pressY = event.y()-self.height()/2 self.transInfo.buttonPress(event.button(), pressX, pressY ) elif event.type() == QtCore.QEvent.MouseButtonRelease: if self.transInfo.x == self.transInfo.bx and self.transInfo.y == self.transInfo.by: pass self.transInfo.buttonRelease() Window_global.saveInfo() elif event.type() == QtCore.QEvent.MouseMove: pressX = event.x()-self.width()/2 pressY = event.y()-self.height()/2 self.transInfo.drag( pressX, pressY ) self.resize() elif event.type() == QtCore.QEvent.MouseButtonDblClick: self.flip( event.x() ) elif event.type() == QtCore.QEvent.Wheel: pressX = event.x()-self.width()/2 pressY = event.y()-self.height()/2 self.transInfo.buttonPress( QtCore.Qt.RightButton , pressX, pressY) self.transInfo.drag( pressX + event.delta()/2, pressY ) self.transInfo.buttonRelease() self.resize() Window_global.saveInfo() return True