49 #include "codeeditor.hh" 52 CodeEditorWidget::CodeEditorWidget(QWidget *parent) : QPlainTextEdit(parent) {
55 connect(
this, SIGNAL(blockCountChanged(
int)),
this, SLOT(updateLineNumberAreaWidth(
int)));
56 connect(
this, SIGNAL(updateRequest(QRect,
int)),
this, SLOT(updateLineNumberArea(QRect,
int)));
57 connect(
this, SIGNAL(cursorPositionChanged()),
this, SLOT(highlightCurrentLine()));
59 updateLineNumberAreaWidth(0);
60 highlightCurrentLine();
65 int CodeEditorWidget::lineNumberAreaWidth() {
68 int max = qMax(1, blockCount());
74 int space = 3 + fontMetrics().width(QLatin1Char(
'9')) * digits;
81 void CodeEditorWidget::updateLineNumberAreaWidth(
int ) {
82 setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
87 void CodeEditorWidget::updateLineNumberArea(
const QRect &rect,
int dy) {
89 lineNumberArea->scroll(0, dy);
91 lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
93 if (rect.contains(viewport()->rect()))
94 updateLineNumberAreaWidth(0);
99 void CodeEditorWidget::resizeEvent(QResizeEvent *e) {
100 QPlainTextEdit::resizeEvent(e);
102 QRect cr = contentsRect();
103 lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
108 void CodeEditorWidget::highlightCurrentLine() {
109 QList<QTextEdit::ExtraSelection> extraSelections;
112 QTextEdit::ExtraSelection selection;
114 QColor lineColor = QColor(Qt::yellow).lighter(160);
116 selection.format.setBackground(lineColor);
117 selection.format.setProperty(QTextFormat::FullWidthSelection,
true);
118 selection.cursor = textCursor();
119 selection.cursor.clearSelection();
120 extraSelections.append(selection);
123 setExtraSelections(extraSelections);
126 void CodeEditorWidget::highLightErrorLine(
int _line) {
127 QList<QTextEdit::ExtraSelection> extraSelections;
130 QTextEdit::ExtraSelection selection;
132 QColor lineColor = QColor(Qt::red).lighter(160);
134 selection.format.setBackground(lineColor);
135 selection.format.setProperty(QTextFormat::FullWidthSelection,
true);
136 selection.cursor = QTextCursor(document());
137 selection.cursor.movePosition ( QTextCursor::Down, QTextCursor::MoveAnchor, _line - 1 );
138 selection.cursor.clearSelection();
139 extraSelections.append(selection);
142 setExtraSelections(extraSelections);
146 void CodeEditorWidget::lineNumberAreaPaintEvent(QPaintEvent *event) {
148 QPainter painter(lineNumberArea);
149 painter.fillRect(event->rect(), Qt::lightGray);
152 QTextBlock block = firstVisibleBlock();
153 int blockNumber = block.blockNumber();
154 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
155 int bottom = top + (int) blockBoundingRect(block).height();
157 while (block.isValid() && top <=
event->rect().bottom()) {
158 if (block.isVisible() && bottom >=
event->rect().top()) {
159 QString number = QString::number(blockNumber + 1);
160 painter.setPen(Qt::black);
161 painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),Qt::AlignRight, number);
164 block = block.next();
166 bottom = top + (int) blockBoundingRect(block).height();