1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Linguist of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "sourcecodeview.h"
30
31#include <QtCore/QFile>
32#include <QtCore/QFileInfo>
33#include <QtCore/QTextStream>
34
35#include <QtGui/QTextCharFormat>
36#include <QtGui/QTextBlock>
37#include <QtGui/QTextCursor>
38
39QT_BEGIN_NAMESPACE
40
41SourceCodeView::SourceCodeView(QWidget *parent)
42 : QPlainTextEdit(parent),
43 m_isActive(true),
44 m_lineNumToLoad(0)
45{
46 setReadOnly(true);
47}
48
49void SourceCodeView::setSourceContext(const QString &fileName, const int lineNum)
50{
51 m_fileToLoad.clear();
52 setToolTip(fileName);
53
54 if (fileName.isEmpty()) {
55 clear();
56 m_currentFileName.clear();
57 appendHtml(html: tr(s: "<i>Source code not available</i>"));
58 return;
59 }
60
61 if (m_isActive) {
62 showSourceCode(fileName, lineNum);
63 } else {
64 m_fileToLoad = fileName;
65 m_lineNumToLoad = lineNum;
66 }
67}
68
69void SourceCodeView::setActivated(bool activated)
70{
71 m_isActive = activated;
72 if (activated && !m_fileToLoad.isEmpty()) {
73 showSourceCode(fileName: m_fileToLoad, lineNum: m_lineNumToLoad);
74 m_fileToLoad.clear();
75 }
76}
77
78void SourceCodeView::showSourceCode(const QString &absFileName, const int lineNum)
79{
80 QString fileText = fileHash.value(akey: absFileName);
81
82 if (fileText.isNull()) { // File not in hash
83 m_currentFileName.clear();
84
85 // Assume fileName is relative to directory
86 QFile file(absFileName);
87
88 if (!file.exists()) {
89 clear();
90 appendHtml(html: tr(s: "<i>File %1 not available</i>").arg(a: absFileName));
91 return;
92 }
93 if (!file.open(flags: QIODevice::ReadOnly | QIODevice::Text)) {
94 clear();
95 appendHtml(html: tr(s: "<i>File %1 not readable</i>").arg(a: absFileName));
96 return;
97 }
98 fileText = QString::fromUtf8(str: file.readAll());
99 fileHash.insert(akey: absFileName, avalue: fileText);
100 }
101
102
103 if (m_currentFileName != absFileName) {
104 setPlainText(fileText);
105 m_currentFileName = absFileName;
106 }
107
108 QTextCursor cursor = textCursor();
109 cursor.setPosition(pos: document()->findBlockByNumber(blockNumber: lineNum - 1).position());
110 setTextCursor(cursor);
111 centerCursor();
112 cursor.movePosition(op: QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
113 cursor.movePosition(op: QTextCursor::Right, QTextCursor::KeepAnchor);
114
115 QTextEdit::ExtraSelection selectedLine;
116 selectedLine.cursor = cursor;
117
118 // Define custom color for line selection
119 const QColor fg = palette().color(cr: QPalette::Highlight);
120 const QColor bg = palette().color(cr: QPalette::Base);
121 QColor col;
122 const qreal ratio = 0.25;
123 col.setRedF(fg.redF() * ratio + bg.redF() * (1 - ratio));
124 col.setGreenF(fg.greenF() * ratio + bg.greenF() * (1 - ratio));
125 col.setBlueF(fg.blueF() * ratio + bg.blueF() * (1 - ratio));
126
127 selectedLine.format.setBackground(col);
128 selectedLine.format.setProperty(propertyId: QTextFormat::FullWidthSelection, value: true);
129 setExtraSelections(QList<QTextEdit::ExtraSelection>() << selectedLine);
130}
131
132QT_END_NAMESPACE
133

source code of qttools/src/linguist/linguist/sourcecodeview.cpp