qtopengl_lua_editor.cpp
Go to the documentation of this file.
1 
7 #include "qtopengl_lua_editor.h"
9 
10 #include <QPainter>
11 #include <QTextBlock>
12 
13 namespace argos {
14 
15  /****************************************/
16  /****************************************/
17 
19  QPlainTextEdit(pc_parent) {
20  /* Set font */
21  QFont cFont;
22  cFont.setFamily("Monospace");
23  cFont.setStyleHint(QFont::Monospace);
24  cFont.setFixedPitch(true);
25  setFont(cFont);
26  /* Set tab width to 3 */
27  QFontMetrics cFontMetrics(cFont);
28 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
29  setTabStopDistance(3 * cFontMetrics.horizontalAdvance(' '));
30 #else
31  setTabStopWidth(3 * cFontMetrics.width(' '));
32 #endif
33  /* Set syntax highlighting */
34  new CQTOpenGLLuaSyntaxHighlighter(document());
35  /* Set line numbering */
36  m_pcLineNumberArea = new CLineNumberArea(this);
37  /* Connect signals */
38  connect(this, SIGNAL(blockCountChanged(int)),
39  this, SLOT(UpdateLineNumberAreaWidth(int)));
40  connect(this, SIGNAL(updateRequest(const QRect&, int)),
41  this, SLOT(UpdateLineNumberArea(const QRect&, int)));
42  connect(this, SIGNAL(cursorPositionChanged()),
43  this, SLOT(HighlightCurrentLine()));
44  /* Final touches */
45  UpdateLineNumberAreaWidth(0);
46  HighlightCurrentLine();
47  }
48 
49  /****************************************/
50  /****************************************/
51 
52  void CQTOpenGLLuaEditor::LineNumberAreaPaintEvent(QPaintEvent* pc_event) {
53  QPainter cPainter(m_pcLineNumberArea);
54  cPainter.fillRect(pc_event->rect(), Qt::lightGray);
55 
56 
57  QTextBlock cBlock = firstVisibleBlock();
58  int nBlockNumber = cBlock.blockNumber();
59  int nTop = (int) blockBoundingGeometry(cBlock).translated(contentOffset()).top();
60  int nBottom = nTop + (int) blockBoundingRect(cBlock).height();
61 
62  while (cBlock.isValid() && nTop <= pc_event->rect().bottom()) {
63  if (cBlock.isVisible() && nBottom >= pc_event->rect().top()) {
64  QString strNumber = QString::number(nBlockNumber + 1);
65  cPainter.setPen(Qt::black);
66  cPainter.drawText(0, nTop,
67  m_pcLineNumberArea->width(), fontMetrics().height(),
68  Qt::AlignRight, strNumber);
69  }
70 
71  cBlock = cBlock.next();
72  nTop = nBottom;
73  nBottom = nTop + (int) blockBoundingRect(cBlock).height();
74  ++nBlockNumber;
75  }
76  }
77 
78  /****************************************/
79  /****************************************/
80 
82  int nDigits = 1;
83  int nMax = qMax(1, blockCount());
84  while (nMax >= 10) {
85  nMax /= 10;
86  ++nDigits;
87  }
88 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
89  int nSpace = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * nDigits;
90 #else
91  int nSpace = 3 + fontMetrics().width(QLatin1Char('9')) * nDigits;
92 #endif
93  return nSpace;
94  }
95 
96  /****************************************/
97  /****************************************/
98 
99  void CQTOpenGLLuaEditor::resizeEvent(QResizeEvent* pc_event) {
100  QPlainTextEdit::resizeEvent(pc_event);
101  QRect cRect = contentsRect();
102  m_pcLineNumberArea->setGeometry(QRect(cRect.left(), cRect.top(),
103  LineNumberAreaWidth(), cRect.height()));
104  }
105 
106  /****************************************/
107  /****************************************/
108 
109  void CQTOpenGLLuaEditor::UpdateLineNumberAreaWidth(int) {
110  setViewportMargins(LineNumberAreaWidth(), 0, 0, 0);
111  }
112 
113  /****************************************/
114  /****************************************/
115 
116  void CQTOpenGLLuaEditor::HighlightCurrentLine() {
117  QList<QTextEdit::ExtraSelection> cListExtraSel;
118 
119  if (!isReadOnly()) {
120  QTextEdit::ExtraSelection cSel;
121  QColor cLineColor = QColor(Qt::yellow).lighter(160);
122  cSel.format.setBackground(cLineColor);
123  cSel.format.setProperty(QTextFormat::FullWidthSelection, true);
124  cSel.cursor = textCursor();
125  cSel.cursor.clearSelection();
126  cListExtraSel.append(cSel);
127  }
128  setExtraSelections(cListExtraSel);
129  }
130 
131  /****************************************/
132  /****************************************/
133 
134  void CQTOpenGLLuaEditor::UpdateLineNumberArea(const QRect& c_rect,
135  int n_dy) {
136  if(n_dy) {
137  m_pcLineNumberArea->scroll(0, n_dy);
138  }
139  else {
140  m_pcLineNumberArea->update(0, c_rect.y(),
141  m_pcLineNumberArea->width(), c_rect.height());
142  }
143  if(c_rect.contains(viewport()->rect())) {
144  UpdateLineNumberAreaWidth(0);
145  }
146  }
147 
148  /****************************************/
149  /****************************************/
150 
151 }
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
void LineNumberAreaPaintEvent(QPaintEvent *pc_event)
void resizeEvent(QResizeEvent *pc_event)
CQTOpenGLLuaEditor(QWidget *pc_parent)