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