]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitationDialog.C
960363b5afba8bd25e6072918ea64a0cc9ad1fb9
[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 key to browser_cite
136         selectedLB->insertItem(toqstr(form_->bibkeys[sel]));
137         form_->citekeys.push_back(form_->bibkeys[sel]);
138
139         int const n = int(form_->citekeys.size());
140         selectedLB->setSelected(n - 1, true);
141
142         form_->changed();
143         form_->fillStyles();
144         setButtons();
145 }
146
147
148 void QCitationDialog::del()
149 {
150         int const sel = selectedLB->currentItem();
151
152         // Remove the selected key from browser_cite
153         selectedLB->removeItem(sel);
154         form_->citekeys.erase(form_->citekeys.begin() + sel);
155
156         form_->changed();
157         form_->fillStyles();
158         setButtons();
159 }
160
161
162 void QCitationDialog::up()
163 {
164         int const sel = selectedLB->currentItem();
165
166         // Move the selected key up one line
167         vector<string>::iterator it = form_->citekeys.begin() + sel;
168         string const tmp = *it;
169
170         selectedLB->removeItem(sel);
171         form_->citekeys.erase(it);
172
173         selectedLB->insertItem(toqstr(tmp), sel - 1);
174         selectedLB->setSelected(sel - 1, true);
175         form_->citekeys.insert(it - 1, tmp);
176
177         form_->changed();
178         form_->fillStyles();
179         setButtons();
180 }
181
182
183 void QCitationDialog::down()
184 {
185         int const sel = selectedLB->currentItem();
186
187         // Move the selected key down 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::add()
205 {
206         add_->exec();
207 }
208
209
210 void QCitationDialog::changed_adaptor()
211 {
212         form_->changed();
213 }
214
215
216 void QCitationDialog::find(biblio::Direction dir)
217 {
218         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
219
220         biblio::Search const type = add_->searchTypeCB->isChecked()
221                 ? biblio::REGEX : biblio::SIMPLE;
222
223         vector<string>::const_iterator start = form_->bibkeys.begin();
224         int const sel = add_->availableLB->currentItem();
225         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
226                 start += sel;
227
228         // Find the NEXT instance...
229         if (dir == biblio::FORWARD)
230                 start += 1;
231         else
232                 start -= 1;
233
234         bool const casesens = add_->searchCaseCB->isChecked();
235         string const str = fromqstr(add_->searchED->text());
236
237         vector<string>::const_iterator cit =
238                 biblio::searchKeys(theMap, form_->bibkeys, str,
239                                    start, type, dir, casesens);
240
241         // not found. let's loop round
242         if (cit == form_->bibkeys.end()) {
243                 if (dir == biblio::FORWARD) {
244                         start = form_->bibkeys.begin();
245                 }
246                 else start = form_->bibkeys.end() - 1;
247
248                 cit = biblio::searchKeys(theMap, form_->bibkeys, str,
249                                          start, type, dir, casesens);
250
251                 if (cit == form_->bibkeys.end())
252                         return;
253         }
254
255         int const found = int(cit - form_->bibkeys.begin());
256         if (found == sel) {
257                 return;
258         }
259
260         // Update the display
261         add_->availableLB->setSelected(found, true);
262         add_->availableLB->ensureCurrentVisible();
263 }