3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
17 #include "GuiApplication.h"
18 #include "qt_helpers.h"
21 #include "frontends/Clipboard.h"
23 #include "support/docstring.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
27 #include <QTextBrowser>
28 #include <QSyntaxHighlighter>
35 using namespace lyx::support;
41 // Regular expressions needed at several places
42 // FIXME: These regexes are incomplete. It would be good if we could collect those used in LaTeX::scanLogFile
43 // and LaTeX::scanBlgFile and re-use them here!(spitz, 2013-05-27)
44 #if QT_VERSION < 0x060000
46 QRegExp exprInfo("^(Document Class:|LaTeX Font Info:|File:|Package:|Language:|.*> INFO - |\\(|\\\\).*$");
48 QRegExp exprWarning("^(## Warning|LaTeX Warning|LaTeX Font Warning|Package [\\w\\.]+ Warning|Class \\w+ Warning|Warning--|Underfull|Overfull|.*> WARN - ).*$");
50 QRegExp exprError("^(ERROR: |!|.*---line [0-9]+ of file|.*> FATAL - |.*> ERROR - |Missing character: There is no ).*$");
53 QRegularExpression exprInfo("^(Document Class:|LaTeX Font Info:|File:|Package:|Language:|.*> INFO - |\\(|\\\\).*$");
55 QRegularExpression exprWarning("^(## Warning|LaTeX Warning|LaTeX Font Warning|Package [\\w\\-\\.]+ Warning|Class \\w+ Warning|Warning--|Underfull|Overfull|.*> WARN - ).*$");
57 QRegularExpression exprError("^(ERROR: |!|.*---line [0-9]+ of file|.*> FATAL - |.*> ERROR - |Missing character: There is no ).*$");
61 /////////////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////////////
67 class LogHighlighter : public QSyntaxHighlighter
70 LogHighlighter(QTextDocument * parent);
73 void highlightBlock(QString const & text) override;
76 QTextCharFormat infoFormat;
77 QTextCharFormat warningFormat;
78 QTextCharFormat errorFormat;
83 LogHighlighter::LogHighlighter(QTextDocument * parent)
84 : QSyntaxHighlighter(parent)
86 infoFormat.setForeground(Qt::darkGray);
87 warningFormat.setForeground(Qt::darkBlue);
88 errorFormat.setForeground(Qt::red);
92 void LogHighlighter::highlightBlock(QString const & text)
94 #if QT_VERSION < 0x060000
96 int index = exprInfo.indexIn(text);
98 int length = exprInfo.matchedLength();
99 setFormat(index, length, infoFormat);
100 index = exprInfo.indexIn(text, index + length);
103 index = exprWarning.indexIn(text);
105 int length = exprWarning.matchedLength();
106 setFormat(index, length, warningFormat);
107 index = exprWarning.indexIn(text, index + length);
110 index = exprError.indexIn(text);
112 int length = exprError.matchedLength();
113 setFormat(index, length, errorFormat);
114 index = exprError.indexIn(text, index + length);
118 QRegularExpressionMatch match = exprInfo.match(text);
119 int index = match.capturedStart(1);
121 int length = match.capturedEnd(1) - index;
122 setFormat(index, length, infoFormat);
123 match = exprInfo.match(text, index + length);
124 index = match.capturedStart(1);
127 match = exprWarning.match(text);
128 index = match.capturedStart(1);
130 int length = match.capturedEnd(1) - index;
131 setFormat(index, length, warningFormat);
132 match = exprWarning.match(text, index + length);
133 index = match.capturedStart(1);
136 match = exprError.match(text);
137 index = match.capturedStart(1);
139 int length = match.capturedEnd(1) - index;
140 setFormat(index, length, errorFormat);
141 match = exprError.match(text, index + length);
142 index = match.capturedStart(1);
148 /////////////////////////////////////////////////////////////////////
152 /////////////////////////////////////////////////////////////////////
154 GuiLog::GuiLog(GuiView & lv)
155 : GuiDialog(lv, "log", qt_("LaTeX Log")), type_(LatexLog)
159 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
160 this, SLOT(slotButtonBox(QAbstractButton *)));
161 connect(updatePB, SIGNAL(clicked()), this, SLOT(updateContents()));
162 connect(findPB, SIGNAL(clicked()), this, SLOT(find()));
163 connect(findLE, SIGNAL(returnPressed()), this, SLOT(find()));
164 connect(logTypeCO, SIGNAL(activated(int)),
165 this, SLOT(typeChanged(int)));
167 bc().setPolicy(ButtonPolicy::OkCancelPolicy);
169 // set syntax highlighting
170 highlighter = new LogHighlighter(logTB->document());
172 logTB->setReadOnly(true);
173 logTB->setFont(guiApp->typewriterSystemFont());
175 QPushButton * closePB = buttonBox->button(QDialogButtonBox::Close);
176 closePB->setAutoDefault(false);
180 void GuiLog::updateContents()
182 setTitle(toqstr(title()));
187 logTB->setPlainText(toqstr(ss.str()));
189 nextErrorPB->setEnabled(contains(exprError));
190 nextWarningPB->setEnabled(contains(exprWarning));
194 void GuiLog::typeChanged(int i)
197 fromqstr(logTypeCO->itemData(i).toString());
201 else if (type == "bibtex")
203 else if (type == "index")
207 logfile_.changeExtension(ext);
215 logTB->find(findLE->text());
219 void GuiLog::on_nextErrorPB_clicked()
225 void GuiLog::on_nextWarningPB_clicked()
231 void GuiLog::on_openDirPB_clicked()
233 showDirectory(logfile_.onlyPath());
237 #if QT_VERSION < 0x060000
238 void GuiLog::goTo(QRegExp const & exp) const
240 void GuiLog::goTo(QRegularExpression const & exp) const
243 QTextCursor const newc =
244 logTB->document()->find(exp, logTB->textCursor());
245 logTB->setTextCursor(newc);
249 #if QT_VERSION < 0x060000
250 bool GuiLog::contains(QRegExp const & exp) const
252 bool GuiLog::contains(QRegularExpression const & exp) const
255 return !logTB->document()->find(exp, logTB->textCursor()).isNull();
259 bool GuiLog::initialiseParams(string const & sdata)
261 istringstream is(sdata);
265 string logtype, logfile;
269 logfile = lex.getString();
272 // Parsing of the data failed.
275 logTypeCO->setEnabled(logtype == "latex");
278 FileName log(logfile);
280 if (logtype == "latex") {
282 logTypeCO->addItem(qt_("LaTeX"), toqstr(logtype));
284 tmp.changeExtension("blg");
286 if (support::contains(tmp.fileContents("UTF-8"), from_ascii("This is Biber")))
287 logTypeCO->addItem(qt_("Biber"), QString("bibtex"));
289 logTypeCO->addItem(qt_("BibTeX"), QString("bibtex"));
291 tmp.changeExtension("ilg");
293 logTypeCO->addItem(qt_("Index"), QString("index"));
294 // FIXME: not sure "literate" still works.
295 } else if (logtype == "literate") {
297 logTypeCO->addItem(qt_("Literate"), toqstr(logtype));
298 } else if (logtype == "lyx2lyx") {
300 logTypeCO->addItem(qt_("LyX2LyX"), toqstr(logtype));
301 } else if (logtype == "vc") {
303 logTypeCO->addItem(qt_("Version Control"), toqstr(logtype));
315 void GuiLog::clearParams()
321 docstring GuiLog::title() const
325 return _("LaTeX Log");
327 return _("Literate Programming Build Log");
329 return _("lyx2lyx Error Log");
331 return _("Version Control Log");
338 void GuiLog::getContents(ostream & ss) const
340 ifstream in(logfile_.toFilesystemEncoding().c_str());
342 bool success = false;
345 // Our caller interprets the file contents as UTF8, but is that
347 // spitz: No it isn't (generally). The log file encoding depends on the TeX
348 // _output_ encoding (T1 etc.). We should account for that. See #10728.
359 ss << to_utf8(_("Log file not found."));
362 ss << to_utf8(_("No literate programming build log file found."));
365 ss << to_utf8(_("No lyx2lyx error log file found."));
368 ss << to_utf8(_("No version control log file found."));
374 } // namespace frontend
377 #include "moc_GuiLog.cpp"