]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiLog.cpp
Label only command for reference dialog.
[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 <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         logTB->setFont(guiApp->typewriterSystemFont());
137 }
138
139
140 void GuiLog::updateContents()
141 {
142         setTitle(toqstr(title()));
143
144         ostringstream ss;
145         getContents(ss);
146
147         logTB->setPlainText(toqstr(ss.str()));
148
149         nextErrorPB->setEnabled(contains(exprError));
150         nextWarningPB->setEnabled(contains(exprWarning));
151 }
152
153
154 void GuiLog::typeChanged(int i)
155 {
156         string const type =
157                 fromqstr(logTypeCO->itemData(i).toString());
158         string ext;
159         if (type == "latex")
160                 ext = "log";
161         else if (type == "bibtex")
162                 ext = "blg";
163         else if (type == "index")
164                 ext = "ilg";
165
166         if (!ext.empty())
167                 logfile_.changeExtension(ext);
168
169         updateContents();
170 }
171
172
173 void GuiLog::find()
174 {
175         logTB->find(findLE->text());
176 }
177
178
179 void GuiLog::on_nextErrorPB_clicked()
180 {
181         goTo(exprError);
182 }
183
184
185 void GuiLog::on_nextWarningPB_clicked()
186 {
187         goTo(exprWarning);
188 }
189
190
191 void GuiLog::on_openDirPB_clicked()
192 {
193         support::FileName dir = logfile_.onlyPath();
194         if (!dir.exists())
195                 return;
196         QUrl qdir(QUrl::fromLocalFile(toqstr(from_utf8(dir.absFileName()))));
197         // Give hints in case of bugs
198         if (!qdir.isValid()) {
199                 LYXERR0("QUrl is invalid!");
200                 return;
201         }
202         if (!QDesktopServices::openUrl(qdir))
203                 LYXERR0("Unable to open QUrl even though dir exists!");
204 }
205
206
207 void GuiLog::goTo(QRegExp const & exp) const
208 {
209         QTextCursor const newc =
210                 logTB->document()->find(exp, logTB->textCursor());
211         logTB->setTextCursor(newc);
212 }
213
214
215 bool GuiLog::contains(QRegExp const & exp) const
216 {
217         return !logTB->document()->find(exp, logTB->textCursor()).isNull();
218 }
219
220
221 bool GuiLog::initialiseParams(string const & data)
222 {
223         istringstream is(data);
224         Lexer lex;
225         lex.setStream(is);
226
227         string logtype, logfile;
228         lex >> logtype;
229         if (lex) {
230                 lex.next(true);
231                 logfile = lex.getString();
232         }
233         if (!lex)
234                 // Parsing of the data failed.
235                 return false;
236
237         logTypeCO->setEnabled(logtype == "latex");
238         logTypeCO->clear();
239         
240         FileName log(logfile);
241         
242         if (logtype == "latex") {
243                 type_ = LatexLog;
244                 logTypeCO->addItem(qt_("LaTeX"), toqstr(logtype));
245                 FileName tmp = log;
246                 tmp.changeExtension("blg");
247                 if (tmp.exists())
248                         logTypeCO->addItem(qt_("BibTeX"), QString("bibtex"));
249                 tmp.changeExtension("ilg");
250                 if (tmp.exists())
251                         logTypeCO->addItem(qt_("Index"), QString("index"));
252         // FIXME: not sure "literate" still works.
253         } else if (logtype == "literate") {
254                 type_ = LiterateLog;
255                 logTypeCO->addItem(qt_("Literate"), toqstr(logtype));
256         } else if (logtype == "lyx2lyx") {
257                 type_ = Lyx2lyxLog;
258                 logTypeCO->addItem(qt_("LyX2LyX"), toqstr(logtype));
259         } else if (logtype == "vc") {
260                 type_ = VCLog;
261                 logTypeCO->addItem(qt_("Version Control"), toqstr(logtype));
262         } else
263                 return false;
264
265         logfile_ = log;
266
267         updateContents();
268
269         return true;
270 }
271
272
273 void GuiLog::clearParams()
274 {
275         logfile_.erase();
276 }
277
278
279 docstring GuiLog::title() const
280 {
281         switch (type_) {
282         case LatexLog:
283                 return _("LaTeX Log");
284         case LiterateLog:
285                 return _("Literate Programming Build Log");
286         case Lyx2lyxLog:
287                 return _("lyx2lyx Error Log");
288         case VCLog:
289                 return _("Version Control Log");
290         default:
291                 return docstring();
292         }
293 }
294
295
296 void GuiLog::getContents(ostream & ss) const
297 {
298         ifstream in(logfile_.toFilesystemEncoding().c_str());
299
300         bool success = false;
301
302         // FIXME UNICODE
303         // Our caller interprets the file contents as UTF8, but is that
304         // correct?
305         if (in) {
306                 ss << in.rdbuf();
307                 success = ss.good();
308         }
309
310         if (success)
311                 return;
312
313         switch (type_) {
314         case LatexLog:
315                 ss << to_utf8(_("Log file not found."));
316                 break;
317         case LiterateLog:
318                 ss << to_utf8(_("No literate programming build log file found."));
319                 break;
320         case Lyx2lyxLog:
321                 ss << to_utf8(_("No lyx2lyx error log file found."));
322                 break;
323         case VCLog:
324                 ss << to_utf8(_("No version control log file found."));
325                 break;
326         }
327 }
328
329
330 Dialog * createGuiLog(GuiView & lv) { return new GuiLog(lv); }
331
332
333 } // namespace frontend
334 } // namespace lyx
335
336 #include "moc_GuiLog.cpp"