]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiErrorList.cpp
Generate a proper error dialog title for XeTeX/LuaTeX
[features.git] / src / frontends / qt4 / GuiErrorList.cpp
1 /**
2  * \file GuiErrorList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiErrorList.h"
14
15 #include "GuiView.h"
16 #include "qt_helpers.h"
17
18 #include "Buffer.h"
19 #include "BufferView.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "BufferList.h"
23 #include "LyX.h"
24 #include "ParIterator.h"
25 #include "Text.h"
26
27 #include "support/debug.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 #include <QListWidget>
32 #include <QPushButton>
33 #include <QShowEvent>
34 #include <QTextBrowser>
35
36 using namespace std;
37 using namespace lyx::support;
38
39 namespace {
40
41 string const guiErrorType(string const & s)
42 {
43         if (s == "docbook")
44                 return N_("DocBook");
45         else if (s == "literate")
46                 return N_("Literate");
47         else if (s == "platex")
48                 return N_("pLaTeX");
49         else if (s == "latex" || "xetex")
50                 // All LaTeX variants except pLaTeX
51                 // (LaTeX, PDFLaTeX, XeTeX, LuaTeX)
52                 return N_("LaTeX");
53         return s;
54 }
55
56 } // namespace anon
57
58 namespace lyx {
59 namespace frontend {
60
61 GuiErrorList::GuiErrorList(GuiView & lv)
62         : GuiDialog(lv, "errorlist", qt_("Error List")), from_master_(false)
63 {
64         setupUi(this);
65
66         connect(closePB, SIGNAL(clicked()),
67                 this, SLOT(slotClose()));
68         connect(viewLogPB, SIGNAL(clicked()),
69                 this, SLOT(viewLog()));
70         connect(showAnywayPB, SIGNAL(clicked()),
71                 this, SLOT(showAnyway()));
72         connect(errorsLW, SIGNAL(currentRowChanged(int)),
73                 this, SLOT(select()));
74
75         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
76         bc().setCancel(closePB);
77 }
78
79
80 void GuiErrorList::showEvent(QShowEvent * e)
81 {
82         select();
83         paramsToDialog();
84         e->accept();
85 }
86
87
88 void GuiErrorList::select()
89 {
90         int const item = errorsLW->row(errorsLW->currentItem());
91         if (item == -1)
92                 return;
93         goTo(item);
94         descriptionTB->setPlainText(
95                 toqstr(errorList()[item].description));
96 }
97
98
99 void GuiErrorList::viewLog()
100 {
101         if (&buffer() != buf_) {
102                 if (!theBufferList().isLoaded(buf_))
103                         return;
104                 FuncRequest fr(LFUN_BUFFER_SWITCH, buf_->absFileName());
105                 dispatch(fr);
106         }
107         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "latexlog"));
108 }
109
110
111 void GuiErrorList::showAnyway()
112 {
113         dispatch(FuncRequest(LFUN_BUFFER_VIEW_CACHE));
114 }
115
116
117 void GuiErrorList::paramsToDialog()
118 {
119         setTitle(toqstr(name_));
120         errorsLW->clear();
121         descriptionTB->setPlainText(QString());
122
123         ErrorList const & el = errorList();
124         ErrorList::const_iterator it = el.begin();
125         ErrorList::const_iterator const en = el.end();
126         for (; it != en; ++it)
127                 errorsLW->addItem(toqstr(it->error));
128         errorsLW->setCurrentRow(0);
129         showAnywayPB->setEnabled(
130                 lyx::getStatus(FuncRequest(LFUN_BUFFER_VIEW_CACHE)).enabled());
131 }
132
133
134 ErrorList const & GuiErrorList::errorList() const
135 {
136         Buffer const * buffer = from_master_
137                                 ? bufferview()->buffer().masterBuffer()
138                                 : &bufferview()->buffer();
139         if (buffer == buf_)
140                 error_list_ = buffer->errorList(error_type_);
141
142         return error_list_;
143 }
144
145
146 bool GuiErrorList::initialiseParams(string const & data)
147 {
148         from_master_ = prefixIs(data, "from_master|");
149         string error_type = data;
150         if (from_master_)
151                 error_type = split(data, '|');
152         error_type_ = error_type;
153         buf_ = from_master_ ?
154                 bufferview()->buffer().masterBuffer()
155                 : &bufferview()->buffer();
156         name_ = bformat(_("%1$s Errors (%2$s)"), 
157                                 _(guiErrorType(error_type)),
158                                     from_utf8(buf_->absFileName()));
159         paramsToDialog();
160         return true;
161 }
162
163
164 bool GuiErrorList::goTo(int item)
165 {
166         ErrorItem const & err = errorList()[item];
167
168         if (err.par_id == -1)
169                 return false;
170
171         Buffer const * errbuf = err.buffer ? err.buffer : buf_;
172
173         if (&buffer() != errbuf) {
174                 if (!theBufferList().isLoaded(errbuf))
175                         return false;
176                 FuncRequest fr(LFUN_BUFFER_SWITCH, errbuf->absFileName());
177                 dispatch(fr);
178         }
179
180         DocIterator dit = errbuf->getParFromID(err.par_id);
181
182         if (dit == doc_iterator_end(errbuf)) {
183                 // FIXME: Happens when loading a read-only doc with 
184                 // unknown layout. Should this be the case?
185                 LYXERR0("par id " << err.par_id << " not found");
186                 return false;
187         }
188
189         // Don't try to highlight the content of non-editable insets
190         while (!dit.inset().editable())
191                 dit.backwardPos();
192
193         // Now make the selection.
194         BufferView * bv = const_cast<BufferView *>(bufferview());
195         if (bv->selectIfEmpty(dit)) {
196                 // The paragraph is empty but can be selected
197                 bv->processUpdateFlags(Update::Force | Update::FitCursor);
198                 return true;
199         }
200         if (dit.empty()) {
201                 // The paragraph is empty and cannot be selected
202                 return false;
203         }
204         // if pos_end is 0, this means it is end-of-paragraph
205         pos_type const s = dit.lastpos();
206         pos_type const end = err.pos_end ? min(err.pos_end, s) : s;
207         pos_type const start = min(err.pos_start, end);
208         pos_type const range = end == start ? s - start : end - start;
209         // end-of-paragraph cannot be highlighted, so highlight the last thing
210         dit.pos() = range ? start : end - 1;
211         // FIXME LFUN
212         // If we used an LFUN, we would not need these lines:
213         bv->putSelectionAt(dit, max(range, pos_type(1)), false);
214         bv->processUpdateFlags(Update::Force | Update::FitCursor);
215         return true;
216 }
217
218
219 Dialog * createGuiErrorList(GuiView & lv) { return new GuiErrorList(lv); }
220
221 } // namespace frontend
222 } // namespace lyx
223
224
225 #include "moc_GuiErrorList.cpp"