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