]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiLog.cpp
Make code a bit easier to read
[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 QRegularExpression exprInfo("^(Document Class:|LaTeX Font Info:|File:|Package:|Language:|.*> INFO - |\\(|\\\\).*$");
46 // Warnings
47 QRegularExpression exprWarning("^(## Warning|LaTeX Warning|LaTeX Font Warning|Package [\\w\\-\\.]+ Warning|Class \\w+ Warning|Warning--|Underfull|Overfull|.*> WARN - ).*$");
48 // Errors
49 QRegularExpression 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         QRegularExpressionMatch match = exprInfo.match(text);
87         int index = match.capturedStart(1);
88         while (index >= 0) {
89                 int length = match.capturedEnd(1) - index;
90                 setFormat(index, length, infoFormat);
91                 match = exprInfo.match(text, index + length);
92                 index = match.capturedStart(1);
93         }
94         // LaTeX Warning:
95         match = exprWarning.match(text);
96         index = match.capturedStart(1);
97         while (index >= 0) {
98                 int length = match.capturedEnd(1) - index;
99                 setFormat(index, length, warningFormat);
100                 match = exprWarning.match(text, index + length);
101                 index = match.capturedStart(1);
102         }
103         // ! error
104         match = exprError.match(text);
105         index = match.capturedStart(1);
106         while (index >= 0) {
107                 int length = match.capturedEnd(1) - index;
108                 setFormat(index, length, errorFormat);
109                 match = exprError.match(text, index + length);
110                 index = match.capturedStart(1);
111         }
112 }
113
114
115 /////////////////////////////////////////////////////////////////////
116 //
117 // GuiLog
118 //
119 /////////////////////////////////////////////////////////////////////
120
121 GuiLog::GuiLog(GuiView & lv)
122         : GuiDialog(lv, "log", qt_("LaTeX Log")), type_(LatexLog)
123 {
124         setupUi(this);
125
126         connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
127                 this, SLOT(slotButtonBox(QAbstractButton *)));
128         connect(updatePB, SIGNAL(clicked()), this, SLOT(updateContents()));
129         connect(findPB, SIGNAL(clicked()), this, SLOT(find()));
130         connect(findLE, SIGNAL(returnPressed()), this, SLOT(find()));
131         connect(logTypeCO, SIGNAL(activated(int)),
132                 this, SLOT(typeChanged(int)));
133
134         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
135
136         // set syntax highlighting
137         highlighter = new LogHighlighter(logTB->document());
138
139         logTB->setReadOnly(true);
140         logTB->setFont(guiApp->typewriterSystemFont());
141
142         QPushButton * closePB = buttonBox->button(QDialogButtonBox::Close);
143         closePB->setAutoDefault(false);
144 }
145
146
147 void GuiLog::updateContents()
148 {
149         setTitle(toqstr(title()));
150
151         ostringstream ss;
152         getContents(ss);
153
154         logTB->setPlainText(toqstr(ss.str()));
155
156         nextErrorPB->setEnabled(contains(exprError));
157         nextWarningPB->setEnabled(contains(exprWarning));
158 }
159
160
161 void GuiLog::typeChanged(int i)
162 {
163         string const type =
164                 fromqstr(logTypeCO->itemData(i).toString());
165         string ext;
166         if (type == "latex")
167                 ext = "log";
168         else if (type == "bibtex")
169                 ext = "blg";
170         else if (type == "index")
171                 ext = "ilg";
172
173         if (!ext.empty())
174                 logfile_.changeExtension(ext);
175
176         updateContents();
177 }
178
179
180 void GuiLog::find()
181 {
182         logTB->find(findLE->text());
183 }
184
185
186 void GuiLog::on_nextErrorPB_clicked()
187 {
188         goTo(exprError);
189 }
190
191
192 void GuiLog::on_nextWarningPB_clicked()
193 {
194         goTo(exprWarning);
195 }
196
197
198 void GuiLog::on_openDirPB_clicked()
199 {       
200         showDirectory(logfile_.onlyPath());
201 }
202
203
204 void GuiLog::goTo(QRegularExpression const & exp) const
205 {
206         QTextCursor const newc =
207                 logTB->document()->find(exp, logTB->textCursor());
208         logTB->setTextCursor(newc);
209 }
210
211
212 bool GuiLog::contains(QRegularExpression const & exp) const
213 {
214         return !logTB->document()->find(exp, logTB->textCursor()).isNull();
215 }
216
217
218 bool GuiLog::initialiseParams(string const & sdata)
219 {
220         istringstream is(sdata);
221         Lexer lex;
222         lex.setStream(is);
223
224         string logtype, logfile;
225         lex >> logtype;
226         if (lex) {
227                 lex.next(true);
228                 logfile = lex.getString();
229         }
230         if (!lex)
231                 // Parsing of the data failed.
232                 return false;
233
234         logTypeCO->setEnabled(logtype == "latex");
235         logTypeCO->clear();
236
237         FileName log(logfile);
238
239         if (logtype == "latex") {
240                 type_ = LatexLog;
241                 logTypeCO->addItem(qt_("LaTeX"), toqstr(logtype));
242                 FileName tmp = log;
243                 tmp.changeExtension("blg");
244                 if (tmp.exists()) {
245                         if (support::contains(tmp.fileContents("UTF-8"), from_ascii("This is Biber")))
246                                 logTypeCO->addItem(qt_("Biber"), QString("bibtex"));
247                         else
248                                 logTypeCO->addItem(qt_("BibTeX"), QString("bibtex"));
249                 }
250                 tmp.changeExtension("ilg");
251                 if (tmp.exists())
252                         logTypeCO->addItem(qt_("Index"), QString("index"));
253         // FIXME: not sure "literate" still works.
254         } else if (logtype == "literate") {
255                 type_ = LiterateLog;
256                 logTypeCO->addItem(qt_("Literate"), toqstr(logtype));
257         } else if (logtype == "lyx2lyx") {
258                 type_ = Lyx2lyxLog;
259                 logTypeCO->addItem(qt_("LyX2LyX"), toqstr(logtype));
260         } else if (logtype == "vc") {
261                 type_ = VCLog;
262                 logTypeCO->addItem(qt_("Version Control"), toqstr(logtype));
263         } else
264                 return false;
265
266         logfile_ = log;
267
268         updateContents();
269
270         return true;
271 }
272
273
274 void GuiLog::clearParams()
275 {
276         logfile_.erase();
277 }
278
279
280 docstring GuiLog::title() const
281 {
282         switch (type_) {
283         case LatexLog:
284                 return _("LaTeX Log");
285         case LiterateLog:
286                 return _("Literate Programming Build Log");
287         case Lyx2lyxLog:
288                 return _("lyx2lyx Error Log");
289         case VCLog:
290                 return _("Version Control Log");
291         default:
292                 return docstring();
293         }
294 }
295
296
297 void GuiLog::getContents(ostream & ss) const
298 {
299         ifstream in(logfile_.toFilesystemEncoding().c_str());
300
301         bool success = false;
302
303         // FIXME UNICODE
304         // Our caller interprets the file contents as UTF8, but is that
305         // correct?
306         // spitz: No it isn't (generally). The log file encoding depends on the TeX
307         // _output_ encoding (T1 etc.). We should account for that. See #10728.
308         if (in) {
309                 ss << in.rdbuf();
310                 success = ss.good();
311         }
312
313         if (success)
314                 return;
315
316         switch (type_) {
317         case LatexLog:
318                 ss << to_utf8(_("Log file not found."));
319                 break;
320         case LiterateLog:
321                 ss << to_utf8(_("No literate programming build log file found."));
322                 break;
323         case Lyx2lyxLog:
324                 ss << to_utf8(_("No lyx2lyx error log file found."));
325                 break;
326         case VCLog:
327                 ss << to_utf8(_("No version control log file found."));
328                 break;
329         }
330 }
331
332
333 } // namespace frontend
334 } // namespace lyx
335
336 #include "moc_GuiLog.cpp"