0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the Qt Linguist of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "sourcecodeview.h"
|
|
43 |
|
|
44 |
#include <QtCore/QFile>
|
|
45 |
#include <QtCore/QFileInfo>
|
|
46 |
#include <QtCore/QTextStream>
|
|
47 |
|
|
48 |
#include <QtGui/QTextCharFormat>
|
|
49 |
#include <QtGui/QTextBlock>
|
|
50 |
#include <QtGui/QTextCursor>
|
|
51 |
|
|
52 |
QT_BEGIN_NAMESPACE
|
|
53 |
|
|
54 |
SourceCodeView::SourceCodeView(QWidget *parent)
|
|
55 |
: QPlainTextEdit(parent),
|
|
56 |
m_isActive(true),
|
|
57 |
m_lineNumToLoad(0)
|
|
58 |
{
|
|
59 |
setReadOnly(true);
|
|
60 |
}
|
|
61 |
|
|
62 |
void SourceCodeView::setSourceContext(const QString &fileName, const int lineNum)
|
|
63 |
{
|
|
64 |
m_fileToLoad.clear();
|
|
65 |
setToolTip(fileName);
|
|
66 |
|
|
67 |
if (fileName.isNull()) {
|
|
68 |
clear();
|
|
69 |
m_currentFileName.clear();
|
|
70 |
appendHtml(tr("<i>Source code not available</i>"));
|
|
71 |
return;
|
|
72 |
}
|
|
73 |
|
|
74 |
if (m_isActive) {
|
|
75 |
showSourceCode(fileName, lineNum);
|
|
76 |
} else {
|
|
77 |
m_fileToLoad = fileName;
|
|
78 |
m_lineNumToLoad = lineNum;
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
void SourceCodeView::setActivated(bool activated)
|
|
83 |
{
|
|
84 |
m_isActive = activated;
|
|
85 |
if (activated && !m_fileToLoad.isEmpty()) {
|
|
86 |
showSourceCode(m_fileToLoad, m_lineNumToLoad);
|
|
87 |
m_fileToLoad.clear();
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
void SourceCodeView::showSourceCode(const QString &absFileName, const int lineNum)
|
|
92 |
{
|
|
93 |
QString fileText = fileHash.value(absFileName);
|
|
94 |
|
|
95 |
if (fileText.isNull()) { // File not in hash
|
|
96 |
m_currentFileName.clear();
|
|
97 |
|
|
98 |
// Assume fileName is relative to directory
|
|
99 |
QFile file(absFileName);
|
|
100 |
|
|
101 |
if (!file.exists()) {
|
|
102 |
clear();
|
|
103 |
appendHtml(tr("<i>File %1 not available</i>").arg(absFileName));
|
|
104 |
return;
|
|
105 |
}
|
|
106 |
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
107 |
clear();
|
|
108 |
appendHtml(tr("<i>File %1 not readable</i>").arg(absFileName));
|
|
109 |
return;
|
|
110 |
}
|
|
111 |
fileText = QString::fromLatin1(file.readAll());
|
|
112 |
fileHash.insert(absFileName, fileText);
|
|
113 |
}
|
|
114 |
|
|
115 |
|
|
116 |
if (m_currentFileName != absFileName) {
|
|
117 |
setPlainText(fileText);
|
|
118 |
m_currentFileName = absFileName;
|
|
119 |
}
|
|
120 |
|
|
121 |
QTextCursor cursor = textCursor();
|
|
122 |
cursor.setPosition(document()->findBlockByNumber(lineNum - 1).position());
|
|
123 |
setTextCursor(cursor);
|
|
124 |
centerCursor();
|
|
125 |
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
|
126 |
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
|
|
127 |
|
|
128 |
QTextEdit::ExtraSelection selectedLine;
|
|
129 |
selectedLine.cursor = cursor;
|
|
130 |
|
|
131 |
// Define custom color for line selection
|
|
132 |
const QColor fg = palette().color(QPalette::Highlight);
|
|
133 |
const QColor bg = palette().color(QPalette::Base);
|
|
134 |
QColor col;
|
|
135 |
const qreal ratio = 0.25;
|
|
136 |
col.setRedF(fg.redF() * ratio + bg.redF() * (1 - ratio));
|
|
137 |
col.setGreenF(fg.greenF() * ratio + bg.greenF() * (1 - ratio));
|
|
138 |
col.setBlueF(fg.blueF() * ratio + bg.blueF() * (1 - ratio));
|
|
139 |
|
|
140 |
selectedLine.format.setBackground(col);
|
|
141 |
selectedLine.format.setProperty(QTextFormat::FullWidthSelection, true);
|
|
142 |
setExtraSelections(QList<QTextEdit::ExtraSelection>() << selectedLine);
|
|
143 |
}
|
|
144 |
|
|
145 |
QT_END_NAMESPACE
|