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