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 connect(citationStyleCO, SIGNAL(activated(int)),
78 this, SLOT(changed()));
79 connect(fulllistCB, SIGNAL(clicked()),
80 this, SLOT(changed()));
81 connect(forceuppercaseCB, SIGNAL(clicked()),
82 this, SLOT(changed()));
83 connect(textBeforeED, SIGNAL(textChanged(const QString&)),
84 this, SLOT(changed()));
85 connect(textAfterED, SIGNAL(textChanged(const QString&)),
86 this, SLOT(changed()));
87 connect(clearPB, SIGNAL(clicked()),
88 findLE, SLOT(clear()));
89 connect(this, SIGNAL(rejected()), this, SLOT(cleanUp()));
92 new GuiSelectionManager(availableLV, selectedLV,
93 addPB, deletePB, upPB, downPB, available(), selected());
94 connect(selectionManager, SIGNAL(selectionChanged()),
95 this, SLOT(setCitedKeys()));
96 connect(selectionManager, SIGNAL(updateHook()),
97 this, SLOT(updateDialog()));
98 connect(selectionManager, SIGNAL(okHook()),
99 this, SLOT(on_okPB_clicked()));
101 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
105 ControlCitation & GuiCitationDialog::controller()
107 return static_cast<ControlCitation &>(GuiDialog::controller());
111 void GuiCitationDialog::cleanUp()
114 controller().clearParams();
119 void GuiCitationDialog::closeEvent(QCloseEvent * e)
122 controller().clearParams();
127 void GuiCitationDialog::applyView()
129 int const choice = std::max(0, citationStyleCO->currentIndex());
131 bool const full = fulllistCB->isChecked();
132 bool const force = forceuppercaseCB->isChecked();
134 QString const before = textBeforeED->text();
135 QString const after = textAfterED->text();
137 apply(choice, full, force, before, after);
141 void GuiCitationDialog::hideView()
143 controller().clearParams();
148 void GuiCitationDialog::showView()
151 availableLV->setFocus();
158 bool GuiCitationDialog::isVisibleView() const
160 return QDialog::isVisible();
164 void GuiCitationDialog::on_okPB_clicked()
172 void GuiCitationDialog::on_cancelPB_clicked()
179 void GuiCitationDialog::on_applyPB_clicked()
185 void GuiCitationDialog::on_restorePB_clicked()
192 void GuiCitationDialog::updateView()
200 // The main point of separating this out is that the fill*() methods
201 // called in update() do not need to be called for INTERNAL updates,
202 // such as when addPB is pressed, as the list of fields, entries, etc,
203 // will not have changed. At the moment, however, the division between
204 // fillStyles() and updateStyles() doesn't lend itself to dividing the
205 // two methods, though they should be divisible.
206 void GuiCitationDialog::updateDialog()
208 if (selectionManager->selectedFocused()) {
209 if (selectedLV->selectionModel()->selectedIndexes().isEmpty())
210 updateInfo(availableLV->currentIndex());
212 updateInfo(selectedLV->currentIndex());
214 if (availableLV->selectionModel()->selectedIndexes().isEmpty())
215 updateInfo(QModelIndex());
217 updateInfo(availableLV->currentIndex());
221 textBeforeED->setText(textBefore());
222 textAfterED->setText(textAfter());
228 void GuiCitationDialog::updateStyle()
230 biblio::CiteEngine const engine = controller().getEngine();
231 bool const natbib_engine =
232 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
233 engine == biblio::ENGINE_NATBIB_NUMERICAL;
234 bool const basic_engine = engine == biblio::ENGINE_BASIC;
236 bool const haveSelection =
237 selectedLV->model()->rowCount() > 0;
238 fulllistCB->setEnabled(natbib_engine && haveSelection);
239 forceuppercaseCB->setEnabled(natbib_engine && haveSelection);
240 textBeforeED->setEnabled(!basic_engine && haveSelection);
241 textBeforeLA->setEnabled(!basic_engine && haveSelection);
242 textAfterED->setEnabled(haveSelection);
243 textAfterLA->setEnabled(haveSelection);
244 citationStyleCO->setEnabled(!basic_engine && haveSelection);
245 citationStyleLA->setEnabled(!basic_engine && haveSelection);
247 string const & command = controller().params().getCmdName();
249 // Find the style of the citekeys
250 vector<biblio::CiteStyle> const & styles =
251 ControlCitation::getCiteStyles();
252 biblio::CitationStyle const cs(command);
254 vector<biblio::CiteStyle>::const_iterator cit =
255 std::find(styles.begin(), styles.end(), cs.style);
257 // restore the latest natbib style
258 if (style_ >= 0 && style_ < citationStyleCO->count())
259 citationStyleCO->setCurrentIndex(style_);
261 citationStyleCO->setCurrentIndex(0);
263 if (cit != styles.end()) {
264 int const i = int(cit - styles.begin());
265 citationStyleCO->setCurrentIndex(i);
266 fulllistCB->setChecked(cs.full);
267 forceuppercaseCB->setChecked(cs.forceUCase);
269 fulllistCB->setChecked(false);
270 forceuppercaseCB->setChecked(false);
275 //This one needs to be called whenever citationStyleCO needs
276 //to be updated---and this would be on anything that changes the
277 //selection in selectedLV, or on a general update.
278 void GuiCitationDialog::fillStyles()
280 int const oldIndex = citationStyleCO->currentIndex();
282 citationStyleCO->clear();
284 QStringList selected_keys = selected()->stringList();
285 if (selected_keys.empty()) {
286 citationStyleCO->setEnabled(false);
287 citationStyleLA->setEnabled(false);
291 int curr = selectedLV->model()->rowCount() - 1;
295 if (!selectedLV->selectionModel()->selectedIndexes().empty())
296 curr = selectedLV->selectionModel()->selectedIndexes()[0].row();
298 QStringList sty = citationStyles(curr);
300 bool const basic_engine =
301 (controller().getEngine() == biblio::ENGINE_BASIC);
303 citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
304 citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
306 if (sty.isEmpty() || basic_engine)
309 citationStyleCO->insertItems(0, sty);
311 if (oldIndex != -1 && oldIndex < citationStyleCO->count())
312 citationStyleCO->setCurrentIndex(oldIndex);
316 void GuiCitationDialog::fillFields()
318 fieldsCO->blockSignals(true);
319 int const oldIndex = fieldsCO->currentIndex();
321 QStringList const & fields = getFieldsAsQStringList();
322 fieldsCO->insertItem(0, qt_("All Fields"));
323 fieldsCO->insertItem(1, qt_("Keys"));
324 fieldsCO->insertItems(2, fields);
325 if (oldIndex != -1 && oldIndex < fieldsCO->count())
326 fieldsCO->setCurrentIndex(oldIndex);
327 fieldsCO->blockSignals(false);
331 void GuiCitationDialog::fillEntries()
333 entriesCO->blockSignals(true);
334 int const oldIndex = entriesCO->currentIndex();
336 QStringList const & entries = getEntriesAsQStringList();
337 entriesCO->insertItem(0, qt_("All Entry Types"));
338 entriesCO->insertItems(1, entries);
339 if (oldIndex != -1 && oldIndex < entriesCO->count())
340 entriesCO->setCurrentIndex(oldIndex);
341 entriesCO->blockSignals(false);
345 bool GuiCitationDialog::isSelected(const QModelIndex & idx)
347 QString const str = idx.data().toString();
348 return selected()->stringList().contains(str);
352 void GuiCitationDialog::setButtons()
354 selectionManager->updateView();
355 int const srows = selectedLV->model()->rowCount();
356 applyPB->setEnabled(srows > 0);
357 okPB->setEnabled(srows > 0);
361 void GuiCitationDialog::updateInfo(QModelIndex const & idx)
364 QString const keytxt = getKeyInfo(idx.data().toString());
365 infoML->document()->setPlainText(keytxt);
367 infoML->document()->clear();
371 void GuiCitationDialog::findText(QString const & text, bool reset)
373 //"All Fields" and "Keys" are the first two
374 int index = fieldsCO->currentIndex() - 2;
375 vector<docstring> const & fields = controller().availableFields();
378 if (index <= -1 || index >= int(fields.size()))
379 //either "All Fields" or "Keys" or an invalid value
380 field = from_ascii("");
382 field = fields[index];
385 bool const onlyKeys = index == -1;
387 //"All Entry Types" is first.
388 index = entriesCO->currentIndex() - 1;
389 vector<docstring> const & entries = controller().availableEntries();
391 if (index < 0 || index >= int(entries.size()))
392 entryType = from_ascii("");
394 entryType = entries[index];
396 bool const case_sentitive = caseCB->checkState();
397 bool const reg_exp = regexCB->checkState();
398 findKey(text, onlyKeys, field, entryType,
399 case_sentitive, reg_exp, reset);
401 //It'd be nice to save and restore the current selection in
402 //availableLV. Currently, we get an automatic reset, since the
409 void GuiCitationDialog::on_fieldsCO_currentIndexChanged(int /*index*/)
411 findText(findLE->text(), true);
415 void GuiCitationDialog::on_entriesCO_currentIndexChanged(int /*index*/)
417 findText(findLE->text(), true);
421 void GuiCitationDialog::on_findLE_textChanged(const QString & text)
423 clearPB->setDisabled(text.isEmpty());
430 void GuiCitationDialog::on_caseCB_stateChanged(int)
432 findText(findLE->text());
436 void GuiCitationDialog::on_regexCB_stateChanged(int)
438 findText(findLE->text());
442 void GuiCitationDialog::changed()
449 void GuiCitationDialog::apply(int const choice,
450 bool const full, bool const force,
451 QString before, QString after)
453 if (cited_keys_.isEmpty())
456 vector<biblio::CiteStyle> const & styles =
457 ControlCitation::getCiteStyles();
459 string const command =
460 biblio::CitationStyle(styles[choice], full, force)
463 controller().params().setCmdName(command);
464 controller().params()["key"] = qstring_to_ucs4(cited_keys_.join(","));
465 controller().params()["before"] = qstring_to_ucs4(before);
466 controller().params()["after"] = qstring_to_ucs4(after);
467 controller().dispatchParams();
471 void GuiCitationDialog::clearSelection()
474 selected_model_.setStringList(cited_keys_);
478 QString GuiCitationDialog::textBefore()
480 return toqstr(controller().params()["before"]);
484 QString GuiCitationDialog::textAfter()
486 return toqstr(controller().params()["after"]);
490 bool GuiCitationDialog::initialiseParams(std::string const & data)
492 if (!controller().initialiseParams(data))
499 void GuiCitationDialog::init()
501 // Make the list of all available bibliography keys
502 all_keys_ = to_qstring_list(controller().availableKeys());
503 available_model_.setStringList(all_keys_);
505 // Ditto for the keys cited in this inset
506 QString str = toqstr(controller().params()["key"]);
510 cited_keys_ = str.split(",");
511 selected_model_.setStringList(cited_keys_);
515 void GuiCitationDialog::findKey(QString const & str, bool only_keys,
516 docstring field, docstring entryType,
517 bool case_sensitive, bool reg_exp, bool reset)
519 // Used for optimisation: store last searched string.
520 static QString last_searched_string;
521 // Used to disable the above optimisation.
522 static bool last_case_sensitive;
523 static bool last_reg_exp;
524 // Reset last_searched_string in case of changed option.
525 if (last_case_sensitive != case_sensitive
526 || last_reg_exp != reg_exp) {
527 LYXERR(Debug::GUI) << "GuiCitation::findKey: optimisation disabled!" << std::endl;
528 last_searched_string.clear();
530 // save option for next search.
531 last_case_sensitive = case_sensitive;
532 last_reg_exp = reg_exp;
534 Qt::CaseSensitivity qtcase = case_sensitive?
535 Qt::CaseSensitive: Qt::CaseInsensitive;
537 // If new string (str) contains the last searched one...
539 !last_searched_string.isEmpty() &&
541 str.contains(last_searched_string, qtcase))
542 // ... then only search within already found list.
543 keys = available_model_.stringList();
545 // ... else search all keys.
547 // save searched string for next search.
548 last_searched_string = str;
552 // First, filter by entryType, which will be faster than
553 // what follows, so we may get to do that on less.
554 vector<docstring> keyVector = to_docstring_vector(keys);
555 controller().filterByEntryType(keyVector, entryType);
558 result = to_qstring_list(keyVector);
560 result = to_qstring_list(controller().searchKeys(keyVector, only_keys,
561 qstring_to_ucs4(str), field, case_sensitive, reg_exp));
563 available_model_.setStringList(result);
567 QStringList GuiCitationDialog::getFieldsAsQStringList()
569 return to_qstring_list(controller().availableFields());
573 QStringList GuiCitationDialog::getEntriesAsQStringList()
575 return to_qstring_list(controller().availableEntries());
579 QStringList GuiCitationDialog::citationStyles(int sel)
581 docstring const key = qstring_to_ucs4(cited_keys_[sel]);
582 return to_qstring_list(controller().getCiteStrings(key));
586 QString GuiCitationDialog::getKeyInfo(QString const & sel)
588 return toqstr(controller().getInfo(qstring_to_ucs4(sel)));
592 void GuiCitationDialog::setCitedKeys()
594 cited_keys_ = selected_model_.stringList();
597 } // namespace frontend
600 #include "GuiCitation_moc.cpp"