]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiLog.cpp
Really use qstr to convert a string in a QString
[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 // FIXME: These regexes are incomplete. It would be good if we could collect those used in LaTeX::scanLogFile
41 //        and LaTeX::scanBlgFile and re-use them here!(spitz, 2013-05-27)
42 // Information
43 QRegExp exprInfo("^(Document Class:|LaTeX Font Info:|File:|Package:|Language:|Underfull|Overfull|.*> INFO - |\\(|\\\\).*$");
44 // Warnings
45 QRegExp exprWarning("^(LaTeX Warning|LaTeX Font Warning|Package [\\w\\.]+ Warning|Class \\w+ Warning|Warning--|.*> WARN - ).*$");
46 // Errors
47 QRegExp exprError("^(!|.*---line [0-9]+ of file|.*> FATAL - |.*> ERROR - ).*$");
48
49
50 /////////////////////////////////////////////////////////////////////
51 //
52 // LogHighlighter
53 //
54 ////////////////////////////////////////////////////////////////////
55
56 class LogHighlighter : public QSyntaxHighlighter
57 {
58 public:
59         LogHighlighter(QTextDocument * parent);
60
61 private:
62         void highlightBlock(QString const & text);
63
64 private:
65         QTextCharFormat infoFormat;
66         QTextCharFormat warningFormat;
67         QTextCharFormat errorFormat;
68 };
69
70
71
72 LogHighlighter::LogHighlighter(QTextDocument * parent)
73         : QSyntaxHighlighter(parent)
74 {
75         infoFormat.setForeground(Qt::darkGray);
76         warningFormat.setForeground(Qt::darkBlue);
77         errorFormat.setForeground(Qt::red);
78 }
79
80
81 void LogHighlighter::highlightBlock(QString const & text)
82 {
83         // Info
84         int index = exprInfo.indexIn(text);
85         while (index >= 0) {
86                 int length = exprInfo.matchedLength();
87                 setFormat(index, length, infoFormat);
88                 index = exprInfo.indexIn(text, index + length);
89         }
90         // LaTeX Warning:
91         index = exprWarning.indexIn(text);
92         while (index >= 0) {
93                 int length = exprWarning.matchedLength();
94                 setFormat(index, length, warningFormat);
95                 index = exprWarning.indexIn(text, index + length);
96         }
97         // ! error
98         index = exprError.indexIn(text);
99         while (index >= 0) {
100                 int length = exprError.matchedLength();
101                 setFormat(index, length, errorFormat);
102                 index = exprError.indexIn(text, index + length);
103         }
104 }
105
106
107 /////////////////////////////////////////////////////////////////////
108 //
109 // GuiLog
110 //
111 /////////////////////////////////////////////////////////////////////
112
113 GuiLog::GuiLog(GuiView & lv)
114         : GuiDialog(lv, "log", qt_("LaTeX Log")), type_(LatexLog)
115 {
116         setupUi(this);
117
118         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
119         connect(updatePB, SIGNAL(clicked()), this, SLOT(updateContents()));
120         connect(findPB, SIGNAL(clicked()), this, SLOT(find()));
121         // FIXME: find via returnPressed() does not work!
122         connect(findLE, SIGNAL(returnPressed()), this, SLOT(find()));
123         connect(logTypeCO, SIGNAL(activated(int)),
124                 this, SLOT(typeChanged(int)));
125
126         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
127
128         // set syntax highlighting
129         highlighter = new LogHighlighter(logTB->document());
130
131         logTB->setReadOnly(true);
132         QFont font(guiApp->typewriterFontName());
133         font.setKerning(false);
134         font.setFixedPitch(true);
135         font.setStyleHint(QFont::TypeWriter);
136         logTB->setFont(font);
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::goTo(QRegExp const & exp) const
192 {
193         QTextCursor const newc =
194                 logTB->document()->find(exp, logTB->textCursor());
195         logTB->setTextCursor(newc);
196 }
197
198
199 bool GuiLog::contains(QRegExp const & exp) const
200 {
201         return !logTB->document()->find(exp, logTB->textCursor()).isNull();
202 }
203
204
205 bool GuiLog::initialiseParams(string const & data)
206 {
207         istringstream is(data);
208         Lexer lex;
209         lex.setStream(is);
210
211         string logtype, logfile;
212         lex >> logtype;
213         if (lex) {
214                 lex.next(true);
215                 logfile = lex.getString();
216         }
217         if (!lex)
218                 // Parsing of the data failed.
219                 return false;
220
221         logTypeCO->setEnabled(logtype == "latex");
222         logTypeCO->clear();
223         
224         FileName log(logfile);
225         
226         if (logtype == "latex") {
227                 type_ = LatexLog;
228                 logTypeCO->addItem(qt_("LaTeX"), toqstr(logtype));
229                 FileName tmp = log;
230                 tmp.changeExtension("blg");
231                 if (tmp.exists())
232                         logTypeCO->addItem(qt_("BibTeX"), QString("bibtex"));
233                 tmp.changeExtension("ilg");
234                 if (tmp.exists())
235                         logTypeCO->addItem(qt_("Index"), QString("index"));
236         // FIXME: not sure "literate" still works.
237         } else if (logtype == "literate") {
238                 type_ = LiterateLog;
239                 logTypeCO->addItem(qt_("Literate"), toqstr(logtype));
240         } else if (logtype == "lyx2lyx") {
241                 type_ = Lyx2lyxLog;
242                 logTypeCO->addItem(qt_("LyX2LyX"), toqstr(logtype));
243         } else if (logtype == "vc") {
244                 type_ = VCLog;
245                 logTypeCO->addItem(qt_("Version Control"), toqstr(logtype));
246         } else
247                 return false;
248
249         logfile_ = log;
250
251         updateContents();
252
253         return true;
254 }
255
256
257 void GuiLog::clearParams()
258 {
259         logfile_.erase();
260 }
261
262
263 docstring GuiLog::title() const
264 {
265         switch (type_) {
266         case LatexLog:
267                 return _("LaTeX Log");
268         case LiterateLog:
269                 return _("Literate Programming Build Log");
270         case Lyx2lyxLog:
271                 return _("lyx2lyx Error Log");
272         case VCLog:
273                 return _("Version Control Log");
274         default:
275                 return docstring();
276         }
277 }
278
279
280 void GuiLog::getContents(ostream & ss) const
281 {
282         ifstream in(logfile_.toFilesystemEncoding().c_str());
283
284         bool success = false;
285
286         // FIXME UNICODE
287         // Our caller interprets the file contents as UTF8, but is that
288         // correct?
289         if (in) {
290                 ss << in.rdbuf();
291                 success = ss.good();
292         }
293
294         if (success)
295                 return;
296
297         switch (type_) {
298         case LatexLog:
299                 ss << to_utf8(_("Log file not found."));
300                 break;
301         case LiterateLog:
302                 ss << to_utf8(_("No literate programming build log file found."));
303                 break;
304         case Lyx2lyxLog:
305                 ss << to_utf8(_("No lyx2lyx error log file found."));
306                 break;
307         case VCLog:
308                 ss << to_utf8(_("No version control log file found."));
309                 break;
310         }
311 }
312
313
314 void GuiLog::on_copyPB_clicked()
315 {
316         qApp->clipboard()->setText(logTB->toPlainText());
317 }
318
319
320 Dialog * createGuiLog(GuiView & lv) { return new GuiLog(lv); }
321
322
323 } // namespace frontend
324 } // namespace lyx
325
326 #include "moc_GuiLog.cpp"