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 "EmbeddedFiles.h"
21 #include "ui_BibtexAddUi.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
26 #include "ButtonPolicy.h"
28 #include "frontends/alert.h"
30 #include "insets/InsetBibtex.h"
32 #include "support/debug.h"
33 #include "support/ExceptionMessage.h"
34 #include "support/FileFilterList.h"
35 #include "support/filetools.h" // changeExtension
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
39 #include <QPushButton>
40 #include <QListWidget>
45 using namespace lyx::support;
51 GuiBibtex::GuiBibtex(GuiView & lv)
52 : GuiCommand(lv, "bibtex", qt_("BibTeX Bibliography"))
56 QDialog::setModal(true);
58 connect(okPB, SIGNAL(clicked()),
59 this, SLOT(slotOK()));
60 connect(closePB, SIGNAL(clicked()),
61 this, SLOT(slotClose()));
62 connect(stylePB, SIGNAL(clicked()),
63 this, SLOT(browsePressed()));
64 connect(deletePB, SIGNAL(clicked()),
65 this, SLOT(deletePressed()));
66 connect(upPB, SIGNAL(clicked()),
67 this, SLOT(upPressed()));
68 connect(downPB, SIGNAL(clicked()),
69 this, SLOT(downPressed()));
70 connect(styleCB, SIGNAL(editTextChanged(QString)),
71 this, SLOT(change_adaptor()));
72 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
73 this, SLOT(databaseChanged()));
74 connect(bibtocCB, SIGNAL(clicked()),
75 this, SLOT(change_adaptor()));
76 connect(btPrintCO, SIGNAL(activated(int)),
77 this, SLOT(change_adaptor()));
78 connect(addBibPB, SIGNAL(clicked()),
79 this, SLOT(addPressed()));
81 add_ = new GuiBibtexAddDialog(this);
82 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
83 add_bc_.setOK(add_->addPB);
84 add_bc_.setCancel(add_->closePB);
85 add_bc_.addCheckedLineEdit(add_->bibED, 0);
87 connect(add_->bibED, SIGNAL(textChanged(QString)),
88 this, SLOT(bibEDChanged()));
89 connect(add_->addPB, SIGNAL(clicked()),
90 this, SLOT(addDatabase()));
91 connect(add_->addPB, SIGNAL(clicked()),
92 add_, SLOT(accept()));
93 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
94 this, SLOT(addDatabase()));
95 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
96 add_, SLOT(accept()));
97 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
98 this, SLOT(availableChanged()));
99 connect(add_->browsePB, SIGNAL(clicked()),
100 this, SLOT(browseBibPressed()));
101 connect(add_->closePB, SIGNAL(clicked()),
102 add_, SLOT(reject()));
104 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
106 bc().setCancel(closePB);
107 bc().addReadOnly(databaseLW);
108 bc().addReadOnly(stylePB);
109 bc().addReadOnly(styleCB);
110 bc().addReadOnly(bibtocCB);
111 bc().addReadOnly(addBibPB);
112 // Delete/Up/Down are handled with more conditions in
113 // databaseChanged().
115 // Make sure the delete/up/down buttons are disabled if necessary.
120 void GuiBibtex::bibEDChanged()
122 // Indicate to the button controller that the contents have
123 // changed. The actual test of validity is carried out by
124 // the checkedLineEdit.
125 add_bc_.setValid(true);
129 void GuiBibtex::change_adaptor()
135 void GuiBibtex::browsePressed()
137 QString const file = browseBst(QString());
142 QString const filen = changeExtension(file, "");
143 bool present = false;
144 unsigned int pres = 0;
146 for (int i = 0; i != styleCB->count(); ++i) {
147 if (styleCB->itemText(i) == filen) {
154 styleCB->insertItem(0, filen);
156 styleCB->setCurrentIndex(pres);
161 void GuiBibtex::browseBibPressed()
163 QString const file = browseBib(QString()).trimmed();
168 QString const f = changeExtension(file, "");
169 bool present = false;
171 for (int i = 0; i < add_->bibLW->count(); ++i) {
172 if (add_->bibLW->item(i)->text() == f)
177 add_->bibLW->addItem(f);
181 add_->bibED->setText(f);
185 void GuiBibtex::addPressed()
187 add_bc_.setValid(false);
192 void GuiBibtex::addDatabase()
194 int const sel = add_->bibLW->currentRow();
195 QString const file = add_->bibED->text().trimmed();
197 if (sel < 0 && file.isEmpty())
200 // Add the selected browser_bib keys to browser_database
201 // multiple selections are possible
202 for (int i = 0; i != add_->bibLW->count(); ++i) {
203 QListWidgetItem * const item = add_->bibLW->item(i);
204 if (add_->bibLW->isItemSelected(item)) {
205 add_->bibLW->setItemSelected(item, false);
206 QList<QListWidgetItem *> matches =
207 databaseLW->findItems(item->text(), Qt::MatchExactly);
208 if (matches.empty()) {
209 QString label = item->text();
210 QListWidgetItem * db = new QListWidgetItem(label);
211 db->setFlags(db->flags() | Qt::ItemIsSelectable);
212 databaseLW->addItem(db);
217 if (!file.isEmpty()) {
218 add_->bibED->clear();
219 QString const f = changeExtension(file, "");
220 QList<QListWidgetItem *> matches =
221 databaseLW->findItems(f, Qt::MatchExactly);
222 if (matches.empty()) {
223 QListWidgetItem * db = new QListWidgetItem(f);
224 db->setFlags(db->flags() | Qt::ItemIsSelectable);
225 databaseLW->addItem(db);
234 void GuiBibtex::deletePressed()
236 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
245 void GuiBibtex::upPressed()
247 int row = databaseLW->currentRow();
248 QListWidgetItem *cur = databaseLW->takeItem(row);
249 databaseLW->insertItem(row - 1, cur);
250 databaseLW->setCurrentItem(cur);
255 void GuiBibtex::downPressed()
257 int row = databaseLW->currentRow();
258 QListWidgetItem *cur = databaseLW->takeItem(row);
259 databaseLW->insertItem(row + 1, cur);
260 databaseLW->setCurrentItem(cur);
265 void GuiBibtex::databaseChanged()
267 bool readOnly = isBufferReadonly();
268 int count = databaseLW->count();
269 int row = databaseLW->currentRow();
270 deletePB->setEnabled(!readOnly && row != -1);
271 upPB->setEnabled(!readOnly && count > 1 && row > 0);
272 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
276 void GuiBibtex::availableChanged()
278 add_bc_.setValid(true);
282 void GuiBibtex::updateContents()
284 bool bibtopic = usingBibtopic();
288 docstring bibs = params_["bibfiles"];
291 while (!bibs.empty()) {
292 bibs = split(bibs, bib, ',');
295 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
296 db->setFlags(db->flags() | Qt::ItemIsSelectable);
297 databaseLW->addItem(db);
301 add_->bibLW->clear();
303 QStringList bibfiles = bibFiles();
304 for (int i = 0; i != bibfiles.count(); ++i)
305 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
307 QString bibstyle = styleFile();
309 bibtocCB->setChecked(bibtotoc() && !bibtopic);
310 bibtocCB->setEnabled(!bibtopic);
312 if (!bibtopic && btPrintCO->count() == 3)
313 btPrintCO->removeItem(1);
314 else if (bibtopic && btPrintCO->count() < 3)
315 btPrintCO->insertItem(1, qt_("all uncited references", 0));
317 docstring btprint = params_["btprint"];
319 if ((bibtopic && btprint == "btPrintNotCited") ||
320 (!bibtopic && btprint == "btPrintAll"))
322 else if (bibtopic && btprint == "btPrintAll")
325 btPrintCO->setCurrentIndex(btp);
331 QStringList str = bibStyles();
332 for (int i = 0; i != str.count(); ++i) {
333 QString item = changeExtension(str[i], "");
334 if (item == bibstyle)
336 styleCB->addItem(item);
339 if (item_nr == -1 && !bibstyle.isEmpty()) {
340 styleCB->addItem(bibstyle);
341 item_nr = styleCB->count() - 1;
345 styleCB->setCurrentIndex(item_nr);
347 styleCB->clearEditText();
351 void GuiBibtex::applyView()
355 unsigned int maxCount = databaseLW->count();
356 for (unsigned int i = 0; i < maxCount; i++) {
359 QString item = databaseLW->item(i)->text();
360 docstring bibfile = qstring_to_ucs4(item);
364 params_["bibfiles"] = dbs;
366 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
367 bool const bibtotoc = bibtocCB->isChecked();
369 if (bibtotoc && !bibstyle.empty()) {
370 // both bibtotoc and style
371 params_["options"] = "bibtotoc," + bibstyle;
372 } else if (bibtotoc) {
373 // bibtotoc and no style
374 params_["options"] = from_ascii("bibtotoc");
376 // only style. An empty one is valid, because some
377 // documentclasses have an own \bibliographystyle{}
379 params_["options"] = bibstyle;
382 int btp = btPrintCO->currentIndex();
384 if (usingBibtopic()) {
385 // bibtopic allows three kinds of sections:
386 // 1. sections that include all cited references of the database(s)
387 // 2. sections that include all uncited references of the database(s)
388 // 3. sections that include all references of the database(s), cited or not
391 params_["btprint"] = from_ascii("btPrintCited");
394 params_["btprint"] = from_ascii("btPrintNotCited");
397 params_["btprint"] = from_ascii("btPrintAll");
403 params_["btprint"] = docstring();
407 params_["btprint"] = from_ascii("btPrintAll");
414 bool GuiBibtex::isValid()
416 return databaseLW->count() != 0;
420 QString GuiBibtex::browseBib(QString const & in_name) const
422 QString const label1 = qt_("Documents|#o#O");
423 QString const dir1 = toqstr(lyxrc.document_path);
424 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
425 return browseRelFile(in_name, bufferFilepath(),
426 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
430 QString GuiBibtex::browseBst(QString const & in_name) const
432 QString const label1 = qt_("Documents|#o#O");
433 QString const dir1 = toqstr(lyxrc.document_path);
434 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
435 return browseRelFile(in_name, bufferFilepath(),
436 qt_("Select a BibTeX style"), filter, false, label1, dir1);
440 QStringList GuiBibtex::bibStyles() const
442 QStringList data = texFileList("bstFiles.lst");
443 // test whether we have a valid list, otherwise run rescan
444 if (data.isEmpty()) {
446 data = texFileList("bstFiles.lst");
448 for (int i = 0; i != data.size(); ++i)
449 data[i] = onlyFilename(data[i]);
450 // sort on filename only (no path)
456 QStringList GuiBibtex::bibFiles() const
458 QStringList data = texFileList("bibFiles.lst");
459 // test whether we have a valid list, otherwise run rescan
460 if (data.isEmpty()) {
462 data = texFileList("bibFiles.lst");
464 for (int i = 0; i != data.size(); ++i)
465 data[i] = onlyFilename(data[i]);
466 // sort on filename only (no path)
472 void GuiBibtex::rescanBibStyles() const
478 bool GuiBibtex::usingBibtopic() const
480 return buffer().params().use_bibtopic;
484 bool GuiBibtex::bibtotoc() const
486 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
490 QString GuiBibtex::styleFile() const
492 // the different bibtex packages have (and need) their
493 // own "plain" stylefiles
494 biblio::CiteEngine const engine = buffer().params().citeEngine();
495 QString defaultstyle;
497 case biblio::ENGINE_BASIC:
498 defaultstyle = "plain";
500 case biblio::ENGINE_NATBIB_AUTHORYEAR:
501 defaultstyle = "plainnat";
503 case biblio::ENGINE_NATBIB_NUMERICAL:
504 defaultstyle = "plainnat";
506 case biblio::ENGINE_JURABIB:
507 defaultstyle = "jurabib";
511 QString bst = toqstr(params_["options"]);
514 int pos = bst.indexOf(',');
517 // docstring bibtotoc = from_ascii("bibtotoc");
518 // bst = split(bst, bibtotoc, ',');
525 // propose default style file for new insets
526 // existing insets might have (legally) no bst files
527 // (if the class already provides a style)
528 if (bst.isEmpty() && params_["bibfiles"].empty())
535 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
538 } // namespace frontend
541 #include "GuiBibtex_moc.cpp"