Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about qt (old posts, page 4)

It shall be called Bookrest, and it has an outline view.

Yes, the pro­gram known so far as "my rst2pdf ed­i­tor/pre­view­er ap­pli­ca­tion" is now called Bookrest.

What's a bookrest? It's a thing you put a book on.

Why Bookrest? I hope some­day peo­ple will have books open in bookrest. Plus, it ends with "rest", which is the pre­ferred ab­bre­vi­a­tion for re­Struc­tured Tex­t.

And what's the out­line view? It's a click­able tree with the out­line of the doc­u­men­t, of course.

As usu­al, let's go to the video:

The back­ground ren­der­ing was done us­ing python's awe­some mul­ti­pro­cess­ing mod­ule.

rst2pdf previewer: a new feature

I am in the mid­dle of that hon­ey­moon you get start­ing a new ap­p. Ev­ery new fea­ture seems tobe just 50 lines of code away, there is no lega­cy code (in fac­t, you are cre­at­ing that lega­cy code), and you learn new tricks all the time.

So, I did a new fea­ture to­day.

A day or two ago, my ed­i­tor start­ed show­ing a yel­low bar high­light­ing the cur­rent line.

But then I though... would­n't it be more use­ful to have a sim­i­lar bar fol­low­ing you in the PDF?

That way, when you are on a giv­en line, you can im­me­di­ate­ly see where you are in the out­put. Neat, right?

Here is a video show­ing it:

Sad­ly it's not per­fec­t, and prob­a­bly nev­er will be be­cause of do­cu­tils lim­i­ta­tion­s, but it's pret­ty nice!

More fun with rst2pdf, poppler and PyQt

First: I re­al­ly, re­al­ly need a name for this thing. I am tired of say­ing "my rst2pdf pre­view­er/ed­i­tor ap­p".

Now, here's a video of how it looks nowa­days af­ter all the yak shav­ing (sor­ry about my en­glish, I write lots of it, but nev­er speak it):

As you can see, the ba­sic app is fair­ly com­plete, even if it lacks all the ameni­ties that would make some­one use it (like, search? save? ;-).

It has one big prob­lem, though: I can't pub­lish it yet.

Why? Be­cause I need to use pop­pler from PyQt, and the code I found to do it has no li­cense (see the code).

I am try­ing to con­tact the au­thor (Ra­jeev J Se­bas­tian), so there should be news soon!

As soon as that's cleared, the PDF wid­get is a whole post by it­self, be­cause it's pret­ty neat, if I may say so my­self.

UP­DATE: the bind­ing is now un­der MIT li­cense, thanks to Ra­jeev Se­bas­tian!

Yak Shavings for September 22, 2009

yak shaving

(id­iomat­ic) Any ap­par­ent­ly use­less ac­tiv­i­ty which, by al­low­ing you to over­come in­ter­me­di­ate dif­fi­cul­ties, al­lows you to solve a larg­er prob­lem.

This yak is start­ing to look bet­ter.

For my sec­ond pile of yak shav­ings: turn­ing QPlain­TextE­d­it in­to a de­cent edit­ing wid­get for pro­gram­mer­s.

As work ad­vanced in my rst2pdf ed­i­tor (BTW: need a name!), it be­came ob­vi­ous that the piece of the UI the us­er will use most is just a cou­ple of plain text ed­i­tors.

Qt comes with a wid­get for that, of course, called QPlain­TextE­d­it. How­ev­er, it's a very, very bad wid­get for pro­gram­mer­s.

Here's the least I want­ed:

  1. Syn­­tax high­­­light­ing for two lan­guages: re­struc­­tured text and javascrip­t. This yak is al­ready shaved.

  2. Line num­bers

  3. Cur­rent line high­­­light

  4. Er­ror high­­­light when it makes sense (like, in the stylesheet)

One way to achieve this would be to dump QPlain­TextE­d­it and use QS­ciScin­til­la which is the ba­sis for the code ed­i­tor in er­ic and (an­oth­er ver­sion) in scite.

How­ev­er, I ex­pe­ri­enced a bad bug in QS­ciScin­til­la, where I can't type ac­cent­ed char­ac­ter­s! With­out that, (de­cen­t) span­ish is im­pos­si­ble, and the bug seems to be at least two years old, so... no go.

So, did I get those fea­tures? I say yes!

Here is the video (yes, I am get­ting ad­dict­ed to mak­ing the­se, since qt-record­my­desk­top makes them so easy ;-):

The ba­sis for this is the Code Ed­i­tor ex­am­ple that comes with Qt it­self, plus a bit of my own handy­work.

First, I port­ed Code Ed­i­tor from C++ to Python, which was very sim­ple and took a few min­utes. That takes care of points 2 and 3.

Then, the syn­tax high­light was plugged in, which was point 1.

Then, how about re­al­time javascript val­i­da­tion? Easy us­ing sim­ple­j­son! Just make sure to run this when­ev­er you want val­i­da­tion (I do it on ev­ery keystroke).

Re­place self­.ui.style.­toPlain­Text with what­ev­er your wid­get is called, of course:

def validateStyle(self):
    style=unicode(self.ui.style.toPlainText())
    #no point in validating an empty string
    if not style.strip():
        return
    pos=None
    try:
        json.loads(style)
    except ValueError, e:
        s=str(e)
        print s
        if s == 'No JSON object could be decoded':
            pos=0
        elif s.startswith('Expecting '):
            pos=int(s.split(' ')[-1][:-1])
        else:
            print 'UNKNOWN ERROR'

    # This makes a red bar appear in the line
    # containing position pos
    self.ui.style.highlightError(pos)

highlightError(pos) simply stores pos in the Code Editor, which will draw a red bar in that line, the same way it highlights the current line.

And that's it. Here is the code for codeed­i­tor.py

Having a little fun with poppler, PyQt and rst2pdf

In­spired by a post by An­dré Roberge I want­ed to see if rst2pdf was too slow to be used for re­al-­time pre­views in a re­struc­tured text ed­i­tor.

It would al­so be very use­ful, for ex­am­ple, as a way to test stylesheet changes, mak­ing rst2pdf much more use­ful in gen­er­al.

And af­ter a cou­ple of hours of gen­tle hack­ing, you know... it does­n't suck at all. I im­ple­ment­ed the (still very prim­i­tive) PDF view­er us­ing a python/pop­pler/Qt bind­ing I found via google, the UI is PyQt.

Here's the video:

A note: the video was record­ed us­ing qt-record­my­desk­top and that pro­gram is awe­some. It was triv­ial to do.

I ex­pect this will not be good enough when long doc­u­ments are pro­cessed, but the rst2pdf man­u­al (about 25 pages) ren­ders in 5 sec­ond­s.


Contents © 2000-2024 Roberto Alsina