Friday, January 29, 2010

PyQt: Emitting events from non-QObject subclasses.

In order to emit events in PyQt the emitting class must inherit directly from QObject. This can be quite a nuisance, especially so because multiple inheritance is impossible when using PyQt. E.g., I recently had the problem that I had task running in a QThreadPool that I would like to emit a signal when progress had been made, so that I could update a progressbar in my UI. To make the task runnable in a QThreadPool the task class had to inherit from QRunnable, and as QRunnable is not a subclass of QObject it was impossible to emit signals from within the task class. I found a solution to this problem though - one that I find quite elegant, so I'd like to share it with you :-)

The trick is to use another object as the mediator for emitting signals, and then using Python properties to hide the fact that a mediator is being used. Consider the example below:
class Mediator(QObject):
mySignal = pyqtSignal(int)
def __init__(self):
super(Mediator, self).__init__()

class Task(QRunnable):
def __init__(self):
super(Task, self).__init__()
self._mediator = Mediator()

def mySignal():
def fget(self):
return self._mediator.mySignal
return locals()
mySignal = property(**mySignal())
Using this approach any classes working with Task instances can connect directly to the mySignal signal as if it was bound directly to the Task class. So the property hides the fact that a mediator is in use.

Wednesday, January 27, 2010

Embedding fonts in Gnuplot generated PDFs

I have recently submitted two papers to an IEEE conference, and in doing so I had to make sure that _all_ fonts were embedded within the PDFs I submitted. This was not the case with one of my papers, and after some detective work I found out that this pertained to my using Gnuplot's PDF terminal to generate PDF graphs for my paper. Apparently Gnuplot is incapable of embedding fonts into the generated PDFs...

I found this solution though. You need to pass the PDF that Gnuplot generated through ghostscript with the following command:
gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=tmp.pdf\
-dCompatibilityLevel=1.5 -dPDFSETTINGS=/prepress -c .setpdfwrite\
-f infile.pdf
Where 'infile.pdf' is your Gnuplot graph. If you follow the link to where I found this hack, you may notice that I have changed the commandline slightly, so that it now creates PDF 1.5 compatible output. This is a requirement of IEEE papers I think.

Thursday, January 14, 2010

Avoiding "Size mismatch" Errors When Upgrading Maemo 5 Scratchbox

I have just returned to developing Maemo application, and as usual when I come back to my Maemo scratchbox installation, I start off by upgrading the development environment. So I opened my scratchbox and issued the commands
fakeroot apt-get update
fakeroot apt-get dist-upgrade
Unfortunately this did not work for me this time because of some inconsistencies in the index files kept at the Maemo servers (I think). I kept getting these "Failed to fetch http://someurl Size mismatch" errors. An example:
...
Failed to fetch http://repository.maemo.org/pool/maemo5.0/nokia-binaries/4bc37c7c77ebe90177c050b805a8dc79/nokia-binaries/h/hildon-application-manager-l10n/hildon-application-manager-l10n-plpl_6.0+r7389+0m5_all.deb Size mismatch
Failed to fetch http://repository.maemo.org/pool/maemo5.0/nokia-binaries/4bc37c7c77ebe90177c050b805a8dc79/nokia-binaries/h/hildon-application-manager-l10n/hildon-application-manager-l10n-ptpt_6.0+r7389+0m5_all.deb Size mismatch
Failed to fetch http://repository.maemo.org/pool/maemo5.0/nokia-binaries/4bc37c7c77ebe90177c050b805a8dc79/nokia-binaries/h/hildon-application-manager-l10n/hildon-application-manager-l10n-ruru_6.0+r7389+0m5_all.deb Size mismatch
Failed to fetch http://repository.maemo.org/pool/maemo5.0/nokia-binaries/4bc37c7c77ebe90177c050b805a8dc79/nokia-binaries/h/hildon-application-manager-l10n/hildon-application-manager-l10n-svse_6.0+r7389+0m5_all.deb Size mismatch
Failed to fetch http://repository.maemo.org/pool/maemo5.0/nokia-binaries/4bc37c7c77ebe90177c050b805a8dc79/nokia-binaries/h/hildon-theme-alpha/hildon-theme-alpha_9.1+0m5_all.deb Size mismatch
Failed to fetch http://repository.maemo.org/pool/maemo5.0/free/m/maemo-launcher/maemo-launcher_0.35-7+0m5_i386.deb Size mismatch
Failed to fetch http://repository.maemo.org/pool/maemo5.0/free/m/maemo-launcher/maemo-launcher-dev_0.35-7+0m5_all.deb Size mismatch
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
To avoid these errors I manually downloaded the packages involved and installed them with dpkg. To make this process easier I simply copied the error messages into a text file, removed the "Failed to fetch" prefix and the "Size mismatch" postfix from all lines using search/replace, and saved this as files.txt. Then I invoked the following commands from within my scratchbox targets:
cat files.txt | xargs wget
dpkg -i *.deb
And voila, it works ;-)