]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiLog.cpp
add missing include
[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 #include <QClipboard>
26
27 #include <fstream>
28 #include <sstream>
29
30 using std::istringstream;
31 using std::ostream;
32 using std::string;
33
34
35 namespace lyx {
36 namespace frontend {
37
38 using support::FileName;
39
40 /////////////////////////////////////////////////////////////////////
41 //
42 // LogHighlighter
43 //
44 ////////////////////////////////////////////////////////////////////
45
46 class LogHighlighter : public QSyntaxHighlighter
47 {
48 public:
49         LogHighlighter(QTextDocument * parent);
50
51 private:
52         void highlightBlock(QString const & text);
53
54 private:
55         QTextCharFormat infoFormat;
56         QTextCharFormat warningFormat;
57         QTextCharFormat errorFormat;
58 };
59
60
61
62 LogHighlighter::LogHighlighter(QTextDocument * parent)
63         : QSyntaxHighlighter(parent)
64 {
65         infoFormat.setForeground(Qt::darkGray);
66         warningFormat.setForeground(Qt::darkBlue);
67         errorFormat.setForeground(Qt::red);
68 }
69
70
71 void LogHighlighter::highlightBlock(QString const & text)
72 {
73         // Info
74         QRegExp exprInfo("^(Document Class:|LaTeX Font Info:|File:|Package:|Language:|Underfull|Overfull|\\(|\\\\).*$");
75         int index = text.indexOf(exprInfo);
76         while (index >= 0) {
77                 int length = exprInfo.matchedLength();
78                 setFormat(index, length, infoFormat);
79                 index = text.indexOf(exprInfo, index + length);
80         }
81         // LaTeX Warning:
82         QRegExp exprWarning("^LaTeX Warning.*$");
83         index = text.indexOf(exprWarning);
84         while (index >= 0) {
85                 int length = exprWarning.matchedLength();
86                 setFormat(index, length, warningFormat);
87                 index = text.indexOf(exprWarning, index + length);
88         }
89         // ! error
90         QRegExp exprError("^!.*$");
91         index = text.indexOf(exprError);
92         while (index >= 0) {
93                 int length = exprError.matchedLength();
94                 setFormat(index, length, errorFormat);
95                 index = text.indexOf(exprError, index + length);
96         }
97 }
98
99
100 /////////////////////////////////////////////////////////////////////
101 //
102 // GuiLog
103 //
104 /////////////////////////////////////////////////////////////////////
105
106 GuiLog::GuiLog(LyXView & lv)
107         : GuiDialog(lv, "log"), type_(LatexLog)
108 {
109         setupUi(this);
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 void GuiLog::on_copyPB_clicked()
236 {
237         qApp->clipboard()->setText(logTB->toPlainText());
238 }
239
240
241 Dialog * createGuiLog(LyXView & lv) { return new GuiLog(lv); }
242
243
244 } // namespace frontend
245 } // namespace lyx
246
247 #include "GuiLog_moc.cpp"