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