2 * \file QCitationDialog.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Kalle Dalheimer
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "QCitationDialog.h"
16 #include "ui/QCitationFindUi.h"
17 #include "QCitation.h"
19 #include "qt_helpers.h"
21 #include "bufferparams.h"
23 #include "controllers/ControlCitation.h"
24 #include "controllers/ButtonController.h"
26 #include "support/lstrings.h"
39 using support::getStringFromVector;
40 using support::getVectorFromString;
45 void updateBrowser(Q3ListBox * browser,
46 vector<string> const & keys)
50 for (vector<string>::const_iterator it = keys.begin();
51 it < keys.end(); ++it) {
52 string const key = trim(*it);
53 // FIXME: why the .empty() test ?
55 browser->insertItem(toqstr(key));
59 QCitationDialog::QCitationDialog(QCitation * form)
64 /* connect(restorePB, SIGNAL(clicked()),
65 form, SLOT(slotRestore()));
66 connect(okPB, SIGNAL(clicked()),
67 form, SLOT(slotOK()));
68 connect(applyPB, SIGNAL(clicked()),
69 form, SLOT(slotApply()));
70 connect(closePB, SIGNAL(clicked()),
71 form, SLOT(slotClose()));
73 // Manage the ok, apply, restore and cancel/close buttons
74 form_->bcview().setOK(okPB);
75 form_->bcview().setApply(applyPB);
76 form_->bcview().setCancel(closePB);
77 form_->bcview().setRestore(restorePB);
79 form_->bcview().addReadOnly(addPB);
80 form_->bcview().addReadOnly(deletePB);
81 form_->bcview().addReadOnly(upPB);
82 form_->bcview().addReadOnly(downPB);
83 form_->bcview().addReadOnly(citationStyleCO);
84 form_->bcview().addReadOnly(forceuppercaseCB);
85 form_->bcview().addReadOnly(fulllistCB);
86 form_->bcview().addReadOnly(textBeforeED);
87 form_->bcview().addReadOnly(textAfterED);
89 selectedLV->setModel(form_->selected());
90 availableLV->setModel(form_->available());
92 // foundLV.setModel(form_->found());
94 connect( citationStyleCO, SIGNAL( activated(int) ), this, SLOT( changed() ) );
95 connect( fulllistCB, SIGNAL( clicked() ), this, SLOT( changed() ) );
96 connect( forceuppercaseCB, SIGNAL( clicked() ), this, SLOT( changed() ) );
97 connect( textBeforeED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed() ) );
98 connect( textAfterED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed() ) );
101 // find_ = new QCitationFind(form_, this);
103 // connect(selectedLV, SIGNAL(doubleClicked(const QModelIndex & index)),
104 // form_, SLOT(on_okPB_clicked()));//SLOT(slotOK()));
107 QCitationDialog::~QCitationDialog()
112 void QCitationDialog::on_okPB_clicked()
118 void QCitationDialog::on_cancelPB_clicked()
123 void QCitationDialog::on_applyPB_clicked()
128 void QCitationDialog::on_restorePB_clicked()
130 form_->update_contents();
133 void QCitationDialog::apply(InsetCommandParams & params)
135 vector<biblio::CiteStyle> const & styles =
136 ControlCitation::getCiteStyles();
138 int const choice = std::max(0, citationStyleCO->currentItem());
139 bool const full = fulllistCB->isChecked();
140 bool const force = forceuppercaseCB->isChecked();
142 string const command =
143 biblio::CitationStyle(styles[choice], full, force)
146 params.setCmdName(command);
148 string const before = fromqstr(textBeforeED->text());
149 params.setSecOptions(before);
151 string const after = fromqstr(textAfterED->text());
152 params.setOptions(after);
158 void QCitationDialog::update(InsetCommandParams const & params)
160 // No keys have been selected yet, so...
161 infoML->document()->clear();
164 textBeforeED->setText(
165 toqstr(params.getSecOptions()));
166 textAfterED->setText(
167 toqstr(params.getOptions()));
175 void QCitationDialog::updateStyle()
177 biblio::CiteEngine const engine = form_->controller().getEngine();
178 bool const natbib_engine =
179 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
180 engine == biblio::ENGINE_NATBIB_NUMERICAL;
181 bool const basic_engine = engine == biblio::ENGINE_BASIC;
183 fulllistCB->setEnabled(natbib_engine);
184 forceuppercaseCB->setEnabled(natbib_engine);
185 textBeforeED->setEnabled(!basic_engine);
187 string const & command = form_->controller().params().getCmdName();
189 // Find the style of the citekeys
190 vector<biblio::CiteStyle> const & styles =
191 ControlCitation::getCiteStyles();
192 biblio::CitationStyle const cs(command);
194 vector<biblio::CiteStyle>::const_iterator cit =
195 std::find(styles.begin(), styles.end(), cs.style);
197 // restore the latest natbib style
198 if (style_ >= 0 && style_ < citationStyleCO->count())
199 citationStyleCO->setCurrentItem(style_);
201 citationStyleCO->setCurrentItem(0);
202 fulllistCB->setChecked(false);
203 forceuppercaseCB->setChecked(false);
205 if (cit != styles.end()) {
206 int const i = int(cit - styles.begin());
207 citationStyleCO->setCurrentItem(i);
208 fulllistCB->setChecked(cs.full);
209 forceuppercaseCB->setChecked(cs.forceUCase);
214 void QCitationDialog::fillStyles()
216 if (citekeys.empty()) {
217 citationStyleCO->setEnabled(false);
218 citationStyleLA->setEnabled(false);
222 int const orig = citationStyleCO->currentItem();
224 citationStyleCO->clear();
226 QStringList selected_keys = form_->selected()->stringList();
227 if (selected_keys.empty())
230 if (selectedLV->selectionModel()->selectedIndexes().empty())
233 int curr = selectedLV->selectionModel()->selectedIndexes()[0].row();//selectedLV->currentItem();
235 string key = fromqstr(selected_keys[curr]);
237 vector<string> const & sty = form_->controller().getCiteStrings(key);
239 biblio::CiteEngine const engine = form_->controller().getEngine();
240 bool const basic_engine = engine == biblio::ENGINE_BASIC;
242 citationStyleCO->setEnabled(!sty.empty() && !basic_engine);
243 citationStyleLA->setEnabled(!sty.empty() && !basic_engine);
245 for (vector<string>::const_iterator it = sty.begin();
246 it != sty.end(); ++it) {
247 citationStyleCO->insertItem(toqstr(*it));
250 if (orig != -1 && orig < citationStyleCO->count())
251 citationStyleCO->setCurrentItem(orig);
255 void QCitationDialog::setButtons()
257 if (form_->readOnly())
260 int const row_count = selectedLV->model()->rowCount();
263 if (! selectedLV->selectionModel()->selectedIndexes().empty()) {
265 selectedLV->selectionModel()->selectedIndexes()[0].row();
268 deletePB->setEnabled(sel_nr >= 0);
269 upPB->setEnabled(sel_nr > 0);
270 downPB->setEnabled(sel_nr >= 0 && sel_nr < row_count - 1);
274 void QCitationDialog::on_selectedLV_currentChanged(Q3ListBoxItem*)
277 infoML->document()->clear();
279 int const sel = selectedLB->currentItem();
285 infoML->document()->setPlainText(form_->getKeyInfo(sel));
292 void QCitationDialog::on_addPB_clicked()
294 form_->addKeys(availableLV->selectionModel()->selectedIndexes());
297 void QCitationDialog::on_deletePB_clicked()
299 form_->addKeys(selectedLV->selectionModel()->selectedIndexes());
304 void QCitationDialog::on_upPB_clicked()
306 form_->upKey(selectedLV->selectionModel()->selectedIndexes());
311 void QCitationDialog::on_downPB_clicked()
313 form_->downKey(selectedLV->selectionModel()->selectedIndexes());
317 void QCitationDialog::on_findLE_textChanged(const QString & text)
319 QModelIndex const index = form_->findKey(text);
320 if (! index.isValid())
323 // QItemSelection selection(index, index);
324 availableLV->selectionModel()->select(index, QItemSelectionModel::Select);
328 void QCitationDialog::on_advancedSearchPB_clicked()
335 void QCitationDialog::changed()
343 QCitationFind::QCitationFind(QCitation * form, QWidget * parent, Qt::WFlags f)
344 : form_(form), QDialog(parent, f)
347 connect(addPB, SIGNAL(clicked()), this, SLOT(accept()));
348 connect(closePB, SIGNAL(clicked()), this, SLOT(reject()));
349 connect(previousPB, SIGNAL(clicked()), this, SLOT(previous()));
350 connect(nextPB, SIGNAL(clicked()), this, SLOT(next()));
353 void QCitationFind::update()
355 // updateBrowser(availableLB, form_->availableKeys());
358 void QCitationFind::on_availableLB_currentChanged(Q3ListBoxItem *)
360 infoML->document()->clear();
362 int const sel = availableLB->currentItem();
364 addPB->setEnabled(false);
368 addPB->setEnabled(true);
369 // infoML->document()->setPlainText(form_->getKeyInfo(sel));
373 void QCitationFind::on_availableLB_selected(Q3ListBoxItem *)
375 int const sel = availableLB->currentItem();
377 // foundkeys.push_back(form_->availableKeys()[sel]);
382 void QCitationFind::on_addPB_clicked()
384 // form_->addKeys(availableLB->selectionModel()->selectedIndexes());
386 int const sel = availableLB->currentItem();
391 QStringList bibkeys = form_->available()->stringList();
393 // Add the selected browser_bib keys to browser_cite
394 // multiple selections are possible
395 for (unsigned int i = 0; i != availableLB->count(); i++) {
396 if (availableLB->isSelected(i)) {
397 foundkeys.push_back(fromqstr(bibkeys[i]));
406 void QCitationFind::previous()
408 find(biblio::BACKWARD);
412 void QCitationFind::next()
414 find(biblio::FORWARD);
418 void QCitationFind::find(biblio::Direction dir)
420 /* QStringList bibkeys = form_->available()->stringList();
422 biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
424 biblio::Search const type = searchTypeCB->isChecked()
425 ? biblio::REGEX : biblio::SIMPLE;
427 vector<string>::const_iterator start = bibkeys.begin();
428 int const sel = availableLB->currentItem();
429 if (sel >= 0 && sel <= int(bibkeys.size()-1))
432 // Find the NEXT instance...
433 if (dir == biblio::FORWARD)
436 bool const casesens = searchCaseCB->isChecked();
437 string const str = fromqstr(searchED->text());
439 vector<string>::const_iterator cit =
440 biblio::searchKeys(theMap, bibkeys, str,
441 start, type, dir, casesens);
443 // not found. let's loop round
444 if (cit == bibkeys.end()) {
445 if (dir == biblio::FORWARD) {
446 start = bibkeys.begin();
448 else start = bibkeys.end() - 1;
450 cit = biblio::searchKeys(theMap, bibkeys, str,
451 start, type, dir, casesens);
453 if (cit == bibkeys.end())
457 int const found = int(cit - bibkeys.begin());
462 // Update the display
463 // note that we have multi selection mode!
464 availableLB->setSelected(sel, false);
465 availableLB->setSelected(found, true);
466 availableLB->setCurrentItem(found);
467 availableLB->ensureCurrentVisible();
471 } // namespace frontend