diff --git a/todo b/todo index 93c132f..46df642 100644 --- a/todo +++ b/todo @@ -24,7 +24,6 @@ optipng for png and gif, jpegoptim for jpeg. both are available in ubuntu as dep ========================================== todo app wise - implement drag and drop on table -- clean up code, make it PEP-8 - implement threading (and include advpng then) - errors need to go to standarderror - test with filenames in spaces (probably works already) diff --git a/trimage.py b/trimage.py index cead2c1..e749b67 100644 --- a/trimage.py +++ b/trimage.py @@ -18,13 +18,21 @@ class StartQT4(QMainWindow): QWidget.__init__(self, parent) self.ui = Ui_trimage() self.ui.setupUi(self) - # todo use standardKey Quit. + #TODO use standardKey Quit. self.quit_shortcut = QShortcut(QKeySequence("Ctrl+Q"), self) - # set recompress to false + + # disable recompress self.ui.recompress.setEnabled(False) + + # init imagelist self.imagelist = [] + + # show application on load (or not if there are command line options) self.showapp = True + # activate command line options + self.commandline_options() + # connect signals with slots QObject.connect(self.ui.addfiles, SIGNAL("clicked()"), self.file_dialog) @@ -33,25 +41,27 @@ class StartQT4(QMainWindow): QObject.connect(self.quit_shortcut, SIGNAL("activated()"), qApp, SLOT('quit()')) + def commandline_options(self): + """Set up the command line options.""" parser = OptionParser(version="%prog 1.0", description="GUI front-end to compress png and jpg images via " "optipng and jpegoptim") - parser.add_option("-f", "--file", action="store", type="string", dest="filename", help="image to compress") - parser.add_option("-d", "--directory", action="store", type="string", dest="directory", help="directory of images to compress") options, args = parser.parse_args() + # do something with the file or directory if options.filename: self.file_from_cmd(options.filename) - if options.directory: self.dir_from_cmd(options.directory) def dir_from_cmd(self, directory): + """Read the files in the directory and send all files to + compress_file.""" self.showapp = False imagedir = listdir(directory) for image in imagedir: @@ -61,18 +71,22 @@ class StartQT4(QMainWindow): self.compress_file(image) def file_from_cmd(self, image): + """Get the file and send it to compress_file""" self.showapp = False if self.checkname(image): self.compress_file(image) - def file_drop(self): + def file_drop(self, files): + #TODO implement file drop handling print("booya") def checkname(self, name): + """Check if the file is a jpg or png.""" if path.splitext(str(name))[1].lower() in [".jpg", ".jpeg", ".png"]: return True def file_dialog(self): + """Open a file dialog and send the selected images to compress_file.""" fd = QFileDialog(self) images = fd.getOpenFileNames(self, "Select one or more image files to compress", @@ -83,47 +97,58 @@ class StartQT4(QMainWindow): self.compress_file(image) def enable_recompress(self): + """Enable the recompress button.""" self.ui.recompress.setEnabled(True) def recompress_files(self): + """Send each file in the current file list to compress_file again.""" imagelistcopy = self.imagelist self.imagelist = [] for image in imagelistcopy: self.compress_file(image[-1]) def compress_file(self, filename): - print(filename) + """Compress the given file, get data from it and call update_table.""" + + #gather old file data oldfile = QFileInfo(filename) name = oldfile.fileName() oldfilesize = oldfile.size() oldfilesizestr = size(oldfilesize, system=alternative) + #decide with tool to use if path.splitext(str(filename))[1].lower() in [".jpg", ".jpeg"]: runstr = 'jpegoptim --strip-all -f "' + str(filename) + '"' runfile = system(runstr) elif path.splitext(str(filename))[1].lower() in [".png"]: + # don't do advpng yet #runstr = ('optipng -force -o7 "' + str(filename) - #+ '"; advpng -z4 "' + str(filename) + '"') ## don't do advpng yet + #+ '"; advpng -z4 "' + str(filename) + '"') runstr = 'optipng -force -o7 "' + str(filename) + '"' runfile = system(runstr) if runfile == 0: + #gather new file data newfile = QFile(filename) newfilesize = newfile.size() newfilesizestr = size(newfilesize, system=alternative) + #calculate ratio and make a nice string ratio = 100 - (float(newfilesize) / float(oldfilesize) * 100) ratiostr = "%.1f%%" % ratio + # append current image to list self.imagelist.append( (name, oldfilesizestr, newfilesizestr, ratiostr, filename)) self.update_table() else: - print("uh. not good") #throw dialogbox error or something? + # TODO nice error recovery + print("uh. not good") def update_table(self): + """Update the table view with the latest file data.""" tview = self.ui.processedfiles # set table model @@ -143,7 +168,11 @@ class StartQT4(QMainWindow): nrows = len(self.imagelist) for row in range(nrows): tview.setRowHeight(row, 25) + + # set the first column to be longest tview.setColumnWidth(0, 300) + + # enable recompress button self.enable_recompress() @@ -160,12 +189,15 @@ class TriTableModel(QAbstractTableModel): self.header = header def rowCount(self, parent): + """Count the number of rows.""" return len(self.imagelist) def columnCount(self, parent): + """Count the number of columns.""" return len(self.header) def data(self, index, role): + """Get data.""" if not index.isValid(): return QVariant() elif role != Qt.DisplayRole: @@ -173,6 +205,7 @@ class TriTableModel(QAbstractTableModel): return QVariant(self.imagelist[index.row()][index.column()]) def headerData(self, col, orientation, role): + """Get header data.""" if orientation == Qt.Horizontal and role == Qt.DisplayRole: return QVariant(self.header[col]) return QVariant() @@ -183,6 +216,7 @@ if __name__ == "__main__": myapp = StartQT4() if myapp.showapp: + # no command line options called myapp.show() else: quit()