MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / drawSegmentSelection

Method drawSegmentSelection

app/src/UI/Widgets/Terminal.cpp:154–208  ·  view source on GitHub ↗

* @brief Draws the selection highlight for one word-wrapped segment of a line. */

Source from the content-addressed store, hash-verified

152 * @brief Draws the selection highlight for one word-wrapped segment of a line.
153 */
154void Widgets::Terminal::drawSegmentSelection(
155 QPainter* painter, const QString& line, int lineIndex, int segStart, int segEnd, int y)
156{
157 if (m_selectionEnd.isNull())
158 return;
159
160 if (lineIndex < m_selectionStart.y() || lineIndex > m_selectionEnd.y())
161 return;
162
163 int selStartX, selEndX;
164 if (lineIndex == m_selectionStart.y() && lineIndex == m_selectionEnd.y()) {
165 selStartX = qMax(m_selectionStart.x(), segStart);
166 selEndX = qMin(m_selectionEnd.x(), segEnd);
167 } else if (lineIndex == m_selectionStart.y()) {
168 selStartX = qMax(m_selectionStart.x(), segStart);
169 selEndX = segEnd;
170 } else if (lineIndex == m_selectionEnd.y()) {
171 selStartX = segStart;
172 selEndX = qMin(m_selectionEnd.x(), segEnd);
173 } else {
174 selStartX = segStart;
175 selEndX = segEnd;
176 }
177
178 if (selStartX >= selEndX)
179 return;
180
181 int leadingOffset = 0;
182 int selectionWidth = 0;
183 for (int j = segStart; j < selEndX; ++j) {
184 const int charWidth = painter->fontMetrics().horizontalAdvance(line[j]);
185 if (j < selStartX)
186 leadingOffset += charWidth;
187 else
188 selectionWidth += charWidth;
189 }
190
191 const bool rtl = Misc::Translator::instance().rtl();
192 const int maxWidth = width() - 2 * m_borderX;
193 int startX = 0;
194
195 if (rtl) {
196 const int rightEdge = width() - m_borderX;
197 startX = rightEdge - leadingOffset - selectionWidth;
198 selectionWidth = qMin(selectionWidth, maxWidth);
199 }
200
201 else {
202 startX = m_borderX + leadingOffset;
203 selectionWidth = qMin(selectionWidth, maxWidth - leadingOffset);
204 }
205
206 painter->fillRect(QRect(startX, y, selectionWidth, m_cHeight),
207 m_palette.color(QPalette::Highlight));
208}
209
210/**
211 * @brief Renders a word-wrapped segment with a single uniform text color.

Callers

nothing calls this directly

Calls 3

xMethod · 0.80
rtlMethod · 0.80
fillRectMethod · 0.80

Tested by

no test coverage detected