7#include <QtCore/QFileInfo>
8#include <QtCore/QTextStream>
10#include <QtGui/QTextCharFormat>
11#include <QtGui/QTextBlock>
12#include <QtGui/QTextCursor>
24void SourceCodeView::setSourceContext(
const QString &fileName,
const int lineNum)
29 if (fileName.isEmpty()) {
31 m_currentFileName.clear();
32 appendHtml(tr(
"<i>Source code not available</i>"));
37 showSourceCode(fileName, lineNum);
39 m_fileToLoad = fileName;
40 m_lineNumToLoad = lineNum;
44void SourceCodeView::setActivated(
bool activated)
46 m_isActive = activated;
47 if (activated && !m_fileToLoad.isEmpty()) {
48 showSourceCode(m_fileToLoad, m_lineNumToLoad);
53void SourceCodeView::showSourceCode(
const QString &absFileName,
const int lineNum)
55 QString fileText = fileHash.value(absFileName);
57 if (fileText.isNull()) {
58 m_currentFileName.clear();
61 QFile file(absFileName);
65 appendHtml(tr(
"<i>File %1 not available</i>").arg(absFileName));
68 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
70 appendHtml(tr(
"<i>File %1 not readable</i>").arg(absFileName));
73 fileText = QString::fromUtf8(file.readAll());
74 fileHash.insert(absFileName, fileText);
78 if (m_currentFileName != absFileName) {
79 setPlainText(fileText);
80 m_currentFileName = absFileName;
83 QTextCursor cursor = textCursor();
84 cursor.setPosition(document()->findBlockByNumber(lineNum - 1).position());
85 setTextCursor(cursor);
87 cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
88 cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
90 QTextEdit::ExtraSelection selectedLine;
91 selectedLine.cursor = cursor;
94 const QColor fg = palette().color(QPalette::Highlight);
95 const QColor bg = palette().color(QPalette::Base);
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));
102 selectedLine.format.setBackground(col);
103 selectedLine.format.setProperty(QTextFormat::FullWidthSelection,
true);
104 setExtraSelections(QList<QTextEdit::ExtraSelection>() << selectedLine);
Combined button and popup list for selecting options.