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/FileName.h"
25 #include "support/gettext.h"
27 #include <QDesktopServices>
28 #include <QTextBrowser>
29 #include <QSyntaxHighlighter>
37 using namespace lyx::support;
43 // Regular expressions needed at several places
44 // FIXME: These regexes are incomplete. It would be good if we could collect those used in LaTeX::scanLogFile
45 // and LaTeX::scanBlgFile and re-use them here!(spitz, 2013-05-27)
47 QRegExp exprInfo("^(Document Class:|LaTeX Font Info:|File:|Package:|Language:|Underfull|Overfull|.*> INFO - |\\(|\\\\).*$");
49 QRegExp exprWarning("^(LaTeX Warning|LaTeX Font Warning|Package [\\w\\.]+ Warning|Class \\w+ Warning|Warning--|.*> WARN - ).*$");
51 QRegExp exprError("^(!|.*---line [0-9]+ of file|.*> FATAL - |.*> ERROR - |Missing character: There is no ).*$");
54 /////////////////////////////////////////////////////////////////////
58 ////////////////////////////////////////////////////////////////////
60 class LogHighlighter : public QSyntaxHighlighter
63 LogHighlighter(QTextDocument * parent);
66 void highlightBlock(QString const & text);
69 QTextCharFormat infoFormat;
70 QTextCharFormat warningFormat;
71 QTextCharFormat errorFormat;
76 LogHighlighter::LogHighlighter(QTextDocument * parent)
77 : QSyntaxHighlighter(parent)
79 infoFormat.setForeground(Qt::darkGray);
80 warningFormat.setForeground(Qt::darkBlue);
81 errorFormat.setForeground(Qt::red);
85 void LogHighlighter::highlightBlock(QString const & text)
88 int index = exprInfo.indexIn(text);
90 int length = exprInfo.matchedLength();
91 setFormat(index, length, infoFormat);
92 index = exprInfo.indexIn(text, index + length);
95 index = exprWarning.indexIn(text);
97 int length = exprWarning.matchedLength();
98 setFormat(index, length, warningFormat);
99 index = exprWarning.indexIn(text, index + length);
102 index = exprError.indexIn(text);
104 int length = exprError.matchedLength();
105 setFormat(index, length, errorFormat);
106 index = exprError.indexIn(text, index + length);
111 /////////////////////////////////////////////////////////////////////
115 /////////////////////////////////////////////////////////////////////
117 GuiLog::GuiLog(GuiView & lv)
118 : GuiDialog(lv, "log", qt_("LaTeX Log")), type_(LatexLog)
122 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
123 connect(updatePB, SIGNAL(clicked()), this, SLOT(updateContents()));
124 connect(findPB, SIGNAL(clicked()), this, SLOT(find()));
125 // FIXME: find via returnPressed() does not work!
126 connect(findLE, SIGNAL(returnPressed()), this, SLOT(find()));
127 connect(logTypeCO, SIGNAL(activated(int)),
128 this, SLOT(typeChanged(int)));
130 bc().setPolicy(ButtonPolicy::OkCancelPolicy);
132 // set syntax highlighting
133 highlighter = new LogHighlighter(logTB->document());
135 logTB->setReadOnly(true);
136 QFont font(guiApp->typewriterFontName());
137 font.setFixedPitch(true);
138 font.setStyleHint(QFont::TypeWriter);
139 logTB->setFont(font);
143 void GuiLog::updateContents()
145 setTitle(toqstr(title()));
150 logTB->setPlainText(toqstr(ss.str()));
152 nextErrorPB->setEnabled(contains(exprError));
153 nextWarningPB->setEnabled(contains(exprWarning));
157 void GuiLog::typeChanged(int i)
160 fromqstr(logTypeCO->itemData(i).toString());
164 else if (type == "bibtex")
166 else if (type == "index")
170 logfile_.changeExtension(ext);
178 logTB->find(findLE->text());
182 void GuiLog::on_nextErrorPB_clicked()
188 void GuiLog::on_nextWarningPB_clicked()
194 void GuiLog::on_openDirPB_clicked()
196 support::FileName dir = logfile_.onlyPath();
199 QUrl qdir(QUrl::fromLocalFile(toqstr(from_utf8(dir.absFileName()))));
200 // Give hints in case of bugs
201 if (!qdir.isValid()) {
202 LYXERR0("QUrl is invalid!");
205 if (!QDesktopServices::openUrl(qdir))
206 LYXERR0("Unable to open QUrl even though dir exists!");
210 void GuiLog::goTo(QRegExp const & exp) const
212 QTextCursor const newc =
213 logTB->document()->find(exp, logTB->textCursor());
214 logTB->setTextCursor(newc);
218 bool GuiLog::contains(QRegExp const & exp) const
220 return !logTB->document()->find(exp, logTB->textCursor()).isNull();
224 bool GuiLog::initialiseParams(string const & data)
226 istringstream is(data);
230 string logtype, logfile;
234 logfile = lex.getString();
237 // Parsing of the data failed.
240 logTypeCO->setEnabled(logtype == "latex");
243 FileName log(logfile);
245 if (logtype == "latex") {
247 logTypeCO->addItem(qt_("LaTeX"), toqstr(logtype));
249 tmp.changeExtension("blg");
251 logTypeCO->addItem(qt_("BibTeX"), QString("bibtex"));
252 tmp.changeExtension("ilg");
254 logTypeCO->addItem(qt_("Index"), QString("index"));
255 // FIXME: not sure "literate" still works.
256 } else if (logtype == "literate") {
258 logTypeCO->addItem(qt_("Literate"), toqstr(logtype));
259 } else if (logtype == "lyx2lyx") {
261 logTypeCO->addItem(qt_("LyX2LyX"), toqstr(logtype));
262 } else if (logtype == "vc") {
264 logTypeCO->addItem(qt_("Version Control"), toqstr(logtype));
276 void GuiLog::clearParams()
282 docstring GuiLog::title() const
286 return _("LaTeX Log");
288 return _("Literate Programming Build Log");
290 return _("lyx2lyx Error Log");
292 return _("Version Control Log");
299 void GuiLog::getContents(ostream & ss) const
301 ifstream in(logfile_.toFilesystemEncoding().c_str());
303 bool success = false;
306 // Our caller interprets the file contents as UTF8, but is that
318 ss << to_utf8(_("Log file not found."));
321 ss << to_utf8(_("No literate programming build log file found."));
324 ss << to_utf8(_("No lyx2lyx error log file found."));
327 ss << to_utf8(_("No version control log file found."));
333 void GuiLog::on_copyPB_clicked()
335 theClipboard().put(fromqstr(logTB->toPlainText()));
339 Dialog * createGuiLog(GuiView & lv) { return new GuiLog(lv); }
342 } // namespace frontend
345 #include "moc_GuiLog.cpp"