Debian Codegraph @

HercConfiguration/ConfigurationFreeEditor.cpp

raw symbols SCIP
1 #include "ConfigurationFreeEditor.h"
2 #include "HerculesStudio.h"
3
4 #include <QtGui>
5
6 ConfigurationFreeEditor::ConfigurationFreeEditor(QWidget *parent) :
7 QPlainTextEdit(parent)
8 {
9 mLineNumberArea = new LineNumberArea(this);
10
11 connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
12 connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
13 connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
14
15 updateLineNumberAreaWidth(0);
16 highlightCurrentLine();
17 }
18
19 int ConfigurationFreeEditor::lineNumberAreaWidth()
20 {
21 int digits = 1;
22 int max = qMax(1, blockCount());
23 while (max >= 10) {
24 max /= 10;
25 ++digits;
26 }
27
28 int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
29
30 return space;
31 }
32
33 void ConfigurationFreeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
34 {
35 setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
36 }
37
38
39
40 void ConfigurationFreeEditor::updateLineNumberArea(const QRect &rect, int dy)
41 {
42 if (dy)
43 mLineNumberArea->scroll(0, dy);
44 else
45 mLineNumberArea->update(0, rect.y(), mLineNumberArea->width(), rect.height());
46
47 if (rect.contains(viewport()->rect()))
48 updateLineNumberAreaWidth(0);
49 }
50
51
52
53 void ConfigurationFreeEditor::resizeEvent(QResizeEvent *e)
54 {
55 QPlainTextEdit::resizeEvent(e);
56
57 QRect cr = contentsRect();
58 mLineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
59 }
60
61
62
63 void ConfigurationFreeEditor::highlightCurrentLine()
64 {
65 QList<QTextEdit::ExtraSelection> extraSelections;
66
67 if (!isReadOnly())
68 {
69 QTextEdit::ExtraSelection selection;
70
71 QColor lineColor = QColor(Qt::yellow).lighter(170);
72
73 selection.format.setBackground(lineColor);
74 selection.format.setProperty(QTextFormat::FullWidthSelection, true);
75 selection.cursor = textCursor();
76 selection.cursor.clearSelection();
77 extraSelections.append(selection);
78 }
79
80 setExtraSelections(extraSelections);
81 }
82
83
84
85 void ConfigurationFreeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
86 {
87 QPainter painter(mLineNumberArea);
88 painter.fillRect(event->rect(), Qt::gray);
89
90
91 QTextBlock block = firstVisibleBlock();
92 int blockNumber = block.blockNumber();
93 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
94 int bottom = top + (int) blockBoundingRect(block).height();
95
96 while (block.isValid() && top <= event->rect().bottom())
97 {
98 if (block.isVisible() && bottom >= event->rect().top())
99 {
100 QString number = QString::number(blockNumber + 1);
101 painter.setPen(Qt::black);
102 painter.drawText(0, top, mLineNumberArea->width(), fontMetrics().height(),
103 Qt::AlignRight, number);
104 }
105
106 block = block.next();
107 top = bottom;
108 bottom = top + (int) blockBoundingRect(block).height();
109 ++blockNumber;
110 }
111 }