(idiomatic) Any apparently useless activity which, by allowing you to overcome intermediate difficulties, allows you to solve a larger problem.
This yak is starting to look better.
For my second pile of yak shavings: turning QPlainTextEdit into a decent editing widget for programmers.
As work advanced in my rst2pdf editor (BTW: need a name!), it became obvious that the piece of the UI the user will use most is just a couple of plain text editors.
Qt comes with a widget for that, of course, called QPlainTextEdit. However, it's a very, very bad widget for programmers.
Here's the least I wanted:
Syntax highlighting for two languages: restructured text and javascript. This yak is already shaved.
Line numbers
Current line highlight
Error highlight when it makes sense (like, in the stylesheet)
One way to achieve this would be to dump QPlainTextEdit and use QSciScintilla which is the basis for the code editor in eric and (another version) in scite.
However, I experienced a bad bug in QSciScintilla, where I can't type accented characters! Without that, (decent) spanish is impossible, and the bug seems to be at least two years old, so... no go.
So, did I get those features? I say yes!
Here is the video (yes, I am getting addicted to making these, since qt-recordmydesktop makes them so easy ;-):
The basis for this is the Code Editor example that comes with Qt itself, plus a bit of my own handywork.
First, I ported Code Editor from C++ to Python, which was very simple and took a few minutes. That takes care of points 2 and 3.
Then, the syntax highlight was plugged in, which was point 1.
Then, how about realtime javascript validation? Easy using simplejson! Just make sure to run this whenever you want validation (I do it on every keystroke).
Replace self.ui.style.toPlainText with whatever your widget is called, of course:
defvalidateStyle(self):style=unicode(self.ui.style.toPlainText())#no point in validating an empty stringifnotstyle.strip():returnpos=Nonetry:json.loads(style)exceptValueError,e:s=str(e)printsifs=='No JSON object could be decoded':pos=0elifs.startswith('Expecting '):pos=int(s.split(' ')[-1][:-1])else:print'UNKNOWN ERROR'# This makes a red bar appear in the line# containing position posself.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.
(idiomatic) Any apparently useless activity which, by allowing you to overcome intermediate difficulties, allows you to solve a larger problem.
And boy, are my yaks hairy!
I started trying to do a rst2pdf stylesheet editor (see here).
One thing lead to another, and I have now at least three interesting mini-projects because of it.
Here's today's: abuse pygments to use it as a generic syntax highlighter in a Qt interface.
Why pygments? Because it's the only reStructured Text highlighter I found. That's probably because reSt is pretty damn hard to highlight!
AFAIK, this is the first time anyone has managed to use pygments for this, in an editable window. And there are good reasons for that:
It's pure python, so maybe you expect it to be too slow
It doesn't do partial or progressive lexing, so you need to lex the whole thing (again, maybe you expect it to be too slow)
It has a file-oriented API, it generates a file with all the formatting in it, and for this kind of thing we need to access stuff in the middle of the data.
So, of course, it turns out it works pretty well, as you can see in this video:
Lesson learned: computers are fast nowadays.
Here's the code for highlighter.py with extensive comments.
You can just run it and get the same demo you saw on the video (minus the typing ;-)
Inspired by a post by André Roberge I wanted to see if rst2pdf was too slow to be used for real-time previews in a restructured text editor.
It would also be very useful, for example, as a way to test stylesheet changes, making rst2pdf much more useful in general.
And after a couple of hours of gentle hacking, you know... it doesn't suck at all. I implemented the (still very primitive) PDF viewer using a python/poppler/Qt binding I found via google, the UI is PyQt.
Here's the video:
A note: the video was recorded using qt-recordmydesktop and that program is awesome. It was trivial to do.
I expect this will not be good enough when long documents are processed, but the rst2pdf manual (about 25 pages) renders in 5 seconds.