Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
sourcecodeview.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <QtCore/QFile>
7#include <QtCore/QFileInfo>
8#include <QtCore/QTextStream>
9
10#include <QtGui/QTextCharFormat>
11#include <QtGui/QTextBlock>
12#include <QtGui/QTextCursor>
13
15
16SourceCodeView::SourceCodeView(QWidget *parent)
18 m_isActive(true),
19 m_lineNumToLoad(0)
20{
21 setReadOnly(true);
22}
23
24void SourceCodeView::setSourceContext(const QString &fileName, const int lineNum)
25{
26 m_fileToLoad.clear();
27 setToolTip(fileName);
28
29 if (fileName.isEmpty()) {
30 clear();
31 m_currentFileName.clear();
32 appendHtml(tr("<i>Source code not available</i>"));
33 return;
34 }
35
36 if (m_isActive) {
37 showSourceCode(fileName, lineNum);
38 } else {
39 m_fileToLoad = fileName;
40 m_lineNumToLoad = lineNum;
41 }
42}
43
44void SourceCodeView::setActivated(bool activated)
45{
46 m_isActive = activated;
47 if (activated && !m_fileToLoad.isEmpty()) {
48 showSourceCode(m_fileToLoad, m_lineNumToLoad);
49 m_fileToLoad.clear();
50 }
51}
52
53void SourceCodeView::showSourceCode(const QString &absFileName, const int lineNum)
54{
55 QString fileText = fileHash.value(absFileName);
56
57 if (fileText.isNull()) { // File not in hash
58 m_currentFileName.clear();
59
60 // Assume fileName is relative to directory
61 QFile file(absFileName);
62
63 if (!file.exists()) {
64 clear();
65 appendHtml(tr("<i>File %1 not available</i>").arg(absFileName));
66 return;
67 }
68 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
69 clear();
70 appendHtml(tr("<i>File %1 not readable</i>").arg(absFileName));
71 return;
72 }
73 fileText = QString::fromUtf8(file.readAll());
74 fileHash.insert(absFileName, fileText);
75 }
76
77
78 if (m_currentFileName != absFileName) {
79 setPlainText(fileText);
80 m_currentFileName = absFileName;
81 }
82
83 QTextCursor cursor = textCursor();
84 cursor.setPosition(document()->findBlockByNumber(lineNum - 1).position());
85 setTextCursor(cursor);
86 centerCursor();
87 cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
88 cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
89
90 QTextEdit::ExtraSelection selectedLine;
91 selectedLine.cursor = cursor;
92
93 // Define custom color for line selection
94 const QColor fg = palette().color(QPalette::Highlight);
95 const QColor bg = palette().color(QPalette::Base);
96 QColor col;
97 const qreal ratio = 0.25;
98 col.setRedF(fg.redF() * ratio + bg.redF() * (1 - ratio));
99 col.setGreenF(fg.greenF() * ratio + bg.greenF() * (1 - ratio));
100 col.setBlueF(fg.blueF() * ratio + bg.blueF() * (1 - ratio));
101
102 selectedLine.format.setBackground(col);
103 selectedLine.format.setProperty(QTextFormat::FullWidthSelection, true);
104 setExtraSelections(QList<QTextEdit::ExtraSelection>() << selectedLine);
105}
106
107QT_END_NAMESPACE
QObject * parent
Definition qobject.h:73
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
Combined button and popup list for selecting options.