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