]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiErrorList.cpp
Comments.
[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 "BufferList.h"
22 #include "ParIterator.h"
23 #include "Text.h"
24
25 #include "support/debug.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28
29 #include <QListWidget>
30 #include <QPushButton>
31 #include <QShowEvent>
32 #include <QTextBrowser>
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace {
38
39 string const guiErrorType(string const s)
40 {
41         if (s == "docbook")
42                 return N_("DocBook");
43         else if (s == "literate")
44                 return N_("Literate");
45         else if (s == "platex")
46                 return N_("pLaTeX");
47         else if (s == "latex")
48                 return N_("LaTeX");
49         return s;
50 }
51
52 } // namespace anon
53
54 namespace lyx {
55 namespace frontend {
56
57 GuiErrorList::GuiErrorList(GuiView & lv)
58         : GuiDialog(lv, "errorlist", qt_("Error List"))
59 {
60         setupUi(this);
61
62         connect(closePB, SIGNAL(clicked()),
63                 this, SLOT(slotClose()));
64         connect(viewLogPB, SIGNAL(clicked()),
65                 this, SLOT(viewLog()));
66         connect(errorsLW, SIGNAL(currentRowChanged(int)),
67                 this, SLOT(select()));
68
69         bc().setPolicy(ButtonPolicy::OkCancelPolicy);
70         bc().setCancel(closePB);
71 }
72
73
74 void GuiErrorList::showEvent(QShowEvent * e)
75 {
76         select();
77         paramsToDialog();
78         e->accept();
79 }
80
81
82 void GuiErrorList::select()
83 {
84         int const item = errorsLW->row(errorsLW->currentItem());
85         if (item == -1)
86                 return;
87         goTo(item);
88         descriptionTB->setPlainText(
89                 toqstr(errorList()[item].description));
90 }
91
92
93 void GuiErrorList::viewLog()
94 {
95         if (&buffer() != buf_) {
96                 if (!theBufferList().isLoaded(buf_))
97                         return;
98                 FuncRequest fr(LFUN_BUFFER_SWITCH, buf_->absFileName());
99                 dispatch(fr);
100         }
101         dispatch(FuncRequest(LFUN_DIALOG_SHOW, "latexlog"));
102 }
103
104
105 void GuiErrorList::paramsToDialog()
106 {
107         setTitle(toqstr(name_));
108         errorsLW->clear();
109         descriptionTB->setPlainText(QString());
110
111         ErrorList const & el = errorList();
112         ErrorList::const_iterator it = el.begin();
113         ErrorList::const_iterator const en = el.end();
114         for (; it != en; ++it)
115                 errorsLW->addItem(toqstr(it->error));
116         errorsLW->setCurrentRow(0);
117 }
118
119
120 ErrorList const & GuiErrorList::errorList() const
121 {
122         if (&bufferview()->buffer() == buf_) {
123                 error_list_ = from_master_ ?
124                         bufferview()->buffer().masterBuffer()->errorList(error_type_)
125                         : bufferview()->buffer().errorList(error_type_);
126         }
127         return error_list_;
128 }
129
130
131 bool GuiErrorList::initialiseParams(string const & data)
132 {
133         from_master_ = prefixIs(data, "from_master|");
134         string error_type = data;
135         if (from_master_)
136                 error_type = split(data, '|');
137         error_type_ = error_type;
138         buf_ = from_master_ ?
139                 bufferview()->buffer().masterBuffer()
140                 : &bufferview()->buffer();
141         name_ = bformat(_("%1$s Errors (%2$s)"), 
142                                 _(guiErrorType(error_type)),
143                                     from_utf8(buf_->absFileName()));
144         paramsToDialog();
145         return true;
146 }
147
148
149 bool GuiErrorList::goTo(int item)
150 {
151         if (&buffer() != buf_) {
152                 if (!theBufferList().isLoaded(buf_))
153                         return false;
154                 FuncRequest fr(LFUN_BUFFER_SWITCH, buf_->absFileName());
155                 dispatch(fr);
156         }
157         ErrorItem const & err = errorList()[item];
158
159         if (err.par_id == -1)
160                 return false;
161
162         if (from_master_)
163                 // FIXME: implement
164                 return false;
165
166         DocIterator dit = buf_->getParFromID(err.par_id);
167
168         if (dit == doc_iterator_end(buf_)) {
169         // FIXME: Happens when loading a read-only doc with 
170         // unknown layout. Should this be the case?
171                 LYXERR0("par id " << err.par_id << " not found");
172                 return false;
173         }
174
175         // Now make the selection.
176         // if pos_end is 0, this means it is end-of-paragraph
177         pos_type const s = dit.paragraph().size();
178         pos_type const end = err.pos_end ? min(err.pos_end, s) : s;
179         pos_type const start = min(err.pos_start, end);
180         pos_type const range = end - start;
181         dit.pos() = start;
182         BufferView * bv = const_cast<BufferView *>(bufferview());
183         // FIXME LFUN
184         // If we used an LFUN, we would not need these lines:
185         bv->putSelectionAt(dit, range, false);
186         bv->processUpdateFlags(Update::Force | Update::FitCursor);
187         return true;
188 }
189
190
191 Dialog * createGuiErrorList(GuiView & lv) { return new GuiErrorList(lv); }
192
193 } // namespace frontend
194 } // namespace lyx
195
196
197 #include "moc_GuiErrorList.cpp"