From 1abf30ba81e773e01030a3b5eba00543b487bbaa Mon Sep 17 00:00:00 2001 From: Kilian Valkhof Date: Fri, 5 Feb 2010 20:30:21 +0100 Subject: [PATCH] pep8ing because i'm stuck --- todo | 2 +- trimage.py | 44 +++++++++++++++++++++++++++++--------------- ui.py | 47 +++++++++++++++++++++++++++++------------------ 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/todo b/todo index 2d7e55f..4f49688 100644 --- a/todo +++ b/todo @@ -23,7 +23,7 @@ later versions: check for double files when adding pnypng api? http://www.gracepointafterfive.com/punypng/api imagemagick/graphicmagick? - + always on top option thanks to: Neil Wallace diff --git a/trimage.py b/trimage.py index ff3c091..d668379 100644 --- a/trimage.py +++ b/trimage.py @@ -20,14 +20,18 @@ imagelist = [] # show application on load (or not if there are command line options) showapp = True + class StartQT4(QMainWindow): def __init__(self, parent=None): QWidget.__init__(self, parent) self.ui = Ui_trimage() self.ui.setupUi(self) + + #add quit shortcut if hasattr(QKeySequence, 'Quit'): - self.quit_shortcut = QShortcut(QKeySequence(QKeySequence.Quit), self) + self.quit_shortcut = QShortcut(QKeySequence(QKeySequence.Quit), + self) else: self.quit_shortcut = QShortcut(QKeySequence("Ctrl+Q"), self) @@ -65,14 +69,16 @@ class StartQT4(QMainWindow): options, args = parser.parse_args() - # do something with the file or directory + # send to correct function if options.filename: self.file_from_cmd(options.filename) if options.directory: self.dir_from_cmd(options.directory) + """ Input functions """ + def dir_from_cmd(self, directory): """Read the files in the directory and send all files to compress_file.""" @@ -120,21 +126,28 @@ class StartQT4(QMainWindow): newimagelist.append(image[4]) imagelist = [] self.delegator(newimagelist) + """ Compress functions """ + def delegator(self, images): + """Recieve all images, check them and send them to the worker + thread.""" delegatorlist = [] for image in images: if self.checkname(image): delegatorlist.append((image, QIcon(image))) - #TODO figure out how to animate - imagelist.append(("Compressing...", "", "", "", image, QIcon(QPixmap("compressing.gif")))) + imagelist.append(("Compressing...", "", "", "", image, + QIcon(QPixmap("compressing.gif")))) + else: + print("[error] " + image + " not an image file") self.thread.compress_file(delegatorlist) """ UI Functions """ + def update_table(self): """Update the table view with the latest file data.""" tview = self.ui.processedfiles @@ -165,6 +178,7 @@ class StartQT4(QMainWindow): """ Helper functions """ + def checkname(self, name): """Check if the file is a jpg or png.""" if path.splitext(str(name))[1].lower() in [".jpg", ".jpeg", ".png"]: @@ -174,6 +188,7 @@ class StartQT4(QMainWindow): """Enable the recompress button.""" self.ui.recompress.setEnabled(True) + class TriTableModel(QAbstractTableModel): def __init__(self, parent, imagelist, header, *args): @@ -195,13 +210,13 @@ class TriTableModel(QAbstractTableModel): return len(self.header) def data(self, index, role): - """Get data.""" + """fill the table with data""" if not index.isValid(): return QVariant() elif role == Qt.DisplayRole: data = imagelist[index.row()][index.column()] return QVariant(data) - elif index.column()==0 and role == Qt.DecorationRole: + elif index.column() == 0 and role == Qt.DecorationRole: # decorate column 0 with an icon of the image itself f_icon = imagelist[index.row()][5] return QVariant(f_icon) @@ -209,7 +224,7 @@ class TriTableModel(QAbstractTableModel): return QVariant() def headerData(self, col, orientation, role): - """Get header data.""" + """Fill the table headers.""" if orientation == Qt.Horizontal and (role == Qt.DisplayRole or role == Qt.DecorationRole): return QVariant(self.header[col]) @@ -218,17 +233,16 @@ class TriTableModel(QAbstractTableModel): class Worker(QThread): - def __init__(self, parent = None): - + def __init__(self, parent=None): QThread.__init__(self, parent) self.exiting = False def __del__(self): - self.exiting = True self.wait() def compress_file(self, images): + """Start the worker thread.""" self.images = images self.start() @@ -274,18 +288,18 @@ class Worker(QThread): for i, image in enumerate(imagelist): if image[4] == filename: imagelist.remove(image) - imagelist.insert(i, (name, oldfilesizestr, newfilesizestr, ratiostr, - filename, icon)) + imagelist.insert(i, (name, oldfilesizestr, + newfilesizestr, ratiostr, filename, icon)) self.emit(SIGNAL("updateUi")) global showapp if showapp != True: # we work via the commandline print("File:" + filename + ", Old Size:" + oldfilesizestr + - ", New Size:" + newfilesizestr + ", Ratio:" + ratiostr) + ", New Size:" + newfilesizestr + + ", Ratio:" + ratiostr) else: - # TODO nice error recovery - print("uh. not good") + print("[error]" + runfile) if __name__ == "__main__": diff --git a/ui.py b/ui.py index 41d7dd4..e7f73d7 100644 --- a/ui.py +++ b/ui.py @@ -4,20 +4,17 @@ from PyQt4.QtGui import * DEBUG = False -class TrimageTableView(QTableView): +class TrimageTableView(QTableView): + """Init the table drop event.""" def __init__(self, parent=None): super(TrimageTableView, self).__init__(parent) self.setAcceptDrops(True) def dragEnterEvent(self, event): if event.mimeData().hasFormat("text/uri-list"): - if DEBUG: - print("Accepting event: %s" % list(event.mimeData().formats())) event.accept() else: - if DEBUG: - print("Rejecting event: %s" % list(event.mimeData().formats())) event.ignore() def dragMoveEvent(self, event): @@ -29,8 +26,8 @@ class TrimageTableView(QTableView): files[i] = QUrl(QString(file)).toLocalFile() self.emit(SIGNAL("fileDropEvent"), (files)) -class Ui_trimage(object): +class Ui_trimage(object): def setupUi(self, trimage): trimage.setObjectName("trimage") trimage.resize(600, 170) @@ -49,7 +46,8 @@ class Ui_trimage(object): sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(1) sizePolicy.setVerticalStretch(1) - sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth()) + sizePolicy.setHeightForWidth( + self.widget.sizePolicy().hasHeightForWidth()) self.widget.setSizePolicy(sizePolicy) self.widget.setObjectName("widget") @@ -93,7 +91,8 @@ class Ui_trimage(object): self.label.setObjectName("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.recompress = QPushButton(self.frame) font = QFont() @@ -135,13 +134,25 @@ class Ui_trimage(object): QMetaObject.connectSlotsByName(trimage) def retranslateUi(self, trimage): - trimage.setWindowTitle(QApplication.translate("trimage", "Trimage image compressor", None, QApplication.UnicodeUTF8)) - self.addfiles.setToolTip(QApplication.translate("trimage", "Add file to the compression list", None, QApplication.UnicodeUTF8)) - self.addfiles.setText(QApplication.translate("trimage", "&Add and compress", None, QApplication.UnicodeUTF8)) - self.addfiles.setShortcut(QApplication.translate("trimage", "Alt+A", 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)) + trimage.setWindowTitle(QApplication.translate("trimage", + "Trimage image compressor", None, QApplication.UnicodeUTF8)) + self.addfiles.setToolTip(QApplication.translate("trimage", + "Add file to the compression list", None, + QApplication.UnicodeUTF8)) + self.addfiles.setText(QApplication.translate("trimage", + "&Add and compress", None, QApplication.UnicodeUTF8)) + self.addfiles.setShortcut(QApplication.translate("trimage", + "Alt+A", 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))