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"
18 #include "support/lstrings.h"
19 #include "helper_funcs.h"
25 FormCitation::FormCitation(ControlCitation & c)
26 : KFormBase<ControlCitation, CitationDialog>(c),
27 keys(0), chosenkeys(0)
32 void FormCitation::apply()
34 controller().params().setCmdName("cite");
35 controller().params().setContents(getStringFromVector(chosenkeys));
36 controller().params().setOptions(dialog_->after->text());
40 void FormCitation::hide()
44 selectedChosenKey.erase();
46 KFormBase<ControlCitation, CitationDialog>::hide();
50 void FormCitation::build()
52 dialog_.reset(new CitationDialog(this, 0, "Citation", false));
54 // FIXME: apply, restore buttons
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);
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);
75 void FormCitation::update()
78 keys = controller().getBibkeys();
79 updateAvailableList();
82 chosenkeys = getVectorFromString(controller().params().getContents());
84 selectedChosenKey.erase();
86 dialog_->entry->setText("");
87 dialog_->after->setText(controller().params().getOptions().c_str());
91 if (controller().isReadonly()) {
92 dialog_->keys->setFocusPolicy(QWidget::NoFocus);
93 dialog_->chosen->setFocusPolicy(QWidget::NoFocus);
94 dialog_->after->setFocusPolicy(QWidget::NoFocus);
96 dialog_->keys->setFocusPolicy(QWidget::StrongFocus);
97 dialog_->chosen->setFocusPolicy(QWidget::StrongFocus);
98 dialog_->after->setFocusPolicy(QWidget::StrongFocus);
104 void FormCitation::updateButtons()
106 if (controller().isReadonly()) {
107 dialog_->add->setEnabled(false);
108 dialog_->remove->setEnabled(false);
109 dialog_->up->setEnabled(false);
110 dialog_->down->setEnabled(false);
114 bool ischosenkey = !selectedChosenKey.empty();
116 vector<string>::const_iterator iter =
117 find(chosenkeys.begin(), chosenkeys.end(), selectedKey);
119 dialog_->add->setEnabled(!selectedKey.empty() && iter == chosenkeys.end());
120 dialog_->remove->setEnabled(ischosenkey);
121 dialog_->up->setEnabled(ischosenkey);
122 dialog_->down->setEnabled(ischosenkey);
126 void FormCitation::updateChosenList()
128 updateList(dialog_->chosen, chosenkeys);
132 void FormCitation::updateAvailableList()
134 updateList(dialog_->keys, keys);
138 void FormCitation::updateList(QListBox * lb, vector<string> const & keys)
140 lb->setAutoUpdate(false);
143 for (vector<string>::const_iterator iter = keys.begin();
144 iter != keys.end(); ++iter) {
145 lb->insertItem(iter->c_str());
147 lb->setAutoUpdate(true);
152 void FormCitation::selectChosen()
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);
164 ButtonPolicy::SMInput FormCitation::add()
166 if (selectedKey.empty())
167 return ButtonPolicy::SMI_NOOP;
169 for (vector<string>::const_iterator iter = keys.begin();
170 iter != keys.end(); ++iter) {
171 if (*iter == selectedKey) {
172 chosenkeys.push_back(*iter);
177 selectedChosenKey.erase();
180 return ButtonPolicy::SMI_VALID;
184 ButtonPolicy::SMInput FormCitation::remove()
186 if (selectedChosenKey.empty())
187 return ButtonPolicy::SMI_NOOP;
189 for (vector< string >::iterator iter = chosenkeys.begin();
190 iter != chosenkeys.end(); ++iter) {
191 if (*iter==selectedChosenKey) {
192 chosenkeys.erase(iter);
196 selectedChosenKey.erase();
199 return ButtonPolicy::SMI_VALID;
203 ButtonPolicy::SMInput FormCitation::up()
205 if (selectedChosenKey.empty())
206 return ButtonPolicy::SMI_NOOP;
208 // Qt will select the first one on redo, so we need this
209 string tmp = selectedChosenKey;
211 vector< string >::iterator iter = chosenkeys.begin();
213 for (; iter != chosenkeys.end(); ++iter) {
214 if (*iter==selectedChosenKey && iter!=chosenkeys.begin()) {
216 chosenkeys.erase(iter);
217 chosenkeys.insert(iter-1,tmp);
221 if (iter==chosenkeys.end())
222 return ButtonPolicy::SMI_NOOP;
225 selectedChosenKey=tmp;
227 return ButtonPolicy::SMI_VALID;
231 ButtonPolicy::SMInput FormCitation::down()
233 if (selectedChosenKey.empty())
234 return ButtonPolicy::SMI_NOOP;
236 // Qt will select the first one on redo, so we need this
237 string tmp = selectedChosenKey;
239 vector< string >::iterator iter = chosenkeys.begin();
241 for (; iter != chosenkeys.end(); ++iter) {
242 if (*iter == selectedChosenKey && (iter+1)!=chosenkeys.end()) {
244 chosenkeys.erase(iter);
245 chosenkeys.insert(iter+1, tmp);
249 if (iter == chosenkeys.end())
250 return ButtonPolicy::SMI_NOOP;
253 selectedChosenKey=tmp;
255 return ButtonPolicy::SMI_VALID;
259 ButtonPolicy::SMInput FormCitation::select_key(char const * key)
261 if (controller().isReadonly())
262 return ButtonPolicy::SMI_INVALID;
264 vector<string>::const_iterator iter =
265 find(chosenkeys.begin(), chosenkeys.end(), key);
267 if (iter != chosenkeys.end())
268 return ButtonPolicy::SMI_NOOP;
274 return ButtonPolicy::SMI_VALID;
278 void FormCitation::highlight_key(char const * key)
280 highlight(key, dialog_->chosen, selectedKey, selectedChosenKey);
284 void FormCitation::highlight_chosen(char const * key)
286 highlight(key, dialog_->keys, selectedChosenKey, selectedKey);
290 void FormCitation::highlight(char const * key, QListBox * lb,
291 string & selected1, string & selected2)
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());
304 lb->clearSelection();
310 if (i == keys.size())
311 dialog_->entry->setText(_("Key not found."));