52 #if QT_VERSION >= 0x050000 59 #include "codeeditor.hh" 62 CodeEditorWidget::CodeEditorWidget(QWidget *parent) : QPlainTextEdit(parent) {
65 connect(
this, SIGNAL(blockCountChanged(
int)),
this, SLOT(updateLineNumberAreaWidth(
int)));
66 connect(
this, SIGNAL(updateRequest(QRect,
int)),
this, SLOT(updateLineNumberArea(QRect,
int)));
67 connect(
this, SIGNAL(cursorPositionChanged()),
this, SLOT(highlightCurrentLine()));
69 updateLineNumberAreaWidth(0);
70 highlightCurrentLine();
75 int CodeEditorWidget::lineNumberAreaWidth() {
78 int max = qMax(1, blockCount());
84 int space = 3 + fontMetrics().width(QLatin1Char(
'9')) * digits;
91 void CodeEditorWidget::updateLineNumberAreaWidth(
int ) {
92 setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
97 void CodeEditorWidget::updateLineNumberArea(
const QRect &rect,
int dy) {
99 lineNumberArea->scroll(0, dy);
101 lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
103 if (rect.contains(viewport()->rect()))
104 updateLineNumberAreaWidth(0);
109 void CodeEditorWidget::resizeEvent(QResizeEvent *e) {
110 QPlainTextEdit::resizeEvent(e);
112 QRect cr = contentsRect();
113 lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
118 void CodeEditorWidget::highlightCurrentLine() {
119 QList<QTextEdit::ExtraSelection> extraSelections;
122 QTextEdit::ExtraSelection selection;
124 QColor lineColor = QColor(Qt::yellow).lighter(160);
126 selection.format.setBackground(lineColor);
127 selection.format.setProperty(QTextFormat::FullWidthSelection,
true);
128 selection.cursor = textCursor();
129 selection.cursor.clearSelection();
130 extraSelections.append(selection);
133 setExtraSelections(extraSelections);
136 void CodeEditorWidget::highLightErrorLine(
int _line) {
137 QList<QTextEdit::ExtraSelection> extraSelections;
140 QTextEdit::ExtraSelection selection;
142 QColor lineColor = QColor(Qt::red).lighter(160);
144 selection.format.setBackground(lineColor);
145 selection.format.setProperty(QTextFormat::FullWidthSelection,
true);
146 selection.cursor = QTextCursor(document());
147 selection.cursor.movePosition ( QTextCursor::Down, QTextCursor::MoveAnchor, _line - 1 );
148 selection.cursor.clearSelection();
149 extraSelections.append(selection);
152 setExtraSelections(extraSelections);
156 void CodeEditorWidget::lineNumberAreaPaintEvent(QPaintEvent *event) {
158 QPainter painter(lineNumberArea);
159 painter.fillRect(event->rect(), Qt::lightGray);
162 QTextBlock block = firstVisibleBlock();
163 int blockNumber = block.blockNumber();
164 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
165 int bottom = top + (int) blockBoundingRect(block).height();
167 while (block.isValid() && top <=
event->rect().bottom()) {
168 if (block.isVisible() && bottom >=
event->rect().top()) {
169 QString number = QString::number(blockNumber + 1);
170 painter.setPen(Qt::black);
171 painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),Qt::AlignRight, number);
174 block = block.next();
176 bottom = top + (int) blockBoundingRect(block).height();