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