PDA

View Full Version : Html text bounding rectangle



bunjee
12th May 2008, 21:49
/* virtual */ const unsigned int ZeInfoItem::UpdateHeight(const unsigned int width)
{
QTextDocument document;
document.setHtml(mTextComponent.getText());
document.setTextWidth(width);

mHeight = document.size().height();
return mHeight;
}

Here is a function to retrieve the height of an HTML text bounding rectangle.
Unfortunately it's quite slow, when called repetitively.

Do you have a better solution ?
Thanks.

wysota
12th May 2008, 22:33
Unfortunately it has to be slow, because the document has to be laid out. Try reducing the number of iterations.

patrik08
13th May 2008, 08:18
Befor yo update the document.setTextWidth(width);
check the exist width ... large image or table ..
and its become slow if paitdevice go outside paint the width..

QGraphicsTextItem , QTextEdit or QTextBrowser, use a private class name QTextControl
this class is very large ....

to paint and edit text you need only QTextDocument,QTextCursor,QAbstractTextDocumentLay out

show and test http://www.qt-apps.org/content/show.php/GraphicsViewEdit+Layer?content=80234




/* QTextDocument *_d; */
QRectF TextWriter::boundingRect() const
{
if (_d) {
_d->adjustSize(); ///// !!!!!!
QAbstractTextDocumentLayout *Layout = _d->documentLayout();
return Layout->frameBoundingRect(_d->rootFrame());
} else {
return QRectF();
}
}

bunjee
13th May 2008, 17:21
Thanks for replying,
I'm not sure I understood.

What impementation do you propose ?

bunjee
14th May 2008, 23:47
painter.setRenderHint(QPainter::Antialiasing, false);

QTextDocument & document = getDisplayDocument();
QFont & font = getDisplayFont();
font.setFamily("Arial");
font.setPointSize(8);
document.setDefaultFont(font);
document.setHtml(text);
ZeSmileyController::get()->InsertSmiley(document);
document.setTextWidth(textRect.width());

QAbstractTextDocumentLayout::PaintContext & context = getDisplayContext();
context.clip = QRect(0, 0, textRect.width(), textRect.height());
painter.translate(textRect.x(), textRect.y());
document.documentLayout()->draw(&painter, context);

painter.restore();

I've come up with that implementation to display an HTML text paragraph.

I'm trying to display an ItemView of HTML items.

The performance are terrible at this point. Anyone has any idea to optimize this ? The paint event simply cannot lay the document at every painting event.

wysota
15th May 2008, 08:47
Try caching as much as you can. If the width didn't change, there is no need to rebuild the layout. I remember implementing a rich text delegate and it worked fine, so yours should too. You can also take a look at QwwRichTextButton from wwWidgets, it uses a QTextDocument internally to do rich text rendering, maybe you can steal some of my code...

bunjee
15th May 2008, 10:39
Thanks wysota,

I've take a look at QwwRichTextButton source code.
Nice widget extension you've got here.

The biggest difference between your code and mine is that you "cache" the QTextDocument in every QwwRichTextButton.

In my case since I plan displaying like 10 000 items like this.
So in a widget context it's working fine since you'll draw one or two of these.
But in an massive itemview list you cannot do that.

Or I have to create a "pool" of textdocument :/. That's a lot of work just to display html paragraphes.

I guess if you can make a compilable example displaying an itemview of 10 000 paragrahes items without caching it I would shut my mouth :).

wysota
15th May 2008, 11:15
In my case since I plan displaying like 10 000 items like this.
So in a widget context it's working fine since you'll draw one or two of these.
But in an massive itemview list you cannot do that.
Yes, that's true, but you can still cache the results as pixmaps and only regenerate them when it is really needed.


I guess if you can make a compilable example displaying an itemview of 10 000 paragrahes items without caching it I would shut my mouth :).

Sorry, no time to do that now :)

bunjee
15th May 2008, 11:25
Pixmap buffering sounds like a good idea.

Where should I start ?