views:

72

answers:

1

I am developing a WYSIWYG document editor. The editor is build around the Qt framework because Linux/KDE is the main platform it will run on.

I have been using the QTextEdit widget so far and have gotten basic text editing and formatting in. However I've come to a standstill on how to implement the following requirement:

A document is split into multiple levels of headings and there must be a automatically generated table of contents at the top.

You should be able to select a style for a paragraph of text (heading 1, heading 2, ..., paragraph) and if you select a heading style then the line is prefixed with a section number.

For example, a paragraph containing the text "Design Specification" with the style 'heading 3' might be displayed as:

3.4.2 Design Specification

Where the prefixed section number '3.4.2' was automatically generated. The section number should update as headers are inserted and removed above it. The user should not be able to modify or remove this prefix themselves. If they do remove the prefix then the paragraph should revert back to the 'paragraph' style rather than remain a header.

As the headers are inserted/modified/removed the table of contents should automatically be updated. The user also should not be able to modify or delete the table of contents.

I am stuck at how to implement that functionality into my editor. I was thinking of perhaps of storing a tree of uneditable ranges (ranges that include the table of contents and each of the prefixes) and each time the cursor position/current selection changes I will set the QTextEdit control to readonly if it the selection overlaps or the cursor is inside of a range. But then there is the issue of keeping all of these ranges up to date (rebuilding the tree) every time something is modified, which would be quite often in a WYSIWYG editor.

I am stuck at how to proceed and I will appreciate any advice on this matter.

A: 

IMO you shouldn't need to add those numbers with an editor: just declare them using CSS and let the HTML renderer add/display them.

ChrisW