2 * \file GuiCitation.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Kalle Dalheimer
8 * \author Abdelrazak Younes
11 * Full author contact details are available in file CREDITS.
16 #include "GuiCitation.h"
20 #include "frontend_helpers.h"
21 #include "qt_helpers.h"
23 #include "support/lstrings.h"
24 #include "support/docstring.h"
29 #include <QCloseEvent>
44 template<typename String>
45 static QStringList to_qstring_list(vector<String> const & v)
49 for (size_t i = 0; i != v.size(); ++i) {
52 qlist.append(lyx::toqstr(v[i]));
58 static vector<lyx::docstring> to_docstring_vector(QStringList const & qlist)
60 vector<lyx::docstring> v;
61 for (int i = 0; i != qlist.size(); ++i) {
62 if (qlist[i].isEmpty())
64 v.push_back(lyx::qstring_to_ucs4(qlist[i]));
70 GuiCitationDialog::GuiCitationDialog(LyXView & lv)
71 : GuiDialog(lv, "citation")
74 setViewTitle(_("Citation"));
75 setController(new ControlCitation(*this));
77 setWindowTitle(toqstr("LyX: " + getViewTitle()));
79 connect(citationStyleCO, SIGNAL(activated(int)),
80 this, SLOT(changed()));
81 connect(fulllistCB, SIGNAL(clicked()),
82 this, SLOT(changed()));
83 connect(forceuppercaseCB, SIGNAL(clicked()),
84 this, SLOT(changed()));
85 connect(textBeforeED, SIGNAL(textChanged(const QString&)),
86 this, SLOT(changed()));
87 connect(textAfterED, SIGNAL(textChanged(const QString&)),
88 this, SLOT(changed()));
89 connect(clearPB, SIGNAL(clicked()),
90 findLE, SLOT(clear()));
91 connect(this, SIGNAL(rejected()), this, SLOT(cleanUp()));
94 new GuiSelectionManager(availableLV, selectedLV,
95 addPB, deletePB, upPB, downPB, available(), selected());
96 connect(selectionManager, SIGNAL(selectionChanged()),
97 this, SLOT(setCitedKeys()));
98 connect(selectionManager, SIGNAL(updateHook()),
99 this, SLOT(updateDialog()));
100 connect(selectionManager, SIGNAL(okHook()),
101 this, SLOT(on_okPB_clicked()));
103 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
107 ControlCitation & GuiCitationDialog::controller() const
109 return static_cast<ControlCitation &>(Dialog::controller());
113 void GuiCitationDialog::cleanUp()
116 controller().clearParams();
121 void GuiCitationDialog::closeEvent(QCloseEvent * e)
124 controller().clearParams();
129 void GuiCitationDialog::applyView()
131 int const choice = std::max(0, citationStyleCO->currentIndex());
133 bool const full = fulllistCB->isChecked();
134 bool const force = forceuppercaseCB->isChecked();
136 QString const before = textBeforeED->text();
137 QString const after = textAfterED->text();
139 apply(choice, full, force, before, after);
143 void GuiCitationDialog::hideView()
145 controller().clearParams();
150 void GuiCitationDialog::showView()
153 availableLV->setFocus();
160 bool GuiCitationDialog::isVisibleView() const
162 return QDialog::isVisible();
166 void GuiCitationDialog::on_okPB_clicked()
174 void GuiCitationDialog::on_cancelPB_clicked()
181 void GuiCitationDialog::on_applyPB_clicked()
187 void GuiCitationDialog::on_restorePB_clicked()
194 void GuiCitationDialog::updateView()
202 // The main point of separating this out is that the fill*() methods
203 // called in update() do not need to be called for INTERNAL updates,
204 // such as when addPB is pressed, as the list of fields, entries, etc,
205 // will not have changed. At the moment, however, the division between
206 // fillStyles() and updateStyles() doesn't lend itself to dividing the
207 // two methods, though they should be divisible.
208 void GuiCitationDialog::updateDialog()
210 if (selectionManager->selectedFocused()) {
211 if (selectedLV->selectionModel()->selectedIndexes().isEmpty())
212 updateInfo(availableLV->currentIndex());
214 updateInfo(selectedLV->currentIndex());
216 if (availableLV->selectionModel()->selectedIndexes().isEmpty())
217 updateInfo(QModelIndex());
219 updateInfo(availableLV->currentIndex());
223 textBeforeED->setText(textBefore());
224 textAfterED->setText(textAfter());
230 void GuiCitationDialog::updateStyle()
232 biblio::CiteEngine const engine = controller().getEngine();
233 bool const natbib_engine =
234 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
235 engine == biblio::ENGINE_NATBIB_NUMERICAL;
236 bool const basic_engine = engine == biblio::ENGINE_BASIC;
238 bool const haveSelection =
239 selectedLV->model()->rowCount() > 0;
240 fulllistCB->setEnabled(natbib_engine && haveSelection);
241 forceuppercaseCB->setEnabled(natbib_engine && haveSelection);
242 textBeforeED->setEnabled(!basic_engine && haveSelection);
243 textBeforeLA->setEnabled(!basic_engine && haveSelection);
244 textAfterED->setEnabled(haveSelection);
245 textAfterLA->setEnabled(haveSelection);
246 citationStyleCO->setEnabled(!basic_engine && haveSelection);
247 citationStyleLA->setEnabled(!basic_engine && haveSelection);
249 string const & command = controller().params().getCmdName();
251 // Find the style of the citekeys
252 vector<biblio::CiteStyle> const & styles =
253 ControlCitation::getCiteStyles();
254 biblio::CitationStyle const cs(command);
256 vector<biblio::CiteStyle>::const_iterator cit =
257 std::find(styles.begin(), styles.end(), cs.style);
259 // restore the latest natbib style
260 if (style_ >= 0 && style_ < citationStyleCO->count())
261 citationStyleCO->setCurrentIndex(style_);
263 citationStyleCO->setCurrentIndex(0);
265 if (cit != styles.end()) {
266 int const i = int(cit - styles.begin());
267 citationStyleCO->setCurrentIndex(i);
268 fulllistCB->setChecked(cs.full);
269 forceuppercaseCB->setChecked(cs.forceUCase);
271 fulllistCB->setChecked(false);
272 forceuppercaseCB->setChecked(false);
277 //This one needs to be called whenever citationStyleCO needs
278 //to be updated---and this would be on anything that changes the
279 //selection in selectedLV, or on a general update.
280 void GuiCitationDialog::fillStyles()
282 int const oldIndex = citationStyleCO->currentIndex();
284 citationStyleCO->clear();
286 QStringList selected_keys = selected()->stringList();
287 if (selected_keys.empty()) {
288 citationStyleCO->setEnabled(false);
289 citationStyleLA->setEnabled(false);
293 int curr = selectedLV->model()->rowCount() - 1;
297 if (!selectedLV->selectionModel()->selectedIndexes().empty())
298 curr = selectedLV->selectionModel()->selectedIndexes()[0].row();
300 QStringList sty = citationStyles(curr);
302 bool const basic_engine =
303 (controller().getEngine() == biblio::ENGINE_BASIC);
305 citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
306 citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
308 if (sty.isEmpty() || basic_engine)
311 citationStyleCO->insertItems(0, sty);
313 if (oldIndex != -1 && oldIndex < citationStyleCO->count())
314 citationStyleCO->setCurrentIndex(oldIndex);
318 void GuiCitationDialog::fillFields()
320 fieldsCO->blockSignals(true);
321 int const oldIndex = fieldsCO->currentIndex();
323 QStringList const & fields = getFieldsAsQStringList();
324 fieldsCO->insertItem(0, qt_("All Fields"));
325 fieldsCO->insertItem(1, qt_("Keys"));
326 fieldsCO->insertItems(2, fields);
327 if (oldIndex != -1 && oldIndex < fieldsCO->count())
328 fieldsCO->setCurrentIndex(oldIndex);
329 fieldsCO->blockSignals(false);
333 void GuiCitationDialog::fillEntries()
335 entriesCO->blockSignals(true);
336 int const oldIndex = entriesCO->currentIndex();
338 QStringList const & entries = getEntriesAsQStringList();
339 entriesCO->insertItem(0, qt_("All Entry Types"));
340 entriesCO->insertItems(1, entries);
341 if (oldIndex != -1 && oldIndex < entriesCO->count())
342 entriesCO->setCurrentIndex(oldIndex);
343 entriesCO->blockSignals(false);
347 bool GuiCitationDialog::isSelected(const QModelIndex & idx)
349 QString const str = idx.data().toString();
350 return selected()->stringList().contains(str);
354 void GuiCitationDialog::setButtons()
356 selectionManager->updateView();
357 int const srows = selectedLV->model()->rowCount();
358 applyPB->setEnabled(srows > 0);
359 okPB->setEnabled(srows > 0);
363 void GuiCitationDialog::updateInfo(QModelIndex const & idx)
366 QString const keytxt = getKeyInfo(idx.data().toString());
367 infoML->document()->setPlainText(keytxt);
369 infoML->document()->clear();
373 void GuiCitationDialog::findText(QString const & text, bool reset)
375 //"All Fields" and "Keys" are the first two
376 int index = fieldsCO->currentIndex() - 2;
377 vector<docstring> const & fields = controller().availableFields();
380 if (index <= -1 || index >= int(fields.size()))
381 //either "All Fields" or "Keys" or an invalid value
382 field = from_ascii("");
384 field = fields[index];
387 bool const onlyKeys = index == -1;
389 //"All Entry Types" is first.
390 index = entriesCO->currentIndex() - 1;
391 vector<docstring> const & entries = controller().availableEntries();
393 if (index < 0 || index >= int(entries.size()))
394 entryType = from_ascii("");
396 entryType = entries[index];
398 bool const case_sentitive = caseCB->checkState();
399 bool const reg_exp = regexCB->checkState();
400 findKey(text, onlyKeys, field, entryType,
401 case_sentitive, reg_exp, reset);
403 //It'd be nice to save and restore the current selection in
404 //availableLV. Currently, we get an automatic reset, since the
411 void GuiCitationDialog::on_fieldsCO_currentIndexChanged(int /*index*/)
413 findText(findLE->text(), true);
417 void GuiCitationDialog::on_entriesCO_currentIndexChanged(int /*index*/)
419 findText(findLE->text(), true);
423 void GuiCitationDialog::on_findLE_textChanged(const QString & text)
425 clearPB->setDisabled(text.isEmpty());
432 void GuiCitationDialog::on_caseCB_stateChanged(int)
434 findText(findLE->text());
438 void GuiCitationDialog::on_regexCB_stateChanged(int)
440 findText(findLE->text());
444 void GuiCitationDialog::changed()
451 void GuiCitationDialog::apply(int const choice,
452 bool const full, bool const force,
453 QString before, QString after)
455 if (cited_keys_.isEmpty())
458 vector<biblio::CiteStyle> const & styles =
459 ControlCitation::getCiteStyles();
461 string const command =
462 biblio::CitationStyle(styles[choice], full, force)
465 controller().params().setCmdName(command);
466 controller().params()["key"] = qstring_to_ucs4(cited_keys_.join(","));
467 controller().params()["before"] = qstring_to_ucs4(before);
468 controller().params()["after"] = qstring_to_ucs4(after);
469 controller().dispatchParams();
473 void GuiCitationDialog::clearSelection()
476 selected_model_.setStringList(cited_keys_);
480 QString GuiCitationDialog::textBefore()
482 return toqstr(controller().params()["before"]);
486 QString GuiCitationDialog::textAfter()
488 return toqstr(controller().params()["after"]);
492 bool GuiCitationDialog::initialiseParams(std::string const & data)
494 if (!controller().initialiseParams(data))
501 void GuiCitationDialog::init()
503 // Make the list of all available bibliography keys
504 all_keys_ = to_qstring_list(controller().availableKeys());
505 available_model_.setStringList(all_keys_);
507 // Ditto for the keys cited in this inset
508 QString str = toqstr(controller().params()["key"]);
512 cited_keys_ = str.split(",");
513 selected_model_.setStringList(cited_keys_);
517 void GuiCitationDialog::findKey(QString const & str, bool only_keys,
518 docstring field, docstring entryType,
519 bool case_sensitive, bool reg_exp, bool reset)
521 // Used for optimisation: store last searched string.
522 static QString last_searched_string;
523 // Used to disable the above optimisation.
524 static bool last_case_sensitive;
525 static bool last_reg_exp;
526 // Reset last_searched_string in case of changed option.
527 if (last_case_sensitive != case_sensitive
528 || last_reg_exp != reg_exp) {
529 LYXERR(Debug::GUI) << "GuiCitation::findKey: optimisation disabled!" << std::endl;
530 last_searched_string.clear();
532 // save option for next search.
533 last_case_sensitive = case_sensitive;
534 last_reg_exp = reg_exp;
536 Qt::CaseSensitivity qtcase = case_sensitive?
537 Qt::CaseSensitive: Qt::CaseInsensitive;
539 // If new string (str) contains the last searched one...
541 !last_searched_string.isEmpty() &&
543 str.contains(last_searched_string, qtcase))
544 // ... then only search within already found list.
545 keys = available_model_.stringList();
547 // ... else search all keys.
549 // save searched string for next search.
550 last_searched_string = str;
554 // First, filter by entryType, which will be faster than
555 // what follows, so we may get to do that on less.
556 vector<docstring> keyVector = to_docstring_vector(keys);
557 controller().filterByEntryType(keyVector, entryType);
560 result = to_qstring_list(keyVector);
562 result = to_qstring_list(controller().searchKeys(keyVector, only_keys,
563 qstring_to_ucs4(str), field, case_sensitive, reg_exp));
565 available_model_.setStringList(result);
569 QStringList GuiCitationDialog::getFieldsAsQStringList()
571 return to_qstring_list(controller().availableFields());
575 QStringList GuiCitationDialog::getEntriesAsQStringList()
577 return to_qstring_list(controller().availableEntries());
581 QStringList GuiCitationDialog::citationStyles(int sel)
583 docstring const key = qstring_to_ucs4(cited_keys_[sel]);
584 return to_qstring_list(controller().getCiteStrings(key));
588 QString GuiCitationDialog::getKeyInfo(QString const & sel)
590 return toqstr(controller().getInfo(qstring_to_ucs4(sel)));
594 void GuiCitationDialog::setCitedKeys()
596 cited_keys_ = selected_model_.stringList();
599 } // namespace frontend
602 #include "GuiCitation_moc.cpp"