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