Lots of fixes/changes there

- ImageRow
 - Recompress fixed
 - Adding a file again recompresses it
 - Status indicators
 - cli working again
 - more crash resistant
This commit is contained in:
Kálmán Tarnay 2010-04-02 07:18:18 +02:00
parent 2c9599813a
commit 5782698dda

View file

@ -132,19 +132,11 @@ class StartQT4(QMainWindow):
# this is a fix for file dialog differentiating between cases # this is a fix for file dialog differentiating between cases
"Image files (*.png *.jpg *.jpeg *.PNG *.JPG *.JPEG)") "Image files (*.png *.jpg *.jpeg *.PNG *.JPG *.JPEG)")
imagelist = [] self.delegator([unicode(fullpath) for fullpath in images])
for i, image in enumerate(images):
imagelist.append(unicode(image))
self.delegator(imagelist)
def recompress_files(self): def recompress_files(self):
"""Send each file in the current file list to compress_file again.""" """Send each file in the current file list to compress_file again."""
newimagelist = [] self.delegator([row.image.fullpath for row in self.imagelist])
for image in self.imagelist:
newimagelist.append(image[4])
self.imagelist = []
self.delegator(newimagelist)
""" """
Compress functions Compress functions
@ -155,14 +147,21 @@ class StartQT4(QMainWindow):
Recieve all images, check them and send them to the worker thread. Recieve all images, check them and send them to the worker thread.
""" """
delegatorlist = [] delegatorlist = []
for image in images: for fullpath in images:
image=Image(image) try: # do not add already existing images again, recompress them instead
if image.valid: image=(i.image for i in self.imagelist
delegatorlist.append(image) if i.image.fullpath == fullpath).next()
self.imagelist.append(("Compressing...", "", "", "", image, if image.compressed:
QIcon(QPixmap(self.ui.get_image("pixmaps/compressing.gif"))))) image.reset()
else: image.recompression=True
print >>sys.stderr, u"[error] %s not a supported image file" % image.fullpath delegatorlist.append(image)
except StopIteration:
image=Image(fullpath)
if image.valid:
delegatorlist.append(image)
self.imagelist.append(ImageRow(image,QIcon(QPixmap(self.ui.get_image("pixmaps/compressing.gif")))))
else:
print >>sys.stderr, u"[error] %s not a supported image file" % image.fullpath
self.update_table() self.update_table()
self.thread.compress_file(delegatorlist, self.showapp, self.verbose, self.thread.compress_file(delegatorlist, self.showapp, self.verbose,
@ -267,7 +266,7 @@ class TriTableModel(QAbstractTableModel):
return QVariant(data) 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 # decorate column 0 with an icon of the image itself
f_icon = self.imagelist[index.row()][5] f_icon = self.imagelist[index.row()][4]
return QVariant(f_icon) return QVariant(f_icon)
else: else:
return QVariant() return QVariant()
@ -279,9 +278,42 @@ class TriTableModel(QAbstractTableModel):
return QVariant(self.header[col]) return QVariant(self.header[col])
return QVariant() return QVariant()
class ImageRow:
def __init__(self, image, waitingIcon=None):
self.image=image
d={
'shortname': lambda i: self.statusStr() % i.shortname,
'oldfilesizestr': lambda i: size(i.oldfilesize, system=alternative) if i.compressed else "",
'newfilesizestr': lambda i: size(i.newfilesize, system=alternative) if i.compressed else "",
'ratiostr': lambda i:
"%.1f%%" % (100 - (float(i.newfilesize) / i.oldfilesize * 100)) if i.compressed else "",
'icon': lambda i: i.icon if i.compressed else waitingIcon,
'fullpath': lambda i: i.fullpath, #only used by cli
}
for i,n in enumerate(['shortname','oldfilesizestr','newfilesizestr','ratiostr','icon']):
d[i]=d[n]
self.d = d
def statusStr(self):
if self.image.failed:
return "ERROR: %s"
if self.image.compressing:
return "In Progress..."
if not self.image.compressed and self.image.recompression:
return "Queued for recompression..."
if not self.image.compressed:
return "Queued..."
return "%s"
def __getitem__(self, key):
return self.d[key](self.image)
class Image: class Image:
def __init__(self, fullpath): def __init__(self, fullpath):
self.valid = False self.valid = False
self.reset()
self.fullpath = fullpath self.fullpath = fullpath
if path.isfile(self.fullpath): if path.isfile(self.fullpath):
self.filetype = determinetype(self.fullpath) self.filetype = determinetype(self.fullpath)
@ -300,19 +332,35 @@ class Image:
self.filetype=None self.filetype=None
return self.filetype return self.filetype
def reset(self):
self.failed = False
self.compressed = False
self.compressing = False
self.recompression= False
def compress(self): def compress(self):
if not self.valid: if not self.valid:
raise "Tried to compress invalid image (unsupported format or not file)" raise "Tried to compress invalid image (unsupported format or not file)"
self.reset()
self.compressing=True
runString = { runString = {
"jpeg": u"jpegoptim -f --strip-all '%(file)s'", "jpeg": u"jpegoptim -f --strip-all '%(file)s'",
"png" : u"optipng -force -o7 '%(file)s'&&advpng -z4 '%(file)s'"} "png" : u"optipng -force -o7 '%(file)s'&&advpng -z4 '%(file)s'"}
retcode = call(runString[self.filetype] % {"file": self.fullpath}, try:
shell = True, stdout=PIPE) retcode = call(runString[self.filetype] % {"file": self.fullpath},
shell = True, stdout=PIPE)
except:
retcode = -1
if retcode == 0: if retcode == 0:
self.newfilesize = QFile(self.fullpath).size() self.newfilesize = QFile(self.fullpath).size()
self.compressed=True
else:
self.failed=True
self.compressing=False
self.retcode=retcode self.retcode=retcode
return self return self
class Worker(QThread): class Worker(QThread):
def __init__(self, parent=None): def __init__(self, parent=None):
@ -336,31 +384,18 @@ class Worker(QThread):
def run(self): def run(self):
"""Compress the given file, get data from it and call update_table.""" """Compress the given file, get data from it and call update_table."""
tp = self.threadpool tp = self.threadpool
while self.showapp or not (tp.__active_workers==0 and tp.__jobs.empty()): while self.showapp or not (tp._ThreadPool__active_worker_count==0 and tp._ThreadPool__jobs.empty()):
image = self.toDisplay.get() image = self.toDisplay.get()
if image.retcode==0: self.emit(SIGNAL("updateUi"))
#calculate ratio and make a nice string
oldfilesizestr = size(image.oldfilesize, system=alternative)
newfilesizestr = size(image.newfilesize, system=alternative)
ratio = 100 - (float(image.newfilesize) / float(image.oldfilesize) * 100)
ratiostr = "%.1f%%" % ratio
# append current image to list if not self.showapp and self.verbose: # we work via the commandline
for i, listitem in enumerate(self.imagelist): if image.retcode==0:
if listitem[4] == image: ir=ImageRow(image)
self.imagelist.remove(listitem) print("File: " + ir['fullpath'] + ", Old Size: "
self.imagelist.insert(i, (image.shortname, oldfilesizestr, + ir['oldfilesizestr'] + ", New Size: " + ir['newfilesizestr']
newfilesizestr, ratiostr, image.fullpath, image.icon)) + ", Ratio: " + ir['ratiostr'])
else:
self.emit(SIGNAL("updateUi")) print >>sys.stderr, u"[error] %s could not be compressed" % image.fullpath
if not self.showapp and self.verbose:
# we work via the commandline
print("File: " + image.fullpath + ", Old Size: "
+ oldfilesizestr + ", New Size: " + newfilesizestr
+ ", Ratio: " + ratiostr)
else:
print >>sys.stderr, u"[error] %s could not be compressed" % image.fullpath
class TrimageTableView(QTableView): class TrimageTableView(QTableView):