]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiLog.cpp
Spoiling some fun from Andre': put Application on a diet and remove unnecessary strin...
[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 "gettext.h"
19 #include "Lexer.h"
20
21 #include <QCloseEvent>
22 #include <QTextBrowser>
23 #include <QSyntaxHighlighter>
24 #include <QClipboard>
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"), type_(LatexLog)
107 {
108         setupUi(this);
109
110         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
111         connect(updatePB, SIGNAL(clicked()), this, SLOT(updateContents()));
112
113         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
114
115         // set syntax highlighting
116         highlighter = new LogHighlighter(logTB->document());
117
118         logTB->setReadOnly(true);
119         QFont font(guiApp->typewriterFontName());
120         font.setKerning(false);
121         font.setFixedPitch(true);
122         font.setStyleHint(QFont::TypeWriter);
123         logTB->setFont(font);
124 }
125
126
127 void GuiLog::closeEvent(QCloseEvent * e)
128 {
129         slotClose();
130         e->accept();
131 }
132
133
134 void GuiLog::updateContents()
135 {
136         setViewTitle(title());
137
138         std::ostringstream ss;
139         getContents(ss);
140
141         logTB->setPlainText(toqstr(ss.str()));
142 }
143
144
145 bool GuiLog::initialiseParams(string const & data)
146 {
147         istringstream is(data);
148         Lexer lex(0,0);
149         lex.setStream(is);
150
151         string logtype, logfile;
152         lex >> logtype;
153         if (lex) {
154                 lex.next(true);
155                 logfile = lex.getString();
156         }
157         if (!lex)
158                 // Parsing of the data failed.
159                 return false;
160
161         if (logtype == "latex")
162                 type_ = LatexLog;
163         else if (logtype == "literate")
164                 type_ = LiterateLog;
165         else if (logtype == "lyx2lyx")
166                 type_ = Lyx2lyxLog;
167         else if (logtype == "vc")
168                 type_ = VCLog;
169         else
170                 return false;
171
172         logfile_ = FileName(logfile);
173         return true;
174 }
175
176
177 void GuiLog::clearParams()
178 {
179         logfile_.erase();
180 }
181
182
183 docstring GuiLog::title() const
184 {
185         switch (type_) {
186         case LatexLog:
187                 return _("LaTeX Log");
188         case LiterateLog:
189                 return _("Literate Programming Build Log");
190         case Lyx2lyxLog:
191                 return _("lyx2lyx Error Log");
192         case VCLog:
193                 return _("Version Control Log");
194         default:
195                 return docstring();
196         }
197 }
198
199
200 void GuiLog::getContents(std::ostream & ss) const
201 {
202         std::ifstream in(logfile_.toFilesystemEncoding().c_str());
203
204         bool success = false;
205
206         // FIXME UNICODE
207         // Our caller interprets the file contents as UTF8, but is that
208         // correct?
209         if (in) {
210                 ss << in.rdbuf();
211                 success = ss.good();
212         }
213
214         if (success)
215                 return;
216
217         switch (type_) {
218         case LatexLog:
219                 ss << to_utf8(_("No LaTeX log file found."));
220                 break;
221         case LiterateLog:
222                 ss << to_utf8(_("No literate programming build log file found."));
223                 break;
224         case Lyx2lyxLog:
225                 ss << to_utf8(_("No lyx2lyx error log file found."));
226                 break;
227         case VCLog:
228                 ss << to_utf8(_("No version control log file found."));
229                 break;
230         }
231 }
232
233
234 void GuiLog::on_copyPB_clicked()
235 {
236         qApp->clipboard()->setText(logTB->toPlainText());
237 }
238
239
240 Dialog * createGuiLog(LyXView & lv) { return new GuiLog(lv); }
241
242
243 } // namespace frontend
244 } // namespace lyx
245
246 #include "GuiLog_moc.cpp"