]> git.lyx.org Git - lyx.git/blob - src/frontends/kde/FormCitation.C
small cleanup, doxygen, formatting changes
[lyx.git] / src / frontends / kde / FormCitation.C
1 /**
2  * \file FormCitation.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon
7  */
8
9 #include <config.h>
10
11 #include <algorithm>
12
13 #include "Dialogs.h"
14 #include "FormCitation.h"
15 #include "gettext.h"
16 #include "buffer.h"
17 #include "LyXView.h"
18 #include "lyxfunc.h"
19 #include "citationdlg.h"
20
21 using std::vector;
22 using std::pair;
23 using std::find;
24
25 FormCitation::FormCitation(LyXView *v, Dialogs *d)
26         : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
27         keys(0), chosenkeys(0)
28 {
29         d->showCitation.connect(slot(this, &FormCitation::showCitation));
30         d->createCitation.connect(slot(this, &FormCitation::createCitation));
31 }
32
33  
34 FormCitation::~FormCitation()
35 {
36         delete dialog_;
37 }
38
39  
40 void FormCitation::showCitation(InsetCommand * const inset)
41 {
42         // FIXME: when could inset be 0 here ?
43         if (inset==0)
44                 return;
45
46         inset_ = inset;
47         readonly = lv_->buffer()->isReadonly();
48         ih_ = inset_->hide.connect(slot(this,&FormCitation::hide));
49         params = inset->params();
50         
51         show();
52 }
53
54
55 void FormCitation::createCitation(string const & arg)
56 {
57         // we could already be showing stuff, clear it out
58         if (inset_)
59                 close();
60
61         readonly = lv_->buffer()->isReadonly();
62         params.setFromString(arg);
63         show();
64 }
65
66
67 void FormCitation::updateButtons()
68 {
69         if (readonly) {
70                 dialog_->add->setEnabled(false);
71                 dialog_->remove->setEnabled(false);
72                 dialog_->up->setEnabled(false);
73                 dialog_->down->setEnabled(false);
74                 return;
75         }
76
77         bool ischosenkey = !selectedChosenKey.empty();
78
79         vector<string>::const_iterator iter =
80                 find(chosenkeys.begin(), chosenkeys.end(), selectedKey);
81
82         dialog_->add->setEnabled(!selectedKey.empty() && iter == chosenkeys.end());
83         dialog_->remove->setEnabled(ischosenkey);
84         dialog_->up->setEnabled(ischosenkey);
85         dialog_->down->setEnabled(ischosenkey);
86 }
87
88
89 void FormCitation::updateChosenList()
90 {
91         dialog_->chosen->setAutoUpdate(false);
92         dialog_->chosen->clear();
93
94         for (vector< string >::const_iterator iter = chosenkeys.begin();
95                 iter != chosenkeys.end(); ++iter) {
96                 dialog_->chosen->insertItem(iter->c_str());
97         }
98         dialog_->chosen->setAutoUpdate(true);
99         dialog_->chosen->update();
100 }
101
102
103 void FormCitation::updateAvailableList()
104 {
105         dialog_->keys->setAutoUpdate(false);
106         dialog_->keys->clear();
107
108         for (vector< pair<string,string> >::const_iterator iter = keys.begin();
109                 iter != keys.end(); ++iter) {
110                 dialog_->keys->insertItem(iter->first.c_str());
111         }
112         dialog_->keys->setAutoUpdate(true);
113         dialog_->keys->update();
114 }
115
116
117 // we can safely ignore the parameter because we can always update
118 void FormCitation::update(bool)
119 {
120         keys.clear();
121
122         vector < pair<string,string> > const ckeys = lv_->buffer()->getBibkeyList();
123
124         for (vector< pair<string,string> >::const_iterator iter = ckeys.begin();
125                 iter != ckeys.end(); ++iter) {
126                 keys.push_back(*iter);
127         }
128
129         updateAvailableList();
130         selectedKey.erase();
131
132         chosenkeys.clear();
133
134         string tmp, paramkeys(params.getContents());
135         paramkeys = frontStrip(split(paramkeys, tmp, ','));
136
137         while (!tmp.empty()) {
138                 chosenkeys.push_back(tmp);
139                 paramkeys = frontStrip(split(paramkeys, tmp, ','));
140         }
141
142         updateChosenList();
143         selectedChosenKey.erase();
144
145         dialog_->entry->setText("");
146
147         dialog_->after->setText(params.getOptions().c_str());
148
149         updateButtons();
150
151         if (readonly) {
152                 dialog_->keys->setFocusPolicy(QWidget::NoFocus);
153                 dialog_->chosen->setFocusPolicy(QWidget::NoFocus);
154                 dialog_->after->setFocusPolicy(QWidget::NoFocus);
155                 dialog_->buttonOk->setEnabled(false);
156                 dialog_->buttonCancel->setText(_("&Close"));
157         } else {
158                 dialog_->keys->setFocusPolicy(QWidget::StrongFocus);
159                 dialog_->chosen->setFocusPolicy(QWidget::StrongFocus);
160                 dialog_->after->setFocusPolicy(QWidget::StrongFocus);
161                 dialog_->buttonOk->setEnabled(true);
162                 dialog_->buttonCancel->setText(_("&Cancel"));
163         }
164 }
165
166 void FormCitation::apply()
167 {
168         if (readonly)
169                 return;
170
171         string contents;
172
173         for (vector< string >::const_iterator iter = chosenkeys.begin();
174                 iter != chosenkeys.end(); ++iter) {
175                 if (iter != chosenkeys.begin())
176                         contents += ", ";
177                 contents += *iter;
178         }
179                 
180         params.setContents(contents);
181         params.setOptions(dialog_->after->text());
182         
183         if (inset_ != 0) {
184                 if (params != inset_->params()) {
185                         inset_->setParams(params);
186                         lv_->view()->updateInset(inset_, true);
187                 }
188         } else
189                 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT, params.getAsString().c_str());
190 }
191
192 void FormCitation::show()
193 {
194         if (!dialog_)
195                 dialog_ = new CitationDialog(this, 0, _("LyX: Citation Reference"), false);
196
197         if (!dialog_->isVisible()) {
198                 h_ = d_->hideBufferDependent.connect(slot(this, &FormCitation::hide));
199                 u_ = d_->updateBufferDependent.connect(slot(this, &FormCitation::update));
200         }
201
202         dialog_->raise();
203         dialog_->setActiveWindow();
204
205         update();
206         dialog_->show();
207 }
208
209 void FormCitation::close()
210 {
211         h_.disconnect();
212         u_.disconnect();
213         ih_.disconnect();
214         inset_ = 0;
215 }
216
217 void FormCitation::hide()
218 {
219         dialog_->hide();
220         close();
221 }
222
223 void FormCitation::selectChosen()
224 {
225         for (unsigned int i=0; i < dialog_->chosen->count(); ++i) {
226                 if (dialog_->chosen->text(i)==selectedChosenKey) {
227                         dialog_->chosen->setSelected(i,true);
228                         dialog_->chosen->setTopItem(i);
229                         break;
230                 }
231         }
232 }
233
234 void FormCitation::add()
235 {
236         if (selectedKey.empty())
237                 return;
238
239         for (vector< pair<string,string> >::const_iterator iter = keys.begin();
240                 iter != keys.end(); ++iter) {
241                 if (iter->first == selectedKey) {
242                         chosenkeys.push_back(iter->first);
243                         break;
244                 }
245         }
246                 
247         selectedChosenKey.erase();
248         updateChosenList();
249         updateButtons();
250 }
251
252 void FormCitation::remove()
253 {
254         if (selectedChosenKey.empty())
255                 return;
256
257         for (vector< string >::iterator iter = chosenkeys.begin();
258                 iter != chosenkeys.end(); ++iter) {
259                 if (*iter==selectedChosenKey) {
260                         chosenkeys.erase(iter);
261                         break;
262                 }
263         }
264         selectedChosenKey.erase();
265         updateChosenList();
266         updateButtons();
267 }
268
269 void FormCitation::up()
270 {
271         if (selectedChosenKey.empty())
272                 return;
273
274         // Qt will select the first one on redo, so we need this
275         string tmp = selectedChosenKey;
276
277         vector< string >::iterator iter = chosenkeys.begin();
278
279         for (; iter != chosenkeys.end(); ++iter) {
280                 if (*iter==selectedChosenKey && iter!=chosenkeys.begin()) {
281                         string tmp = *iter;
282                         chosenkeys.erase(iter);
283                         chosenkeys.insert(iter-1,tmp);
284                         break;
285                 }
286         }
287         if (iter==chosenkeys.end())
288                 return;
289
290         updateChosenList();
291         selectedChosenKey=tmp;
292         selectChosen();
293 }
294
295  
296 void FormCitation::down()
297 {
298         if (selectedChosenKey.empty())
299                 return;
300
301         // Qt will select the first one on redo, so we need this
302         string tmp = selectedChosenKey;
303
304         vector< string >::iterator iter = chosenkeys.begin();
305
306         for (; iter != chosenkeys.end(); ++iter) {
307                 if (*iter == selectedChosenKey && (iter+1)!=chosenkeys.end()) {
308                         string tmp = *iter;
309                         chosenkeys.erase(iter);
310                         chosenkeys.insert(iter+1, tmp);
311                         break;
312                 }
313         }
314         if (iter == chosenkeys.end())
315                 return;
316
317         updateChosenList();
318         selectedChosenKey=tmp;
319         selectChosen();
320 }
321
322  
323 void FormCitation::select_key(char const * key)
324 {
325         if (readonly)
326                 return;
327
328         vector<string>::const_iterator iter =
329                 find(chosenkeys.begin(), chosenkeys.end(), key);
330
331         if (iter != chosenkeys.end())
332                 return;
333
334         selectedKey.erase();
335         selectedKey = key;
336
337         add();
338 }
339
340  
341 void FormCitation::highlight_key(char const * key)
342 {
343         if (readonly)
344                 return;
345
346         selectedKey.erase();
347         selectedKey = key;
348
349         for (unsigned int i=0; i < keys.size(); i++) {
350                 if (keys[i].first == key) {
351                         dialog_->entry->setText(keys[i].second.c_str());
352                         dialog_->chosen->clearFocus();
353                         dialog_->chosen->clearSelection();
354                         selectedChosenKey.erase();
355                         break;
356                 }
357         }
358
359         updateButtons();
360 }
361
362  
363 void FormCitation::highlight_chosen(char const * key)
364 {
365         selectedChosenKey.erase();
366         selectedChosenKey = key;
367
368         unsigned int i;
369
370         for (i=0; i < keys.size(); i++) {
371                 if (keys[i].first == key) {
372                         if (keys[i].second.compare(dialog_->entry->text()))
373                                 dialog_->entry->setText(keys[i].second.c_str());
374                         dialog_->keys->clearFocus();
375                         dialog_->keys->clearSelection();
376                         selectedKey.erase();
377                         break;
378                 }
379         }
380
381         if (i == keys.size())
382                 dialog_->entry->setText(_("Key not found in references."));
383
384         updateButtons();
385 }