3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
7 * \author Angus Leeming
14 #include "FormCitation.h"
15 #include "ControlCitation.h"
16 #include "citationdlg.h"
19 #include "support/lstrings.h"
20 #include "helper_funcs.h"
26 FormCitation::FormCitation(ControlCitation & c)
27 : KFormBase<ControlCitation, CitationDialog>(c),
28 keys(0), chosenkeys(0)
33 void FormCitation::apply()
35 controller().params().setCmdName("cite");
36 controller().params().setContents(getStringFromVector(chosenkeys));
37 controller().params().setOptions(dialog_->line_after->text());
41 void FormCitation::hide()
45 selectedChosenKey.erase();
47 KFormBase<ControlCitation, CitationDialog>::hide();
51 void FormCitation::build()
53 dialog_.reset(new CitationDialog(this, 0, _("Citation")));
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);
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);
72 void FormCitation::update()
74 keys = biblio::getKeys(controller().bibkeysInfo());
76 updateAvailableList();
79 chosenkeys = getVectorFromString(controller().params().getContents());
81 selectedChosenKey.erase();
83 dialog_->line_details->setText("");
84 dialog_->line_after->setText(controller().params().getOptions().c_str());
86 if (!controller().isReadonly())
91 void FormCitation::updateButtons()
93 bool ischosenkey = !selectedChosenKey.empty();
95 vector<string>::const_iterator iter =
96 find(chosenkeys.begin(), chosenkeys.end(), selectedKey);
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);
105 void FormCitation::updateChosenList()
107 updateList(dialog_->list_chosen, chosenkeys);
111 void FormCitation::updateAvailableList()
113 updateList(dialog_->list_available, keys);
117 void FormCitation::updateList(QListBox * lb, vector<string> const & keys)
119 lb->setAutoUpdate(false);
122 for (vector<string>::const_iterator iter = keys.begin();
123 iter != keys.end(); ++iter) {
125 lb->insertItem(iter->c_str());
127 lb->setAutoUpdate(true);
132 void FormCitation::selectChosen()
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);
144 ButtonPolicy::SMInput FormCitation::add()
146 if (selectedKey.empty())
147 return ButtonPolicy::SMI_NOOP;
149 for (vector<string>::const_iterator iter = keys.begin();
150 iter != keys.end(); ++iter) {
151 if (*iter == selectedKey) {
152 chosenkeys.push_back(*iter);
157 selectedChosenKey.erase();
160 return ButtonPolicy::SMI_VALID;
164 ButtonPolicy::SMInput FormCitation::remove()
166 if (selectedChosenKey.empty())
167 return ButtonPolicy::SMI_NOOP;
169 for (vector< string >::iterator iter = chosenkeys.begin();
170 iter != chosenkeys.end(); ++iter) {
171 if (*iter==selectedChosenKey) {
172 chosenkeys.erase(iter);
176 selectedChosenKey.erase();
179 return ButtonPolicy::SMI_VALID;
183 ButtonPolicy::SMInput FormCitation::up()
185 if (selectedChosenKey.empty())
186 return ButtonPolicy::SMI_NOOP;
188 // Qt will select the first one on redo, so we need this
189 string tmp = selectedChosenKey;
191 vector< string >::iterator iter = chosenkeys.begin();
193 for (; iter != chosenkeys.end(); ++iter) {
194 if (*iter==selectedChosenKey && iter!=chosenkeys.begin()) {
196 chosenkeys.erase(iter);
197 chosenkeys.insert(iter-1,tmp);
201 if (iter==chosenkeys.end())
202 return ButtonPolicy::SMI_NOOP;
205 selectedChosenKey=tmp;
207 return ButtonPolicy::SMI_VALID;
211 ButtonPolicy::SMInput FormCitation::down()
213 if (selectedChosenKey.empty())
214 return ButtonPolicy::SMI_NOOP;
216 // Qt will select the first one on redo, so we need this
217 string tmp = selectedChosenKey;
219 vector< string >::iterator iter = chosenkeys.begin();
221 for (; iter != chosenkeys.end(); ++iter) {
222 if (*iter == selectedChosenKey && (iter+1)!=chosenkeys.end()) {
224 chosenkeys.erase(iter);
225 chosenkeys.insert(iter+1, tmp);
229 if (iter == chosenkeys.end())
230 return ButtonPolicy::SMI_NOOP;
233 selectedChosenKey=tmp;
235 return ButtonPolicy::SMI_VALID;
239 ButtonPolicy::SMInput FormCitation::select_key(char const * key)
241 if (controller().isReadonly())
242 return ButtonPolicy::SMI_INVALID;
244 vector<string>::const_iterator iter =
245 find(chosenkeys.begin(), chosenkeys.end(), key);
247 if (iter != chosenkeys.end())
248 return ButtonPolicy::SMI_NOOP;
254 return ButtonPolicy::SMI_VALID;
258 void FormCitation::highlight_key(char const * key)
260 highlight(key, dialog_->list_chosen, selectedKey, selectedChosenKey);
264 void FormCitation::highlight_chosen(char const * key)
266 highlight(key, dialog_->list_available, selectedChosenKey, selectedKey);
270 void FormCitation::highlight(char const * key, QListBox * lb,
271 string & selected1, string & selected2)
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());
283 lb->clearSelection();
289 if (i == keys.size())
290 dialog_->line_details->setText(_("Key not found."));