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 "ui_BibtexAddUi.h"
21 #include "qt_helpers.h"
22 #include "Validator.h"
25 #include "ButtonPolicy.h"
27 #include "support/debug.h"
28 #include "support/FileFilterList.h"
29 #include "support/filetools.h" // changeExtension
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
33 #include <QPushButton>
34 #include <QListWidget>
39 using namespace lyx::support;
45 GuiBibtex::GuiBibtex(GuiView & lv)
46 : GuiCommand(lv, "bibtex", qt_("BibTeX Bibliography"))
50 QDialog::setModal(true);
52 connect(okPB, SIGNAL(clicked()),
53 this, SLOT(slotOK()));
54 connect(closePB, SIGNAL(clicked()),
55 this, SLOT(slotClose()));
56 connect(stylePB, SIGNAL(clicked()),
57 this, SLOT(browsePressed()));
58 connect(deletePB, SIGNAL(clicked()),
59 this, SLOT(deletePressed()));
60 connect(upPB, SIGNAL(clicked()),
61 this, SLOT(upPressed()));
62 connect(downPB, SIGNAL(clicked()),
63 this, SLOT(downPressed()));
64 connect(styleCB, SIGNAL(editTextChanged(QString)),
65 this, SLOT(change_adaptor()));
66 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
67 this, SLOT(databaseChanged()));
68 connect(bibtocCB, SIGNAL(clicked()),
69 this, SLOT(change_adaptor()));
70 connect(btPrintCO, SIGNAL(activated(int)),
71 this, SLOT(change_adaptor()));
72 connect(addBibPB, SIGNAL(clicked()),
73 this, SLOT(addPressed()));
75 add_ = new GuiBibtexAddDialog(this);
76 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
77 add_bc_.setOK(add_->addPB);
78 add_bc_.setCancel(add_->closePB);
79 add_bc_.addCheckedLineEdit(add_->bibED, 0);
81 connect(add_->bibED, SIGNAL(textChanged(QString)),
82 this, SLOT(bibEDChanged()));
83 connect(add_->addPB, SIGNAL(clicked()),
84 this, SLOT(addDatabase()));
85 connect(add_->addPB, SIGNAL(clicked()),
86 add_, SLOT(accept()));
87 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
88 this, SLOT(addDatabase()));
89 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
90 add_, SLOT(accept()));
91 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
92 this, SLOT(availableChanged()));
93 connect(add_->browsePB, SIGNAL(clicked()),
94 this, SLOT(browseBibPressed()));
95 connect(add_->closePB, SIGNAL(clicked()),
96 add_, SLOT(reject()));
98 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
100 bc().setCancel(closePB);
101 bc().addReadOnly(databaseLW);
102 bc().addReadOnly(stylePB);
103 bc().addReadOnly(styleCB);
104 bc().addReadOnly(bibtocCB);
105 bc().addReadOnly(addBibPB);
106 // Delete/Up/Down are handled with more conditions in
107 // databaseChanged().
109 // Make sure the delete/up/down buttons are disabled if necessary.
114 void GuiBibtex::bibEDChanged()
116 // Indicate to the button controller that the contents have
117 // changed. The actual test of validity is carried out by
118 // the checkedLineEdit.
119 add_bc_.setValid(true);
123 void GuiBibtex::change_adaptor()
129 void GuiBibtex::browsePressed()
131 QString const file = browseBst(QString());
136 QString const filen = changeExtension(file, "");
137 bool present = false;
138 unsigned int pres = 0;
140 for (int i = 0; i != styleCB->count(); ++i) {
141 if (styleCB->itemText(i) == filen) {
148 styleCB->insertItem(0, filen);
150 styleCB->setCurrentIndex(pres);
155 void GuiBibtex::browseBibPressed()
157 QString const file = browseBib(QString()).trimmed();
162 QString const f = changeExtension(file, "");
163 bool present = false;
165 for (int i = 0; i < add_->bibLW->count(); ++i) {
166 if (add_->bibLW->item(i)->text() == f)
171 add_->bibLW->addItem(f);
175 add_->bibED->setText(f);
179 void GuiBibtex::addPressed()
181 add_bc_.setValid(false);
186 void GuiBibtex::addDatabase()
188 int const sel = add_->bibLW->currentRow();
189 QString const file = add_->bibED->text().trimmed();
191 if (sel < 0 && file.isEmpty())
194 // Add the selected browser_bib keys to browser_database
195 // multiple selections are possible
196 for (int i = 0; i != add_->bibLW->count(); ++i) {
197 QListWidgetItem * const item = add_->bibLW->item(i);
198 if (add_->bibLW->isItemSelected(item)) {
199 add_->bibLW->setItemSelected(item, false);
200 QList<QListWidgetItem *> matches =
201 databaseLW->findItems(item->text(), Qt::MatchExactly);
202 if (matches.empty()) {
203 QString label = item->text();
204 QListWidgetItem * db = new QListWidgetItem(label);
205 db->setFlags(db->flags() | Qt::ItemIsSelectable
206 | Qt::ItemIsUserCheckable);
207 db->setCheckState(Qt::Checked);
208 databaseLW->addItem(db);
213 if (!file.isEmpty()) {
214 add_->bibED->clear();
215 QString const f = changeExtension(file, "");
216 QList<QListWidgetItem *> matches =
217 databaseLW->findItems(f, Qt::MatchExactly);
218 if (matches.empty()) {
219 QListWidgetItem * db = new QListWidgetItem(f);
220 db->setFlags(db->flags() | Qt::ItemIsSelectable
221 | Qt::ItemIsUserCheckable);
222 db->setCheckState(Qt::Checked);
223 databaseLW->addItem(db);
232 void GuiBibtex::deletePressed()
234 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
243 void GuiBibtex::upPressed()
245 int row = databaseLW->currentRow();
246 QListWidgetItem *cur = databaseLW->takeItem(row);
247 databaseLW->insertItem(row - 1, cur);
248 databaseLW->setCurrentItem(cur);
253 void GuiBibtex::downPressed()
255 int row = databaseLW->currentRow();
256 QListWidgetItem *cur = databaseLW->takeItem(row);
257 databaseLW->insertItem(row + 1, cur);
258 databaseLW->setCurrentItem(cur);
263 void GuiBibtex::databaseChanged()
265 bool readOnly = isBufferReadonly();
266 int count = databaseLW->count();
267 int row = databaseLW->currentRow();
268 deletePB->setEnabled(!readOnly && row != -1);
269 upPB->setEnabled(!readOnly && count > 1 && row > 0);
270 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
274 void GuiBibtex::availableChanged()
276 add_bc_.setValid(true);
280 void GuiBibtex::updateContents()
282 bool bibtopic = usingBibtopic();
286 docstring bibs = params_["bibfiles"];
287 docstring embs = params_["embed"];
291 while (!bibs.empty()) {
292 bibs = split(bibs, bib, ',');
293 embs = split(embs, emb, ',');
296 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
297 db->setFlags(db->flags() | Qt::ItemIsSelectable
298 | Qt::ItemIsUserCheckable);
299 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
300 databaseLW->addItem(db);
304 add_->bibLW->clear();
306 QStringList bibfiles = bibFiles();
307 for (int i = 0; i != bibfiles.count(); ++i)
308 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
310 QString bibstyle = styleFile();
312 bibtocCB->setChecked(bibtotoc() && !bibtopic);
313 bibtocCB->setEnabled(!bibtopic);
315 if (!bibtopic && btPrintCO->count() == 3)
316 btPrintCO->removeItem(1);
317 else if (bibtopic && btPrintCO->count() < 3)
318 btPrintCO->insertItem(1, qt_("all uncited references", 0));
320 docstring btprint = params_["btprint"];
322 if ((bibtopic && btprint == "btPrintNotCited") ||
323 (!bibtopic && btprint == "btPrintAll"))
325 else if (bibtopic && btprint == "btPrintAll")
328 btPrintCO->setCurrentIndex(btp);
334 QStringList str = bibStyles();
335 for (int i = 0; i != str.count(); ++i) {
336 QString item = changeExtension(str[i], "");
337 if (item == bibstyle)
339 styleCB->addItem(item);
342 if (item_nr == -1 && !bibstyle.isEmpty()) {
343 styleCB->addItem(bibstyle);
344 item_nr = styleCB->count() - 1;
348 styleCB->setCurrentIndex(item_nr);
350 styleCB->clearEditText();
354 void GuiBibtex::applyView()
356 QString dbs = databaseLW->item(0)->text();
357 docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
359 unsigned int maxCount = databaseLW->count();
360 for (unsigned int i = 1; i < maxCount; i++) {
362 dbs += databaseLW->item(i)->text();
364 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
367 params_["bibfiles"] = qstring_to_ucs4(dbs);
368 params_["embed"] = emb;
370 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
371 bool const bibtotoc = bibtocCB->isChecked();
373 if (bibtotoc && !bibstyle.empty()) {
374 // both bibtotoc and style
375 params_["options"] = "bibtotoc," + bibstyle;
376 } else if (bibtotoc) {
377 // bibtotoc and no style
378 params_["options"] = from_ascii("bibtotoc");
380 // only style. An empty one is valid, because some
381 // documentclasses have an own \bibliographystyle{}
383 params_["options"] = bibstyle;
386 int btp = btPrintCO->currentIndex();
388 if (usingBibtopic()) {
389 // bibtopic allows three kinds of sections:
390 // 1. sections that include all cited references of the database(s)
391 // 2. sections that include all uncited references of the database(s)
392 // 3. sections that include all references of the database(s), cited or not
395 params_["btprint"] = from_ascii("btPrintCited");
398 params_["btprint"] = from_ascii("btPrintNotCited");
401 params_["btprint"] = from_ascii("btPrintAll");
407 params_["btprint"] = docstring();
411 params_["btprint"] = from_ascii("btPrintAll");
418 bool GuiBibtex::isValid()
420 return databaseLW->count() != 0;
424 QString GuiBibtex::browseBib(QString const & in_name) const
426 QString const label1 = qt_("Documents|#o#O");
427 QString const dir1 = toqstr(lyxrc.document_path);
428 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
429 return browseRelFile(in_name, bufferFilepath(),
430 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
434 QString GuiBibtex::browseBst(QString const & in_name) const
436 QString const label1 = qt_("Documents|#o#O");
437 QString const dir1 = toqstr(lyxrc.document_path);
438 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
439 return browseRelFile(in_name, bufferFilepath(),
440 qt_("Select a BibTeX style"), filter, false, label1, dir1);
444 QStringList GuiBibtex::bibStyles() const
446 QStringList data = texFileList("bstFiles.lst");
447 // test whether we have a valid list, otherwise run rescan
448 if (data.isEmpty()) {
450 data = texFileList("bstFiles.lst");
452 for (int i = 0; i != data.size(); ++i)
453 data[i] = onlyFilename(data[i]);
454 // sort on filename only (no path)
460 QStringList GuiBibtex::bibFiles() const
462 QStringList data = texFileList("bibFiles.lst");
463 // test whether we have a valid list, otherwise run rescan
464 if (data.isEmpty()) {
466 data = texFileList("bibFiles.lst");
468 for (int i = 0; i != data.size(); ++i)
469 data[i] = onlyFilename(data[i]);
470 // sort on filename only (no path)
476 void GuiBibtex::rescanBibStyles() const
482 bool GuiBibtex::usingBibtopic() const
484 return buffer().params().use_bibtopic;
488 bool GuiBibtex::bibtotoc() const
490 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
494 QString GuiBibtex::styleFile() const
496 // the different bibtex packages have (and need) their
497 // own "plain" stylefiles
498 biblio::CiteEngine const engine = buffer().params().getEngine();
499 QString defaultstyle;
501 case biblio::ENGINE_BASIC:
502 defaultstyle = "plain";
504 case biblio::ENGINE_NATBIB_AUTHORYEAR:
505 defaultstyle = "plainnat";
507 case biblio::ENGINE_NATBIB_NUMERICAL:
508 defaultstyle = "plainnat";
510 case biblio::ENGINE_JURABIB:
511 defaultstyle = "jurabib";
515 QString bst = toqstr(params_["options"]);
518 int pos = bst.indexOf(',');
521 // docstring bibtotoc = from_ascii("bibtotoc");
522 // bst = split(bst, bibtotoc, ',');
529 // propose default style file for new insets
530 // existing insets might have (legally) no bst files
531 // (if the class already provides a style)
532 if (bst.isEmpty() && params_["bibfiles"].empty())
539 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
542 } // namespace frontend
545 #include "GuiBibtex_moc.cpp"