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