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