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