4 * John Levon, moz@compsoc.man.ac.uk
7 /***************************************************************************
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. *
14 ***************************************************************************/
21 #include "FormCitation.h"
26 #include "formcitationdialog.h"
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)
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));
43 FormCitation::~FormCitation()
48 void FormCitation::showCitation(InsetCommand * const inset)
50 // FIXME: when could inset be 0 here ?
55 readonly = lv_->buffer()->isReadonly();
56 ih_ = inset_->hide.connect(slot(this,&FormCitation::hide));
57 params = inset->params();
62 void FormCitation::createCitation(string const & arg)
64 // we could already be showing stuff, clear it out
68 readonly = lv_->buffer()->isReadonly();
69 params.setFromString(arg);
73 void FormCitation::updateButtons()
75 bool iskey,ischosenkey;
77 iskey = !selectedKey.empty();
78 ischosenkey = !selectedChosenKey.empty();
80 dialog_->remove->setEnabled(ischosenkey);
81 dialog_->add->setEnabled(iskey);
82 dialog_->up->setEnabled(ischosenkey);
83 dialog_->down->setEnabled(ischosenkey);
86 void FormCitation::updateChosenList()
88 dialog_->chosen->setAutoUpdate(false);
89 dialog_->chosen->clear();
91 for (vector< string >::const_iterator iter = chosenkeys.begin();
92 iter != chosenkeys.end(); ++iter) {
93 dialog_->chosen->insertItem(iter->c_str());
95 dialog_->chosen->setAutoUpdate(true);
96 dialog_->chosen->update();
99 void FormCitation::updateAvailableList()
101 dialog_->keys->setAutoUpdate(false);
102 dialog_->keys->clear();
104 for (vector< pair<string,string> >::const_iterator iter = keys.begin();
105 iter != keys.end(); ++iter) {
106 dialog_->keys->insertItem(iter->first.c_str());
108 dialog_->keys->setAutoUpdate(true);
109 dialog_->keys->update();
112 void FormCitation::update()
116 vector < pair<string,string> > ckeys = lv_->buffer()->getBibkeyList();
118 for (vector< pair<string,string> >::const_iterator iter = ckeys.begin();
119 iter != ckeys.end(); ++iter) {
120 keys.push_back(*iter);
123 updateAvailableList();
128 string tmp, paramkeys(params.getContents());
129 paramkeys = frontStrip(split(paramkeys, tmp, ','));
131 while (!tmp.empty()) {
132 chosenkeys.push_back(tmp);
133 paramkeys = frontStrip(split(paramkeys, tmp, ','));
137 selectedChosenKey.erase();
139 dialog_->entry->setText("");
141 dialog_->after->setText(params.getOptions().c_str());
146 dialog_->keys->setFocusPolicy(QWidget::NoFocus);
147 dialog_->chosen->setFocusPolicy(QWidget::NoFocus);
148 dialog_->after->setFocusPolicy(QWidget::NoFocus);
149 dialog_->buttonOk->setEnabled(false);
150 dialog_->buttonCancel->setText(_("Close"));
152 dialog_->keys->setFocusPolicy(QWidget::StrongFocus);
153 dialog_->chosen->setFocusPolicy(QWidget::StrongFocus);
154 dialog_->after->setFocusPolicy(QWidget::StrongFocus);
155 dialog_->buttonOk->setEnabled(true);
156 dialog_->buttonCancel->setText(_("Cancel"));
160 void FormCitation::apply()
167 for (vector< string >::const_iterator iter = chosenkeys.begin();
168 iter != chosenkeys.end(); ++iter) {
169 if (iter != chosenkeys.begin())
174 params.setContents(contents);
175 params.setOptions(dialog_->after->text());
178 if (params != inset_->params()) {
179 inset_->setParams(params);
180 lv_->view()->updateInset(inset_, true);
183 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT, params.getAsString().c_str());
186 void FormCitation::show()
189 dialog_ = new FormCitationDialog(this, 0, _("LyX: Citation Reference"), false);
191 if (!dialog_->isVisible()) {
192 h_ = d_->hideBufferDependent.connect(slot(this, &FormCitation::hide));
193 u_ = d_->updateBufferDependent.connect(slot(this, &FormCitation::update));
197 dialog_->setActiveWindow();
203 void FormCitation::close()
211 void FormCitation::hide()
217 void FormCitation::selectChosen()
219 for (unsigned int i=0; i < dialog_->chosen->count(); ++i) {
220 if (dialog_->chosen->text(i)==selectedChosenKey) {
221 dialog_->chosen->setSelected(i,true);
222 dialog_->chosen->setTopItem(i);
228 void FormCitation::add()
230 if (selectedKey.empty())
233 for (vector< pair<string,string> >::const_iterator iter = keys.begin();
234 iter != keys.end(); ++iter) {
235 if (iter->first == selectedKey) {
236 chosenkeys.push_back(iter->first);
241 selectedChosenKey.erase();
246 void FormCitation::remove()
248 if (selectedChosenKey.empty())
251 for (vector< string >::iterator iter = chosenkeys.begin();
252 iter != chosenkeys.end(); ++iter) {
253 if (*iter==selectedChosenKey) {
254 chosenkeys.erase(iter);
258 selectedChosenKey.erase();
263 void FormCitation::up()
265 if (selectedChosenKey.empty())
268 // Qt will select the first one on redo, so we need this
269 string tmp = selectedChosenKey;
271 vector< string >::iterator iter = chosenkeys.begin();
273 for (; iter != chosenkeys.end(); ++iter) {
274 if (*iter==selectedChosenKey && iter!=chosenkeys.begin()) {
276 chosenkeys.erase(iter);
277 chosenkeys.insert(iter-1,tmp);
281 if (iter==chosenkeys.end())
285 selectedChosenKey=tmp;
289 void FormCitation::down()
291 if (selectedChosenKey.empty())
294 // Qt will select the first one on redo, so we need this
295 string tmp = selectedChosenKey;
297 vector< string >::iterator iter = chosenkeys.begin();
299 for (; iter != chosenkeys.end(); ++iter) {
300 if (*iter==selectedChosenKey && (iter+1)!=chosenkeys.end()) {
302 chosenkeys.erase(iter);
303 chosenkeys.insert(iter+1, tmp);
307 if (iter==chosenkeys.end())
311 selectedChosenKey=tmp;
315 void FormCitation::select_key(const char *key)
323 void FormCitation::highlight_key(const char *key)
328 for (unsigned int i=0; i < keys.size(); i++) {
329 if (keys[i].first==key) {
330 dialog_->entry->setText(keys[i].second.c_str());
338 void FormCitation::highlight_chosen(const char *key)
340 selectedChosenKey.erase();
341 selectedChosenKey = key;
345 for (i=0; i < keys.size(); i++) {
346 if (keys[i].first==key) {
347 if (keys[i].second.compare(dialog_->entry->text()))
348 dialog_->entry->setText(keys[i].second.c_str());
354 dialog_->entry->setText(_("Key not found in references."));