Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

New Bartleblog Feature: Menu Editor

Took a while to im­ple­men­t, but Bartle­Blog fi­nal­ly got a func­tion­al menu ed­i­tor:

bartleblog12.png

Right now, it on­ly works with the mootool­s-based menu gad­get, but I will start work­ing on the ya­hoo menu ver­sion in a mo­men­t.

The on­ly thing not work­ing is the pre­view but­ton, be­cause it needs more sup­port on the back­end side.

Python Trick: Save anything in config files

The Python con­fig ob­jects are con­ve­nient and sim­ple, but they have a prob­lem: you can on­ly save strings. That means you need to store num­bers as strings and re­mem­ber to use the get­int()/get­float() meth­ods (or co­erce by hand!), which is er­ror prone and an­ti-python­ic. Stor­ing a list is even ugli­er.

You could store ascii pick­les, but those are pret­ty un­pleas­ant to read in some cas­es.

Here's my so­lu­tion: En­code it us­ing a JSON en­coder first! (I am us­ing demj­son)

Sil­ly ob­vi­ous code frag­men­t:

def getValue(section,key,default=None):
    try:
        return JSON().decode(conf.get (section,key))
    except:
        return default

def setValue(section,key,value):
    value=JSON().encode(value)
    try:
        r=conf.set(section,key,value)
    except ConfigParser.NoSectionError:
        conf.add_section(section)
        r=conf.set(section,key,value)
    f=open(os.path.expanduser('~/.bartleblog/config'),'w')
    conf.write(f)
    return r

With just a lit­tle ef­fort you can have a read­able ascii typed python con­fig file.

Today's first hour of hacking...

... has been all about UI.

I have al­ways had a prob­lem when writ­ing PyQt app­s: stock icon­s.

Which ones should I use? Where are they?

I usu­al­ly fished through the crys­talsvg icon set un­til I found one that seemed to be what I need­ed, and then copied it to my ap­p.

Sad­ly, that's an­noy­ing in sev­er­al ways:

  1. Since those are PNG icon­s, you need to find the right size.

  2. Not all icons are there for all sizes!

  3. Be­­cause of 2, I need to check three or four fold­ers to see all the icon­s.

So, I de­cid­ed to cut my loss­es, and see what else could be done. And here it is:

bartleblog11.png

I am now us­ing all SVG icon­s, from the rein­hardt set that will look equal­ly out of place in all OS­s, but which I like (and I think look awe­some with this re­laxed Domi­no the­me). And be­cause they are all SVG, I don't care about sizes, and they are all in the same place, and all is good.

And when­ev­er Oxy­gen is re­leased, all I need to do is switch the files around and that's that. Which is nice, too.

Of course there is a catch... it does look out of place, and I ex­pect many to find it ug­ly. So what, since I am the on­ly us­er of this ap­p! ;-)

Today's two hours of hacking

  • Done with the main blog con­­fig di­a­log.

  • Fixed a dozen bugs

  • Gen­er­ate the blog in a rea­­son­able place

  • Fixed a lot of UI bugs (tab or­der­s, sizes)

Still lots and lots of things to be done, tho.

Making your QTextBrowser show remote images

It's re­mark­ably easy to turn your QTextBrows­er in­to a lim­it­ed web browser, at least good enough to show im­ages from the we­b.

Here's all the code:

from PyQt4 import QtCore,QtGui
import urllib, os, md5

class PBrowser(QtGui.QTextBrowser):
    def loadResource(self, type, name):
        url=unicode(name.toString())
        ret=QtCore.QVariant()
        if url.startswith('http://'):
            dn=os.path.expanduser('~/.bartleblog/cache/')
            if not os.path.isdir(dn):
                os.mkdir(dn)
            m=md5.new()
            m.update(url)
            fn=os.path.join(dn,m.hexdigest())
            if not os.path.isfile(fn):
                urllib.urlretrieve(url, fn)
            ret=QtGui.QTextBrowser.loadResource(self, type, QtCore.QUrl(fn))
        else:
            ret=QtGui.QTextBrowser.loadResource(self, type, name)
        return ret

And here's bartle­blog tak­ing ad­van­tage of it:

bartleblog10.png

It even has a prim­i­tive cache and ev­ery­thing ;-)


Contents © 2000-2024 Roberto Alsina