File: latexlog.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (184 lines) | stat: -rw-r--r-- 5,131 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "latexlog.h"
#include "qlinemarksinfocenter.h"

LatexLogModel::LatexLogModel(QObject *parent): QAbstractTableModel(parent)
{
	markIDs[LT_NONE] = -1;
	markIDs[LT_ERROR] = QLineMarksInfoCenter::instance()->markTypeId("error");
	markIDs[LT_WARNING] = QLineMarksInfoCenter::instance()->markTypeId("warning");
	markIDs[LT_BADBOX] = QLineMarksInfoCenter::instance()->markTypeId("badbox");
	foundType[LT_NONE] = foundType[LT_ERROR] = foundType[LT_WARNING] = foundType[LT_BADBOX] = false;
}

int LatexLogModel::columnCount(const QModelIndex &parent) const
{
	return parent.isValid() ? 0 : 4;
}

int LatexLogModel::rowCount(const QModelIndex &parent) const
{
	return parent.isValid() ? 0 : log.count();
}

QVariant LatexLogModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid()) return QVariant();
	if (index.row() >= log.count() || index.row() < 0) return QVariant();
	if (role == Qt::ToolTipRole) return tr("Click to jump to the line");
	if (role == Qt::ForegroundRole) return LatexLogEntry::textColor(log.at(index.row()).type);
	if (role != Qt::DisplayRole) return QVariant();
	switch (index.column()) {
	case 0:
		return log.at(index.row()).file.mid(log.at(index.row()).file.lastIndexOf("/") + 1); //show relative names
	case 1:
		switch (log.at(index.row()).type) {
		case LT_ERROR:
			return tr("error");
		case LT_WARNING:
			return tr("warning");
		case LT_BADBOX:
			return tr("bad box");
		default:
			return QVariant(); //return Texstudio::tr("unknown");
		}
	case 2: {
		int line = log.at(index.row()).oldline;
		if (line > 0)
			return tr("line") + QString(" %1").arg(line);
		return QString();
	}
	case 3:
		return log.at(index.row()).message;
	default:
		return QVariant();
	}
}

QVariant LatexLogModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (role != Qt::DisplayRole) return QVariant();
	if (orientation != Qt::Horizontal) return QVariant();
	switch (section) {
	case 0:
		return tr("File");
	case 1:
		return tr("Type");
	case 2:
		return tr("Line");
	case 3:
		return tr("Message");
	default:
		return QVariant();
	}
}

int LatexLogModel::count() const
{
	return log.count();
}

void LatexLogModel::clear()
{
	beginResetModel();
	log.clear();
	endResetModel();
}

const LatexLogEntry &LatexLogModel::at(int i)
{
	return log.at(i);
}

//Parse a latex log file to find errors, warnings, bad boxes...
void LatexLogModel::parseLogDocument(QTextDocument *doc, QString baseFileName)
{
	LatexOutputFilter outputFilter;
	//TODO: investigate why it crashes if outputFilter is a member variable, m_infoList is set to a global variable by the LatexLogModel constructor instead here, but only if the m_filelookup member of LatexOutputFilter does exist
	outputFilter.setSource(baseFileName);
	outputFilter.run(doc);

	beginResetModel();
	log.clear();
	QList<LatexLogEntry> laterLog;
	for (int i = 0; i < outputFilter.m_infoList.count(); i++) {
		LatexLogEntry cur = outputFilter.m_infoList.at(i);
		if (cur.type == LT_ERROR) log << cur;
		else laterLog << cur;
	}
	log << laterLog;

	foundType[LT_ERROR] = outputFilter.m_nErrors > 0;
	foundType[LT_BADBOX] = outputFilter.m_nBadBoxes > 0;
	foundType[LT_WARNING] = outputFilter.m_nWarnings > 0;
	endResetModel();
}

bool LatexLogModel::found(LogType lt) const
{
	Q_ASSERT_X(lt > 0 && lt < 4, "found logtype", "unbound array index");
	return foundType[lt];
}

int LatexLogModel::markID(LogType lt) const
{
	Q_ASSERT_X(lt > 0 && lt < 4, "markID logtype", "unbound array index");
	return markIDs[lt];
}

int LatexLogModel::logLineNumberToLogEntryNumber(int logLine) const
{
	int res = -1;
	for (int i = 0; i < log.count(); i++)
		if (log.at(i).logline <= logLine)
			if (res == -1 || log.at(i).logline > log.at(res).logline)
				res = i;
	return res;
}

bool LatexLogModel::existsReRunWarning() const
{
	if (!found(LT_WARNING)) return false;
	static QRegExp rReRun ("(No file.*\\.(aux|toc))|"
	                       "( Rerun )");
	foreach (const LatexLogEntry &l, log) {
		if (l.type != LT_WARNING || l.oldline != 0) continue;
		if (l.message.contains(rReRun) && !l.message.contains("No file \\jobname .aux")) return true;
	}
	return false;
}

QStringList LatexLogModel::getMissingCitations() const
{
	QStringList sl;
	static QRegExp rCitation ("Citation +[`'\"]([^`'\"]+)[`'\"] +.*undefined");
	foreach (const LatexLogEntry &l, log) {
		if (l.type != LT_WARNING) continue;
		if (rCitation.indexIn(l.message) >= 0)
			sl.append(rCitation.cap(1));
	}
	return sl;
}

QString LatexLogModel::htmlErrorTable(const QList<int> &errors)
{
	QString msg = "<table>";
	foreach (int error, errors) {
		if (error < 0 || error >= count()) continue;
		msg.append(at(error).niceMessage());
	}
	return msg.append("</table>");
}

QString LatexLogModel::returnString(LogType type)
{
	switch (type) {
	case LT_ERROR:
		return tr("error");
	case LT_WARNING:
		return tr("warning");
	case LT_BADBOX:
		return tr("bad box");
	default:
		return QString();
	}
}