1 | /* $Id: UIVMLogViewerTextEdit.cpp 103923 2024-03-19 17:01:11Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIVMLogViewer class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QApplication>
|
---|
30 | #if defined(RT_OS_SOLARIS)
|
---|
31 | # include <QFontDatabase>
|
---|
32 | #endif
|
---|
33 | #include <QMenu>
|
---|
34 | #include <QPainter>
|
---|
35 | #include <QPainterPath>
|
---|
36 | #include <QPlainTextEdit>
|
---|
37 | #include <QLabel>
|
---|
38 | #include <QScrollBar>
|
---|
39 | #include <QStyle>
|
---|
40 | #include <QTextBlock>
|
---|
41 |
|
---|
42 | /* GUI includes: */
|
---|
43 | #include "UIIconPool.h"
|
---|
44 | #include "UITranslationEventListener.h"
|
---|
45 | #include "UIVMLogViewerTextEdit.h"
|
---|
46 | #include "UIVMLogViewerWidget.h"
|
---|
47 |
|
---|
48 | /* Other VBox includes: */
|
---|
49 | #include "iprt/assert.h"
|
---|
50 |
|
---|
51 | /** We use a modified scrollbar style for our QPlainTextEdits to get the
|
---|
52 | markings on the scrollbars correctly. The default scrollbarstyle does not
|
---|
53 | reveal the height of the pushbuttons on the scrollbar (on either side of it, with arrow on them)
|
---|
54 | to compute the marking locations correctly. Thus we turn these push buttons off: */
|
---|
55 | const QString verticalScrollBarStyle("QScrollBar:vertical {"
|
---|
56 | "border: 1px ridge grey; "
|
---|
57 | "margin: 0px 0px 0 0px;}"
|
---|
58 | "QScrollBar::handle:vertical {"
|
---|
59 | "min-height: 10px;"
|
---|
60 | "background: grey;}"
|
---|
61 | "QScrollBar::add-line:vertical {"
|
---|
62 | "width: 0px;}"
|
---|
63 | "QScrollBar::sub-line:vertical {"
|
---|
64 | "width: 0px;}");
|
---|
65 |
|
---|
66 | const QString horizontalScrollBarStyle("QScrollBar:horizontal {"
|
---|
67 | "border: 1px ridge grey; "
|
---|
68 | "margin: 0px 0px 0 0px;}"
|
---|
69 | "QScrollBar::handle:horizontal {"
|
---|
70 | "min-height: 10px;"
|
---|
71 | "background: grey;}"
|
---|
72 | "QScrollBar::add-line:horizontal {"
|
---|
73 | "height: 0px;}"
|
---|
74 | "QScrollBar::sub-line:horizontal {"
|
---|
75 | "height: 0px;}");
|
---|
76 |
|
---|
77 | /*********************************************************************************************************************************
|
---|
78 | * UILogScrollLabel definition. *
|
---|
79 | *********************************************************************************************************************************/
|
---|
80 |
|
---|
81 | class UILogScrollLabel : public QLabel
|
---|
82 | {
|
---|
83 | Q_OBJECT;
|
---|
84 |
|
---|
85 | public:
|
---|
86 |
|
---|
87 | UILogScrollLabel(QWidget *pParent);
|
---|
88 | void setOpacity(qreal fOpacity);
|
---|
89 |
|
---|
90 | protected:
|
---|
91 |
|
---|
92 | virtual void paintEvent(QPaintEvent *pEvent) RT_OVERRIDE;
|
---|
93 |
|
---|
94 | private:
|
---|
95 |
|
---|
96 | qreal m_fOpacity;
|
---|
97 | };
|
---|
98 |
|
---|
99 |
|
---|
100 | /*********************************************************************************************************************************
|
---|
101 | * UIIndicatorScrollBar definition. *
|
---|
102 | *********************************************************************************************************************************/
|
---|
103 |
|
---|
104 | class UIIndicatorScrollBar : public QScrollBar
|
---|
105 | {
|
---|
106 | Q_OBJECT;
|
---|
107 |
|
---|
108 | public:
|
---|
109 |
|
---|
110 | UIIndicatorScrollBar(QWidget *parent = 0);
|
---|
111 | void setMarkingsVector(const QVector<float> &vector);
|
---|
112 | void clearMarkingsVector();
|
---|
113 |
|
---|
114 | protected:
|
---|
115 |
|
---|
116 | virtual void paintEvent(QPaintEvent *pEvent) RT_OVERRIDE;
|
---|
117 |
|
---|
118 | private:
|
---|
119 |
|
---|
120 | /* Stores the relative (to scrollbar's height) positions of markings,
|
---|
121 | where we draw a horizontal line. Values are in [0.0, 1.0]*/
|
---|
122 | QVector<float> m_markingsVector;
|
---|
123 | };
|
---|
124 |
|
---|
125 |
|
---|
126 | /*********************************************************************************************************************************
|
---|
127 | * UILogScrollLabel definition. *
|
---|
128 | *********************************************************************************************************************************/
|
---|
129 |
|
---|
130 | UILogScrollLabel::UILogScrollLabel(QWidget *pParent)
|
---|
131 | : QLabel(pParent)
|
---|
132 | , m_fOpacity(1.f)
|
---|
133 | {
|
---|
134 | }
|
---|
135 |
|
---|
136 | void UILogScrollLabel::setOpacity(qreal fOpacity)
|
---|
137 | {
|
---|
138 | if (m_fOpacity == fOpacity)
|
---|
139 | return;
|
---|
140 | m_fOpacity = fOpacity;
|
---|
141 | update();
|
---|
142 | }
|
---|
143 |
|
---|
144 | void UILogScrollLabel::paintEvent(QPaintEvent *pEvent)
|
---|
145 | {
|
---|
146 | Q_UNUSED(pEvent);
|
---|
147 | QPainter painter(this);
|
---|
148 | painter.setBrush(Qt::red);
|
---|
149 | painter.setOpacity(m_fOpacity);
|
---|
150 | painter.drawPixmap(0, 0, pixmap());
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /*********************************************************************************************************************************
|
---|
155 | * UIIndicatorScrollBar implemetation. *
|
---|
156 | *********************************************************************************************************************************/
|
---|
157 |
|
---|
158 | UIIndicatorScrollBar::UIIndicatorScrollBar(QWidget *parent /*= 0 */)
|
---|
159 | :QScrollBar(parent)
|
---|
160 | {
|
---|
161 | setStyleSheet(verticalScrollBarStyle);
|
---|
162 | }
|
---|
163 |
|
---|
164 | void UIIndicatorScrollBar::setMarkingsVector(const QVector<float> &vector)
|
---|
165 | {
|
---|
166 | m_markingsVector = vector;
|
---|
167 | }
|
---|
168 |
|
---|
169 | void UIIndicatorScrollBar::clearMarkingsVector()
|
---|
170 | {
|
---|
171 | m_markingsVector.clear();
|
---|
172 | }
|
---|
173 |
|
---|
174 | void UIIndicatorScrollBar::paintEvent(QPaintEvent *pEvent) /* override */
|
---|
175 | {
|
---|
176 | QScrollBar::paintEvent(pEvent);
|
---|
177 | /* Put a red line to mark the bookmark positions: */
|
---|
178 | for (int i = 0; i < m_markingsVector.size(); ++i)
|
---|
179 | {
|
---|
180 | QPointF p1 = QPointF(0, m_markingsVector[i] * height());
|
---|
181 | QPointF p2 = QPointF(width(), m_markingsVector[i] * height());
|
---|
182 |
|
---|
183 | QPainter painter(this);
|
---|
184 | painter.setRenderHint(QPainter::Antialiasing, true);
|
---|
185 | painter.setPen(QPen(QColor(255, 0, 0, 75), 1.1f));
|
---|
186 | painter.drawLine(p1, p2);
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | /*********************************************************************************************************************************
|
---|
192 | * UILineNumberArea definition. *
|
---|
193 | *********************************************************************************************************************************/
|
---|
194 |
|
---|
195 | class UILineNumberArea : public QWidget
|
---|
196 | {
|
---|
197 | public:
|
---|
198 | UILineNumberArea(UIVMLogViewerTextEdit *textEdit);
|
---|
199 | QSize sizeHint() const;
|
---|
200 |
|
---|
201 | protected:
|
---|
202 |
|
---|
203 | void paintEvent(QPaintEvent *event);
|
---|
204 | void mouseMoveEvent(QMouseEvent *pEvent);
|
---|
205 | void mousePressEvent(QMouseEvent *pEvent);
|
---|
206 |
|
---|
207 | private:
|
---|
208 | UIVMLogViewerTextEdit *m_pTextEdit;
|
---|
209 | };
|
---|
210 |
|
---|
211 |
|
---|
212 | /*********************************************************************************************************************************
|
---|
213 | * UILineNumberArea implemetation. *
|
---|
214 | *********************************************************************************************************************************/
|
---|
215 |
|
---|
216 | UILineNumberArea::UILineNumberArea(UIVMLogViewerTextEdit *textEdit)
|
---|
217 | :QWidget(textEdit)
|
---|
218 | , m_pTextEdit(textEdit)
|
---|
219 | {
|
---|
220 | setMouseTracking(true);
|
---|
221 | }
|
---|
222 |
|
---|
223 | QSize UILineNumberArea::sizeHint() const
|
---|
224 | {
|
---|
225 | if (!m_pTextEdit)
|
---|
226 | return QSize();
|
---|
227 | return QSize(m_pTextEdit->lineNumberAreaWidth(), 0);
|
---|
228 | }
|
---|
229 |
|
---|
230 | void UILineNumberArea::paintEvent(QPaintEvent *event)
|
---|
231 | {
|
---|
232 | if (m_pTextEdit)
|
---|
233 | m_pTextEdit->lineNumberAreaPaintEvent(event);
|
---|
234 | }
|
---|
235 |
|
---|
236 | void UILineNumberArea::mouseMoveEvent(QMouseEvent *pEvent)
|
---|
237 | {
|
---|
238 | if (m_pTextEdit)
|
---|
239 | m_pTextEdit->setMouseCursorLine(m_pTextEdit->lineNumberForPos(pEvent->position().toPoint()));
|
---|
240 | update();
|
---|
241 | }
|
---|
242 |
|
---|
243 | void UILineNumberArea::mousePressEvent(QMouseEvent *pEvent)
|
---|
244 | {
|
---|
245 | if (m_pTextEdit)
|
---|
246 | m_pTextEdit->toggleBookmark(m_pTextEdit->bookmarkForPos(pEvent->position().toPoint()));
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | /*********************************************************************************************************************************
|
---|
251 | * UIVMLogViewerTextEdit implemetation. *
|
---|
252 | *********************************************************************************************************************************/
|
---|
253 |
|
---|
254 | UIVMLogViewerTextEdit::UIVMLogViewerTextEdit(QWidget* parent /* = 0 */)
|
---|
255 | : QPlainTextEdit(parent)
|
---|
256 | , m_pLineNumberArea(0)
|
---|
257 | , m_mouseCursorLine(-1)
|
---|
258 | , m_bShownTextIsFiltered(false)
|
---|
259 | , m_bShowLineNumbers(true)
|
---|
260 | , m_bWrapLines(true)
|
---|
261 | , m_bHasContextMenu(false)
|
---|
262 | , m_iVerticalScrollBarValue(0)
|
---|
263 | , m_pScrollToBottomLabel(0)
|
---|
264 | , m_pScrollToTopLabel(0)
|
---|
265 | {
|
---|
266 | configure();
|
---|
267 | prepare();
|
---|
268 | }
|
---|
269 |
|
---|
270 | void UIVMLogViewerTextEdit::configure()
|
---|
271 | {
|
---|
272 | setMouseTracking(true);
|
---|
273 |
|
---|
274 | /* Prepare modified standard palette: */
|
---|
275 | QPalette pal = QApplication::palette();
|
---|
276 | pal.setColor(QPalette::Inactive, QPalette::Highlight, pal.color(QPalette::Active, QPalette::Highlight));
|
---|
277 | pal.setColor(QPalette::Inactive, QPalette::HighlightedText, pal.color(QPalette::Active, QPalette::HighlightedText));
|
---|
278 | setPalette(pal);
|
---|
279 |
|
---|
280 | /* Configure this' wrap mode: */
|
---|
281 | setWrapLines(false);
|
---|
282 | setReadOnly(true);
|
---|
283 | m_originalCursor = cursor();
|
---|
284 | }
|
---|
285 |
|
---|
286 | void UIVMLogViewerTextEdit::prepare()
|
---|
287 | {
|
---|
288 | prepareWidgets();
|
---|
289 | sltRetranslateUI();
|
---|
290 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
291 | this, &UIVMLogViewerTextEdit::sltRetranslateUI);
|
---|
292 | }
|
---|
293 |
|
---|
294 | void UIVMLogViewerTextEdit::prepareWidgets()
|
---|
295 | {
|
---|
296 | m_pLineNumberArea = new UILineNumberArea(this);
|
---|
297 | AssertReturnVoid(m_pLineNumberArea);
|
---|
298 |
|
---|
299 | m_pScrollToBottomLabel = new UILogScrollLabel(this);
|
---|
300 | m_pScrollToTopLabel = new UILogScrollLabel(this);
|
---|
301 | AssertReturnVoid(m_pScrollToBottomLabel);
|
---|
302 | AssertReturnVoid(m_pScrollToTopLabel);
|
---|
303 |
|
---|
304 | m_pScrollToBottomLabel->installEventFilter(this);
|
---|
305 | m_pScrollToTopLabel->installEventFilter(this);
|
---|
306 |
|
---|
307 | const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
|
---|
308 | m_pScrollToBottomLabel->setPixmap(UIIconPool::iconSet(":/arrow_to_bottom_32px.png").pixmap(iIconMetric, iIconMetric));
|
---|
309 | m_pScrollToBottomLabel->setOpacity(0.4);
|
---|
310 |
|
---|
311 | m_pScrollToTopLabel->setPixmap(UIIconPool::iconSet(":/arrow_to_top_32px.png").pixmap(iIconMetric, iIconMetric));
|
---|
312 | m_pScrollToTopLabel->setOpacity(0.4);
|
---|
313 |
|
---|
314 | connect(this, &UIVMLogViewerTextEdit::blockCountChanged, this, &UIVMLogViewerTextEdit::sltUpdateLineNumberAreaWidth);
|
---|
315 | connect(this, &UIVMLogViewerTextEdit::updateRequest, this, &UIVMLogViewerTextEdit::sltHandleUpdateRequest);
|
---|
316 | sltUpdateLineNumberAreaWidth(0);
|
---|
317 |
|
---|
318 | setVerticalScrollBar(new UIIndicatorScrollBar());
|
---|
319 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
---|
320 | QScrollBar *pHorizontalScrollBar = horizontalScrollBar();
|
---|
321 | if (pHorizontalScrollBar)
|
---|
322 | pHorizontalScrollBar->setStyleSheet(horizontalScrollBarStyle);
|
---|
323 | }
|
---|
324 |
|
---|
325 | void UIVMLogViewerTextEdit::setCurrentFont(QFont font)
|
---|
326 | {
|
---|
327 | setFont(font);
|
---|
328 | if (m_pLineNumberArea)
|
---|
329 | m_pLineNumberArea->setFont(font);
|
---|
330 | }
|
---|
331 |
|
---|
332 | void UIVMLogViewerTextEdit::saveScrollBarPosition()
|
---|
333 | {
|
---|
334 | if (verticalScrollBar())
|
---|
335 | m_iVerticalScrollBarValue = verticalScrollBar()->value();
|
---|
336 | }
|
---|
337 |
|
---|
338 | void UIVMLogViewerTextEdit::restoreScrollBarPosition()
|
---|
339 | {
|
---|
340 | QScrollBar *pBar = verticalScrollBar();
|
---|
341 | if (pBar && pBar->maximum() >= m_iVerticalScrollBarValue && pBar->minimum() <= m_iVerticalScrollBarValue)
|
---|
342 | pBar->setValue(m_iVerticalScrollBarValue);
|
---|
343 | }
|
---|
344 |
|
---|
345 | void UIVMLogViewerTextEdit::setCursorPosition(int iPosition)
|
---|
346 | {
|
---|
347 | QTextCursor cursor = textCursor();
|
---|
348 | cursor.setPosition(iPosition);
|
---|
349 | setTextCursor(cursor);
|
---|
350 | centerCursor();
|
---|
351 | }
|
---|
352 |
|
---|
353 | int UIVMLogViewerTextEdit::lineNumberAreaWidth()
|
---|
354 | {
|
---|
355 | if (!m_bShowLineNumbers)
|
---|
356 | return 0;
|
---|
357 |
|
---|
358 | int digits = 1;
|
---|
359 | int max = qMax(1, blockCount());
|
---|
360 | while (max >= 10) {
|
---|
361 | max /= 10;
|
---|
362 | ++digits;
|
---|
363 | }
|
---|
364 |
|
---|
365 | #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
|
---|
366 | int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
|
---|
367 | #else
|
---|
368 | int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
|
---|
369 | #endif
|
---|
370 |
|
---|
371 | return space;
|
---|
372 | }
|
---|
373 |
|
---|
374 | void UIVMLogViewerTextEdit::lineNumberAreaPaintEvent(QPaintEvent *event)
|
---|
375 | {
|
---|
376 | if (!m_bShowLineNumbers)
|
---|
377 | return;
|
---|
378 | QPainter painter(m_pLineNumberArea);
|
---|
379 | painter.fillRect(event->rect(), Qt::lightGray);
|
---|
380 | QTextBlock block = firstVisibleBlock();
|
---|
381 | int blockNumber = block.blockNumber();
|
---|
382 | int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
|
---|
383 | int bottom = top + (int) blockBoundingRect(block).height();
|
---|
384 | while (block.isValid() && top <= event->rect().bottom()) {
|
---|
385 | if (block.isVisible() && bottom >= event->rect().top()) {
|
---|
386 | QString number = QString::number(blockNumber + 1);
|
---|
387 | /* Mark this line if it is bookmarked, but only if the text is not filtered. */
|
---|
388 | if (m_bookmarkLineSet.contains(blockNumber + 1) && !m_bShownTextIsFiltered)
|
---|
389 | {
|
---|
390 | QPainterPath path;
|
---|
391 | path.addRect(0, top, m_pLineNumberArea->width(), m_pLineNumberArea->fontMetrics().lineSpacing());
|
---|
392 | painter.fillPath(path, QColor(204, 255, 51, 125));
|
---|
393 | painter.drawPath(path);
|
---|
394 | }
|
---|
395 | /* Draw a unfilled red rectangled around the line number to indicate line the mouse cursor is currently
|
---|
396 | hovering on. Do this only if mouse is over the ext edit or the context menu is around: */
|
---|
397 | if ((blockNumber + 1) == m_mouseCursorLine && (underMouse() || m_bHasContextMenu))
|
---|
398 | {
|
---|
399 | painter.setPen(Qt::red);
|
---|
400 | painter.drawRect(0, top, m_pLineNumberArea->width(), m_pLineNumberArea->fontMetrics().lineSpacing());
|
---|
401 | }
|
---|
402 |
|
---|
403 | painter.setPen(Qt::black);
|
---|
404 | painter.drawText(0, top, m_pLineNumberArea->width(), m_pLineNumberArea->fontMetrics().lineSpacing(),
|
---|
405 | Qt::AlignRight, number);
|
---|
406 | }
|
---|
407 | block = block.next();
|
---|
408 | top = bottom;
|
---|
409 | bottom = top + (int) blockBoundingRect(block).height();
|
---|
410 | ++blockNumber;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | void UIVMLogViewerTextEdit::sltRetranslateUI()
|
---|
415 | {
|
---|
416 | m_strBackgroungText = QString(UIVMLogViewerWidget::tr("Filtered"));
|
---|
417 | }
|
---|
418 |
|
---|
419 | bool UIVMLogViewerTextEdit::eventFilter(QObject *pObject, QEvent *pEvent)
|
---|
420 | {
|
---|
421 | if (pObject == m_pScrollToBottomLabel || pObject == m_pScrollToTopLabel)
|
---|
422 | {
|
---|
423 | UILogScrollLabel *pLabel = qobject_cast<UILogScrollLabel*>(pObject);
|
---|
424 | if (pLabel)
|
---|
425 | {
|
---|
426 | switch(pEvent->type())
|
---|
427 | {
|
---|
428 | case (QEvent::Enter):
|
---|
429 | {
|
---|
430 | setCursor(Qt::PointingHandCursor);
|
---|
431 | pLabel->setOpacity(1.0);
|
---|
432 | break;
|
---|
433 | }
|
---|
434 | case (QEvent::Leave):
|
---|
435 | {
|
---|
436 | setCursor(m_originalCursor);
|
---|
437 | pLabel->setOpacity(0.4);
|
---|
438 | break;
|
---|
439 | }
|
---|
440 | case (QEvent::MouseButtonPress):
|
---|
441 | {
|
---|
442 | if (pObject == m_pScrollToBottomLabel)
|
---|
443 | scrollToBottom();
|
---|
444 | else
|
---|
445 | scrollToTop();
|
---|
446 | break;
|
---|
447 | }
|
---|
448 | default:
|
---|
449 | break;
|
---|
450 | }
|
---|
451 | }
|
---|
452 | }
|
---|
453 | return QPlainTextEdit::eventFilter(pObject, pEvent);
|
---|
454 | }
|
---|
455 |
|
---|
456 | void UIVMLogViewerTextEdit::contextMenuEvent(QContextMenuEvent *pEvent)
|
---|
457 | {
|
---|
458 | /* If shown text is filtered, do not create Bookmark action since
|
---|
459 | we disable all bookmarking related functionalities in this case. */
|
---|
460 | if (m_bShownTextIsFiltered)
|
---|
461 | {
|
---|
462 | QPlainTextEdit::contextMenuEvent(pEvent);
|
---|
463 | return;
|
---|
464 | }
|
---|
465 | m_bHasContextMenu = true;
|
---|
466 | QMenu *menu = createStandardContextMenu();
|
---|
467 |
|
---|
468 |
|
---|
469 | QAction *pAction = menu->addAction(UIVMLogViewerWidget::tr("Bookmark"));
|
---|
470 | if (pAction)
|
---|
471 | {
|
---|
472 | pAction->setCheckable(true);
|
---|
473 | UIVMLogBookmark menuBookmark = bookmarkForPos(pEvent->pos());
|
---|
474 | pAction->setChecked(m_bookmarkLineSet.contains(menuBookmark.m_iLineNumber));
|
---|
475 | if (pAction->isChecked())
|
---|
476 | pAction->setIcon(UIIconPool::iconSet(":/log_viewer_bookmark_on_16px.png"));
|
---|
477 | else
|
---|
478 | pAction->setIcon(UIIconPool::iconSet(":/log_viewer_bookmark_off_16px.png"));
|
---|
479 |
|
---|
480 | m_iContextMenuBookmark = menuBookmark;
|
---|
481 | connect(pAction, &QAction::triggered, this, &UIVMLogViewerTextEdit::sltBookmark);
|
---|
482 |
|
---|
483 | }
|
---|
484 | menu->exec(pEvent->globalPos());
|
---|
485 |
|
---|
486 | if (pAction)
|
---|
487 | disconnect(pAction, &QAction::triggered, this, &UIVMLogViewerTextEdit::sltBookmark);
|
---|
488 |
|
---|
489 | delete menu;
|
---|
490 | m_bHasContextMenu = false;
|
---|
491 | }
|
---|
492 |
|
---|
493 | void UIVMLogViewerTextEdit::resizeEvent(QResizeEvent *pEvent)
|
---|
494 | {
|
---|
495 | QPlainTextEdit::resizeEvent(pEvent);
|
---|
496 | if (m_pLineNumberArea)
|
---|
497 | {
|
---|
498 | QRect cr = contentsRect();
|
---|
499 | m_pLineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
|
---|
500 | }
|
---|
501 |
|
---|
502 | if (m_pScrollToBottomLabel && m_pScrollToTopLabel)
|
---|
503 | {
|
---|
504 | QScrollBar *pVScrollBar = verticalScrollBar();
|
---|
505 | QScrollBar *pHScrollBar = horizontalScrollBar();
|
---|
506 |
|
---|
507 | int iMarginX = 0;
|
---|
508 | if (pVScrollBar)
|
---|
509 | iMarginX = pVScrollBar->width();
|
---|
510 | if (iMarginX == 0)
|
---|
511 | iMarginX = m_pScrollToBottomLabel->width();
|
---|
512 | iMarginX += 2 * QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin);
|
---|
513 | int iMarginY = 0;
|
---|
514 | if (pHScrollBar)
|
---|
515 | iMarginY = pHScrollBar->height();
|
---|
516 | if (iMarginY == 0)
|
---|
517 | iMarginY = m_pScrollToTopLabel->height();
|
---|
518 |
|
---|
519 |
|
---|
520 | m_pScrollToBottomLabel->move(width() - iMarginX - m_pScrollToBottomLabel->width(),
|
---|
521 | 0.5 * m_pScrollToBottomLabel->height());
|
---|
522 |
|
---|
523 | m_pScrollToTopLabel->move(width() - iMarginX - m_pScrollToTopLabel->width(),
|
---|
524 | height() - iMarginY - 1.5 * m_pScrollToTopLabel->height());
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | void UIVMLogViewerTextEdit::mouseMoveEvent(QMouseEvent *pEvent)
|
---|
529 | {
|
---|
530 | setMouseCursorLine(lineNumberForPos(pEvent->position().toPoint()));
|
---|
531 | if (m_pLineNumberArea)
|
---|
532 | m_pLineNumberArea->update();
|
---|
533 | QPlainTextEdit::mouseMoveEvent(pEvent);
|
---|
534 | }
|
---|
535 |
|
---|
536 | void UIVMLogViewerTextEdit::leaveEvent(QEvent * pEvent)
|
---|
537 | {
|
---|
538 | QPlainTextEdit::leaveEvent(pEvent);
|
---|
539 | /* Force a redraw as mouse leaves this to remove the mouse
|
---|
540 | cursor track rectangle (the red rectangle we draw on the line number area). */
|
---|
541 | update();
|
---|
542 | }
|
---|
543 |
|
---|
544 | void UIVMLogViewerTextEdit::sltUpdateLineNumberAreaWidth(int /* newBlockCount */)
|
---|
545 | {
|
---|
546 | setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
|
---|
547 | }
|
---|
548 |
|
---|
549 | void UIVMLogViewerTextEdit::sltHandleUpdateRequest(const QRect &rect, int dy)
|
---|
550 | {
|
---|
551 | if (dy)
|
---|
552 | m_pLineNumberArea->scroll(0, dy);
|
---|
553 | else
|
---|
554 | m_pLineNumberArea->update(0, rect.y(), m_pLineNumberArea->width(), rect.height());
|
---|
555 |
|
---|
556 | if (rect.contains(viewport()->rect()))
|
---|
557 | sltUpdateLineNumberAreaWidth(0);
|
---|
558 |
|
---|
559 | if (viewport())
|
---|
560 | viewport()->update();
|
---|
561 | }
|
---|
562 |
|
---|
563 | void UIVMLogViewerTextEdit::sltBookmark()
|
---|
564 | {
|
---|
565 | toggleBookmark(m_iContextMenuBookmark);
|
---|
566 | }
|
---|
567 |
|
---|
568 | void UIVMLogViewerTextEdit::setScrollBarMarkingsVector(const QVector<float> &vector)
|
---|
569 | {
|
---|
570 | UIIndicatorScrollBar* vScrollBar = qobject_cast<UIIndicatorScrollBar*>(verticalScrollBar());
|
---|
571 | if (vScrollBar)
|
---|
572 | vScrollBar->setMarkingsVector(vector);
|
---|
573 | }
|
---|
574 |
|
---|
575 | void UIVMLogViewerTextEdit::clearScrollBarMarkingsVector()
|
---|
576 | {
|
---|
577 | UIIndicatorScrollBar* vScrollBar = qobject_cast<UIIndicatorScrollBar*>(verticalScrollBar());
|
---|
578 | if (vScrollBar)
|
---|
579 | vScrollBar->clearMarkingsVector();
|
---|
580 | }
|
---|
581 |
|
---|
582 | void UIVMLogViewerTextEdit::scrollToLine(int lineNumber)
|
---|
583 | {
|
---|
584 | QTextDocument* pDocument = document();
|
---|
585 | if (!pDocument)
|
---|
586 | return;
|
---|
587 |
|
---|
588 | moveCursor(QTextCursor::End);
|
---|
589 | int halfPageLineCount = 0.5 * visibleLineCount() ;
|
---|
590 | QTextCursor cursor(pDocument->findBlockByLineNumber(qMax(lineNumber - halfPageLineCount, 0)));
|
---|
591 | setTextCursor(cursor);
|
---|
592 | }
|
---|
593 |
|
---|
594 | void UIVMLogViewerTextEdit::scrollToBottom()
|
---|
595 | {
|
---|
596 | moveCursor(QTextCursor::End);
|
---|
597 | ensureCursorVisible();
|
---|
598 | }
|
---|
599 |
|
---|
600 | void UIVMLogViewerTextEdit::scrollToTop()
|
---|
601 | {
|
---|
602 | moveCursor(QTextCursor::Start);
|
---|
603 | ensureCursorVisible();
|
---|
604 | }
|
---|
605 |
|
---|
606 | int UIVMLogViewerTextEdit::visibleLineCount()
|
---|
607 | {
|
---|
608 | int height = 0;
|
---|
609 | if (viewport())
|
---|
610 | height = viewport()->height();
|
---|
611 | if (verticalScrollBar() && verticalScrollBar()->isVisible())
|
---|
612 | height -= horizontalScrollBar()->height();
|
---|
613 | int singleLineHeight = fontMetrics().lineSpacing();
|
---|
614 | if (singleLineHeight == 0)
|
---|
615 | return 0;
|
---|
616 | return height / singleLineHeight;
|
---|
617 | }
|
---|
618 |
|
---|
619 | void UIVMLogViewerTextEdit::setBookmarkLineSet(const QSet<int>& lineSet)
|
---|
620 | {
|
---|
621 | m_bookmarkLineSet = lineSet;
|
---|
622 | update();
|
---|
623 | }
|
---|
624 |
|
---|
625 | int UIVMLogViewerTextEdit::lineNumberForPos(const QPoint &position)
|
---|
626 | {
|
---|
627 | QTextCursor cursor = cursorForPosition(position);
|
---|
628 | QTextBlock block = cursor.block();
|
---|
629 | return block.blockNumber() + 1;
|
---|
630 | }
|
---|
631 |
|
---|
632 | UIVMLogBookmark UIVMLogViewerTextEdit::bookmarkForPos(const QPoint &position)
|
---|
633 | {
|
---|
634 | QTextCursor cursor = cursorForPosition(position);
|
---|
635 | QTextBlock block = cursor.block();
|
---|
636 | return UIVMLogBookmark(block.blockNumber() + 1, cursor.position(), block.text());
|
---|
637 | }
|
---|
638 |
|
---|
639 | void UIVMLogViewerTextEdit::setMouseCursorLine(int lineNumber)
|
---|
640 | {
|
---|
641 | m_mouseCursorLine = lineNumber;
|
---|
642 | }
|
---|
643 |
|
---|
644 | void UIVMLogViewerTextEdit::toggleBookmark(const UIVMLogBookmark& bookmark)
|
---|
645 | {
|
---|
646 | if (m_bShownTextIsFiltered)
|
---|
647 | return;
|
---|
648 |
|
---|
649 | if (m_bookmarkLineSet.contains(bookmark.m_iLineNumber))
|
---|
650 | emit sigDeleteBookmark(bookmark);
|
---|
651 | else
|
---|
652 | emit sigAddBookmark(bookmark);
|
---|
653 | }
|
---|
654 |
|
---|
655 | void UIVMLogViewerTextEdit::setShownTextIsFiltered(bool warning)
|
---|
656 | {
|
---|
657 | if (m_bShownTextIsFiltered == warning)
|
---|
658 | return;
|
---|
659 | m_bShownTextIsFiltered = warning;
|
---|
660 | if (viewport())
|
---|
661 | viewport()->update();
|
---|
662 | }
|
---|
663 |
|
---|
664 | void UIVMLogViewerTextEdit::setShowLineNumbers(bool bShowLineNumbers)
|
---|
665 | {
|
---|
666 | if (m_bShowLineNumbers == bShowLineNumbers)
|
---|
667 | return;
|
---|
668 | m_bShowLineNumbers = bShowLineNumbers;
|
---|
669 | emit updateRequest(viewport()->rect(), 0);
|
---|
670 | }
|
---|
671 |
|
---|
672 | bool UIVMLogViewerTextEdit::showLineNumbers() const
|
---|
673 | {
|
---|
674 | return m_bShowLineNumbers;
|
---|
675 | }
|
---|
676 |
|
---|
677 | void UIVMLogViewerTextEdit::setWrapLines(bool bWrapLines)
|
---|
678 | {
|
---|
679 | if (m_bWrapLines == bWrapLines)
|
---|
680 | return;
|
---|
681 | m_bWrapLines = bWrapLines;
|
---|
682 | if (m_bWrapLines)
|
---|
683 | {
|
---|
684 | setLineWrapMode(QPlainTextEdit::WidgetWidth);
|
---|
685 | setWordWrapMode(QTextOption::WordWrap);
|
---|
686 | }
|
---|
687 | else
|
---|
688 | {
|
---|
689 | setLineWrapMode(QPlainTextEdit::NoWrap);
|
---|
690 | setWordWrapMode(QTextOption::NoWrap);
|
---|
691 | }
|
---|
692 | update();
|
---|
693 | }
|
---|
694 |
|
---|
695 | bool UIVMLogViewerTextEdit::wrapLines() const
|
---|
696 | {
|
---|
697 | return m_bWrapLines;
|
---|
698 | }
|
---|
699 |
|
---|
700 | int UIVMLogViewerTextEdit::currentVerticalScrollBarValue() const
|
---|
701 | {
|
---|
702 | if (!verticalScrollBar())
|
---|
703 | return -1;
|
---|
704 | return verticalScrollBar()->value();
|
---|
705 | }
|
---|
706 |
|
---|
707 | void UIVMLogViewerTextEdit::setCurrentVerticalScrollBarValue(int value)
|
---|
708 | {
|
---|
709 | if (!verticalScrollBar())
|
---|
710 | return;
|
---|
711 |
|
---|
712 | setCenterOnScroll(true);
|
---|
713 |
|
---|
714 | verticalScrollBar()->setValue(value);
|
---|
715 | verticalScrollBar()->setSliderPosition(value);
|
---|
716 | viewport()->update();
|
---|
717 | update();
|
---|
718 | }
|
---|
719 |
|
---|
720 | #include "UIVMLogViewerTextEdit.moc"
|
---|