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 selectionManager = new GuiSelectionManager(this, availableLV, selectedLV,
94 addBibPB, deletePB, upPB, downPB, &available_model_, &selected_model_);
95 connect(selectionManager, SIGNAL(selectionChanged()),
96 this, SLOT(databaseChanged()));
97 connect(selectionManager, SIGNAL(updateHook()),
98 this, SLOT(selUpdated()));
99 connect(selectionManager, SIGNAL(okHook()),
100 this, SLOT(on_buttonBox_accepted()));
102 connect(filter_, SIGNAL(rightButtonClicked()),
103 this, SLOT(resetFilter()));
104 connect(filter_, SIGNAL(textEdited(QString)),
105 this, SLOT(filterChanged(QString)));
106 connect(filter_, SIGNAL(returnPressed()),
107 this, SLOT(filterPressed()));
108 #if (QT_VERSION < 0x050000)
109 connect(filter_, SIGNAL(downPressed()),
110 availableLV, SLOT(setFocus()));
112 connect(filter_, &FancyLineEdit::downPressed,
113 availableLV, [=](){ focusAndHighlight(availableLV); });
116 availableLV->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
117 "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
118 "If you want to reuse your own database, this is the place you should store it.")));
120 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
121 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
122 bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
123 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
124 bc().addReadOnly(stylePB);
125 bc().addReadOnly(styleCB);
126 bc().addReadOnly(bibtocCB);
127 bc().addReadOnly(bibEncodingCO);
129 // Always put the default encoding in the first position.
130 bibEncodingCO->addItem(qt_("Document Encoding"), "default");
131 QMap<QString, QString> encodinglist;
132 for (auto const & encvar : encodings) {
133 if (!encvar.unsafe() && !encvar.guiName().empty())
134 encodinglist.insert(qt_(encvar.guiName()), toqstr(encvar.name()));
136 QMap<QString, QString>::const_iterator it = encodinglist.constBegin();
137 while (it != encodinglist.constEnd()) {
138 bibEncodingCO->addItem(it.key(), it.value());
142 setFocusProxy(filter_);
146 void GuiBibtex::init()
148 all_bibs_ = bibFiles(false);
149 available_model_.setStringList(all_bibs_);
151 QString bibs = toqstr(params_["bibfiles"]);
153 selected_bibs_.clear();
155 selected_bibs_ = bibs.split(",");
156 setSelectedBibs(selected_bibs_);
158 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
159 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
160 selectionManager->update();
164 void GuiBibtex::change_adaptor()
171 void GuiBibtex::setButtons()
173 int const srows = selectedLV->model()->rowCount();
174 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(srows > 0);
175 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(srows > 0);
179 void GuiBibtex::selUpdated()
181 selectionManager->update();
186 void GuiBibtex::on_buttonBox_accepted()
194 void GuiBibtex::browseBstPressed()
196 QString const file = browseBst(QString());
201 QString const filen = changeExtension(file, "");
202 bool present = false;
203 unsigned int pres = 0;
205 for (int i = 0; i != styleCB->count(); ++i) {
206 if (styleCB->itemText(i) == filen) {
213 styleCB->insertItem(0, filen);
215 styleCB->setCurrentIndex(pres);
220 void GuiBibtex::browseBibPressed()
222 QString const file = browseBib(QString()).trimmed();
227 QString const f = changeExtension(file, "");
229 if (!selected_bibs_.contains(f)) {
230 selected_bibs_.append(f);
231 setSelectedBibs(selected_bibs_);
237 void GuiBibtex::rescanClicked()
244 void GuiBibtex::clearSelection()
246 selected_bibs_.clear();
247 setSelectedBibs(selected_bibs_);
251 void GuiBibtex::setSelectedBibs(QStringList const sl)
253 selected_model_.clear();
254 QStringList::const_iterator it = sl.begin();
255 QStringList::const_iterator end = sl.end();
256 for (int i = 0; it != end; ++it, ++i) {
257 QStandardItem * si = new QStandardItem();
261 si->setEditable(false);
262 selected_model_.insertRow(i, si);
267 QStringList GuiBibtex::selectedBibs()
270 for (int i = 0; i != selected_model_.rowCount(); ++i) {
271 QStandardItem const * item = selected_model_.item(i);
273 res.append(item->text());
279 void GuiBibtex::databaseChanged()
281 QString const item = selectionManager->getSelectedIndex().data().toString();
282 if (!selected_bibs_.contains(item)) {
283 selected_bibs_.append(item);
285 selected_bibs_ = selectedBibs();
286 setSelectedBibs(selected_bibs_);
290 void GuiBibtex::updateContents()
292 bool bibtopic = usingBibtopic();
293 bool biblatex = usingBiblatex();
296 setTitle(qt_("Biblatex Bibliography"));
298 setTitle(qt_("BibTeX Bibliography"));
300 QString const bibstyle = styleFile();
302 bibtocCB->setChecked(bibtotoc() && !bibtopic);
303 bibtocCB->setEnabled(!bibtopic);
306 btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
308 btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
309 btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
310 if (usingBiblatex() && !buffer().masterParams().multibib.empty())
311 btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
313 docstring btprint = params_["btprint"];
316 btprint = from_ascii("btPrintCited");
317 btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
319 docstring encoding = params_["encoding"];
320 if (encoding.empty())
322 encoding = from_ascii("default");
323 bibEncodingCO->setCurrentIndex(bibEncodingCO->findData(toqstr(encoding)));
325 // Only useful for biblatex
326 biblatexOptsLA->setVisible(biblatex);
327 biblatexOptsLE->setVisible(biblatex);
329 // only useful for BibTeX
330 bstGB->setVisible(!biblatex);
337 QStringList const str = bibStyles();
338 for (int i = 0; i != str.count(); ++i) {
339 QString item = changeExtension(str[i], "");
340 if (item == bibstyle)
342 styleCB->addItem(item);
345 if (item_nr == -1 && !bibstyle.isEmpty()) {
346 styleCB->addItem(bibstyle);
347 item_nr = styleCB->count() - 1;
352 styleCB->setCurrentIndex(item_nr);
354 styleCB->clearEditText();
356 biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
360 void GuiBibtex::applyView()
364 unsigned int maxCount = selected_bibs_.count();
365 for (unsigned int i = 0; i < maxCount; i++) {
368 QString item = selected_bibs_.at(i);
369 docstring bibfile = qstring_to_ucs4(item);
373 params_["bibfiles"] = dbs;
375 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
376 bool const bibtotoc = bibtocCB->isChecked();
378 if (bibtotoc && !bibstyle.empty()) {
379 // both bibtotoc and style
380 params_["options"] = "bibtotoc," + bibstyle;
381 } else if (bibtotoc) {
382 // bibtotoc and no style
383 params_["options"] = from_ascii("bibtotoc");
385 // only style. An empty one is valid, because some
386 // documentclasses have an own \bibliographystyle{}
388 params_["options"] = bibstyle;
391 params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
393 params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
395 params_["encoding"] = qstring_to_ucs4(bibEncodingCO->itemData(bibEncodingCO->currentIndex()).toString());
399 QString GuiBibtex::browseBib(QString const & in_name) const
401 QString const label1 = qt_("D&ocuments");
402 QString const dir1 = toqstr(lyxrc.document_path);
403 QStringList const filter(qt_("BibTeX Databases (*.bib)"));
404 return browseRelToParent(in_name, bufferFilePath(),
405 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
409 QString GuiBibtex::browseBst(QString const & in_name) const
411 QString const label1 = qt_("D&ocuments");
412 QString const dir1 = toqstr(lyxrc.document_path);
413 QStringList const filter(qt_("BibTeX Styles (*.bst)"));
414 return browseRelToParent(in_name, bufferFilePath(),
415 qt_("Select a BibTeX style"), filter, false, label1, dir1);
419 QStringList GuiBibtex::bibStyles() const
421 QStringList sdata = texFileList("bstFiles.lst");
422 // test whether we have a valid list, otherwise run rescan
423 if (sdata.isEmpty()) {
425 sdata = texFileList("bstFiles.lst");
427 for (int i = 0; i != sdata.size(); ++i)
428 sdata[i] = onlyFileName(sdata[i]);
429 // sort on filename only (no path)
435 QStringList GuiBibtex::bibFiles(bool const extension) const
437 QStringList sdata = texFileList("bibFiles.lst");
438 // test whether we have a valid list, otherwise run rescan
439 if (sdata.isEmpty()) {
441 sdata = texFileList("bibFiles.lst");
443 for (int i = 0; i != sdata.size(); ++i)
444 sdata[i] = extension ? onlyFileName(sdata[i])
445 : changeExtension(onlyFileName(sdata[i]), "");
446 // sort on filename only (no path)
452 void GuiBibtex::rescanBibStyles() const
455 rescanTexStyles("bib");
457 rescanTexStyles("bst bib");
461 void GuiBibtex::findText(QString const & text)
463 QStringList const result = bibFiles(false).filter(text);
464 available_model_.setStringList(result);
468 void GuiBibtex::filterChanged(const QString & text)
470 if (!text.isEmpty()) {
471 findText(filter_->text());
474 findText(filter_->text());
479 void GuiBibtex::filterPressed()
481 findText(filter_->text());
485 void GuiBibtex::resetFilter()
487 filter_->setText(QString());
488 findText(filter_->text());
493 bool GuiBibtex::usingBibtopic() const
495 return buffer().params().useBibtopic();
499 bool GuiBibtex::bibtotoc() const
501 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
505 bool GuiBibtex::usingBiblatex() const
507 return buffer().masterBuffer()->params().useBiblatex();
511 QString GuiBibtex::styleFile() const
513 // the different bibtex packages have (and need) their
514 // own "plain" stylefiles
515 QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
517 QString bst = toqstr(params_["options"]);
520 int pos = bst.indexOf(',');
523 // docstring bibtotoc = from_ascii("bibtotoc");
524 // bst = split(bst, bibtotoc, ',');
525 bst = bst.mid(pos + 1);
531 // propose default style file for new insets
532 // existing insets might have (legally) no bst files
533 // (if the class already provides a style)
534 if (bst.isEmpty() && params_["bibfiles"].empty())
541 bool GuiBibtex::initialiseParams(std::string const & sdata)
543 InsetCommand::string2params(sdata, params_);
549 void GuiBibtex::dispatchParams()
551 std::string const lfun = InsetCommand::params2string(params_);
552 dispatch(FuncRequest(getLfun(), lfun));
556 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
559 } // namespace frontend
562 #include "moc_GuiBibtex.cpp"