3 * \file GuiCitation.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
8 * \author Kalle Dalheimer
9 * \author Abdelrazak Younes
10 * \author Richard Heck
12 * Full author contact details are available in file CREDITS.
17 #include "GuiCitation.h"
19 #include "GuiSelectionManager.h"
20 #include "qt_helpers.h"
23 #include "BufferView.h"
24 #include "BiblioInfo.h"
25 #include "BufferParams.h"
26 #include "FuncRequest.h"
28 #include "insets/InsetCommand.h"
30 #include "support/debug.h"
31 #include "support/docstring.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
35 #include <QCloseEvent>
45 #include "support/regex.h"
52 using namespace lyx::support;
58 // I am guessing that it would not hurt to make these private members.
59 static vector<string> citeCmds_;
60 static vector<CitationStyle> citeStyles_;
63 template<typename String>
64 static QStringList to_qstring_list(vector<String> const & v)
68 for (size_t i = 0; i != v.size(); ++i) {
71 qlist.append(lyx::toqstr(v[i]));
77 static vector<lyx::docstring> to_docstring_vector(QStringList const & qlist)
79 vector<lyx::docstring> v;
80 for (int i = 0; i != qlist.size(); ++i) {
81 if (qlist[i].isEmpty())
83 v.push_back(lyx::qstring_to_ucs4(qlist[i]));
89 GuiCitation::GuiCitation(GuiView & lv)
90 : DialogView(lv, "citation", qt_("Citation")),
91 params_(insetCode("citation"))
95 connect(citationStyleCO, SIGNAL(activated(int)),
96 this, SLOT(on_citationStyleCO_currentIndexChanged(int)));
97 connect(fulllistCB, SIGNAL(clicked()),
98 this, SLOT(changed()));
99 connect(forceuppercaseCB, SIGNAL(clicked()),
100 this, SLOT(changed()));
101 connect(textBeforeED, SIGNAL(textChanged(QString)),
102 this, SLOT(updateStyles()));
103 connect(textAfterED, SIGNAL(textChanged(QString)),
104 this, SLOT(updateStyles()));
105 connect(findLE, SIGNAL(returnPressed()),
106 this, SLOT(on_searchPB_clicked()));
107 connect(textBeforeED, SIGNAL(returnPressed()),
108 this, SLOT(on_okPB_clicked()));
109 connect(textAfterED, SIGNAL(returnPressed()),
110 this, SLOT(on_okPB_clicked()));
112 selectionManager = new GuiSelectionManager(availableLV, selectedLV,
113 addPB, deletePB, upPB, downPB, &available_model_, &selected_model_);
114 connect(selectionManager, SIGNAL(selectionChanged()),
115 this, SLOT(setCitedKeys()));
116 connect(selectionManager, SIGNAL(updateHook()),
117 this, SLOT(updateControls()));
118 connect(selectionManager, SIGNAL(okHook()),
119 this, SLOT(on_okPB_clicked()));
121 setFocusProxy(availableLV);
125 GuiCitation::~GuiCitation()
127 delete selectionManager;
131 void GuiCitation::closeEvent(QCloseEvent * e)
134 DialogView::closeEvent(e);
138 void GuiCitation::applyView()
140 int const choice = max(0, citationStyleCO->currentIndex());
142 bool const full = fulllistCB->isChecked();
143 bool const force = forceuppercaseCB->isChecked();
145 QString const before = textBeforeED->text();
146 QString const after = textAfterED->text();
148 apply(choice, full, force, before, after);
152 void GuiCitation::showEvent(QShowEvent * e)
155 availableLV->setFocus();
156 DialogView::showEvent(e);
160 void GuiCitation::on_okPB_clicked()
168 void GuiCitation::on_cancelPB_clicked()
175 void GuiCitation::on_applyPB_clicked()
181 void GuiCitation::on_restorePB_clicked()
187 void GuiCitation::updateControls()
189 BiblioInfo const & bi = bibInfo();
194 // The main point of separating this out is that the fill*() methods
195 // called in update() do not need to be called for INTERNAL updates,
196 // such as when addPB is pressed, as the list of fields, entries, etc,
197 // will not have changed.
198 void GuiCitation::updateControls(BiblioInfo const & bi)
200 QModelIndex idx = selectionManager->getSelectedIndex();
202 selectionManager->update();
206 void GuiCitation::updateFormatting(CitationStyle currentStyle)
208 bool const force = currentStyle.forceUpperCase;
209 bool const full = currentStyle.fullAuthorList &&
210 documentBuffer().params().fullAuthorList();
211 bool const textbefore = currentStyle.textBefore;
212 bool const textafter = currentStyle.textAfter;
214 bool const haveSelection =
215 selectedLV->model()->rowCount() > 0;
217 forceuppercaseCB->setEnabled(force && haveSelection);
218 fulllistCB->setEnabled(full && haveSelection);
219 textBeforeED->setEnabled(textbefore && haveSelection);
220 textBeforeLA->setEnabled(textbefore && haveSelection);
221 textAfterED->setEnabled(textafter && haveSelection);
222 textAfterLA->setEnabled(textafter && haveSelection);
223 citationStyleCO->setEnabled(haveSelection);
224 citationStyleLA->setEnabled(haveSelection);
228 // Update the styles for the style combo, citationStyleCO, and mark the
229 // settings as changed. Called upon changing the cited keys (including
230 // merely reordering the keys) or editing the text before/after fields.
231 void GuiCitation::updateStyles()
233 BiblioInfo const & bi = bibInfo();
239 // Update the styles for the style combo, citationStyleCO.
240 void GuiCitation::updateStyles(BiblioInfo const & bi)
242 QStringList selected_keys = selected_model_.stringList();
243 int curr = selectedLV->model()->rowCount() - 1;
245 if (curr < 0 || selected_keys.empty()) {
246 citationStyleCO->clear();
247 citationStyleCO->setEnabled(false);
248 citationStyleLA->setEnabled(false);
252 if (!selectedLV->selectionModel()->selectedIndexes().empty())
253 curr = selectedLV->selectionModel()->selectedIndexes()[0].row();
255 QStringList sty = citationStyles(bi);
259 citationStyleCO->setEnabled(false);
260 citationStyleLA->setEnabled(false);
261 citationStyleCO->clear();
265 citationStyleCO->blockSignals(true);
268 int const oldIndex = citationStyleCO->currentIndex();
269 citationStyleCO->clear();
270 citationStyleCO->insertItems(0, sty);
271 citationStyleCO->setEnabled(true);
272 citationStyleLA->setEnabled(true);
274 if (oldIndex != -1 && oldIndex < citationStyleCO->count())
275 citationStyleCO->setCurrentIndex(oldIndex);
277 citationStyleCO->blockSignals(false);
281 void GuiCitation::fillFields(BiblioInfo const & bi)
283 fieldsCO->blockSignals(true);
284 int const oldIndex = fieldsCO->currentIndex();
286 QStringList const fields = to_qstring_list(bi.getFields());
287 fieldsCO->insertItem(0, qt_("All fields"));
288 fieldsCO->insertItem(1, qt_("Keys"));
289 fieldsCO->insertItems(2, fields);
290 if (oldIndex != -1 && oldIndex < fieldsCO->count())
291 fieldsCO->setCurrentIndex(oldIndex);
292 fieldsCO->blockSignals(false);
296 void GuiCitation::fillEntries(BiblioInfo const & bi)
298 entriesCO->blockSignals(true);
299 int const oldIndex = entriesCO->currentIndex();
301 QStringList const entries = to_qstring_list(bi.getEntries());
302 entriesCO->insertItem(0, qt_("All entry types"));
303 entriesCO->insertItems(1, entries);
304 if (oldIndex != -1 && oldIndex < entriesCO->count())
305 entriesCO->setCurrentIndex(oldIndex);
306 entriesCO->blockSignals(false);
310 bool GuiCitation::isSelected(QModelIndex const & idx)
312 QString const str = idx.data().toString();
313 return selected_model_.stringList().contains(str);
317 void GuiCitation::setButtons()
319 int const srows = selectedLV->model()->rowCount();
320 applyPB->setEnabled(srows > 0);
321 okPB->setEnabled(srows > 0);
325 void GuiCitation::updateInfo(BiblioInfo const & bi, QModelIndex const & idx)
327 if (!idx.isValid() || bi.empty()) {
328 infoML->document()->clear();
332 QString const keytxt = toqstr(
333 bi.getInfo(qstring_to_ucs4(idx.data().toString()), documentBuffer(), true));
334 infoML->document()->setHtml(keytxt);
338 void GuiCitation::findText(QString const & text, bool reset)
340 //"All Fields" and "Keys" are the first two
341 int index = fieldsCO->currentIndex() - 2;
342 BiblioInfo const & bi = bibInfo();
343 vector<docstring> const & fields = bi.getFields();
346 if (index <= -1 || index >= int(fields.size()))
347 //either "All Fields" or "Keys" or an invalid value
348 field = from_ascii("");
350 field = fields[index];
353 bool const onlyKeys = index == -1;
355 //"All Entry Types" is first.
356 index = entriesCO->currentIndex() - 1;
357 vector<docstring> const & entries = bi.getEntries();
358 docstring entry_type;
359 if (index < 0 || index >= int(entries.size()))
360 entry_type = from_ascii("");
362 entry_type = entries[index];
364 bool const case_sentitive = caseCB->checkState();
365 bool const reg_exp = regexCB->checkState();
366 findKey(bi, text, onlyKeys, field, entry_type,
367 case_sentitive, reg_exp, reset);
369 //It'd be nice to save and restore the current selection in
370 //availableLV. Currently, we get an automatic reset, since the
377 void GuiCitation::on_fieldsCO_currentIndexChanged(int /*index*/)
379 findText(findLE->text(), true);
383 void GuiCitation::on_entriesCO_currentIndexChanged(int /*index*/)
385 findText(findLE->text(), true);
389 void GuiCitation::on_citationStyleCO_currentIndexChanged(int index)
391 if (index >= 0 && index < citationStyleCO->count()) {
392 vector<CitationStyle> const & styles = citeStyles_;
393 updateFormatting(styles[index]);
399 void GuiCitation::on_findLE_textChanged(const QString & text)
401 bool const searchAsWeGo = (asTypeCB->checkState() == Qt::Checked);
402 searchPB->setDisabled(text.isEmpty() || searchAsWeGo);
403 if (!text.isEmpty()) {
405 findText(findLE->text());
408 findText(findLE->text());
412 void GuiCitation::on_searchPB_clicked()
414 findText(findLE->text(), true);
418 void GuiCitation::on_caseCB_stateChanged(int)
420 findText(findLE->text());
424 void GuiCitation::on_regexCB_stateChanged(int)
426 findText(findLE->text());
430 void GuiCitation::on_asTypeCB_stateChanged(int)
432 bool const searchAsWeGo = (asTypeCB->checkState() == Qt::Checked);
433 searchPB->setDisabled(findLE->text().isEmpty() || searchAsWeGo);
435 findText(findLE->text(), true);
439 void GuiCitation::changed()
445 void GuiCitation::apply(int const choice, bool full, bool force,
446 QString before, QString after)
448 if (cited_keys_.isEmpty())
451 vector<CitationStyle> const & styles = citeStyles_;
453 CitationStyle cs = styles[choice];
460 cs.forceUpperCase &= force;
461 cs.fullAuthorList &= full;
462 string const command = citationStyleToString(cs);
464 params_.setCmdName(command);
465 params_["key"] = qstring_to_ucs4(cited_keys_.join(","));
466 params_["before"] = qstring_to_ucs4(before);
467 params_["after"] = qstring_to_ucs4(after);
472 void GuiCitation::clearSelection()
475 selected_model_.setStringList(cited_keys_);
479 void GuiCitation::init()
481 // Make the list of all available bibliography keys
482 BiblioInfo const & bi = bibInfo();
483 all_keys_ = to_qstring_list(bi.getKeys());
484 available_model_.setStringList(all_keys_);
486 // Ditto for the keys cited in this inset
487 QString str = toqstr(params_["key"]);
491 cited_keys_ = str.split(",");
492 selected_model_.setStringList(cited_keys_);
494 // Initialize the drop downs
498 // Initialize the citation formatting
499 string const & cmd = params_.getCmdName();
500 CitationStyle const cs = citationStyleFromString(cmd);
501 forceuppercaseCB->setChecked(cs.forceUpperCase);
502 fulllistCB->setChecked(cs.fullAuthorList &&
503 documentBuffer().params().fullAuthorList());
504 textBeforeED->setText(toqstr(params_["before"]));
505 textAfterED->setText(toqstr(params_["after"]));
507 // Update the interface
510 if (selected_model_.rowCount()) {
511 selectedLV->blockSignals(true);
512 selectedLV->setFocus();
513 QModelIndex idx = selected_model_.index(0, 0);
514 selectedLV->selectionModel()->select(idx,
515 QItemSelectionModel::ClearAndSelect);
516 selectedLV->blockSignals(false);
518 // Find the citation style
519 vector<string> const & cmds = citeCmds_;
520 vector<string>::const_iterator cit =
521 std::find(cmds.begin(), cmds.end(), cs.cmd);
523 if (cit != cmds.end())
524 i = int(cit - cmds.begin());
526 // Set the style combo appropriately
527 citationStyleCO->blockSignals(true);
528 citationStyleCO->setCurrentIndex(i);
529 citationStyleCO->blockSignals(false);
530 updateFormatting(citeStyles_[i]);
532 availableLV->setFocus();
534 applyPB->setEnabled(false);
535 okPB->setEnabled(false);
539 void GuiCitation::findKey(BiblioInfo const & bi,
540 QString const & str, bool only_keys,
541 docstring field, docstring entry_type,
542 bool case_sensitive, bool reg_exp, bool reset)
545 // Used for optimisation: store last searched string.
546 static QString last_searched_string;
547 // Used to disable the above optimisation.
548 static bool last_case_sensitive;
549 static bool last_reg_exp;
550 // Reset last_searched_string in case of changed option.
551 if (last_case_sensitive != case_sensitive
552 || last_reg_exp != reg_exp) {
553 LYXERR(Debug::GUI, "GuiCitation::findKey: optimisation disabled!");
554 last_searched_string.clear();
556 // save option for next search.
557 last_case_sensitive = case_sensitive;
558 last_reg_exp = reg_exp;
560 Qt::CaseSensitivity qtcase = case_sensitive ?
561 Qt::CaseSensitive: Qt::CaseInsensitive;
563 // If new string (str) contains the last searched one...
565 !last_searched_string.isEmpty() &&
567 str.contains(last_searched_string, qtcase))
568 // ... then only search within already found list.
569 keys = available_model_.stringList();
571 // ... else search all keys.
573 // save searched string for next search.
574 last_searched_string = str;
578 // First, filter by entry_type, which will be faster than
579 // what follows, so we may get to do that on less.
580 vector<docstring> keyVector = to_docstring_vector(keys);
581 filterByEntryType(bi, keyVector, entry_type);
584 result = to_qstring_list(keyVector);
586 result = to_qstring_list(searchKeys(bi, keyVector, only_keys,
587 qstring_to_ucs4(str), field, case_sensitive, reg_exp));
589 available_model_.setStringList(result);
593 QStringList GuiCitation::citationStyles(BiblioInfo const & bi)
595 docstring const before = qstring_to_ucs4(textBeforeED->text());
596 docstring const after = qstring_to_ucs4(textAfterED->text());
597 vector<docstring> const keys = to_docstring_vector(cited_keys_);
598 vector<CitationStyle> styles = citeStyles_;
599 // FIXME: pass a dictionary instead of individual before, after, dialog, etc.
600 vector<docstring> ret = bi.getCiteStrings(keys, styles, documentBuffer(),
601 false, before, after, from_utf8("dialog"));
602 return to_qstring_list(ret);
606 void GuiCitation::setCitedKeys()
608 cited_keys_ = selected_model_.stringList();
613 bool GuiCitation::initialiseParams(string const & data)
615 InsetCommand::string2params(data, params_);
616 citeCmds_ = documentBuffer().params().citeCommands();
617 citeStyles_ = documentBuffer().params().citeStyles();
623 void GuiCitation::clearParams()
629 void GuiCitation::filterByEntryType(BiblioInfo const & bi,
630 vector<docstring> & keyVector, docstring entry_type)
632 if (entry_type.empty())
635 vector<docstring>::iterator it = keyVector.begin();
636 vector<docstring>::iterator end = keyVector.end();
638 vector<docstring> result;
639 for (; it != end; ++it) {
640 docstring const key = *it;
641 BiblioInfo::const_iterator cit = bi.find(key);
644 if (cit->second.entryType() == entry_type)
645 result.push_back(key);
651 // Escape special chars.
652 // All characters are literals except: '.|*?+(){}[]^$\'
653 // These characters are literals when preceded by a "\", which is done here
654 // @todo: This function should be moved to support, and then the test in tests
655 // should be moved there as well.
656 static docstring escape_special_chars(docstring const & expr)
658 // Search for all chars '.|*?+(){}[^$]\'
659 // Note that '[' and '\' must be escaped.
660 // This is a limitation of lyx::regex, but all other chars in BREs
661 // are assumed literal.
662 static const lyx::regex reg("[].|*?+(){}^$\\[\\\\]");
664 // $& is a perl-like expression that expands to all
665 // of the current match
666 // The '$' must be prefixed with the escape character '\' for
667 // boost to treat it as a literal.
668 // Thus, to prefix a matched expression with '\', we use:
670 return from_utf8(lyx::regex_replace(to_utf8(expr), reg, string("\\\\$&")));
674 vector<docstring> GuiCitation::searchKeys(BiblioInfo const & bi,
675 vector<docstring> const & keys_to_search, bool only_keys,
676 docstring const & search_expression, docstring field,
677 bool case_sensitive, bool regex)
679 vector<docstring> foundKeys;
681 docstring expr = trim(search_expression);
686 // We must escape special chars in the search_expr so that
687 // it is treated as a simple string by lyx::regex.
688 expr = escape_special_chars(expr);
692 reg_exp.assign(to_utf8(expr), case_sensitive ?
693 lyx::regex_constants::ECMAScript : lyx::regex_constants::icase);
694 } catch (lyx::regex_error const & e) {
695 // lyx::regex throws an exception if the regular expression is not
697 LYXERR(Debug::GUI, e.what());
698 return vector<docstring>();
701 vector<docstring>::const_iterator it = keys_to_search.begin();
702 vector<docstring>::const_iterator end = keys_to_search.end();
703 for (; it != end; ++it ) {
704 BiblioInfo::const_iterator info = bi.find(*it);
705 if (info == bi.end())
708 BibTeXInfo const & kvm = info->second;
712 else if (field.empty())
713 data = to_utf8(*it) + ' ' + to_utf8(kvm.allData());
715 data = to_utf8(kvm[field]);
721 if (lyx::regex_search(data, reg_exp))
722 foundKeys.push_back(*it);
724 catch (lyx::regex_error const & e) {
725 LYXERR(Debug::GUI, e.what());
726 return vector<docstring>();
733 void GuiCitation::dispatchParams()
735 std::string const lfun = InsetCommand::params2string(params_);
736 dispatch(FuncRequest(getLfun(), lfun));
740 BiblioInfo const & GuiCitation::bibInfo() const
742 Buffer const & buf = documentBuffer();
743 buf.reloadBibInfoCache();
744 return buf.masterBibInfo();
748 void GuiCitation::saveSession() const
750 Dialog::saveSession();
753 sessionKey() + "/regex", regexCB->isChecked());
755 sessionKey() + "/casesensitive", caseCB->isChecked());
757 sessionKey() + "/autofind", asTypeCB->isChecked());
761 void GuiCitation::restoreSession()
763 Dialog::restoreSession();
766 settings.value(sessionKey() + "/regex").toBool());
768 settings.value(sessionKey() + "/casesensitive").toBool());
769 asTypeCB->setChecked(
770 settings.value(sessionKey() + "/autofind").toBool());
774 Dialog * createGuiCitation(GuiView & lv) { return new GuiCitation(lv); }
777 } // namespace frontend
780 #include "moc_GuiCitation.cpp"