Results 1 to 20 of 30

Thread: XML, Highlighting and Model/View

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    Hi,
    I had thought of the answer you told. And the problem is xml highlighting !
    If I simply use treewidget, can I set xml highlighting on it ? I guess I need to use delegates for that, isnt it ?

    Or is there some easy way out

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: XML, Highlighting and Model/View

    One things that will probably work (not sure if it's the best solution) would indeed be to set a delegate on the QTreeView. No need to subclass it.
    Then override the delegates paint method.

    create a QTextDocument, a QSyntaxHighlighter for it and set the (fragment for the QModelIndex of the) XML as plain text (in the QTextDocument).

    Will probably work. (I did something like that once.)
    Perhaps not efficient, though... best give it a try (and tell us about it!)

    HTH

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    I made the treeview of xml subclassing treewidget and treewidgetitem.
    Now I can extract path of the node relative to the root element. Also i can change font of the tree widget item.

    But still havent started code for delegate.
    Before I start it, I haev a question, are delegates called on double click ?? If so it wont help me. I want to render the xml formating on that start itself.

    Also my code is working quite slow, I need to check whats wrong, and am worried how slow it will become even if i set delegate

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: XML, Highlighting and Model/View

    The delegate does
    * painting of all cells (this has nothing to to with mouse clicks)
    * and the delegate is also responsible for creating editors (if the model is editable)

    Use valgrind or something like it to profile your application.

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    painting of all cells (this has nothing to to with mouse clicks)
    How then in Spin box delegate example, do we have to double click to see the spin box ??
    or is it because of the createEditor function ?? Can anyone explain ?

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: XML, Highlighting and Model/View

    the spinBox has nothing to do with drawing; it is (as you guessed) the editor created by createEditor.
    The cells that are not edited do not show a spin box!

    paint: looks for a cell that is not being edited
    createEditor: the editor (which often is laid over the edited cell, giving the impression of a different painting while being edited. it is just an impression. the cell is always painted by paint.)

    HTH

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    Well, started reading Model/View architecture in Qt.. but I wanted some immediate results.
    So I tried the following -
    copied the simple DOM example, and modified it that I show attributes in column 0 along with tag name. So something like an XML Tree. Now moving on with delegates, made a little progress.
    I overrided the drawDisplay method of QItemDelegate -
    Qt Code:
    1. void XMLDelegate::drawDisplay ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text ) const
    2. {
    3. //QItemDelegate::drawDisplay(painter,option,rect,text);
    4.  
    5. QColor colors[3] = {Qt::black,Qt::red,Qt::blue};
    6. QColor red(Qt::red);
    7. QColor blue(Qt::blue);
    8. QColor black(Qt::black);
    9.  
    10. QString textToDisplay = option.fontMetrics.elidedText(text,Qt::ElideNone,rect.width());
    11. int len = textToDisplay.length();
    12. int x,y;
    13. x = rect.x();
    14. y = rect.y()+10;
    15. for(int i=0;i<len;i++)
    16. {
    17. painter->setPen(colors[i%3]);
    18. painter->drawText(x,y,QString(textToDisplay[i]));
    19. x += option.fontMetrics.width(textToDisplay[i]);
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    By this am able to display text in alternate colors. Now thing is,,, I havent handled it for Wrap mode . And without that one display wont look good ...

    Isnt there some simple way to do this ? Currently to display xml, we use Internet explorer and Navigate function for a xml file. It does display the xml with highlighting. But I want to add context menu to sucha tree, and be able to compute xpath of the node clicked...


    Can someone suggest a simpler way ?? Or do I have to do it the hard way

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML, Highlighting and Model/View

    Could you provide a screenshot or a mockup of what you want? I think your problem is easy to solve, but first I need to make sure what you want

  9. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    Even I think the problem must be easy to solve, may be am moving in wrong direction , may be not

    Well, I will restate my problem with snapshots and also give an idea as what I am able to achieve till now.

    First, the problem :
    I need to display xml data as it displays in internet explorer. However, additionaly I want to be able to show a context menu and be able to extract the Xpath of the node that was right clicked.
    Refer : required functionality.jpg
    I know I can achieve this by -
    1) Make a tree out of xml
    2) apply syntax highlighter to each row
    3) Handle multi line
    But I dont know whats the best solution to achieve above steps


    Now the things I have tried till now :
    1) I subclassed QTreeWidget and QTreeWidgetItem and made a Tree from XML Data. This works fine, shows the data like in explorer. I override drawBranches() of treewidget.
    But problem is, I can show it only in one Color.

    2) I modified the Simple DOM Model example, and returned text in column 0 only instead of Column 1 and 2. I also made a custom delegate where I overrided drawDisplay() and was able to show text in different colors. Problem with this solution is, I see row text in single line only, and the text is clipped if its longer than the rect width.
    Refer try 2.jpg

    Now how do I handle multiple line in a single cell ?? Like in iexplorer, if we resize the window, the xml data gets wrapped properly. How do I achieve this functionality ??
    Attached Images Attached Images

  10. #10
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: XML, Highlighting and Model/View

    assuming you do the drawing with QTextDocument; use setTextWidth, pass the size of the rectangle passed to QAbstractItemDelegate:aint (in QStyleOptionViewItem)

    HTH

  11. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, Highlighting and Model/View

    Dont assume !!
    Am not using QTextDocument. If only I cud get QtextDocument for a model index, i cud have set the syntax highlighter for it.

    What I am doing is setting a ItemDelegate for the Tree View in Simple DOM Model example.
    am ONLY overriding ItemDelegate::drawDisplay() function. The code is same as posted few posts back.

    Now say I have text = "Hello, How are you";
    If i need to display each word in different color, I need to first write Hello with one color, then get the width occupied by Hello, and write How with other color and so on. I just cant simply write the whole text in one go Can I ??
    Also see drawDisplay() in QItemDelegate.cpp. There are many things to consider, which are going above my head for the time being

    Also I made a small parser inside the drawDisplay() function, and am able to get xml like highlighting
    But prob remains... single line doesnt gets wrapped..
    Another prob is, values are treated as child nodes, So if i want to display text vlaue of a node like <node> nodeValue </node> , am not able to do it. I will have to look in model how to prevent adding row for #text nodes

  12. #12
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: XML, Highlighting and Model/View

    ok, how about

    i) using a QItemDelegate in the QTreeWidget approach, too?

    ii) pass Qt::TextWordWrap to QPainter::drawText

    in YourItemDelegate:aint(...):
    I would not do the algorithm for drawing colored text myself (unless I had to, that is).
    Doesn't a simple
    Qt Code:
    1. painter->translate(option.rect.topLeft());
    2. QSyntaxHighlighter yourHighlighter(&doc);
    3. // if you want wrapping text
    4. // doc.setTextWidth(option.rect.width());
    5. QString text=index.data().toString(); // assuming you can get the xml to display that way
    6. doc.setHtml(text);
    7. doc.drawContents(painter);
    To copy to clipboard, switch view to plain text mode 
    work for you?
    (Maybe it is inefficient, but it is easy to implement. And maybe it is fast enough, too.

  13. The following user says thank you to caduel for this useful post:

    aamer4yu (14th July 2008)

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML, Highlighting and Model/View

    I'm not sure if a tree is the best way to visualize the document this way, but since you already have most things done, let's keep it this way.

    Drawing wrapped text is easy - use a variant of QPainter::drawText() that takes flags as one of its parameters and pass it a flag to wrap text. But... this way you won't be able to highlight syntax...

    Another way is to use QTextLayout and QTextLine which is quite straightforward, but... no highlighting either.

    Probably the best way would be to have a QTextDocument that the delegate would use to render the xml. You would apply a piece of text as the document, trigger the highlighter, render the document, apply a new piece of text, etc. Just make sure not to create a new document in each pass of the delegate - reuse the old one instead.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.