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