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