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 QPair<QString, QDateTime> hashKey {absFileName, QFileInfo(absFileName).lastModified()};
56 QString fileText = fileHash.value(hashKey);
57
58 if (fileText.isNull()) { // File not in hash
59 m_currentFileName.clear();
60
61 // Assume fileName is relative to directory
62 QFile file(absFileName);
63
64 if (!file.exists()) {
65 clear();
66 appendHtml(tr("<i>File %1 not available</i>").arg(absFileName));
67 return;
68 }
69 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
70 clear();
71 appendHtml(tr("<i>File %1 not readable</i>").arg(absFileName));
72 return;
73 }
74 fileText = QString::fromUtf8(file.readAll());
75 fileHash.insert(hashKey, fileText);
76 }
77
78
79 if (m_currentFileName != absFileName) {
80 setPlainText(fileText);
81 m_currentFileName = absFileName;
82 }
83
84 QTextCursor cursor = textCursor();
85 cursor.setPosition(document()->findBlockByNumber(lineNum - 1).position());
86 setTextCursor(cursor);
87 centerCursor();
88 cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
89 cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
90
91 QTextEdit::ExtraSelection selectedLine;
92 selectedLine.cursor = cursor;
93
94 // Define custom color for line selection
95 const QColor fg = palette().color(QPalette::Highlight);
96 const QColor bg = palette().color(QPalette::Base);
97 QColor col;
98 const qreal ratio = 0.25;
99 col.setRedF(fg.redF() * ratio + bg.redF() * (1 - ratio));
100 col.setGreenF(fg.greenF() * ratio + bg.greenF() * (1 - ratio));
101 col.setBlueF(fg.blueF() * ratio + bg.blueF() * (1 - ratio));
102
103 selectedLine.format.setBackground(col);
104 selectedLine.format.setProperty(QTextFormat::FullWidthSelection, true);
105 setExtraSelections(QList<QTextEdit::ExtraSelection>() << selectedLine);
106}
107
108QT_END_NAMESPACE
QObject * parent
Definition qobject.h:73
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99