]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiLog.cpp
Revision control log is not visible after launching, moreover the title didn't
[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 "Lexer.h"
19
20 #include "support/docstring.h"
21 #include "support/gettext.h"
22
23 #include <QTextBrowser>
24 #include <QSyntaxHighlighter>
25 #include <QClipboard>
26
27 #include <fstream>
28 #include <sstream>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34 namespace frontend {
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", qt_("LaTeX 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::updateContents()
125 {
126         setTitle(toqstr(title()));
127
128         ostringstream ss;
129         getContents(ss);
130
131         logTB->setPlainText(toqstr(ss.str()));
132 }
133
134
135 bool GuiLog::initialiseParams(string const & data)
136 {
137         istringstream is(data);
138         Lexer lex;
139         lex.setStream(is);
140
141         string logtype, logfile;
142         lex >> logtype;
143         if (lex) {
144                 lex.next(true);
145                 logfile = lex.getString();
146         }
147         if (!lex)
148                 // Parsing of the data failed.
149                 return false;
150
151         if (logtype == "latex")
152                 type_ = LatexLog;
153         else if (logtype == "literate")
154                 type_ = LiterateLog;
155         else if (logtype == "lyx2lyx")
156                 type_ = Lyx2lyxLog;
157         else if (logtype == "vc")
158                 type_ = VCLog;
159         else
160                 return false;
161
162         logfile_ = FileName(logfile);
163
164         updateContents();
165
166         return true;
167 }
168
169
170 void GuiLog::clearParams()
171 {
172         logfile_.erase();
173 }
174
175
176 docstring GuiLog::title() const
177 {
178         switch (type_) {
179         case LatexLog:
180                 return _("LaTeX Log");
181         case LiterateLog:
182                 return _("Literate Programming Build Log");
183         case Lyx2lyxLog:
184                 return _("lyx2lyx Error Log");
185         case VCLog:
186                 return _("Version Control Log");
187         default:
188                 return docstring();
189         }
190 }
191
192
193 void GuiLog::getContents(ostream & ss) const
194 {
195         ifstream in(logfile_.toFilesystemEncoding().c_str());
196
197         bool success = false;
198
199         // FIXME UNICODE
200         // Our caller interprets the file contents as UTF8, but is that
201         // correct?
202         if (in) {
203                 ss << in.rdbuf();
204                 success = ss.good();
205         }
206
207         if (success)
208                 return;
209
210         switch (type_) {
211         case LatexLog:
212                 ss << to_utf8(_("No LaTeX log file found."));
213                 break;
214         case LiterateLog:
215                 ss << to_utf8(_("No literate programming build log file found."));
216                 break;
217         case Lyx2lyxLog:
218                 ss << to_utf8(_("No lyx2lyx error log file found."));
219                 break;
220         case VCLog:
221                 ss << to_utf8(_("No version control log file found."));
222                 break;
223         }
224 }
225
226
227 void GuiLog::on_copyPB_clicked()
228 {
229         qApp->clipboard()->setText(logTB->toPlainText());
230 }
231
232
233 Dialog * createGuiLog(GuiView & lv) { return new GuiLog(lv); }
234
235
236 } // namespace frontend
237 } // namespace lyx
238
239 #include "GuiLog_moc.cpp"