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