BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
importtextview.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/dataloader/importtextview.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 
18 //! Based on Qt example "codeeditor"
19 //! Copyright (C) 2016 The Qt Company Ltd.
20 
21 #include <QPainter>
22 #include <QTextBlock>
23 
24 namespace gui2 {
25 
26 namespace {
27 const int line_number_gap = 4;
28 }
29 
30 ImportTextView::ImportTextView(QWidget* parent) : QPlainTextEdit(parent)
31 {
32  lineNumberArea = new LineNumberArea(this);
33 
34  connect(this, &ImportTextView::blockCountChanged, this,
36  connect(this, &ImportTextView::updateRequest, this, &ImportTextView::updateLineNumberArea);
37  connect(this, &ImportTextView::cursorPositionChanged, this,
39 
41  // highlightCurrentLine();
42 
43  setReadOnly(true);
44  setWordWrapMode(QTextOption::NoWrap);
45 
46  setFont(QFont("Monospace", ModelView::Utils::SystemPointSize() * 0.8, QFont::Light));
47 }
48 
50 {
51  int digits = 1;
52  int max = qMax(1, blockCount());
53  while (max >= 10) {
54  max /= 10;
55  ++digits;
56  }
57 
58  int space = line_number_gap * 2 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
59 
60  return space;
61 }
62 
63 void ImportTextView::updateLineNumberAreaWidth(int /* newBlockCount */)
64 {
65  setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
66 }
67 
68 void ImportTextView::updateLineNumberArea(const QRect& rect, int dy)
69 {
70  if (dy)
71  lineNumberArea->scroll(0, dy);
72  else
73  lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
74 
75  if (rect.contains(viewport()->rect()))
77 }
78 
79 void ImportTextView::resizeEvent(QResizeEvent* e)
80 {
81  QPlainTextEdit::resizeEvent(e);
82 
83  QRect cr = contentsRect();
84  lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
85 }
86 
88 {
89  QList<QTextEdit::ExtraSelection> extraSelections;
90 
91  if (!isReadOnly()) {
92  QTextEdit::ExtraSelection selection;
93 
94  QColor lineColor = QColor(Qt::yellow).lighter(160);
95 
96  selection.format.setBackground(lineColor);
97  selection.format.setProperty(QTextFormat::FullWidthSelection, true);
98  selection.cursor = textCursor();
99  selection.cursor.clearSelection();
100  extraSelections.append(selection);
101  }
102 
103  setExtraSelections(extraSelections);
104 }
105 
107 {
108  QPainter painter(lineNumberArea);
109  painter.fillRect(event->rect(), Qt::lightGray);
110 
111  QTextBlock block = firstVisibleBlock();
112  int blockNumber = block.blockNumber();
113  int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
114  int bottom = top + qRound(blockBoundingRect(block).height());
115 
116  while (block.isValid() && top <= event->rect().bottom()) {
117  if (block.isVisible() && bottom >= event->rect().top()) {
118  QString number = QString::number(blockNumber + 1);
119  painter.setPen(Qt::black);
120  painter.drawText(0, top, lineNumberArea->width() - line_number_gap,
121  fontMetrics().height(), Qt::AlignRight, number);
122  }
123 
124  block = block.next();
125  top = bottom;
126  bottom = top + qRound(blockBoundingRect(block).height());
127  ++blockNumber;
128  }
129 }
130 
131 } // namespace gui2
void updateLineNumberArea(const QRect &rect, int dy)
ImportTextView(QWidget *parent=nullptr)
void lineNumberAreaPaintEvent(QPaintEvent *event)
void resizeEvent(QResizeEvent *event) override
void updateLineNumberAreaWidth(int newBlockCount)
Area with line numbers.
Defines class CLASS?
MVVM_VIEW_EXPORT int SystemPointSize()
Returns size in points of default system font.
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Defines class CLASS?