]> git.lyx.org Git - features.git/blob - src/frontends/kde/FormCitation.C
renaming, fix citation (still no search though)
[features.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  * \author Angus Leeming 
8  */
9
10 #include <config.h>
11  
12 #include <algorithm>
13
14 #include "FormCitation.h"
15 #include "ControlCitation.h"
16 #include "citationdlg.h"
17 #include "gettext.h"
18 #include "biblio.h" 
19 #include "support/lstrings.h"
20 #include "helper_funcs.h" 
21
22 using std::vector;
23 using std::pair;
24 using std::find;
25
26 FormCitation::FormCitation(ControlCitation & c)
27         : KFormBase<ControlCitation, CitationDialog>(c),
28           keys(0), chosenkeys(0)
29 {
30 }
31
32  
33 void FormCitation::apply()
34 {
35         controller().params().setCmdName("cite");
36         controller().params().setContents(getStringFromVector(chosenkeys));
37         controller().params().setOptions(dialog_->line_after->text());
38 }
39
40
41 void FormCitation::hide()
42 {
43         chosenkeys.clear();
44         selectedKey.erase();
45         selectedChosenKey.erase();
46
47         KFormBase<ControlCitation, CitationDialog>::hide();
48 }
49
50
51 void FormCitation::build()
52 {
53         dialog_.reset(new CitationDialog(this, 0, _("Citation")));
54
55         // Manage the ok, apply, restore and cancel/close buttons
56         bc().setOK(dialog_->button_ok);
57         bc().setApply(dialog_->button_apply);
58         bc().setRestore(dialog_->button_restore);
59         bc().setCancel(dialog_->button_cancel);
60
61         bc().addReadOnly(dialog_->list_available);
62         bc().addReadOnly(dialog_->list_chosen);
63         bc().addReadOnly(dialog_->line_after); 
64         bc().addReadOnly(dialog_->button_add);
65         bc().addReadOnly(dialog_->button_remove);
66         bc().addReadOnly(dialog_->button_up);
67         bc().addReadOnly(dialog_->button_down);
68         //bc().addReadOnly(dialog_->style);
69 }
70
71
72 void FormCitation::update()
73 {
74         keys = biblio::getKeys(controller().bibkeysInfo());
75
76         updateAvailableList();
77         selectedKey.erase();
78
79         chosenkeys = getVectorFromString(controller().params().getContents());
80         updateChosenList();
81         selectedChosenKey.erase();
82
83         dialog_->line_details->setText("");
84         dialog_->line_after->setText(controller().params().getOptions().c_str());
85
86         if (!controller().isReadonly()) 
87                 updateButtons();
88 }
89
90
91 void FormCitation::updateButtons()
92 {
93         bool ischosenkey = !selectedChosenKey.empty();
94
95         vector<string>::const_iterator iter =
96                 find(chosenkeys.begin(), chosenkeys.end(), selectedKey);
97
98         dialog_->button_add->setEnabled(!selectedKey.empty() && iter == chosenkeys.end());
99         dialog_->button_remove->setEnabled(ischosenkey);
100         dialog_->button_up->setEnabled(ischosenkey);
101         dialog_->button_down->setEnabled(ischosenkey);
102 }
103
104
105 void FormCitation::updateChosenList()
106 {
107         updateList(dialog_->list_chosen, chosenkeys);
108 }
109
110
111 void FormCitation::updateAvailableList()
112 {
113         updateList(dialog_->list_available, keys);
114 }
115
116
117 void FormCitation::updateList(QListBox * lb, vector<string> const & keys)
118 {
119         lb->setAutoUpdate(false);
120         lb->clear();
121
122         for (vector<string>::const_iterator iter = keys.begin();
123                 iter != keys.end(); ++iter) {
124                 if (!iter->empty()) 
125                         lb->insertItem(iter->c_str());
126         }
127         lb->setAutoUpdate(true);
128         lb->update();
129 }
130
131
132 void FormCitation::selectChosen()
133 {
134         for (unsigned int i=0; i < dialog_->list_chosen->count(); ++i) {
135                 if (dialog_->list_chosen->text(i)==selectedChosenKey) {
136                         dialog_->list_chosen->setSelected(i,true);
137                         dialog_->list_chosen->setTopItem(i);
138                         break;
139                 }
140         }
141 }
142
143
144 ButtonPolicy::SMInput FormCitation::add()
145 {
146         if (selectedKey.empty())
147                 return ButtonPolicy::SMI_NOOP;
148
149         for (vector<string>::const_iterator iter = keys.begin();
150                 iter != keys.end(); ++iter) {
151                 if (*iter == selectedKey) {
152                         chosenkeys.push_back(*iter);
153                         break;
154                 }
155         }
156                 
157         selectedChosenKey.erase();
158         updateChosenList();
159         updateButtons();
160         return ButtonPolicy::SMI_VALID;
161 }
162
163
164 ButtonPolicy::SMInput FormCitation::remove()
165 {
166         if (selectedChosenKey.empty())
167                 return ButtonPolicy::SMI_NOOP;
168
169         for (vector< string >::iterator iter = chosenkeys.begin();
170                 iter != chosenkeys.end(); ++iter) {
171                 if (*iter==selectedChosenKey) {
172                         chosenkeys.erase(iter);
173                         break;
174                 }
175         }
176         selectedChosenKey.erase();
177         updateChosenList();
178         updateButtons();
179         return ButtonPolicy::SMI_VALID;
180 }
181
182
183 ButtonPolicy::SMInput FormCitation::up()
184 {
185         if (selectedChosenKey.empty())
186                 return ButtonPolicy::SMI_NOOP;
187
188         // Qt will select the first one on redo, so we need this
189         string tmp = selectedChosenKey;
190
191         vector< string >::iterator iter = chosenkeys.begin();
192
193         for (; iter != chosenkeys.end(); ++iter) {
194                 if (*iter==selectedChosenKey && iter!=chosenkeys.begin()) {
195                         string tmp = *iter;
196                         chosenkeys.erase(iter);
197                         chosenkeys.insert(iter-1,tmp);
198                         break;
199                 }
200         }
201         if (iter==chosenkeys.end())
202                 return ButtonPolicy::SMI_NOOP;
203
204         updateChosenList();
205         selectedChosenKey=tmp;
206         selectChosen();
207         return ButtonPolicy::SMI_VALID;
208 }
209
210  
211 ButtonPolicy::SMInput FormCitation::down()
212 {
213         if (selectedChosenKey.empty())
214                 return ButtonPolicy::SMI_NOOP;
215
216         // Qt will select the first one on redo, so we need this
217         string tmp = selectedChosenKey;
218
219         vector< string >::iterator iter = chosenkeys.begin();
220
221         for (; iter != chosenkeys.end(); ++iter) {
222                 if (*iter == selectedChosenKey && (iter+1)!=chosenkeys.end()) {
223                         string tmp = *iter;
224                         chosenkeys.erase(iter);
225                         chosenkeys.insert(iter+1, tmp);
226                         break;
227                 }
228         }
229         if (iter == chosenkeys.end())
230                 return ButtonPolicy::SMI_NOOP;
231
232         updateChosenList();
233         selectedChosenKey=tmp;
234         selectChosen();
235         return ButtonPolicy::SMI_VALID; 
236 }
237
238  
239 ButtonPolicy::SMInput FormCitation::select_key(char const * key)
240 {
241         if (controller().isReadonly())
242                 return ButtonPolicy::SMI_INVALID;
243
244         vector<string>::const_iterator iter =
245                 find(chosenkeys.begin(), chosenkeys.end(), key);
246
247         if (iter != chosenkeys.end())
248                 return ButtonPolicy::SMI_NOOP;
249
250         selectedKey.erase();
251         selectedKey = key;
252
253         add();
254         return ButtonPolicy::SMI_VALID; 
255 }
256
257  
258 void FormCitation::highlight_key(char const * key)
259 {
260         highlight(key, dialog_->list_chosen, selectedKey, selectedChosenKey);
261 }
262
263  
264 void FormCitation::highlight_chosen(char const * key)
265 {
266         highlight(key, dialog_->list_available, selectedChosenKey, selectedKey);
267 }
268
269
270 void FormCitation::highlight(char const * key, QListBox * lb,
271                              string & selected1, string & selected2)
272 {
273         selected1.erase();
274         selected1 = key;
275
276         unsigned int i;
277
278         for (i=0; i < keys.size(); ++i) {
279                 if (keys[i] == key) {
280                         string const tmp = biblio::getInfo(controller().bibkeysInfo(), key);
281                         dialog_->line_details->setText(tmp.c_str());
282                         lb->clearFocus();
283                         lb->clearSelection();
284                         selected2.erase();
285                         break;
286                 }
287         }
288
289         if (i == keys.size())
290                 dialog_->line_details->setText(_("Key not found."));
291
292         updateButtons();
293 }