]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiErrorList.cpp
17c07673a85223032d1a42b0ec7192a0289048fd
[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_iterator it = errorList().begin();
112         ErrorList::const_iterator end = errorList().end();
113         for (; it != end; ++it)
114                 errorsLW->addItem(toqstr(it->error));
115         errorsLW->setCurrentRow(0);
116 }
117
118
119 ErrorList const & GuiErrorList::errorList() const
120 {
121         if (&bufferview()->buffer() == buf_) {
122                 error_list_ = from_master_ ?
123                         bufferview()->buffer().masterBuffer()->errorList(error_type_)
124                         : bufferview()->buffer().errorList(error_type_);
125         }
126         return error_list_;
127 }
128
129
130 bool GuiErrorList::initialiseParams(string const & data)
131 {
132         from_master_ = prefixIs(data, "from_master|");
133         string error_type = data;
134         if (from_master_)
135                 error_type = split(data, '|');
136         error_type_ = error_type;
137         buf_ = from_master_ ?
138                 bufferview()->buffer().masterBuffer()
139                 : &bufferview()->buffer();
140         name_ = bformat(_("%1$s Errors (%2$s)"), 
141                                 _(guiErrorType(error_type)),
142                                     from_utf8(buf_->absFileName()));
143         paramsToDialog();
144         return true;
145 }
146
147
148 bool GuiErrorList::goTo(int item)
149 {
150         if (&buffer() != buf_) {
151                 if (!theBufferList().isLoaded(buf_))
152                         return false;
153                 FuncRequest fr(LFUN_BUFFER_SWITCH, buf_->absFileName());
154                 dispatch(fr);
155         }
156         ErrorItem const & err = errorList()[item];
157
158         if (err.par_id == -1)
159                 return false;
160
161         if (from_master_)
162                 // FIXME: implement
163                 return false;
164
165         DocIterator dit = buf_->getParFromID(err.par_id);
166
167         if (dit == doc_iterator_end(buf_)) {
168         // FIXME: Happens when loading a read-only doc with 
169         // unknown layout. Should this be the case?
170                 LYXERR0("par id " << err.par_id << " not found");
171                 return false;
172         }
173
174         // Now make the selection.
175         // if pos_end is 0, this means it is end-of-paragraph
176         pos_type const s = dit.paragraph().size();
177         pos_type const end = err.pos_end ? min(err.pos_end, s) : s;
178         pos_type const start = min(err.pos_start, end);
179         pos_type const range = end - start;
180         dit.pos() = start;
181         BufferView * bv = const_cast<BufferView *>(bufferview());
182         // FIXME: If we used an LFUN, we would not need this line:
183         bv->putSelectionAt(dit, range, false);
184         bv->processUpdateFlags(Update::Force | Update::FitCursor);
185         return true;
186 }
187
188
189 Dialog * createGuiErrorList(GuiView & lv) { return new GuiErrorList(lv); }
190
191 } // namespace frontend
192 } // namespace lyx
193
194
195 #include "moc_GuiErrorList.cpp"