]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitationDialog.C
55f333c7e16e3a313c37f37d78d70a28f7972249
[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_->bibkeys[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         form_->setBibButtons(QCitation::OFF);
160         form_->setCiteButtons(QCitation::ON);
161         form_->changed();
162 }
163
164
165 void QCitationDialog::slotDelClicked()
166 {
167         int const sel = citeLB->currentItem();
168
169         // FIXME: why ?
170         if (sel < 0 || sel >= (int)form_->citekeys.size()) {
171                 return;
172         }
173
174         // Remove the selected key from browser_cite
175         citeLB->removeItem(sel);
176         form_->citekeys.erase(form_->citekeys.begin() + sel);
177
178         form_->setBibButtons(QCitation::ON);
179         form_->setCiteButtons(QCitation::OFF);
180         form_->changed();
181 }
182
183
184 void QCitationDialog::slotUpClicked()
185 {
186         int const sel = citeLB->currentItem();
187
188         // FIXME: why ?
189         if (sel < 1 || sel >= (int)form_->citekeys.size()) {
190                 return;
191         }
192
193         // Move the selected key up one line
194         vector<string>::iterator it = form_->citekeys.begin() + sel;
195         string const tmp = *it;
196
197         citeLB->removeItem(sel);
198         form_->citekeys.erase(it);
199
200         citeLB->insertItem(tmp.c_str(), sel - 1);
201         citeLB->setSelected(sel - 1, true);
202         form_->citekeys.insert(it - 1, tmp);
203         form_->setCiteButtons(QCitation::ON);
204         form_->changed();
205 }
206
207
208 void QCitationDialog::slotDownClicked()
209 {
210         int const sel = citeLB->currentItem();
211
212         // FIXME: ?
213         if (sel < 0 || sel >= (int)form_->citekeys.size() - 1) {
214                 return;
215         }
216
217         // Move the selected key down one line
218         vector<string>::iterator it = form_->citekeys.begin() + sel;
219         string const tmp = *it;
220
221         citeLB->removeItem(sel);
222         form_->citekeys.erase(it);
223
224         citeLB->insertItem(tmp.c_str(), sel + 1);
225         citeLB->setSelected(sel + 1, true);
226         form_->citekeys.insert(it + 1, tmp);
227         form_->setCiteButtons(QCitation::ON);
228         form_->changed();
229 }
230
231
232 void QCitationDialog::slotPreviousClicked()
233 {
234         doFind(biblio::BACKWARD);
235 }
236
237
238 void QCitationDialog::slotNextClicked()
239 {
240         doFind(biblio::FORWARD);
241 }
242
243
244 void QCitationDialog::doFind(biblio::Direction dir)
245 {
246         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
247         string const str = searchED->text().latin1();
248
249         biblio::Search const type =
250                 searchTypeCB->isChecked() ?
251                 biblio::REGEX : biblio::SIMPLE;
252
253         vector<string>::const_iterator start = form_->bibkeys.begin();
254         int const sel = bibLB->currentItem();
255         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
256                 start += sel;
257
258         // Find the NEXT instance...
259         if (dir == biblio::FORWARD)
260                 start += 1;
261         else
262                 start -= 1;
263
264         bool const caseSensitive = searchCaseCB->isChecked();
265
266         vector<string>::const_iterator cit =
267                 biblio::searchKeys(theMap, form_->bibkeys, str,
268                         start, type, dir, caseSensitive);
269
270         // FIXME: should work ...
271         if (cit == form_->bibkeys.end()) {
272                 // not found. let's loop round
273                 if (dir == biblio::FORWARD)
274                         start = form_->bibkeys.begin();
275                 else
276                         start = form_->bibkeys.end();
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 }