Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, October 2006
QTextEdit subclass: design question (Qt4)


Message 1 in thread

Dear list,

I've built a widget for displaying and editing mathematical expressions, and
it can create QPictures or QPixmaps of those expressions.  Now I want to
start putting such expressions in QTextEdits, and making them editable.  To
put them in QTextDocuments is no problem, because I can make pixmaps, and
reimplement QTextDocument::loadResource().  But to make them editable is a
different matter.

I see that QTextEdit can show images, but I have never before created an
editor by subclassing QTextEdit.  So before I start doing things The Wrong
Way, I'm writing to ask design advice, or for some guidance from people who
have done similar stuff.

I aim to implement these features:

    - My math expressions appear inline, like an image would in HTML if you
      had <p>blah blah <img src='...'> blah blah</p>.  In fact, I can just
      do <img src='...'> and the loadResource() trick above if needed.

    - Copy and paste should work as one would expect.  It seems that a
      simple reimplementation of the copy() and paste() members of QTextEdit
      will handle this, although I haven't yet looked at the details.

      *** THIS IS THE PART I REALLY WANT TO KNOW HOW TO DO. ***
    - You double click on one and it edits in-place.  That is, a little
      edit widget of equal size with the image opens right on top of it,
      and goes away when it loses focus, or you press enter or something.

Regarding this last point, I don't see functionality in QTextEdit that can
tell me what I double-clicked on.  I could try to hack this together, maybe
mimicking code from QTextControl::anchorAt() and/or
QAbstractTextLayout::anchorAt(), but much of that stuff is not in the public
API.  It might still be possible, but as soon as I start thinking along
these lines, I begin to think I'm doing it The Wrong Way.

Would that there were some way for inserting my own custom character
entities and receiving events when they got double-clicked!  If there is
not, perhaps I will make one via the hacks I suggest above, but I'd rather
not do so until I find out if I'm missing something.

Perhaps someone has done this with images (instead of mathematical
expressions) or something similar before and can give me some pointers?
Thanks in advance.

Nathan


--
 [ signature omitted ] 

Message 2 in thread

>       *** THIS IS THE PART I REALLY WANT TO KNOW HOW TO DO. ***
>     - You double click on one and it edits in-place.  That is, a little
>       edit widget of equal size with the image opens right on top of it,
>       and goes away when it loses focus, or you press enter or something.
> 
> Regarding this last point, I don't see functionality in QTextEdit that can
> tell me what I double-clicked on.  I could try to hack this together, maybe
> mimicking code from QTextControl::anchorAt() and/or
> QAbstractTextLayout::anchorAt(), but much of that stuff is not in the public
> API.  It might still be possible, but as soon as I start thinking along
> these lines, I begin to think I'm doing it The Wrong Way.

Let me get more specific.  It seems that I might be able to find out what
was clicked using the following combination of stuff in the public API,
although I haven't tried yet so it would still be great to get advice from
someone who has.

    QTextDocument::documentLayout()
    QAbstractTextDocumentLayout::hitTest()
    QTextCursor::setPosition()
    QTextCursor::charFormat()
    QTextFormat::stringProperty()
    QTextFormat::ImageName

But this doesn't help me figure out what the bounding rect of one of my
images is in order to then place a widget over it.  I can get the bounding
rect for a QTextBlock, but I don't think that an image is a whole block,
just part of one, so that doesn't solve my problem.

This is a whole new arena for me.  Expert advice welcome!

Nathan


--
 [ signature omitted ] 

Message 3 in thread

> Dear list,

Hi Nathan.

> I've built a widget for displaying and editing mathematical expressions,

No a trivial task as far as I can tell...

> and it can create QPictures or QPixmaps of those expressions. 

> Now I want to start putting such expressions in QTextEdits, and making them 
> editable.  To put them in QTextDocuments is no problem, because I can make 
> pixmaps, and reimplement QTextDocument::loadResource().  But to make them 
> editable is a different matter.

