pep8ing because i'm stuck

This commit is contained in:
Kilian Valkhof 2010-02-05 20:30:21 +01:00
parent 2ed75030b2
commit 1abf30ba81
3 changed files with 59 additions and 34 deletions

2
todo
View file

@ -23,7 +23,7 @@ later versions:
check for double files when adding check for double files when adding
pnypng api? http://www.gracepointafterfive.com/punypng/api pnypng api? http://www.gracepointafterfive.com/punypng/api
imagemagick/graphicmagick? imagemagick/graphicmagick?
always on top option
thanks to: thanks to:
Neil Wallace Neil Wallace

View file

@ -20,14 +20,18 @@ imagelist = []
# show application on load (or not if there are command line options) # show application on load (or not if there are command line options)
showapp = True showapp = True
class StartQT4(QMainWindow): class StartQT4(QMainWindow):
def __init__(self, parent=None): def __init__(self, parent=None):
QWidget.__init__(self, parent) QWidget.__init__(self, parent)
self.ui = Ui_trimage() self.ui = Ui_trimage()
self.ui.setupUi(self) self.ui.setupUi(self)
#add quit shortcut
if hasattr(QKeySequence, 'Quit'): if hasattr(QKeySequence, 'Quit'):
self.quit_shortcut = QShortcut(QKeySequence(QKeySequence.Quit), self) self.quit_shortcut = QShortcut(QKeySequence(QKeySequence.Quit),
self)
else: else:
self.quit_shortcut = QShortcut(QKeySequence("Ctrl+Q"), self) self.quit_shortcut = QShortcut(QKeySequence("Ctrl+Q"), self)
@ -65,14 +69,16 @@ class StartQT4(QMainWindow):
options, args = parser.parse_args() options, args = parser.parse_args()
# do something with the file or directory # send to correct function
if options.filename: if options.filename:
self.file_from_cmd(options.filename) self.file_from_cmd(options.filename)
if options.directory: if options.directory:
self.dir_from_cmd(options.directory) self.dir_from_cmd(options.directory)
""" """
Input functions Input functions
""" """
def dir_from_cmd(self, directory): def dir_from_cmd(self, directory):
"""Read the files in the directory and send all files to """Read the files in the directory and send all files to
compress_file.""" compress_file."""
@ -120,21 +126,28 @@ class StartQT4(QMainWindow):
newimagelist.append(image[4]) newimagelist.append(image[4])
imagelist = [] imagelist = []
self.delegator(newimagelist) self.delegator(newimagelist)
""" """
Compress functions Compress functions
""" """
def delegator(self, images): def delegator(self, images):
"""Recieve all images, check them and send them to the worker
thread."""
delegatorlist = [] delegatorlist = []
for image in images: for image in images:
if self.checkname(image): if self.checkname(image):
delegatorlist.append((image, QIcon(image))) delegatorlist.append((image, QIcon(image)))
#TODO figure out how to animate imagelist.append(("Compressing...", "", "", "", image,
imagelist.append(("Compressing...", "", "", "", image, QIcon(QPixmap("compressing.gif")))) QIcon(QPixmap("compressing.gif"))))
else:
print("[error] " + image + " not an image file")
self.thread.compress_file(delegatorlist) self.thread.compress_file(delegatorlist)
""" """
UI Functions UI Functions
""" """
def update_table(self): def update_table(self):
"""Update the table view with the latest file data.""" """Update the table view with the latest file data."""
tview = self.ui.processedfiles tview = self.ui.processedfiles
@ -165,6 +178,7 @@ class StartQT4(QMainWindow):
""" """
Helper functions Helper functions
""" """
def checkname(self, name): def checkname(self, name):
"""Check if the file is a jpg or png.""" """Check if the file is a jpg or png."""
if path.splitext(str(name))[1].lower() in [".jpg", ".jpeg", ".png"]: if path.splitext(str(name))[1].lower() in [".jpg", ".jpeg", ".png"]:
@ -174,6 +188,7 @@ class StartQT4(QMainWindow):
"""Enable the recompress button.""" """Enable the recompress button."""
self.ui.recompress.setEnabled(True) self.ui.recompress.setEnabled(True)
class TriTableModel(QAbstractTableModel): class TriTableModel(QAbstractTableModel):
def __init__(self, parent, imagelist, header, *args): def __init__(self, parent, imagelist, header, *args):
@ -195,7 +210,7 @@ class TriTableModel(QAbstractTableModel):
return len(self.header) return len(self.header)
def data(self, index, role): def data(self, index, role):
"""Get data.""" """fill the table with data"""
if not index.isValid(): if not index.isValid():
return QVariant() return QVariant()
elif role == Qt.DisplayRole: elif role == Qt.DisplayRole:
@ -209,7 +224,7 @@ class TriTableModel(QAbstractTableModel):
return QVariant() return QVariant()
def headerData(self, col, orientation, role): def headerData(self, col, orientation, role):
"""Get header data.""" """Fill the table headers."""
if orientation == Qt.Horizontal and (role == Qt.DisplayRole or if orientation == Qt.Horizontal and (role == Qt.DisplayRole or
role == Qt.DecorationRole): role == Qt.DecorationRole):
return QVariant(self.header[col]) return QVariant(self.header[col])
@ -219,16 +234,15 @@ class TriTableModel(QAbstractTableModel):
class Worker(QThread): class Worker(QThread):
def __init__(self, parent=None): def __init__(self, parent=None):
QThread.__init__(self, parent) QThread.__init__(self, parent)
self.exiting = False self.exiting = False
def __del__(self): def __del__(self):
self.exiting = True self.exiting = True
self.wait() self.wait()
def compress_file(self, images): def compress_file(self, images):
"""Start the worker thread."""
self.images = images self.images = images
self.start() self.start()
@ -274,18 +288,18 @@ class Worker(QThread):
for i, image in enumerate(imagelist): for i, image in enumerate(imagelist):
if image[4] == filename: if image[4] == filename:
imagelist.remove(image) imagelist.remove(image)
imagelist.insert(i, (name, oldfilesizestr, newfilesizestr, ratiostr, imagelist.insert(i, (name, oldfilesizestr,
filename, icon)) newfilesizestr, ratiostr, filename, icon))
self.emit(SIGNAL("updateUi")) self.emit(SIGNAL("updateUi"))
global showapp global showapp
if showapp != True: if showapp != True:
# we work via the commandline # we work via the commandline
print("File:" + filename + ", Old Size:" + oldfilesizestr + print("File:" + filename + ", Old Size:" + oldfilesizestr +
", New Size:" + newfilesizestr + ", Ratio:" + ratiostr) ", New Size:" + newfilesizestr +
", Ratio:" + ratiostr)
else: else:
# TODO nice error recovery print("[error]" + runfile)
print("uh. not good")
if __name__ == "__main__": if __name__ == "__main__":

