]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitationDialog.C
07cc5ae193eb84de8d3a706cb709cbfc8cff8dd2
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <algorithm>
18
19 #include "qt_helpers.h"
20 #include "controllers/ControlCitation.h"
21 #include "LyXView.h"
22 #include "buffer.h"
23
24 #include <qcheckbox.h>
25 #include <qcombobox.h>
26 #include <qlineedit.h>
27 #include <qlistbox.h>
28 #include <qmultilineedit.h>
29 #include <qpushbutton.h>
30
31 #include "QCitationDialog.h"
32 #include "QCitation.h"
33 #include "support/lstrings.h"
34
35 using std::vector;
36 using std::find;
37 using std::max;
38
39
40 QCitationDialog::QCitationDialog(QCitation * form)
41         : QCitationDialogBase(0, 0, false, 0),
42         form_(form)
43 {
44         connect(restorePB, SIGNAL(clicked()),
45                 form, SLOT(slotRestore()));
46         connect(okPB, SIGNAL(clicked()),
47                 form, SLOT(slotOK()));
48         connect(applyPB, SIGNAL(clicked()),
49                 form, SLOT(slotApply()));
50         connect(closePB, SIGNAL(clicked()),
51                 form, SLOT(slotClose()));
52         connect(searchED, SIGNAL(returnPressed()),
53                 this, SLOT(slotNextClicked()));
54
55         textBeforeED->setText(qt_("Not yet supported"));
56         textBeforeED->setReadOnly(true);
57         textBeforeED->setFocusPolicy(QWidget::NoFocus);
58         citationStyleCO->setEnabled(false);
59         citationStyleCO->setFocusPolicy(QWidget::NoFocus);
60 }
61
62
63 QCitationDialog::~QCitationDialog()
64 {
65 }
66
67
68 void QCitationDialog::slotBibSelected(int sel)
69 {
70         slotBibHighlighted(sel);
71
72         if (form_->readOnly())
73                 return;
74
75         slotAddClicked();
76 }
77
78
79 void QCitationDialog::slotBibHighlighted(int sel)
80 {
81         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
82
83         citeLB->clearSelection();
84
85         // FIXME: why would this happen ?
86         if (sel < 0 || sel >= (int)form_->bibkeys.size()) {
87                 return;
88         }
89
90         // Put into browser_info the additional info associated with
91         // the selected browser_bib key
92         infoML->clear();
93
94         infoML->setText(toqstr(biblio::getInfo(theMap, form_->bibkeys[sel])));
95
96         // Highlight the selected browser_bib key in browser_cite if
97         // present
98         vector<string>::const_iterator cit =
99                 std::find(form_->citekeys.begin(), form_->citekeys.end(),
100                           form_->bibkeys[sel]);
101
102         if (cit != form_->citekeys.end()) {
103                 int const n = int(cit - form_->citekeys.begin());
104                 citeLB->setSelected(n, true);
105                 citeLB->setTopItem(n);
106         }
107
108         if (!form_->readOnly()) {
109                 if (cit != form_->citekeys.end()) {
110                         form_->setBibButtons(QCitation::OFF);
111                         form_->setCiteButtons(QCitation::ON);
112                 } else {
113                         form_->setBibButtons(QCitation::ON);
114                         form_->setCiteButtons(QCitation::OFF);
115                 }
116         }
117 }
118
119
120 void QCitationDialog::slotCiteHighlighted(int sel)
121 {
122         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
123
124         // FIXME: why would this happen ?
125         if (sel < 0 || sel >= (int)form_->citekeys.size()) {
126                 return;
127         }
128
129         if (!form_->readOnly()) {
130                 form_->setBibButtons(QCitation::OFF);
131                 form_->setCiteButtons(QCitation::ON);
132         }
133
134         // Highlight the selected browser_cite key in browser_bib
135         vector<string>::const_iterator cit =
136                 std::find(form_->bibkeys.begin(),
137                 form_->bibkeys.end(), form_->citekeys[sel]);
138
139         if (cit != form_->bibkeys.end()) {
140                 int const n = int(cit - form_->bibkeys.begin());
141                 bibLB->setSelected(n, true);
142                 bibLB->setTopItem(n);
143
144                 // Put into browser_info the additional info associated
145                 // with the selected browser_cite key
146                 infoML->clear();
147                 infoML->setText(toqstr(biblio::getInfo(theMap, form_->citekeys[sel])));
148         }
149 }
150
151
152 void QCitationDialog::slotAddClicked()
153 {
154         int const sel = bibLB->currentItem();
155
156         // FIXME: why ?
157         if (sel < 0 || sel >= (int)form_->bibkeys.size()) {
158                 return;
159         }
160
161         // Add the selected browser_bib key to browser_cite
162         citeLB->insertItem(toqstr(form_->bibkeys[sel]));
163         form_->citekeys.push_back(form_->bibkeys[sel]);
164
165         int const n = int(form_->citekeys.size());
166         citeLB->setSelected(n - 1, true);
167
168         slotBibHighlighted(sel);
169         form_->setBibButtons(QCitation::OFF);
170         form_->setCiteButtons(QCitation::ON);
171         form_->changed();
172         form_->fillStyles();
173 }
174
175
176 void QCitationDialog::slotDelClicked()
177 {
178         int const sel = citeLB->currentItem();
179
180         // FIXME: why ?
181         if (sel < 0 || sel >= (int)form_->citekeys.size()) {
182                 return;
183         }
184
185         // Remove the selected key from browser_cite
186         citeLB->removeItem(sel);
187         form_->citekeys.erase(form_->citekeys.begin() + sel);
188
189         form_->setBibButtons(QCitation::ON);
190         form_->setCiteButtons(QCitation::OFF);
191         form_->changed();
192         form_->fillStyles();
193         form_->updateStyle();
194 }
195
196
197 void QCitationDialog::slotUpClicked()
198 {
199         int const sel = citeLB->currentItem();
200
201         // FIXME: why ?
202         if (sel < 1 || sel >= (int)form_->citekeys.size()) {
203                 return;
204         }
205
206         // Move the selected key up one line
207         vector<string>::iterator it = form_->citekeys.begin() + sel;
208         string const tmp = *it;
209
210         citeLB->removeItem(sel);
211         form_->citekeys.erase(it);
212
213         citeLB->insertItem(toqstr(tmp), sel - 1);
214         citeLB->setSelected(sel - 1, true);
215         form_->citekeys.insert(it - 1, tmp);
216         form_->setCiteButtons(QCitation::ON);
217         form_->changed();
218         form_->fillStyles();
219 }
220
221
222 void QCitationDialog::slotDownClicked()
223 {
224         int const sel = citeLB->currentItem();
225
226         // FIXME: ?
227         if (sel < 0 || sel >= (int)form_->citekeys.size() - 1) {
228                 return;
229         }
230
231         // Move the selected key down one line
232         vector<string>::iterator it = form_->citekeys.begin() + sel;
233         string const tmp = *it;
234
235         citeLB->removeItem(sel);
236         form_->citekeys.erase(it);
237
238         citeLB->insertItem(toqstr(tmp), sel + 1);
239         citeLB->setSelected(sel + 1, true);
240         form_->citekeys.insert(it + 1, tmp);
241         form_->setCiteButtons(QCitation::ON);
242         form_->changed();
243         form_->fillStyles();
244 }
245
246
247 void QCitationDialog::slotPreviousClicked()
248 {
249         doFind(biblio::BACKWARD);
250 }
251
252
253 void QCitationDialog::slotNextClicked()
254 {
255         doFind(biblio::FORWARD);
256 }
257
258
259 void QCitationDialog::changed_adaptor()
260 {
261         form_->changed();
262 }
263
264
265 void QCitationDialog::doFind(biblio::Direction dir)
266 {
267         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
268         string const str = fromqstr(searchED->text());
269
270         biblio::Search const type =
271                 searchTypeCB->isChecked() ?
272                 biblio::REGEX : biblio::SIMPLE;
273
274         vector<string>::const_iterator start = form_->bibkeys.begin();
275         int const sel = bibLB->currentItem();
276         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
277                 start += sel;
278
279         // Find the NEXT instance...
280         if (dir == biblio::FORWARD)
281                 start += 1;
282         else
283                 start -= 1;
284
285         bool const caseSensitive = searchCaseCB->isChecked();
286
287         vector<string>::const_iterator cit =
288                 biblio::searchKeys(theMap, form_->bibkeys, str,
289                                    start, type, dir, caseSensitive);
290
291         // not found. let's loop round
292         if (cit == form_->bibkeys.end()) {
293                 if (dir == biblio::FORWARD) {
294                         start = form_->bibkeys.begin();
295                 }
296                 else start = form_->bibkeys.end() - 1;
297
298                 cit = biblio::searchKeys(theMap, form_->bibkeys, str,
299                                          start, type, dir, caseSensitive);
300
301                 if (cit == form_->bibkeys.end())
302                         return;
303         }
304
305         int const found = int(cit - form_->bibkeys.begin());
306         if (found == sel) {
307                 return;
308         }
309
310         // Update the display
311         int const top = max(found - 5, 1);
312         bibLB->setTopItem(top);
313         bibLB->setSelected(found, true);
314         slotBibHighlighted(0);
315 }