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