47
ui.py
View file

@ -4,20 +4,17 @@ from PyQt4.QtGui import *
DEBUG = False DEBUG = False
class TrimageTableView(QTableView):
class TrimageTableView(QTableView):
"""Init the table drop event."""
def __init__(self, parent=None): def __init__(self, parent=None):
super(TrimageTableView, self).__init__(parent) super(TrimageTableView, self).__init__(parent)
self.setAcceptDrops(True) self.setAcceptDrops(True)
def dragEnterEvent(self, event): def dragEnterEvent(self, event):
if event.mimeData().hasFormat("text/uri-list"): if event.mimeData().hasFormat("text/uri-list"):
if DEBUG:
print("Accepting event: %s" % list(event.mimeData().formats()))
event.accept() event.accept()
else: else:
if DEBUG:
print("Rejecting event: %s" % list(event.mimeData().formats()))
event.ignore() event.ignore()
def dragMoveEvent(self, event): def dragMoveEvent(self, event):
@ -29,8 +26,8 @@ class TrimageTableView(QTableView):
files[i] = QUrl(QString(file)).toLocalFile() files[i] = QUrl(QString(file)).toLocalFile()
self.emit(SIGNAL("fileDropEvent"), (files)) self.emit(SIGNAL("fileDropEvent"), (files))
class Ui_trimage(object):
class Ui_trimage(object):
def setupUi(self, trimage): def setupUi(self, trimage):
trimage.setObjectName("trimage") trimage.setObjectName("trimage")
trimage.resize(600, 170) trimage.resize(600, 170)
@ -49,7 +46,8 @@ class Ui_trimage(object):
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(1) sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(1) sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) sizePolicy.setHeightForWidth(
self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy) self.widget.setSizePolicy(sizePolicy)
self.widget.setObjectName("widget") self.widget.setObjectName("widget")
@ -93,7 +91,8 @@ class Ui_trimage(object):
self.label.setObjectName("label") self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label) self.horizontalLayout.addWidget(self.label)
spacerItem = QSpacerItem(498, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) spacerItem = QSpacerItem(498, 20, QSizePolicy.Expanding,
QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem) self.horizontalLayout.addItem(spacerItem)
self.recompress = QPushButton(self.frame) self.recompress = QPushButton(self.frame)
font = QFont() font = QFont()
@ -135,13 +134,25 @@ class Ui_trimage(object):
QMetaObject.connectSlotsByName(trimage) QMetaObject.connectSlotsByName(trimage)
def retranslateUi(self, trimage): def retranslateUi(self, trimage):
trimage.setWindowTitle(QApplication.translate("trimage", "Trimage image compressor", None, QApplication.UnicodeUTF8)) trimage.setWindowTitle(QApplication.translate("trimage",
self.addfiles.setToolTip(QApplication.translate("trimage", "Add file to the compression list", None, QApplication.UnicodeUTF8)) "Trimage image compressor", None, QApplication.UnicodeUTF8))
self.addfiles.setText(QApplication.translate("trimage", "&Add and compress", None, QApplication.UnicodeUTF8)) self.addfiles.setToolTip(QApplication.translate("trimage",
self.addfiles.setShortcut(QApplication.translate("trimage", "Alt+A", None, QApplication.UnicodeUTF8)) "Add file to the compression list", None,
self.label.setText(QApplication.translate("trimage", "Drag and drop images onto the table", None, QApplication.UnicodeUTF8)) QApplication.UnicodeUTF8))
self.recompress.setToolTip(QApplication.translate("trimage", "Recompress selected images", None, QApplication.UnicodeUTF8)) self.addfiles.setText(QApplication.translate("trimage",
self.recompress.setText(QApplication.translate("trimage", "&Recompress", None, QApplication.UnicodeUTF8)) "&Add and compress", None, QApplication.UnicodeUTF8))
self.recompress.setShortcut(QApplication.translate("trimage", "Alt+R", None, QApplication.UnicodeUTF8)) self.addfiles.setShortcut(QApplication.translate("trimage",
self.processedfiles.setToolTip(QApplication.translate("trimage", "Drag files in here", None, QApplication.UnicodeUTF8)) "Alt+A", None, QApplication.UnicodeUTF8))
self.processedfiles.setWhatsThis(QApplication.translate("trimage", "Drag files in here", None, QApplication.UnicodeUTF8)) self.label.setText(QApplication.translate("trimage",
"Drag and drop images onto the table", None,
QApplication.UnicodeUTF8))
self.recompress.setToolTip(QApplication.translate("trimage",
"Recompress selected images", None, QApplication.UnicodeUTF8))
self.recompress.setText(QApplication.translate("trimage",
"&Recompress", None, QApplication.UnicodeUTF8))
self.recompress.setShortcut(QApplication.translate("trimage",
"Alt+R", None, QApplication.UnicodeUTF8))
self.processedfiles.setToolTip(QApplication.translate("trimage",
"Drag files in here", None, QApplication.UnicodeUTF8))
self.processedfiles.setWhatsThis(QApplication.translate("trimage",
"Drag files in here", None, QApplication.UnicodeUTF8))