]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiLog.cpp
Allow literate documents other than noweb to work out of the box. Currently
[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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiLog.h"
15
16 #include "GuiApplication.h"
17 #include "qt_helpers.h"
18 #include "Lexer.h"
19
20 #include "support/docstring.h"
21 #include "support/gettext.h"
22
23 #include <QTextBrowser>
24 #include <QSyntaxHighlighter>
25 #include <QClipboard>
26
27 #include <fstream>
28 #include <sstream>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34 namespace frontend {
35
36 /////////////////////////////////////////////////////////////////////
37 //
38 // LogHighlighter
39 //
40 ////////////////////////////////////////////////////////////////////
41
42 class LogHighlighter : public QSyntaxHighlighter
43 {
44 public:
45         LogHighlighter(QTextDocument * parent);
46
47 private:
48         void highlightBlock(QString const & text);
49
50 private:
51         QTextCharFormat infoFormat;
52         QTextCharFormat warningFormat;
53         QTextCharFormat errorFormat;
54 };
55
56
57
58 LogHighlighter::LogHighlighter(QTextDocument * parent)
59         : QSyntaxHighlighter(parent)
60 {
61         infoFormat.setForeground(Qt::darkGray);
62         warningFormat.setForeground(Qt::darkBlue);
63         errorFormat.setForeground(Qt::red);
64 }
65
66
67 void LogHighlighter::highlightBlock(QString const & text)
68 {
69         // Info
70         QRegExp exprInfo("^(Document Class:|LaTeX Font Info:|File:|Package:|Language:|Underfull|Overfull|\\(|\\\\).*$");
71         int index = exprInfo.indexIn(text);
72         while (index >= 0) {
73                 int length = exprInfo.matchedLength();
74                 setFormat(index, length, infoFormat);
75                 index = exprInfo.indexIn(text, index + length);
76         }
77         // LaTeX Warning:
78         QRegExp exprWarning("^LaTeX Warning.*$");
79         index = exprWarning.indexIn(text);
80         while (index >= 0) {
81                 int length = exprWarning.matchedLength();
82                 setFormat(index, length, warningFormat);
83                 index = exprWarning.indexIn(text, index + length);
84         }
85         // ! error
86         QRegExp exprError("^!.*$");
87         index = exprError.indexIn(text);
88         while (index >= 0) {
89                 int length = exprError.matchedLength();
90                 setFormat(index, length, errorFormat);
91                 index = exprError.indexIn(text, index + length);
92         }
93 }
94
95
96 /////////////////////////////////////////////////////////////////////
97 //
98 // GuiLog
99 //
100 /////////////////////////////////////////////////////////////////////
101
102 GuiLog::GuiLog(GuiView & lv)
103         : GuiDialog(lv, "log", qt_("LaTeX Log")), type_(LatexLog)
104 {
105         setupUi(this);
106
107         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
108         connect(updatePB, SIGNAL(clicked()), this, SLOT(updateContents()));
109
110         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
111
112         // set syntax highlighting
113         highlighter = new LogHighlighter(logTB->document());
114
115         logTB->setReadOnly(true);
116         QFont font(guiApp->typewriterFontName());
117         font.setKerning(false);
118         font.setFixedPitch(true);
119         font.setStyleHint(QFont::TypeWriter);
120         logTB->setFont(font);
121 }
122
123
124 void GuiLog::updateContents()
125 {
126         setTitle(toqstr(title()));
127
128         ostringstream ss;
129         getContents(ss);
130
131         logTB->setPlainText(toqstr(ss.str()));
132 }
133
134
135 bool GuiLog::initialiseParams(string const & data)
136 {
137         istringstream is(data);
138         Lexer lex;
139         lex.setStream(is);
140
141         string logtype, logfile;
142         lex >> logtype;
143         if (lex) {
144                 lex.next(true);
145                 logfile = lex.getString();
146         }
147         if (!lex)
148                 // Parsing of the data failed.
149                 return false;
150
151         if (logtype == "latex")
152                 type_ = LatexLog;
153         // FIXME: not sure "literate" still works.
154         else if (logtype == "literate")
155                 type_ = LiterateLog;
156         else if (logtype == "lyx2lyx")
157                 type_ = Lyx2lyxLog;
158         else if (logtype == "vc")
159                 type_ = VCLog;
160         else
161                 return false;
162
163         logfile_ = FileName(logfile);
164
165         updateContents();
166
167         return true;
168 }
169
170
171 void GuiLog::clearParams()
172 {
173         logfile_.erase();
174 }
175
176
177 docstring GuiLog::title() const
178 {
179         switch (type_) {
180         case LatexLog:
181                 return _("LaTeX Log");
182         case LiterateLog:
183                 return _("Literate Programming Build Log");
184         case Lyx2lyxLog:
185                 return _("lyx2lyx Error Log");
186         case VCLog:
187                 return _("Version Control Log");
188         default:
189                 return docstring();
190         }
191 }
192
193
194 void GuiLog::getContents(ostream & ss) const
195 {
196         ifstream in(logfile_.toFilesystemEncoding().c_str());
197
198         bool success = false;
199
200         // FIXME UNICODE
201         // Our caller interprets the file contents as UTF8, but is that
202         // correct?
203         if (in) {
204                 ss << in.rdbuf();
205                 success = ss.good();
206         }
207
208         if (success)
209                 return;
210
211         switch (type_) {
212         case LatexLog:
213                 ss << to_utf8(_("No LaTeX log file found."));
214                 break;
215         case LiterateLog:
216                 ss << to_utf8(_("No literate programming build log file found."));
217                 break;
218         case Lyx2lyxLog:
219                 ss << to_utf8(_("No lyx2lyx error log file found."));
220                 break;
221         case VCLog:
222                 ss << to_utf8(_("No version control log file found."));
223                 break;
224         }
225 }
226
227
228 void GuiLog::on_copyPB_clicked()
229 {
230         qApp->clipboard()->setText(logTB->toPlainText());
231 }
232
233
234 Dialog * createGuiLog(GuiView & lv) { return new GuiLog(lv); }
235
236
237 } // namespace frontend
238 } // namespace lyx
239
240 #include "moc_GuiLog.cpp"