]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiErrorList.cpp
Button for showing PDF if compilation error
[lyx.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")
50                 return N_("LaTeX");
51         return s;
52 }
53
54 } // namespace anon
55
56 namespace lyx {
57 namespace frontend {
58
59 GuiErrorList::GuiErrorList(GuiView & lv)
60         : GuiDialog(lv, "errorlist", qt_("Error List"))
61 {
62         setupUi(this);
63
64         connect(closePB, SIGNAL(clicked()),
65                 this, SLOT(slotClose()));
66         connect(viewLogPB, SIGNAL(clicked()),
67                 this, SLOT(viewLog()));
68         connect(showAnywayPB, SIGNAL(clicked()),
69                 this, SLOT(showAnyway()));
70         connect(errorsLW, SIGNAL(currentRowChanged(int)),
71                 this, SLOT(select()));
72
73         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
74         bc().setCancel(closePB);
75         showAnywayPB->setEnabled(lyx::getStatus(FuncRequest(LFUN_BUFFER_VIEW_CACHE)).enabled());
76 }
77
78
79 void GuiErrorList::showEvent(QShowEvent * e)
80 {
81         select();
82         paramsToDialog();
83         e->accept();
84 }
85
86
87 void GuiErrorList::select()
88 {
89         int const item = errorsLW->row(errorsLW->currentItem());
90         if (item == -1)
91                 return;
92         goTo(item);
93         descriptionTB->setPlainText(
94                 toqstr(errorList()[item].description));
95 }
96
97
98 void GuiErrorList::viewLog()
99 {
100         if (&buffer() != buf_) {
101                 if (!theBufferList().isLoaded(buf_))
102                         return;
103                 FuncRequest fr(LFUN_BUFFER_SWITCH, buf_->absFileName());
104                 dispatch(fr);
105         }
106         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "latexlog"));
107 }
108
109
110 void GuiErrorList::showAnyway()
111 {
112         dispatch(FuncRequest(LFUN_BUFFER_VIEW_CACHE));
113 }
114
115
116 void GuiErrorList::paramsToDialog()
117 {
118         setTitle(toqstr(name_));
119         errorsLW->clear();
120         descriptionTB->setPlainText(QString());
121
122         ErrorList const & el = errorList();
123         ErrorList::const_iterator it = el.begin();
124         ErrorList::const_iterator const en = el.end();
125         for (; it != en; ++it)
126                 errorsLW->addItem(toqstr(it->error));
127         errorsLW->setCurrentRow(0);
128 }
129
130
131 ErrorList const & GuiErrorList::errorList() const
132 {
133         Buffer const * buffer = from_master_
134                                 ? bufferview()->buffer().masterBuffer()
135                                 : &bufferview()->buffer();
136         if (buffer == buf_)
137                 error_list_ = buffer->errorList(error_type_);
138
139         return error_list_;
140 }
141
142
143 bool GuiErrorList::initialiseParams(string const & data)
144 {
145         from_master_ = prefixIs(data, "from_master|");
146         string error_type = data;
147         if (from_master_)
148                 error_type = split(data, '|');
149         error_type_ = error_type;
150         buf_ = from_master_ ?
151                 bufferview()->buffer().masterBuffer()
152                 : &bufferview()->buffer();
153         name_ = bformat(_("%1$s Errors (%2$s)"), 
154                                 _(guiErrorType(error_type)),
155                                     from_utf8(buf_->absFileName()));
156         paramsToDialog();
157         return true;
158 }
159
160
161 bool GuiErrorList::goTo(int item)
162 {
163         ErrorItem const & err = errorList()[item];
164
165         if (err.par_id == -1)
166                 return false;
167
168         Buffer const * errbuf = err.buffer ? err.buffer : buf_;
169
170         if (&buffer() != errbuf) {
171                 if (!theBufferList().isLoaded(errbuf))
172                         return false;
173                 FuncRequest fr(LFUN_BUFFER_SWITCH, errbuf->absFileName());
174                 dispatch(fr);
175         }
176
177         DocIterator dit = errbuf->getParFromID(err.par_id);
178
179         if (dit == doc_iterator_end(errbuf)) {
180                 // FIXME: Happens when loading a read-only doc with 
181                 // unknown layout. Should this be the case?
182                 LYXERR0("par id " << err.par_id << " not found");
183                 return false;
184         }
185
186         // Don't try to highlight the content of non-editable insets
187         while (!dit.inset().editable())
188                 dit.backwardPos();
189
190         // Now make the selection.
191         BufferView * bv = const_cast<BufferView *>(bufferview());
192         if (bv->selectIfEmpty(dit)) {
193                 // The paragraph is empty but can be selected
194                 bv->processUpdateFlags(Update::Force | Update::FitCursor);
195                 return true;
196         }
197         if (dit.empty()) {
198                 // The paragraph is empty and cannot be selected
199                 return false;
200         }
201         // if pos_end is 0, this means it is end-of-paragraph
202         pos_type const s = dit.lastpos();
203         pos_type const end = err.pos_end ? min(err.pos_end, s) : s;
204         pos_type const start = min(err.pos_start, end);
205         pos_type const range = end == start ? s - start : end - start;
206         // end-of-paragraph cannot be highlighted, so highlight the last thing
207         dit.pos() = range ? start : end - 1;
208         // FIXME LFUN
209         // If we used an LFUN, we would not need these lines:
210         bv->putSelectionAt(dit, max(range, pos_type(1)), false);
211         bv->processUpdateFlags(Update::Force | Update::FitCursor);
212         return true;
213 }
214
215
216 Dialog * createGuiErrorList(GuiView & lv) { return new GuiErrorList(lv); }
217
218 } // namespace frontend
219 } // namespace lyx
220
221
222 #include "moc_GuiErrorList.cpp"