Are you bound to using QTextEdit? QTextEdit's does not seem to be
intented as a gerneral purpose canvas for custom widgets, it rather
tries to convert everything to something 'static' (text or image).

And do you need to implement it yourself?

Implementing a kind of text editor with embedded formula editing
is something that has been done before, see e.g. www.lyx.org

Regards,
Andre'

--
 [ signature omitted ] 

Message 4 in thread

André Pönitz wrote:

>> I've built a widget for displaying and editing mathematical expressions,
> 
> No a trivial task as far as I can tell...

It was not, but it wasn't too bad either.  The public domain images here
were very helpful:
    http://us.metamath.org/symbols/symbols.html

>> Now I want to start putting such expressions in QTextEdits, and making them
>> editable.  To put them in QTextDocuments is no problem, because I can make
>> pixmaps, and reimplement QTextDocument::loadResource().  But to make them
>> editable is a different matter.
> 
> Are you bound to using QTextEdit? QTextEdit's does not seem to be
> intented as a gerneral purpose canvas for custom widgets, it rather
> tries to convert everything to something 'static' (text or image).

I'm not bound to using a QTextEdit, but I do want to have nice rich text
with math in it.  I don't mind if my math expressions get treated as images
for display purposes.  What I really need is to know when someone
double-clicks one, and what its bounding rect is, so I can slap an editor on
it.

> And do you need to implement it yourself?
> 
> Implementing a kind of text editor with embedded formula editing
> is something that has been done before, see e.g. www.lyx.org

Well I'm looking to embed this in a larger project.  I'm not trying to make
a math text editor; rather I need one as a widget in something completely
different.  Furthermore, I care a good deal about the internal
representation of the expressions, for the use to which I want to put them,
and thus someone else's solution may very well not jive with mine.

I have seen Lyx, however.  It's based on TeX, and thus makes very pretty
output.  However since it is a typesetter, it is more concerned with the
appearance of the mathematics than its meaning.  For instance, does
    x \bigoplus y
mean the symmetric difference of x and y or the tensor product?  Who knows?
\bigoplus shows up all over the place in math.  I need an internal
representation that makes the meaning clear; my editor widget keeps an
unambiguous internal meaning, but displays it in whatever way is
appropriate.

Nathan


--
 [ signature omitted ] 

Message 5 in thread

Nathan Carter wrote:
> I don't mind if my math expressions get treated as images
> for display purposes.  What I really need is to know when someone
> double-clicks one, and what its bounding rect is, so I can 
> slap an editor on it.

What about intercepting the doubleClick either by subclassing or
installing an event filter on the QTextEdit, use 

  QTextCursor QTextEdit::cursorForPosition(QPoint) 

and QTextCursor::position() to get hold of the postion in the
document which than could be "somehow" mapped to your math
data...

> I have seen Lyx, however.  It's based on TeX, and thus makes very pretty
> output.  However since it is a typesetter, it is more concerned with the
> appearance of the mathematics than its meaning.

Sort of true wrt to the '_more_ concerned', but it doesn't mean
it's not able to handle semantics as well.

>  For instance, does
>     x \bigoplus y
> mean the symmetric difference of x and y or the tensor 
> product?  Who knows?
> \bigoplus shows up all over the place in math. I need an internal
> representation that makes the meaning clear; my editor widget keeps an
> unambiguous internal meaning, but displays it in whatever way is
> appropriate.

You are free to define macros, i.e. the equivalent of TeX's

 \def\symdiff#1#2{#1\bigoplus#2}
 \def\tprod#1#2{#1\bigoplus#2}

and use only \symdiff and \tprod in your document.

Output is identical, and the semantics is stored in the doc
as well. In fact, this is even the prefered method as you
are then able to easily replace all 'sydiff oplus' by, say,
\def\symdiff#1#2{#1\bigtriangleup#2} if your editor/supervisor
/whatever tells you so.

Andre'

--
 [ signature omitted ]