]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiLog.cpp
Add Sam Crawley to credits.
[lyx.git] / src / frontends / qt / GuiLog.cpp
1 /**
2  * \file GuiLog.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Angus Leeming
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiLog.h"
16
17 #include "GuiApplication.h"
18 #include "qt_helpers.h"
19 #include "Lexer.h"
20
21 #include "frontends/Clipboard.h"
22
23 #include "support/docstring.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26
27 #include <QTextBrowser>
28 #include <QSyntaxHighlighter>
29 #include <QClipboard>
30
31 #include <fstream>
32 #include <sstream>
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38 namespace frontend {
39
40
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 // Information
45 QRegExp exprInfo("^(Document Class:|LaTeX Font Info:|File:|Package:|Language:|.*> INFO - |\\(|\\\\).*$");
46 // Warnings
47 QRegExp exprWarning("^(## Warning|LaTeX Warning|LaTeX Font Warning|Package [\\w\\.]+ Warning|Class \\w+ Warning|Warning--|Underfull|Overfull|.*> WARN - ).*$");
48 // Errors
49 QRegExp exprError("^(ERROR: |!|.*---line [0-9]+ of file|.*> FATAL - |.*> ERROR - |Missing character: There is no ).*$");
50
51
52 /////////////////////////////////////////////////////////////////////
53 //
54 // LogHighlighter
55 //
56 ////////////////////////////////////////////////////////////////////
57
58 class LogHighlighter : public QSyntaxHighlighter
59 {
60 public:
61         LogHighlighter(QTextDocument * parent);
62
63 private:
64         void highlightBlock(QString const & text) override;
65
66 private:
67         QTextCharFormat infoFormat;
68         QTextCharFormat warningFormat;
69         QTextCharFormat errorFormat;
70 };
71
72
73
74 LogHighlighter::LogHighlighter(QTextDocument * parent)
75         : QSyntaxHighlighter(parent)
76 {
77         infoFormat.setForeground(Qt::darkGray);
78         warningFormat.setForeground(Qt::darkBlue);
79         errorFormat.setForeground(Qt::red);
80 }
81
82
83 void LogHighlighter::highlightBlock(QString const & text)
84 {
85         // Info
86         int index = exprInfo.indexIn(text);
87         while (index >= 0) {
88                 int length = exprInfo.matchedLength();
89                 setFormat(index, length, infoFormat);
90                 index = exprInfo.indexIn(text, index + length);
91         }
92         // LaTeX Warning:
93         index = exprWarning.indexIn(text);
94         while (index >= 0) {
95                 int length = exprWarning.matchedLength();
96                 setFormat(index, length, warningFormat);
97                 index = exprWarning.indexIn(text, index + length);
98         }
99         // ! error
100         index = exprError.indexIn(text);
101         while (index >= 0) {
102                 int length = exprError.matchedLength();
103                 setFormat(index, length, errorFormat);
104                 index = exprError.indexIn(text, index + length);
105         }
106 }
107
108
109 /////////////////////////////////////////////////////////////////////
110 //
111 // GuiLog
112 //
113 /////////////////////////////////////////////////////////////////////
114
115 GuiLog::GuiLog(GuiView & lv)
116         : GuiDialog(lv, "log", qt_("LaTeX Log")), type_(LatexLog)
117 {
118         setupUi(this);
119
120         connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
121                 this, SLOT(slotButtonBox(QAbstractButton *)));
122         connect(updatePB, SIGNAL(clicked()), this, SLOT(updateContents()));
123         connect(findPB, SIGNAL(clicked()), this, SLOT(find()));
124         connect(findLE, SIGNAL(returnPressed()), this, SLOT(find()));
125         connect(logTypeCO, SIGNAL(activated(int)),
126                 this, SLOT(typeChanged(int)));
127
128         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
129
130         // set syntax highlighting
131         highlighter = new LogHighlighter(logTB->document());
132
133         logTB->setReadOnly(true);
134         logTB->setFont(guiApp->typewriterSystemFont());
135
136         QPushButton * closePB = buttonBox->button(QDialogButtonBox::Close);
137         closePB->setAutoDefault(false);
138 }
139
140
141 void GuiLog::updateContents()
142 {
143         setTitle(toqstr(title()));
144
145         ostringstream ss;
146         getContents(ss);
147
148         logTB->setPlainText(toqstr(ss.str()));
149
150         nextErrorPB->setEnabled(contains(exprError));
151         nextWarningPB->setEnabled(contains(exprWarning));
152 }
153
154
155 void GuiLog::typeChanged(int i)
156 {
157         string const type =
158                 fromqstr(logTypeCO->itemData(i).toString());
159         string ext;
160         if (type == "latex")
161                 ext = "log";
162         else if (type == "bibtex")
163                 ext = "blg";
164         else if (type == "index")
165                 ext = "ilg";
166
167         if (!ext.empty())
168                 logfile_.changeExtension(ext);
169
170         updateContents();
171 }
172
173
174 void GuiLog::find()
175 {
176         logTB->find(findLE->text());
177 }
178
179
180 void GuiLog::on_nextErrorPB_clicked()
181 {
182         goTo(exprError);
183 }
184
185
186 void GuiLog::on_nextWarningPB_clicked()
187 {
188         goTo(exprWarning);
189 }
190
191
192 void GuiLog::on_openDirPB_clicked()
193 {       
194         showDirectory(logfile_.onlyPath());
195 }
196
197
198 void GuiLog::goTo(QRegExp const & exp) const
199 {
200         QTextCursor const newc =
201                 logTB->document()->find(exp, logTB->textCursor());
202         logTB->setTextCursor(newc);
203 }
204
205
206 bool GuiLog::contains(QRegExp const & exp) const
207 {
208         return !logTB->document()->find(exp, logTB->textCursor()).isNull();
209 }
210
211
212 bool GuiLog::initialiseParams(string const & sdata)
213 {
214         istringstream is(sdata);
215         Lexer lex;
216         lex.setStream(is);
217
218         string logtype, logfile;
219         lex >> logtype;
220         if (lex) {
221                 lex.next(true);
222                 logfile = lex.getString();
223         }
224         if (!lex)
225                 // Parsing of the data failed.
226                 return false;
227
228         logTypeCO->setEnabled(logtype == "latex");
229         logTypeCO->clear();
230
231         FileName log(logfile);
232
233         if (logtype == "latex") {
234                 type_ = LatexLog;
235                 logTypeCO->addItem(qt_("LaTeX"), toqstr(logtype));
236                 FileName tmp = log;
237                 tmp.changeExtension("blg");
238                 if (tmp.exists()) {
239                         if (support::contains(tmp.fileContents("UTF-8"), from_ascii("This is Biber")))
240                                 logTypeCO->addItem(qt_("Biber"), QString("bibtex"));
241                         else
242                                 logTypeCO->addItem(qt_("BibTeX"), QString("bibtex"));
243                 }
244                 tmp.changeExtension("ilg");
245                 if (tmp.exists())
246                         logTypeCO->addItem(qt_("Index"), QString("index"));
247         // FIXME: not sure "literate" still works.
248         } else if (logtype == "literate") {
249                 type_ = LiterateLog;
250                 logTypeCO->addItem(qt_("Literate"), toqstr(logtype));
251         } else if (logtype == "lyx2lyx") {
252                 type_ = Lyx2lyxLog;
253                 logTypeCO->addItem(qt_("LyX2LyX"), toqstr(logtype));
254         } else if (logtype == "vc") {
255                 type_ = VCLog;
256                 logTypeCO->addItem(qt_("Version Control"), toqstr(logtype));
257         } else
258                 return false;
259
260         logfile_ = log;
261
262         updateContents();
263
264         return true;
265 }
266
267
268 void GuiLog::clearParams()
269 {
270         logfile_.erase();
271 }
272
273
274 docstring GuiLog::title() const
275 {
276         switch (type_) {
277         case LatexLog:
278                 return _("LaTeX Log");
279         case LiterateLog:
280                 return _("Literate Programming Build Log");
281         case Lyx2lyxLog:
282                 return _("lyx2lyx Error Log");
283         case VCLog:
284                 return _("Version Control Log");
285         default:
286                 return docstring();
287         }
288 }
289
290
291 void GuiLog::getContents(ostream & ss) const
292 {
293         ifstream in(logfile_.toFilesystemEncoding().c_str());
294
295         bool success = false;
296
297         // FIXME UNICODE
298         // Our caller interprets the file contents as UTF8, but is that
299         // correct?
300         // spitz: No it isn't (generally). The log file encoding depends on the TeX
301         // _output_ encoding (T1 etc.). We should account for that. See #10728.
302         if (in) {
303                 ss << in.rdbuf();
304                 success = ss.good();
305         }
306
307         if (success)
308                 return;
309
310         switch (type_) {
311         case LatexLog:
312                 ss << to_utf8(_("Log file not found."));
313                 break;
314         case LiterateLog:
315                 ss << to_utf8(_("No literate programming build log file found."));
316                 break;
317         case Lyx2lyxLog:
318                 ss << to_utf8(_("No lyx2lyx error log file found."));
319                 break;
320         case VCLog:
321                 ss << to_utf8(_("No version control log file found."));
322                 break;
323         }
324 }
325
326
327 Dialog * createGuiLog(GuiView & lv) { return new GuiLog(lv); }
328
329
330 } // namespace frontend
331 } // namespace lyx
332
333 #include "moc_GuiLog.cpp"