'Annotation' of lines in a QTextEdit
I'd like to display some text (e.g. some source code) and 'annotate' it by displaying something like icons, line numbers or so to its left.
Is there support for something like that in QTextEdit? Are there other (Qt?) classes to achieve such a 'line based annotation'?
Thank you for any input.
Best regards
Christoph
Re: 'Annotation' of lines in a QTextEdit
Well, not by default.
I believe fullmetalcoder could give you an answer on how to do this.
He wrote Edyuk and all...
Regards
Re: 'Annotation' of lines in a QTextEdit
Quote:
Originally Posted by marcel
He wrote Edyuk and all...
Sure but in this case the whole Edyuk source would be too much... QCodeEdit, which is available as a separate module, would be a lot more helpful and should fit your needs : it has highlighting, parenthesis matching, code folding, text indenting, line marks, line numbering and all such features needed by a code editor. :)
P.S : I didn't mean to press the thank button but the quote one instead...:o
Re: 'Annotation' of lines in a QTextEdit
Quote:
P.S : I didn't mean to press the thank button but the quote one instead...:o
Hey, no problem. You can take "thank you"s back. :)
It was possible, at least last time I checked.
Regards
Re: 'Annotation' of lines in a QTextEdit
Quote:
Originally Posted by
fullmetalcoder
Sure but in this case the whole Edyuk source would be too much... QCodeEdit, which is available as a separate module...
Ok, I take it QCodeEdit is a subset of Edyuk. I will take a look at it.
Speaking of that project
- I could not find information on its license... would it be ok to include (and then perhaps modify) it in an inhouse, or even a commercially distributed app?
- There are no FAQ on the site...
- I might be able to help a bit with your project (after moving my place in the next few weeks). Is there a list of open tasks anywhere?
Best regards
Re: 'Annotation' of lines in a QTextEdit
Quote:
Originally Posted by
caduel
I could not find information on its license... would it be ok to include (and then perhaps modify) it in an inhouse, or even a commercially distributed app?
Well, until now I choose a special dual-licensing model : - embedding the source into a project, either verbatim or modified, make it fall under GPL because the source is part of Edyuk
- anyway, using a shared library created from verbatim source is possible as specified by the LGPL
- I dunno if it is possible because I don't own a commercial license myself but as the author of this code I could exceptionally grant someone the right of using a modified version into a proprietary software
Quote:
Originally Posted by
caduel
There are no FAQ on the site...
Absolutely right but none asked questions that could be put on a FAQ... Remember : FAQ stands for Frequently Asked Questions...;)
Quote:
Originally Posted by
caduel
I might be able to help a bit with your project (after moving my place in the next few weeks). Is there a list of open tasks anywhere?
Not yet but I could easily create one. Indeed, I'm now using the great WebIssues as a bug/tasks tracker. Log at http://edyuk.tuxfamily.org/webissues through a WebIssues client using login anonymous and password anonymous. :)
As for contributing, you're welcome! However it might be a little complicated at the moment since I'm in the process of rewriting most of QCodeEdit from scratch (I've dropped QTextDocument which is way too slow and memory-consuming and thus not suited for code editing... Besides I'm working on two new generic syntax specs, much faster and much more flexible than the old one.)
Re: 'Annotation' of lines in a QTextEdit
Quote:
Originally Posted by
marcel
Well, not by default.
Actually there is support for creating something like this, but I couldn't get it to work :)
Re: 'Annotation' of lines in a QTextEdit
Quote:
Originally Posted by
wysota
Actually there is support for creating something like this, but I couldn't get it to work :)
Really? All I found in the QTextDocument framework which could help achieving this : QTextBlock::userData() but then you've got to render marks on your own...
Re: 'Annotation' of lines in a QTextEdit
QAbstractScrollArea::setViewportMargins( int left, int top, int right, int bottom ) - this should let you add a widget to the text edit and render the icons in there. Views use that approach for inserting headers (QHeaderView instances) but I couldn't make it work for the text edit and I didn't want to waste too much time on this. Maybe someone else has more time, luck and/or skills to make it work.
Re: 'Annotation' of lines in a QTextEdit
Yes, setViewportMargins() and QTextEdit::cursorRect() might be enough to determine the y-coordinates of the lines display... still I should take a look at QCodeEdit
Christoph
Re: 'Annotation' of lines in a QTextEdit
I am getting a lot of:
Code:
QPainter::begin: Widget painting can only begin as a result of a paintEvent
debug calls, and my code is not effective (it does not draw). I think that simply subclassing QTextEdit and reimplementing [b]paintEvent( QPaintEvent * event )[b/] is not enough.
wysota:
What part is failing for you?
Re: 'Annotation' of lines in a QTextEdit
Quote:
Originally Posted by
elcuco
wysota:
What part is failing for you?
I could not put my own widget in the space created by increasing the margin. It just didn't seem to work.
Re: 'Annotation' of lines in a QTextEdit
Should work if you place the widgets manually( no layouts ).
Of course, you must take care of resize events to reposition the margin widgets accordingly.
I have seen it done for a QScrollArea.
Regards
Re: 'Annotation' of lines in a QTextEdit
For my whats failing is this code, the layout system has nothing to do, as the margins are inside the widget.
(edit: just remembered, parts of this code are based on QDevelop, so they are GPL)
Code:
#include <QTextEdit>
{
Q_OBJECT
public:
protected:
};
#include "newedit.h"
#include <QApplication>
#include <QPainter>
#include <QTextBlock>
#include <QTextLayout>
#include <QScrollBar>
newedit
::newedit( QWidget* parent
) {
setViewportMargins( 50, 0, 0, 0 );
}
{
int contentsY = verticalScrollBar()->value();
qreal pageBottom = contentsY + viewport()->height();
int m_lineNumber = 1;
const int ascent = fontMetrics().ascent() +1;
for ( QTextBlock block
= document
()->begin
(); block.
isValid(); block
= block.
next(), m_lineNumber
++ ) {
const QRectF boundingRect
= layout
->boundingRect
();
QPointF position
= layout
->position
();
if ( position.y() +boundingRect.height() < contentsY )
continue;
if ( position.y() > pageBottom )
break;
p.drawText( width() -fm.width( txt ) - 2, qRound( position.y() ) -contentsY +ascent, txt ); // -fm.width( "0" ) is an ampty place/indent
}
}
Re: 'Annotation' of lines in a QTextEdit
Quote:
Originally Posted by
marcel
Should work if you place the widgets manually( no layouts ).
I didn't use layouts. It just didn't want to work :) I probably had some simple mistake but was unable to pinpoint it, although I had the Q*View code for reference... I'll probably try it again some other day.
Quote:
Originally Posted by
elcuco
For my whats failing is this code, the layout system has nothing to do, as the margins are inside the widget.
From what I see you don't use a separate widget for the margin, so the code is "slightly" different.
Re: 'Annotation' of lines in a QTextEdit
as far as in understand from the api, the margins are still part of the control's responsibility.
the problem i am facing, is that when i get the paint callback called, i am trying to create a QPainter, it complains that it's not created on the paintEvent() event.
i hope something see's what i am missing.
Re: 'Annotation' of lines in a QTextEdit
Quote:
Originally Posted by
elcuco
as far as in understand from the api, the margins are still part of the control's responsibility.
The margins are indeed drawn by the scroll area and to paint anything on them it is highly recommened to use a custom layout. Custom is important here because it implies using setViewportMargins() from the layout (actually a public wrapper function but it does not make such a difference).
t
Quote:
Originally Posted by
elcuco
he problem i am facing, is that when i get the paint callback called, i am trying to create a QPainter, it complains that it's not created on the paintEvent() event.
i hope something see's what i am missing.
QPainter MUST be initialized on viewport()!!! Indeed (nearly) *ALL* events recieved by a QAbstractScrollArea are redirected from its viewport... Besides it is generally a bad idea to draw the margins from the widget paint event... You should consider placing this code in another widget which would be parented to the editor and managed by the aforementioned custom layout. FYI I made such a custom layout, inspired of Qt 4 Border Layout example, for use with the next version of QCodeEdit.
Also I hope you consider me as a little more than "something"... ;)
Re: 'Annotation' of lines in a QTextEdit
I did not understand your first comment, but I disagree with you on the second comment:
As far as I understand from the code (I looked inside the sources of Qt), the QScrollArea is composed from a a layout in which they stick in 2 scroll bars, and a viewport. The viewport is what we see as the texteditor, the corner widget will sit generally on the right buttom corner, between the scroll bars.
The function discussed in this thread will make a margin for the central widget (the viewport). This is why my code before was not working: the main widget (which contains the main layout, which contains the scroll bars and the text editor) was nto drawn: only the view port.
I think that the parts outside of the margins should be handeled by a special widget inserted to the scroll area. Not sure how yet, even tough I have seen code which uses this API.
Re: 'Annotation' of lines in a QTextEdit
Quote:
Originally Posted by
elcuco
The function discussed in this thread will make a margin for the central widget (the viewport). This is why my code before was not working: the main widget (which contains the main layout, which contains the scroll bars and the text editor) was nto drawn: only the view port.
Correct me if I'm wrong but I don't think the viewport can be drawn without the scroll area (thus scroll bars) being painted... The only quirk is that widget placed in the margins are NOT updated when the scroll bars move... You MUST explicitely connect the QSlider::valueChanged(int) signal to your panel widgets (widgets put in the margins).
Quote:
Originally Posted by elcuco
I think that the parts outside of the margins should be handeled by a special widget inserted to the scroll area. Not sure how yet, even tough I have seen code which uses this API.
You've seen my code? Well, I'd be happy to know how... The last snippet I'm working on are not even available yet on SVN and the previous versions of QCodeEdit did not make use of setViewportMargins()... As with any other widget I don't quite see how you could play with the internals of QAbstractScrollArea... If you're interested in the layout code I guess I can post it...
Re: 'Annotation' of lines in a QTextEdit
FMC: If you managed to insert a "margin widget" into a scroll area, could you please present a minimal code that accomplishes that? We might be going in circles here and I'm really interested in finding a solution.