3
\$\begingroup\$

I have a little project going on and so far I'm not really having any problems. But since I haven't internalized all of Python's core features yet, I'm pretty sure my code offers pretty many subjects to optimize.

Please point out anything that could be done better. For example, I'm feeling uncomfortable with the method updateMousePosition as it seems kind of ugly to me.

#!/usr/bin/python 
import sys
from PySide import QtCore, QtGui
from PySide.QtGui import QWidget, QApplication, QPixmap, QLabel
from UI_TextureViewer import Ui_UI_TextureViewer
from ExtendedLabel import ExtendedLabel
class TextureViewer(QWidget, Ui_UI_TextureViewer):
 """ A widget that displays a single texture.
 The textures resides in a ExtendedLabel (which enables connecting to a
 mouseMovedSignal) which is put inside a QScrollArea to enable arbitrary
 zooming.
 The widget also shows the u and v coordinates based on the position of the
 mouse."""
 def __init__(self, filename, parent=None):
 """ Default ctor.
 Connects all buttons, loads the texture from file and sets up a default
 zoom value of 1.0"""
 super(TextureViewer, self).__init__(parent)
 self.setupUi(self)
 self.filename = filename
 self.buttonZoomIn.clicked.connect(self.zoomIn)
 self.buttonZoomOut.clicked.connect(self.zoomOut)
 self.buttonResetZoom.clicked.connect(self.resetZoom)
 self.u = 0
 self.v = 0
 self.labelFilename.setText(filename)
 self.zoomValue = 1.0
 self.loadImage()
 def loadImage(self):
 """ Loads the image stored in self.filename and sets up the labels
 showing information about the original (umzoomed) image. """
 self.image = QPixmap()
 self.image.load(self.filename)
 imgs = [str(self.image.size().width()), str(self.image.size().height())]
 self.labelSize.setText("x".join(imgs))
 self.zoom(self.zoomValue)
 def zoom(self, factor):
 """ Zooms the texture by the given factor.
 Zooming is achieved by creating a scaled copy of the original image and
 showing it by setting setPixmap of an ExtendedLabel. """
 imageLabel = ExtendedLabel()
 imageLabel.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
 imageLabel.mouseMovedSignal.connect(self.updateMousePosition)
 ow, oh = [self.image.size().width(), self.image.size().height()]
 sw, sh = [ow * factor, oh * factor]
 zoomedImage = self.image.scaled(sw, sh)
 imageLabel.setPixmap(zoomedImage)
 self.scrollArea.setWidget(imageLabel)
 self.updateZoomLabel()
 def updateZoomLabel(self):
 """ Updates the label that shows the current zoom value. """
 f = self.zoomValue * 100.0
 self.labelZoom.setText("{0:.0f}%".format(f))
 def zoomIn(self):
 """ Zooms in 25% on the image. """
 self.zoomValue += 0.25
 self.zoom(self.zoomValue)
 def zoomOut(self):
 """ Zooms out 25% of the image.
 If the zoom value is less than 25%, then the step size is decreased to
 5%. Total zoom level is clamped to 5%. """
 if self.zoomValue <= 0.25:
 v = 0.05
 else:
 v = 0.25
 if self.zoomValue - v > 0.05:
 self.zoomValue -= v
 self.zoom(self.zoomValue)
 def resetZoom(self):
 """ Resets the zoom factor to 1.0. """
 self.zoomValue = 1.0
 self.zoom(self.zoomValue)
 def updateMousePosition(self, event):
 """ Slot that is called by the mouseMovedSignal of the ExtendedLabel
 which shows the image.
 Computes the u and v coordinates of the current mouse position and
 updates the labels showing the coordinates. """
 absx, absy = [event.x(), event.y()]
 sx, sy = [self.image.width() * self.zoomValue,
 self.image.height() * self.zoomValue]
 self.u = float(absx) / float(sx)
 self.v = float(absy) / float(sy)
 if self.u > 1.0:
 self.u = 1.0
 if self.v > 1.0:
 self.v = 1.0
 self.labelU.setText("{0:.4f}".format(self.u))
 self.labelV.setText("{0:.4f}".format(self.v))
if __name__ == '__main__':
 app = QApplication(sys.argv)
 frame = TextureViewer("../../media/textures/DarkGrass.png")
 frame.show()
 app.exec_()
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jul 3, 2011 at 12:04
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

A few things I can see that will clean up the code a tad are:

def updateMousePosition(self, event):
 """ Slot that is called by the mouseMovedSignal of the ExtendedLabel
 which shows the image.
 Computes the u and v coordinates of the current mouse position and
 updates the labels showing the coordinates. """
 sx = self.image.width() * self.zoomValue
 sy = self.image.height() * self.zoomValue
 self.u = min(float(event.x()) / float(sx), 1.0)
 self.v = min(float(event.y()) / float(sy), 1.0)
 self.labelU.setText("{0:.4f}".format(self.u))
 self.labelV.setText("{0:.4f}".format(self.v))

absx and absy are used once, so I don't see a point in assigning them to temp vars. I'd put sx/sy on separate lines. (As an aside, your use of square brackets is pretty spurious as "x,y = 1,2" has the same net effect as "x,y = [1,2]" without the creation of a temp list.) Your if's are basically capping self.u and self.v at 1.0. Also are you actually using the self.v and self.u anywhere else or are they just locals to get the values for the setText() calls? If so, they could just be local variables like f in updateZoomLabel().

answered Jul 5, 2011 at 21:17
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.