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 "citationdlg.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()
76 dialog_->add->setEnabled(false);
77 dialog_->remove->setEnabled(false);
78 dialog_->up->setEnabled(false);
79 dialog_->down->setEnabled(false);
83 bool ischosenkey = !selectedChosenKey.empty();
85 vector<string>::const_iterator iter =
86 find(chosenkeys.begin(), chosenkeys.end(), selectedKey);
88 dialog_->add->setEnabled(!selectedKey.empty() && iter == chosenkeys.end());
89 dialog_->remove->setEnabled(ischosenkey);
90 dialog_->up->setEnabled(ischosenkey);
91 dialog_->down->setEnabled(ischosenkey);
94 void FormCitation::updateChosenList()
96 dialog_->chosen->setAutoUpdate(false);
97 dialog_->chosen->clear();
99 for (vector< string >::const_iterator iter = chosenkeys.begin();
100 iter != chosenkeys.end(); ++iter) {
101 dialog_->chosen->insertItem(iter->c_str());
103 dialog_->chosen->setAutoUpdate(true);
104 dialog_->chosen->update();
107 void FormCitation::updateAvailableList()
109 dialog_->keys->setAutoUpdate(false);
110 dialog_->keys->clear();
112 for (vector< pair<string,string> >::const_iterator iter = keys.begin();
113 iter != keys.end(); ++iter) {
114 dialog_->keys->insertItem(iter->first.c_str());
116 dialog_->keys->setAutoUpdate(true);
117 dialog_->keys->update();
121 // we can safely ignore the parameter because we can always update
122 void FormCitation::update(bool)
126 vector < pair<string,string> > ckeys = lv_->buffer()->getBibkeyList();
128 for (vector< pair<string,string> >::const_iterator iter = ckeys.begin();
129 iter != ckeys.end(); ++iter) {
130 keys.push_back(*iter);
133 updateAvailableList();
138 string tmp, paramkeys(params.getContents());
139 paramkeys = frontStrip(split(paramkeys, tmp, ','));
141 while (!tmp.empty()) {
142 chosenkeys.push_back(tmp);
143 paramkeys = frontStrip(split(paramkeys, tmp, ','));
147 selectedChosenKey.erase();
149 dialog_->entry->setText("");
151 dialog_->after->setText(params.getOptions().c_str());
156 dialog_->keys->setFocusPolicy(QWidget::NoFocus);
157 dialog_->chosen->setFocusPolicy(QWidget::NoFocus);
158 dialog_->after->setFocusPolicy(QWidget::NoFocus);
159 dialog_->buttonOk->setEnabled(false);
160 dialog_->buttonCancel->setText(_("&Close"));
162 dialog_->keys->setFocusPolicy(QWidget::StrongFocus);
163 dialog_->chosen->setFocusPolicy(QWidget::StrongFocus);
164 dialog_->after->setFocusPolicy(QWidget::StrongFocus);
165 dialog_->buttonOk->setEnabled(true);
166 dialog_->buttonCancel->setText(_("&Cancel"));
170 void FormCitation::apply()
177 for (vector< string >::const_iterator iter = chosenkeys.begin();
178 iter != chosenkeys.end(); ++iter) {
179 if (iter != chosenkeys.begin())
184 params.setContents(contents);
185 params.setOptions(dialog_->after->text());
188 if (params != inset_->params()) {
189 inset_->setParams(params);
190 lv_->view()->updateInset(inset_, true);
193 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT, params.getAsString().c_str());
196 void FormCitation::show()
199 dialog_ = new CitationDialog(this, 0, _("LyX: Citation Reference"), false);
201 if (!dialog_->isVisible()) {
202 h_ = d_->hideBufferDependent.connect(slot(this, &FormCitation::hide));
203 u_ = d_->updateBufferDependent.connect(slot(this, &FormCitation::update));
207 dialog_->setActiveWindow();
213 void FormCitation::close()
221 void FormCitation::hide()
227 void FormCitation::selectChosen()
229 for (unsigned int i=0; i < dialog_->chosen->count(); ++i) {
230 if (dialog_->chosen->text(i)==selectedChosenKey) {
231 dialog_->chosen->setSelected(i,true);
232 dialog_->chosen->setTopItem(i);
238 void FormCitation::add()
240 if (selectedKey.empty())
243 for (vector< pair<string,string> >::const_iterator iter = keys.begin();
244 iter != keys.end(); ++iter) {
245 if (iter->first == selectedKey) {
246 chosenkeys.push_back(iter->first);
251 selectedChosenKey.erase();
256 void FormCitation::remove()
258 if (selectedChosenKey.empty())
261 for (vector< string >::iterator iter = chosenkeys.begin();
262 iter != chosenkeys.end(); ++iter) {
263 if (*iter==selectedChosenKey) {
264 chosenkeys.erase(iter);
268 selectedChosenKey.erase();
273 void FormCitation::up()
275 if (selectedChosenKey.empty())
278 // Qt will select the first one on redo, so we need this
279 string tmp = selectedChosenKey;
281 vector< string >::iterator iter = chosenkeys.begin();
283 for (; iter != chosenkeys.end(); ++iter) {
284 if (*iter==selectedChosenKey && iter!=chosenkeys.begin()) {
286 chosenkeys.erase(iter);
287 chosenkeys.insert(iter-1,tmp);
291 if (iter==chosenkeys.end())
295 selectedChosenKey=tmp;
299 void FormCitation::down()
301 if (selectedChosenKey.empty())
304 // Qt will select the first one on redo, so we need this
305 string tmp = selectedChosenKey;
307 vector< string >::iterator iter = chosenkeys.begin();
309 for (; iter != chosenkeys.end(); ++iter) {
310 if (*iter==selectedChosenKey && (iter+1)!=chosenkeys.end()) {
312 chosenkeys.erase(iter);
313 chosenkeys.insert(iter+1, tmp);
317 if (iter==chosenkeys.end())
321 selectedChosenKey=tmp;
325 void FormCitation::select_key(const char *key)
330 vector<string>::const_iterator iter =
331 find(chosenkeys.begin(), chosenkeys.end(), key);
333 if (iter!=chosenkeys.end())
342 void FormCitation::highlight_key(const char *key)
350 for (unsigned int i=0; i < keys.size(); i++) {
351 if (keys[i].first==key) {
352 dialog_->entry->setText(keys[i].second.c_str());
353 dialog_->chosen->clearFocus();
354 dialog_->chosen->clearSelection();
355 selectedChosenKey.erase();
363 void FormCitation::highlight_chosen(const char *key)
365 selectedChosenKey.erase();
366 selectedChosenKey = key;
370 for (i=0; i < keys.size(); i++) {
371 if (keys[i].first==key) {
372 if (keys[i].second.compare(dialog_->entry->text()))
373 dialog_->entry->setText(keys[i].second.c_str());
374 dialog_->keys->clearFocus();
375 dialog_->keys->clearSelection();
382 dialog_->entry->setText(_("Key not found in references."));