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