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