]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiLog.cpp
On Linux show in crash message box the backtrace
[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
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:|Underfull|Overfull|.*> INFO - |\\(|\\\\).*$");
46 // Warnings
47 QRegExp exprWarning("^(LaTeX Warning|LaTeX Font Warning|Package [\\w\\.]+ Warning|Class \\w+ Warning|Warning--|.*> WARN - ).*$");
48 // Errors
49 QRegExp exprError("^(!|.*---line [0-9]+ of file|.*> FATAL - |.*> ERROR - ).*$");
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);
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(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
121         connect(updatePB, SIGNAL(clicked()), this, SLOT(updateContents()));
122         connect(findPB, SIGNAL(clicked()), this, SLOT(find()));
123         // FIXME: find via returnPressed() does not work!
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         QFont font(guiApp->typewriterFontName());
135         font.setKerning(false);
136         font.setFixedPitch(true);
137         font.setStyleHint(QFont::TypeWriter);
138         logTB->setFont(font);
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::goTo(QRegExp const & exp) const
194 {
195         QTextCursor const newc =
196                 logTB->document()->find(exp, logTB->textCursor());
197         logTB->setTextCursor(newc);
198 }
199
200
201 bool GuiLog::contains(QRegExp const & exp) const
202 {
203         return !logTB->document()->find(exp, logTB->textCursor()).isNull();
204 }
205
206
207 bool GuiLog::initialiseParams(string const & data)
208 {
209         istringstream is(data);
210         Lexer lex;
211         lex.setStream(is);
212
213         string logtype, logfile;
214         lex >> logtype;
215         if (lex) {
216                 lex.next(true);
217                 logfile = lex.getString();
218         }
219         if (!lex)
220                 // Parsing of the data failed.
221                 return false;
222
223         logTypeCO->setEnabled(logtype == "latex");
224         logTypeCO->clear();
225         
226         FileName log(logfile);
227         
228         if (logtype == "latex") {
229                 type_ = LatexLog;
230                 logTypeCO->addItem(qt_("LaTeX"), toqstr(logtype));
231                 FileName tmp = log;
232                 tmp.changeExtension("blg");
233                 if (tmp.exists())
234                         logTypeCO->addItem(qt_("BibTeX"), QString("bibtex"));
235                 tmp.changeExtension("ilg");
236                 if (tmp.exists())
237                         logTypeCO->addItem(qt_("Index"), QString("index"));
238         // FIXME: not sure "literate" still works.
239         } else if (logtype == "literate") {
240                 type_ = LiterateLog;
241                 logTypeCO->addItem(qt_("Literate"), toqstr(logtype));
242         } else if (logtype == "lyx2lyx") {
243                 type_ = Lyx2lyxLog;
244                 logTypeCO->addItem(qt_("LyX2LyX"), toqstr(logtype));
245         } else if (logtype == "vc") {
246                 type_ = VCLog;
247                 logTypeCO->addItem(qt_("Version Control"), toqstr(logtype));
248         } else
249                 return false;
250
251         logfile_ = log;
252
253         updateContents();
254
255         return true;
256 }
257
258
259 void GuiLog::clearParams()
260 {
261         logfile_.erase();
262 }
263
264
265 docstring GuiLog::title() const
266 {
267         switch (type_) {
268         case LatexLog:
269                 return _("LaTeX Log");
270         case LiterateLog:
271                 return _("Literate Programming Build Log");
272         case Lyx2lyxLog:
273                 return _("lyx2lyx Error Log");
274         case VCLog:
275                 return _("Version Control Log");
276         default:
277                 return docstring();
278         }
279 }
280
281
282 void GuiLog::getContents(ostream & ss) const
283 {
284         ifstream in(logfile_.toFilesystemEncoding().c_str());
285
286         bool success = false;
287
288         // FIXME UNICODE
289         // Our caller interprets the file contents as UTF8, but is that
290         // correct?
291         if (in) {
292                 ss << in.rdbuf();
293                 success = ss.good();
294         }
295
296         if (success)
297                 return;
298
299         switch (type_) {
300         case LatexLog:
301                 ss << to_utf8(_("Log file not found."));
302                 break;
303         case LiterateLog:
304                 ss << to_utf8(_("No literate programming build log file found."));
305                 break;
306         case Lyx2lyxLog:
307                 ss << to_utf8(_("No lyx2lyx error log file found."));
308                 break;
309         case VCLog:
310                 ss << to_utf8(_("No version control log file found."));
311                 break;
312         }
313 }
314
315
316 void GuiLog::on_copyPB_clicked()
317 {
318         theClipboard().put(fromqstr(logTB->toPlainText()));
319 }
320
321
322 Dialog * createGuiLog(GuiView & lv) { return new GuiLog(lv); }
323
324
325 } // namespace frontend
326 } // namespace lyx
327
328 #include "moc_GuiLog.cpp"