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