VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogPage.cpp

Last change on this file was 103940, checked in by vboxsync, 2 months ago

FE/Qt. bugref:6699. Some refactoring.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1/* $Id: UIVMLogPage.cpp 103940 2024-03-20 09:14:53Z 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 <QHBoxLayout>
30#if defined(RT_OS_SOLARIS)
31# include <QFontDatabase>
32#endif
33#include <QScrollBar>
34
35/* GUI includes: */
36#include "UIVMLogPage.h"
37#include "UIVMLogViewerTextEdit.h"
38
39
40/*********************************************************************************************************************************
41* UIVMLogBookmarkManager definition. *
42*********************************************************************************************************************************/
43
44class UIVMLogBookmarkManager
45{
46public:
47 void addBookmark(const UIVMLogBookmark& newBookmark);
48 void addBookmark(int iCursorPosition, int iLineNumber, QString strBlockText);
49 void deleteBookmark(const UIVMLogBookmark& bookmark);
50 void deleteBookmarkByIndex(int iIndex);
51 void deleteAllBookmarks();
52 int cursorPosition(int bookmarkIndex);
53 QSet<int> lineSet() const;
54 const QVector<UIVMLogBookmark>& bookmarkList() const;
55
56private:
57
58 QVector<UIVMLogBookmark> m_bookmarks;
59};
60
61
62/*********************************************************************************************************************************
63* UIVMLogBookmarkManager implementation. *
64*********************************************************************************************************************************/
65
66
67void UIVMLogBookmarkManager::addBookmark(const UIVMLogBookmark& newBookmark)
68{
69 foreach (const UIVMLogBookmark& bookmark, m_bookmarks)
70 if (bookmark == newBookmark)
71 return;
72 m_bookmarks << newBookmark;
73}
74
75void UIVMLogBookmarkManager::addBookmark(int iCursorPosition, int iLineNumber, QString strBlockText)
76{
77 foreach (const UIVMLogBookmark& bookmark, m_bookmarks)
78 if (bookmark.m_iLineNumber == iLineNumber)
79 return;
80 m_bookmarks << UIVMLogBookmark(iCursorPosition, iLineNumber, strBlockText);
81}
82
83void UIVMLogBookmarkManager::deleteBookmark(const UIVMLogBookmark& bookmark)
84{
85 int index = -1;
86 for (int i = 0; i < m_bookmarks.size() && index == -1; ++i)
87 {
88 if (bookmark == m_bookmarks[i])
89 index = i;
90 }
91 deleteBookmarkByIndex(index);
92}
93
94void UIVMLogBookmarkManager::deleteBookmarkByIndex(int iIndex)
95{
96 if (iIndex >= m_bookmarks.size() || iIndex < 0)
97 return;
98 m_bookmarks.removeAt(iIndex);
99}
100
101void UIVMLogBookmarkManager::deleteAllBookmarks()
102{
103 m_bookmarks.clear();
104}
105
106int UIVMLogBookmarkManager::cursorPosition(int bookmarkIndex)
107{
108 if (bookmarkIndex >= m_bookmarks.size())
109 return 0;
110 return m_bookmarks[bookmarkIndex].m_iCursorPosition;
111}
112
113QSet<int> UIVMLogBookmarkManager::lineSet() const
114{
115 QSet<int> lines;
116 foreach (const UIVMLogBookmark& bookmark, m_bookmarks)
117 lines << bookmark.m_iLineNumber;
118 return lines;
119}
120
121const QVector<UIVMLogBookmark>& UIVMLogBookmarkManager::bookmarkList() const
122{
123 return m_bookmarks;
124}
125
126
127/*********************************************************************************************************************************
128* UIVMLogTab implementation. *
129*********************************************************************************************************************************/
130
131UIVMLogTab::UIVMLogTab(QWidget *pParent, const QUuid &uMachineId, const QString &strMachineName)
132 : QWidget(pParent)
133 , m_uMachineId(uMachineId)
134 , m_strMachineName(strMachineName)
135{
136}
137const QUuid &UIVMLogTab::machineId() const
138{
139 return m_uMachineId;
140}
141
142const QString UIVMLogTab::machineName() const
143{
144 return m_strMachineName;
145}
146
147
148/*********************************************************************************************************************************
149* UIVMLogPage implementation. *
150*********************************************************************************************************************************/
151
152UIVMLogPage::UIVMLogPage(QWidget *pParent, const QUuid &uMachineId, const QString &strMachineName)
153 : UIVMLogTab(pParent, uMachineId, strMachineName)
154 , m_pMainLayout(0)
155 , m_pTextEdit(0)
156 , m_pBookmarkManager(new UIVMLogBookmarkManager)
157 , m_iSelectedBookmarkIndex(-1)
158 , m_bFiltered(false)
159 , m_iLogFileId(-1)
160{
161 prepare();
162}
163
164UIVMLogPage::~UIVMLogPage()
165{
166 cleanup();
167}
168
169int UIVMLogPage::defaultLogPageWidth() const
170{
171 if (!m_pTextEdit)
172 return 0;
173
174 /* Compute a width for 132 characters plus scrollbar and frame width: */
175#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
176 int iDefaultWidth = m_pTextEdit->fontMetrics().horizontalAdvance(QChar('x')) * 132 +
177#else
178 int iDefaultWidth = m_pTextEdit->fontMetrics().width(QChar('x')) * 132 +
179#endif
180 m_pTextEdit->verticalScrollBar()->width() +
181 m_pTextEdit->frameWidth() * 2;
182
183 return iDefaultWidth;
184}
185
186
187void UIVMLogPage::prepare()
188{
189 prepareWidgets();
190}
191
192void UIVMLogPage::prepareWidgets()
193{
194 m_pMainLayout = new QHBoxLayout();
195 setLayout(m_pMainLayout);
196 m_pMainLayout->setSpacing(0);
197 m_pMainLayout->setContentsMargins(0, 0, 0, 0);
198
199 m_pTextEdit = new UIVMLogViewerTextEdit(this);
200 m_pMainLayout->addWidget(m_pTextEdit);
201
202 connect(m_pTextEdit, &UIVMLogViewerTextEdit::sigAddBookmark, this, &UIVMLogPage::sltAddBookmark);
203 connect(m_pTextEdit, &UIVMLogViewerTextEdit::sigDeleteBookmark, this, &UIVMLogPage::sltDeleteBookmark);
204}
205
206QPlainTextEdit *UIVMLogPage::textEdit()
207{
208 return m_pTextEdit;
209}
210
211QTextDocument* UIVMLogPage::document()
212{
213 if (!m_pTextEdit)
214 return 0;
215 return m_pTextEdit->document();
216}
217
218void UIVMLogPage::cleanup()
219{
220 delete m_pBookmarkManager;
221}
222
223void UIVMLogPage::setLogContent(const QString &strLogContent, bool fError)
224{
225 if (!fError)
226 {
227 m_strLog = strLogContent;
228 setTextEditText(strLogContent);
229 }
230 else
231 {
232 markForError();
233 setTextEditTextAsHtml(strLogContent);
234 }
235}
236
237const QString& UIVMLogPage::logString() const
238{
239 return m_strLog;
240}
241
242void UIVMLogPage::setLogFileName(const QString &strLogFileName)
243{
244 m_strLogFileName = strLogFileName;
245}
246
247const QString& UIVMLogPage::logFileName() const
248{
249 return m_strLogFileName;
250}
251
252void UIVMLogPage::setTextEditText(const QString &strText)
253{
254 if (!m_pTextEdit)
255 return;
256
257 m_pTextEdit->setPlainText(strText);
258 /* Move the cursor position to end: */
259 QTextCursor cursor = m_pTextEdit->textCursor();
260 cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
261 m_pTextEdit->setTextCursor(cursor);
262 update();
263}
264
265void UIVMLogPage::setTextEditTextAsHtml(const QString &strText)
266{
267 if (!m_pTextEdit)
268 return;
269 if (document())
270 document()->setHtml(strText);
271 update();
272}
273
274void UIVMLogPage::markForError()
275{
276 if (!m_pTextEdit)
277 return;
278 m_pTextEdit->setWrapLines(true);
279}
280
281void UIVMLogPage::setScrollBarMarkingsVector(const QVector<float> &vector)
282{
283 if (!m_pTextEdit)
284 return;
285 m_pTextEdit->setScrollBarMarkingsVector(vector);
286 update();
287}
288
289void UIVMLogPage::clearScrollBarMarkingsVector()
290{
291 if (!m_pTextEdit)
292 return;
293 m_pTextEdit->clearScrollBarMarkingsVector();
294 update();
295}
296
297void UIVMLogPage::documentUndo()
298{
299 if (!m_pTextEdit)
300 return;
301 if (m_pTextEdit->document())
302 m_pTextEdit->document()->undo();
303}
304
305
306
307void UIVMLogPage::deleteAllBookmarks()
308{
309 if (m_pBookmarkManager)
310 m_pBookmarkManager->deleteAllBookmarks();
311 updateTextEditBookmarkLineSet();
312}
313
314void UIVMLogPage::scrollToBookmark(int bookmarkIndex)
315{
316 if (!m_pTextEdit)
317 return;
318 if (m_pBookmarkManager)
319 m_pTextEdit->setCursorPosition(m_pBookmarkManager->cursorPosition(bookmarkIndex));
320}
321
322QVector<UIVMLogBookmark> UIVMLogPage::bookmarkList() const
323{
324 if (!m_pBookmarkManager)
325 return QVector<UIVMLogBookmark>();
326 return m_pBookmarkManager->bookmarkList();
327}
328
329void UIVMLogPage::sltAddBookmark(const UIVMLogBookmark& bookmark)
330{
331 if (m_pBookmarkManager)
332 m_pBookmarkManager->addBookmark(bookmark);
333 updateTextEditBookmarkLineSet();
334 emit sigBookmarksUpdated();
335}
336
337void UIVMLogPage::sltDeleteBookmark(const UIVMLogBookmark& bookmark)
338{
339 if (m_pBookmarkManager)
340 m_pBookmarkManager->deleteBookmark(bookmark);
341 updateTextEditBookmarkLineSet();
342 emit sigBookmarksUpdated();
343}
344
345void UIVMLogPage::deleteBookmarkByIndex(int iIndex)
346{
347 if (m_pBookmarkManager)
348 m_pBookmarkManager->deleteBookmarkByIndex(iIndex);
349 updateTextEditBookmarkLineSet();
350 emit sigBookmarksUpdated();
351}
352
353void UIVMLogPage::updateTextEditBookmarkLineSet()
354{
355 if (!m_pTextEdit)
356 return;
357 if (m_pBookmarkManager)
358 m_pTextEdit->setBookmarkLineSet(m_pBookmarkManager->lineSet());
359}
360
361bool UIVMLogPage::isFiltered() const
362{
363 return m_bFiltered;
364}
365
366void UIVMLogPage::setFiltered(bool filtered)
367{
368 if (m_bFiltered == filtered)
369 return;
370 m_bFiltered = filtered;
371 if (m_pTextEdit)
372 {
373 m_pTextEdit->setShownTextIsFiltered(m_bFiltered);
374 m_pTextEdit->update();
375 }
376 emit sigLogPageFilteredChanged(m_bFiltered);
377}
378
379void UIVMLogPage::setShowLineNumbers(bool bShowLineNumbers)
380{
381 if (!m_pTextEdit)
382 return;
383 m_pTextEdit->setShowLineNumbers(bShowLineNumbers);
384}
385
386void UIVMLogPage::setWrapLines(bool bWrapLines)
387{
388 if (!m_pTextEdit)
389 return;
390 m_pTextEdit->setWrapLines(bWrapLines);
391}
392
393QFont UIVMLogPage::currentFont() const
394{
395 if (!m_pTextEdit)
396 return QFont();
397 return m_pTextEdit->font();
398}
399
400void UIVMLogPage::setCurrentFont(QFont font)
401{
402 if (m_pTextEdit)
403 m_pTextEdit->setCurrentFont(font);
404}
405
406void UIVMLogPage::setLogFileId(int iLogFileId)
407{
408 m_iLogFileId = iLogFileId;
409}
410
411int UIVMLogPage::logFileId() const
412{
413 return m_iLogFileId;
414}
415
416void UIVMLogPage::scrollToEnd()
417{
418 if (m_pTextEdit)
419 m_pTextEdit->scrollToBottom();
420}
421
422void UIVMLogPage::saveScrollBarPosition()
423{
424 if (m_pTextEdit)
425 m_pTextEdit->saveScrollBarPosition();
426}
427
428void UIVMLogPage::restoreScrollBarPosition()
429{
430 if (m_pTextEdit)
431 m_pTextEdit->restoreScrollBarPosition();
432}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use