PDA

View Full Version : Problems implementing Google Chrome's search



seand
13th September 2008, 08:38
Hi,

I'm trying to implement a search feature much like Google Chrome has, where keywords found are highlighted in the document, and the vertical scrollbar is decorated with very thin lines, corresponding to the entire height of the document. I found this feature to be quite nice, and would like to put it inside my application as well :)

I've been trying many different ways to easily scale an item into the proper position on the scrollbar, so that a line can be drawn across its width (indicating the location of a found keyword). Here is an example picture for those who haven't used Google Chrome yet:

http://img382.imageshack.us/img382/5867/chromesearchdn0.th.png (http://img382.imageshack.us/my.php?image=chromesearchdn0.png)

Anyway, my current method of attack to this problem is to get the full height of the document using QPlainTextEdit::cursorRect() (putting the cursor at the bottom), getting the partial height of the currently found keyword using the same method, getting a ratio of partialHeight / fullHeight, and then multiplying that ratio by the height of the scrollbar in order to find the position to draw the line. However, this isn't working too well. It seems as though the Y coordinate returned from cursorRect() is dependent on the size of the viewport (if I make it longer, then the Y coord returned is greater).

So, basically, I'm wondering a couple things:

1) Is there some special secret to viewport coordinates I don't know about? I currently have little understanding of them, and can't find anything about them in the docs.

2) Is there a better way of doing what I'm trying to do? As a backup solution, I'm considering using QPlainTextEdit::blockCount() and QTextCursor::blockNumber() and taking the ratio of blockNum / blockCount and using that to get the position in the scrollbar, but that will be far less accurate, because i have several blocks that have multiple lines in them.


Thanks,
Sean

seand
14th September 2008, 05:11
For future reference, I solved my problem (or at least found a good solution) by iterating through all the blocks in the QPlainTextEdit's document, and incrementing a variable called documentHeight by each block's height (using QRectF). When a block containing the searched keyword(s) was found, I would save the current documentHeight in a variable (indicating the current block's height), so I would be able to divide it by the total documentHeight when the loop was finished, and would then multiply that ratio by the height of the scrollbar's slider area to find a position in which to draw the line.

- seand