]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitationDialog.C
get rid of broken_header.h and some unneeded tests
[lyx.git] / src / frontends / qt2 / QCitationDialog.C
1 /**
2  * \file QCitationDialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Kalle Dalheimer
7  * \author John Levon
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "QCitationDialog.h"
16 #include "ui/QCitationFindDialogBase.h"
17 #include "QCitation.h"
18 #include "qt_helpers.h"
19
20 #include "controllers/ControlCitation.h"
21 #include "controllers/ButtonController.h"
22
23 #include <qcheckbox.h>
24 #include <qlineedit.h>
25 #include <qlistbox.h>
26 #include <qmultilineedit.h>
27 #include <qpushbutton.h>
28
29
30 using std::vector;
31 using std::string;
32
33 namespace lyx {
34 namespace frontend {
35
36 QCitationDialog::QCitationDialog(QCitation * form)
37         : QCitationDialogBase(0, 0, false, 0),
38         form_(form)
39 {
40         connect(restorePB, SIGNAL(clicked()),
41                 form, SLOT(slotRestore()));
42         connect(okPB, SIGNAL(clicked()),
43                 form, SLOT(slotOK()));
44         connect(applyPB, SIGNAL(clicked()),
45                 form, SLOT(slotApply()));
46         connect(closePB, SIGNAL(clicked()),
47                 form, SLOT(slotClose()));
48
49         add_ = new QCitationFindDialogBase(this, "", true);
50         connect(add_->previousPB, SIGNAL(clicked()), this, SLOT(previous()));
51         connect(add_->nextPB, SIGNAL(clicked()), this, SLOT(next()));
52         connect(add_->availableLB, SIGNAL(currentChanged(QListBoxItem *)), this, SLOT(availableChanged()));
53         connect(add_->availableLB, SIGNAL(selected(QListBoxItem *)), this, SLOT(addCitation()));
54         connect(add_->availableLB, SIGNAL(selected(QListBoxItem *)), add_, SLOT(accept()));
55         connect(add_->addPB, SIGNAL(clicked()), this, SLOT(addCitation()));
56         connect(selectedLB, SIGNAL(returnPressed(QListBoxItem *)), form, SLOT(slotOK()));
57 }
58
59
60 QCitationDialog::~QCitationDialog()
61 {
62 }
63
64
65 void QCitationDialog::setButtons()
66 {
67         if (form_->readOnly())
68                 return;
69
70         int const sel_nr = selectedLB->currentItem();
71         int const avail_nr = add_->availableLB->currentItem();
72
73         add_->addPB->setEnabled(avail_nr >= 0);
74         deletePB->setEnabled(sel_nr >= 0);
75         upPB->setEnabled(sel_nr > 0);
76         downPB->setEnabled(sel_nr >= 0 && sel_nr < int(selectedLB->count() - 1));
77 }
78
79
80 void QCitationDialog::openFind()
81 {
82         if (form_->readOnly())
83                 return;
84
85         if (selectedLB->count() == 0 && add_->availableLB->count() != 0){
86                 // open the find dialog
87                 add();
88                 // and let the user press ok after a selection
89                 if (selectedLB->count() != 0)
90                         form_->bc().valid();
91         }
92 }
93
94
95 void QCitationDialog::selectedChanged()
96 {
97         form_->fillStyles();
98         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
99         infoML->clear();
100
101         int const sel = selectedLB->currentItem();
102         if (sel < 0) {
103                 setButtons();
104                 return;
105         }
106
107         if (!theMap.empty())
108                 infoML->setText(
109                         toqstr(biblio::getInfo(theMap, form_->citekeys[sel])));
110         setButtons();
111 }
112
113
114 void QCitationDialog::previous()
115 {
116         find(biblio::BACKWARD);
117 }
118
119
120 void QCitationDialog::next()
121 {
122         find(biblio::FORWARD);
123 }
124
125
126 void QCitationDialog::availableChanged()
127 {
128         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
129         add_->infoML->clear();
130
131         int const sel = add_->availableLB->currentItem();
132         if (sel < 0) {
133                 setButtons();
134                 return;
135         }
136
137         if (!theMap.empty())
138                 add_->infoML->setText(
139                         toqstr(biblio::getInfo(theMap, form_->bibkeys[sel])));
140         setButtons();
141 }
142
143
144 void QCitationDialog::addCitation()
145 {
146         int const sel = add_->availableLB->currentItem();
147
148         if (sel < 0)
149                 return;
150
151         // Add the selected browser_bib keys to browser_cite
152         // multiple selections are possible
153         for (unsigned int i = 0; i != add_->availableLB->count(); i++) {
154                 if (add_->availableLB->isSelected(i)) {
155                         // do not allow duplicates
156                         if ((selectedLB->findItem(add_->availableLB->text(i))) == 0) {
157                                 selectedLB->insertItem(toqstr(form_->bibkeys[i]));
158                                 form_->citekeys.push_back(form_->bibkeys[i]);
159                         }
160                 }
161         }
162
163         int const n = int(form_->citekeys.size());
164         selectedLB->setSelected(n - 1, true);
165
166         form_->changed();
167         form_->fillStyles();
168         setButtons();
169 }
170
171
172 void QCitationDialog::del()
173 {
174         int const sel = selectedLB->currentItem();
175
176         // Remove the selected key from browser_cite
177         selectedLB->removeItem(sel);
178         form_->citekeys.erase(form_->citekeys.begin() + sel);
179
180         form_->changed();
181         form_->fillStyles();
182         setButtons();
183 }
184
185
186 void QCitationDialog::up()
187 {
188         int const sel = selectedLB->currentItem();
189
190         // Move the selected key up one line
191         vector<string>::iterator it = form_->citekeys.begin() + sel;
192         string const tmp = *it;
193
194         selectedLB->removeItem(sel);
195         form_->citekeys.erase(it);
196
197         selectedLB->insertItem(toqstr(tmp), sel - 1);
198         selectedLB->setSelected(sel - 1, true);
199         form_->citekeys.insert(it - 1, tmp);
200
201         form_->changed();
202         form_->fillStyles();
203         setButtons();
204 }
205
206
207 void QCitationDialog::down()
208 {
209         int const sel = selectedLB->currentItem();
210
211         // Move the selected key down one line
212         vector<string>::iterator it = form_->citekeys.begin() + sel;
213         string const tmp = *it;
214
215         selectedLB->removeItem(sel);
216         form_->citekeys.erase(it);
217
218         selectedLB->insertItem(toqstr(tmp), sel + 1);
219         selectedLB->setSelected(sel + 1, true);
220         form_->citekeys.insert(it + 1, tmp);
221
222         form_->changed();
223         form_->fillStyles();
224         setButtons();
225 }
226
227
228 void QCitationDialog::add()
229 {
230         add_->exec();
231 }
232
233
234 void QCitationDialog::changed_adaptor()
235 {
236         form_->changed();
237 }
238
239
240 void QCitationDialog::find(biblio::Direction dir)
241 {
242         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
243
244         biblio::Search const type = add_->searchTypeCB->isChecked()
245                 ? biblio::REGEX : biblio::SIMPLE;
246
247         vector<string>::const_iterator start = form_->bibkeys.begin();
248         int const sel = add_->availableLB->currentItem();
249         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
250                 start += sel;
251
252         // Find the NEXT instance...
253         if (dir == biblio::FORWARD)
254                 start += 1;
255         else
256                 start -= 1;
257
258         bool const casesens = add_->searchCaseCB->isChecked();
259         string const str = fromqstr(add_->searchED->text());
260
261         vector<string>::const_iterator cit =
262                 biblio::searchKeys(theMap, form_->bibkeys, str,
263                                    start, type, dir, casesens);
264
265         // not found. let's loop round
266         if (cit == form_->bibkeys.end()) {
267                 if (dir == biblio::FORWARD) {
268                         start = form_->bibkeys.begin();
269                 }
270                 else start = form_->bibkeys.end() - 1;
271
272                 cit = biblio::searchKeys(theMap, form_->bibkeys, str,
273                                          start, type, dir, casesens);
274
275                 if (cit == form_->bibkeys.end())
276                         return;
277         }
278
279         int const found = int(cit - form_->bibkeys.begin());
280         if (found == sel) {
281                 return;
282         }
283
284         // Update the display
285         add_->availableLB->setSelected(found, true);
286         add_->availableLB->ensureCurrentVisible();
287 }
288
289 } // namespace frontend
290 } // namespace lyx