3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Angus Leeming
9 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
16 #include "GuiBibtex.h"
19 #include "BufferParams.h"
20 #include "CiteEnginesList.h"
22 #include "FuncRequest.h"
23 #include "GuiApplication.h"
25 #include "qt_helpers.h"
26 #include "Validator.h"
28 #include "ButtonPolicy.h"
30 #include "frontends/alert.h"
32 #include "insets/InsetBibtex.h"
34 #include "support/debug.h"
35 #include "support/ExceptionMessage.h"
36 #include "support/FileName.h"
37 #include "support/filetools.h" // changeExtension
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
41 #include <QDialogButtonBox>
42 #include <QPushButton>
43 #include <QListWidget>
48 using namespace lyx::support;
54 GuiBibtex::GuiBibtex(GuiView & lv)
55 : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
56 params_(insetCode("bibtex"))
60 QDialog::setModal(true);
61 setWindowModality(Qt::WindowModal);
64 filter_ = new FancyLineEdit(this);
65 filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
66 filter_->setButtonVisible(FancyLineEdit::Right, true);
67 filter_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
68 filter_->setAutoHideButton(FancyLineEdit::Right, true);
69 filter_->setPlaceholderText(qt_("All avail. databases"));
71 filterBarL->addWidget(filter_, 0);
72 findKeysLA->setBuddy(filter_);
74 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
75 this, SLOT(slotButtonBox(QAbstractButton *)));
76 connect(stylePB, SIGNAL(clicked()),
77 this, SLOT(browseBstPressed()));
78 connect(styleCB, SIGNAL(editTextChanged(QString)),
79 this, SLOT(change_adaptor()));
80 connect(bibtocCB, SIGNAL(clicked()),
81 this, SLOT(change_adaptor()));
82 connect(btPrintCO, SIGNAL(activated(int)),
83 this, SLOT(change_adaptor()));
84 connect(rescanPB, SIGNAL(clicked()),
85 this, SLOT(rescanClicked()));
86 connect(biblatexOptsLE, SIGNAL(textChanged(QString)),
87 this, SLOT(change_adaptor()));
88 connect(bibEncodingCO, SIGNAL(activated(int)),
89 this, SLOT(change_adaptor()));
90 connect(browseBibPB, SIGNAL(clicked()),
91 this, SLOT(browseBibPressed()));
93 selected_model_.insertColumns(0, 1);
94 selectionManager = new GuiSelectionManager(this, availableLV, selectedLV,
95 addBibPB, deletePB, upPB, downPB, &available_model_, &selected_model_);
96 connect(selectionManager, SIGNAL(selectionChanged()),
97 this, SLOT(databaseChanged()));
98 connect(selectionManager, SIGNAL(updateHook()),
99 this, SLOT(selUpdated()));
100 connect(selectionManager, SIGNAL(okHook()),
101 this, SLOT(on_buttonBox_accepted()));
103 connect(filter_, SIGNAL(rightButtonClicked()),
104 this, SLOT(resetFilter()));
105 connect(filter_, SIGNAL(textEdited(QString)),
106 this, SLOT(filterChanged(QString)));
107 connect(filter_, SIGNAL(returnPressed()),
108 this, SLOT(filterPressed()));
109 #if (QT_VERSION < 0x050000)
110 connect(filter_, SIGNAL(downPressed()),
111 availableLV, SLOT(setFocus()));
113 connect(filter_, &FancyLineEdit::downPressed,
114 availableLV, [=](){ focusAndHighlight(availableLV); });
117 availableLV->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
118 "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
119 "If you want to reuse your own database, this is the place you should store it.")));
121 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
122 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
123 bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
124 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
125 bc().addReadOnly(stylePB);
126 bc().addReadOnly(styleCB);
127 bc().addReadOnly(bibtocCB);
128 bc().addReadOnly(bibEncodingCO);
130 #if (QT_VERSION < 0x050000)
131 selectedLV->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
133 selectedLV->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
136 // Always put the default encoding in the first position.
137 bibEncodingCO->addItem(qt_("Document Encoding"), "default");
138 for (auto const & encvar : encodings) {
139 if (!encvar.unsafe() && !encvar.guiName().empty())
140 encodings_.insert(qt_(encvar.guiName()), toqstr(encvar.name()));
142 QMap<QString, QString>::const_iterator it = encodings_.constBegin();
143 while (it != encodings_.constEnd()) {
144 bibEncodingCO->addItem(it.key(), it.value());
148 setFocusProxy(filter_);
152 void GuiBibtex::init()
154 all_bibs_ = bibFiles(false);
155 available_model_.setStringList(all_bibs_);
157 QString bibs = toqstr(params_["bibfiles"]);
159 selected_bibs_.clear();
161 selected_bibs_ = bibs.split(",");
162 setSelectedBibs(selected_bibs_);
164 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
165 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
166 selectionManager->update();
170 void GuiBibtex::change_adaptor()
177 void GuiBibtex::setButtons()
179 int const srows = selectedLV->model()->rowCount();
180 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(srows > 0);
181 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(srows > 0);
185 void GuiBibtex::selUpdated()
187 selectionManager->update();
192 void GuiBibtex::on_buttonBox_accepted()
200 void GuiBibtex::browseBstPressed()
202 QString const file = browseBst(QString());
207 QString const filen = changeExtension(file, "");
208 bool present = false;
211 for (int i = 0; i != styleCB->count(); ++i) {
212 if (styleCB->itemText(i) == filen) {
219 styleCB->insertItem(0, filen);
221 styleCB->setCurrentIndex(pres);
226 void GuiBibtex::browseBibPressed()
228 QString const file = browseBib(QString()).trimmed();
233 QString const f = changeExtension(file, "");
235 if (!selected_bibs_.contains(f)) {
236 selected_bibs_.append(f);
237 setSelectedBibs(selected_bibs_);
243 void GuiBibtex::rescanClicked()
250 void GuiBibtex::clearSelection()
252 selected_bibs_.clear();
253 setSelectedBibs(selected_bibs_);
257 void GuiBibtex::setSelectedBibs(QStringList const sl)
259 selected_model_.clear();
261 headers << qt_("Database")
262 << qt_("File Encoding");
263 selected_model_.setHorizontalHeaderLabels(headers);
264 bool const moreencs = usingBiblatex() && sl.count() > 1;
265 selectedLV->setColumnHidden(1, !moreencs);
266 selectedLV->verticalHeader()->setVisible(false);
267 selectedLV->horizontalHeader()->setVisible(moreencs);
269 bibEncodingLA->setText(qt_("General E&ncoding:"));
270 bibEncodingCO->setToolTip(qt_("If your bibliography databases use a different "
271 "encoding than the LyX document, specify it here. "
272 "If indivivual databases have different encodings, "
273 "you can set it in the list above."));
275 bibEncodingLA->setText(qt_("E&ncoding:"));
276 bibEncodingCO->setToolTip(qt_("If your bibliography databases use a different "
277 "encoding than the LyX document, specify it here"));
279 QStringList::const_iterator it = sl.begin();
280 QStringList::const_iterator end = sl.end();
281 for (int i = 0; it != end; ++it, ++i) {
282 QStandardItem * si = new QStandardItem();
286 si->setEditable(false);
287 selected_model_.insertRow(i, si);
288 QComboBox * cb = new QComboBox;
289 cb->addItem(qt_("General Encoding"), "general");
290 cb->addItem(qt_("Document Encoding"), "auto");
291 QMap<QString, QString>::const_iterator it = encodings_.constBegin();
292 while (it != encodings_.constEnd()) {
293 cb->addItem(it.key(), it.value());
296 cb->setToolTip(qt_("If this bibliography database uses a different "
297 "encoding than specified below, set it here"));
298 selectedLV->setIndexWidget(selected_model_.index(i, 1), cb);
303 QStringList GuiBibtex::selectedBibs()
306 for (int i = 0; i != selected_model_.rowCount(); ++i) {
307 QStandardItem const * item = selected_model_.item(i);
309 res.append(item->text());
315 void GuiBibtex::databaseChanged()
317 QString const item = selectionManager->getSelectedIndex().data().toString();
318 if (!selected_bibs_.contains(item)) {
319 selected_bibs_.append(item);
321 selected_bibs_ = selectedBibs();
322 setSelectedBibs(selected_bibs_);
326 void GuiBibtex::updateContents()
328 bool bibtopic = usingBibtopic();
329 bool biblatex = usingBiblatex();
332 setTitle(qt_("Biblatex Bibliography"));
334 setTitle(qt_("BibTeX Bibliography"));
336 QString const bibstyle = styleFile();
338 bibtocCB->setChecked(bibtotoc() && !bibtopic);
339 bibtocCB->setEnabled(!bibtopic);
342 btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
344 btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
345 btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
346 if (usingBiblatex() && !buffer().masterParams().multibib.empty())
347 btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
349 docstring btprint = params_["btprint"];
352 btprint = from_ascii("btPrintCited");
353 btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
355 docstring encoding = params_["encoding"];
356 if (encoding.empty())
358 encoding = from_ascii("default");
359 bibEncodingCO->setCurrentIndex(bibEncodingCO->findData(toqstr(encoding)));
361 // Only useful for biblatex
362 biblatexOptsLA->setVisible(biblatex);
363 biblatexOptsLE->setVisible(biblatex);
365 // only useful for BibTeX
366 bstGB->setVisible(!biblatex);
373 QStringList const str = bibStyles();
374 for (int i = 0; i != str.count(); ++i) {
375 QString item = changeExtension(str[i], "");
376 if (item == bibstyle)
378 styleCB->addItem(item);
381 if (item_nr == -1 && !bibstyle.isEmpty()) {
382 styleCB->addItem(bibstyle);
383 item_nr = styleCB->count() - 1;
388 styleCB->setCurrentIndex(item_nr);
390 styleCB->clearEditText();
392 biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
394 setFileEncodings(getVectorFromString(params_["file_encodings"], from_ascii("\t")));
398 void GuiBibtex::applyView()
402 unsigned int maxCount = selected_bibs_.count();
403 for (unsigned int i = 0; i < maxCount; i++) {
406 QString item = selected_bibs_.at(i);
407 docstring bibfile = qstring_to_ucs4(item);
411 params_["bibfiles"] = dbs;
413 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
414 bool const bibtotoc = bibtocCB->isChecked();
416 if (bibtotoc && !bibstyle.empty()) {
417 // both bibtotoc and style
418 params_["options"] = "bibtotoc," + bibstyle;
419 } else if (bibtotoc) {
420 // bibtotoc and no style
421 params_["options"] = from_ascii("bibtotoc");
423 // only style. An empty one is valid, because some
424 // documentclasses have an own \bibliographystyle{}
426 params_["options"] = bibstyle;
429 params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
431 params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
433 params_["encoding"] = qstring_to_ucs4(bibEncodingCO->itemData(bibEncodingCO->currentIndex()).toString());
436 params_["file_encodings"] = getStringFromVector(getFileEncodings(), from_ascii("\t"));
440 QString GuiBibtex::browseBib(QString const & in_name) const
442 QString const label1 = qt_("D&ocuments");
443 QString const dir1 = toqstr(lyxrc.document_path);
444 QStringList const filter(qt_("BibTeX Databases (*.bib)"));
445 return browseRelToParent(in_name, bufferFilePath(),
446 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
450 QString GuiBibtex::browseBst(QString const & in_name) const
452 QString const label1 = qt_("D&ocuments");
453 QString const dir1 = toqstr(lyxrc.document_path);
454 QStringList const filter(qt_("BibTeX Styles (*.bst)"));
455 return browseRelToParent(in_name, bufferFilePath(),
456 qt_("Select a BibTeX style"), filter, false, label1, dir1);
460 QStringList GuiBibtex::bibStyles() const
462 QStringList sdata = texFileList("bstFiles.lst");
463 // test whether we have a valid list, otherwise run rescan
464 if (sdata.isEmpty()) {
466 sdata = texFileList("bstFiles.lst");
468 for (int i = 0; i != sdata.size(); ++i)
469 sdata[i] = onlyFileName(sdata[i]);
470 // sort on filename only (no path)
476 QStringList GuiBibtex::bibFiles(bool const extension) const
478 QStringList sdata = texFileList("bibFiles.lst");
479 // test whether we have a valid list, otherwise run rescan
480 if (sdata.isEmpty()) {
482 sdata = texFileList("bibFiles.lst");
484 for (int i = 0; i != sdata.size(); ++i)
485 sdata[i] = extension ? onlyFileName(sdata[i])
486 : changeExtension(onlyFileName(sdata[i]), "");
487 // sort on filename only (no path)
493 vector<docstring> GuiBibtex::getFileEncodings()
495 vector<docstring> res;
496 for (int i = 0; i != selected_model_.rowCount(); ++i) {
497 QStandardItem const * key = selected_model_.item(i, 0);
498 QComboBox * cb = qobject_cast<QComboBox*>(selectedLV->indexWidget(selected_model_.index(i, 1)));
499 QString fenc = cb ? cb->itemData(cb->currentIndex()).toString() : QString();
500 if (key && !key->text().isEmpty() && !fenc.isEmpty() && fenc != "general")
501 res.push_back(qstring_to_ucs4(key->text()) + " " + qstring_to_ucs4(fenc));
507 void GuiBibtex::setFileEncodings(vector<docstring> const m)
509 for (docstring const & s: m) {
511 QString enc = toqstr(split(s, key, ' '));
512 QModelIndexList qmil =
513 selected_model_.match(selected_model_.index(0, 0),
514 Qt::DisplayRole, toqstr(key), 1,
515 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap));
517 QComboBox * cb = qobject_cast<QComboBox*>(selectedLV->indexWidget(selected_model_.index(qmil.front().row(), 1)));
518 cb->setCurrentIndex(cb->findData(enc));
524 void GuiBibtex::rescanBibStyles() const
527 rescanTexStyles("bib");
529 rescanTexStyles("bst bib");
533 void GuiBibtex::findText(QString const & text)
535 QStringList const result = bibFiles(false).filter(text);
536 available_model_.setStringList(result);
540 void GuiBibtex::filterChanged(const QString & text)
542 if (!text.isEmpty()) {
543 findText(filter_->text());
546 findText(filter_->text());
551 void GuiBibtex::filterPressed()
553 findText(filter_->text());
557 void GuiBibtex::resetFilter()
559 filter_->setText(QString());
560 findText(filter_->text());
565 bool GuiBibtex::usingBibtopic() const
567 return buffer().params().useBibtopic();
571 bool GuiBibtex::bibtotoc() const
573 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
577 bool GuiBibtex::usingBiblatex() const
579 return buffer().masterBuffer()->params().useBiblatex();
583 QString GuiBibtex::styleFile() const
585 // the different bibtex packages have (and need) their
586 // own "plain" stylefiles
587 QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
589 QString bst = toqstr(params_["options"]);
592 int pos = bst.indexOf(',');
595 // docstring bibtotoc = from_ascii("bibtotoc");
596 // bst = split(bst, bibtotoc, ',');
597 bst = bst.mid(pos + 1);
603 // propose default style file for new insets
604 // existing insets might have (legally) no bst files
605 // (if the class already provides a style)
606 if (bst.isEmpty() && params_["bibfiles"].empty())
613 bool GuiBibtex::initialiseParams(std::string const & sdata)
615 InsetCommand::string2params(sdata, params_);
621 void GuiBibtex::dispatchParams()
623 std::string const lfun = InsetCommand::params2string(params_);
624 dispatch(FuncRequest(getLfun(), lfun));
628 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
631 } // namespace frontend
634 #include "moc_GuiBibtex.cpp"