Skip to main content

Ralsina.Me — Roberto Alsina's website

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

Manuel Muradas / 2009-09-22 17:11:

Se me ocurrió un nombre muymuymuy feo:
FaltaYRst2 (se leería: falta y restu)

Roberto Alsina / 2009-09-22 18:03:

Tenes razón es muy feo! :-D

Manuel Muradas / 2009-09-22 23:04:

Yo te avise =)

kib2 / 2009-09-23 12:57:

Hi,

I had made a Text Editor with Qt a long time ago. It has line numbers, snippets, custom syntax highlighting rules, and some more things.

I don't use Python anymore, but you may check the sources here :

http://kib2.free.fr/PyK/

Unfortunatly I had to stop it and there was a compatibilty issue with a new Qt versions. So the code won't run, but the editor sources are good.

Also, I noticed speed problems with big files.

Hope it helps.

Roberto Alsina / 2009-09-23 14:25:

@kib2 that's **very** impressive!

kib2 / 2009-09-23 21:07:

Hi Roberto,

thanks! Just do whatever you want with it.

See you:

Kib².


Contents © 2000-2024 Roberto Alsina