]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiLog.cpp
'using namespace std' instead of 'using std::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
31 namespace lyx {
32 namespace frontend {
33
34 using support::FileName;
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 = text.indexOf(exprInfo);
72         while (index >= 0) {
73                 int length = exprInfo.matchedLength();
74                 setFormat(index, length, infoFormat);
75                 index = text.indexOf(exprInfo, index + length);
76         }
77         // LaTeX Warning:
78         QRegExp exprWarning("^LaTeX Warning.*$");
79         index = text.indexOf(exprWarning);
80         while (index >= 0) {
81                 int length = exprWarning.matchedLength();
82                 setFormat(index, length, warningFormat);
83                 index = text.indexOf(exprWarning, index + length);
84         }
85         // ! error
86         QRegExp exprError("^!.*$");
87         index = text.indexOf(exprError);
88         while (index >= 0) {
89                 int length = exprError.matchedLength();
90                 setFormat(index, length, errorFormat);
91                 index = text.indexOf(exprError, index + length);
92         }
93 }
94
95
96 /////////////////////////////////////////////////////////////////////
97 //
98 // GuiLog
99 //
100 /////////////////////////////////////////////////////////////////////
101
102 GuiLog::GuiLog(GuiView & lv)
103         : GuiDialog(lv, "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::closeEvent(QCloseEvent * e)
125 {
126         slotClose();
127         e->accept();
128 }
129
130
131 void GuiLog::updateContents()
132 {
133         setViewTitle(title());
134
135         std::ostringstream ss;
136         getContents(ss);
137
138         logTB->setPlainText(toqstr(ss.str()));
139 }
140
141
142 bool GuiLog::initialiseParams(string const & data)
143 {
144         istringstream is(data);
145         Lexer lex(0,0);
146         lex.setStream(is);
147
148         string logtype, logfile;
149         lex >> logtype;
150         if (lex) {
151                 lex.next(true);
152                 logfile = lex.getString();
153         }
154         if (!lex)
155                 // Parsing of the data failed.
156                 return false;
157
158         if (logtype == "latex")
159                 type_ = LatexLog;
160         else if (logtype == "literate")
161                 type_ = LiterateLog;
162         else if (logtype == "lyx2lyx")
163                 type_ = Lyx2lyxLog;
164         else if (logtype == "vc")
165                 type_ = VCLog;
166         else
167                 return false;
168
169         logfile_ = FileName(logfile);
170         return true;
171 }
172
173
174 void GuiLog::clearParams()
175 {
176         logfile_.erase();
177 }
178
179
180 docstring GuiLog::title() const
181 {
182         switch (type_) {
183         case LatexLog:
184                 return _("LaTeX Log");
185         case LiterateLog:
186                 return _("Literate Programming Build Log");
187         case Lyx2lyxLog:
188                 return _("lyx2lyx Error Log");
189         case VCLog:
190                 return _("Version Control Log");
191         default:
192                 return docstring();
193         }
194 }
195
196
197 void GuiLog::getContents(std::ostream & ss) const
198 {
199         std::ifstream in(logfile_.toFilesystemEncoding().c_str());
200
201         bool success = false;
202
203         // FIXME UNICODE
204         // Our caller interprets the file contents as UTF8, but is that
205         // correct?
206         if (in) {
207                 ss << in.rdbuf();
208                 success = ss.good();
209         }
210
211         if (success)
212                 return;
213
214         switch (type_) {
215         case LatexLog:
216                 ss << to_utf8(_("No LaTeX log file found."));
217                 break;
218         case LiterateLog:
219                 ss << to_utf8(_("No literate programming build log file found."));
220                 break;
221         case Lyx2lyxLog:
222                 ss << to_utf8(_("No lyx2lyx error log file found."));
223                 break;
224         case VCLog:
225                 ss << to_utf8(_("No version control log file found."));
226                 break;
227         }
228 }
229
230
231 void GuiLog::on_copyPB_clicked()
232 {
233         qApp->clipboard()->setText(logTB->toPlainText());
234 }
235
236
237 Dialog * createGuiLog(GuiView & lv) { return new GuiLog(lv); }
238
239
240 } // namespace frontend
241 } // namespace lyx
242
243 #include "GuiLog_moc.cpp"