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