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 | Qt::ItemIsUserCheckable);
213 db->setCheckState(Qt::Checked);
214 databaseLW->addItem(db);
219 if (!file.isEmpty()) {
220 add_->bibED->clear();
221 QString const f = changeExtension(file, "");
222 QList<QListWidgetItem *> matches =
223 databaseLW->findItems(f, Qt::MatchExactly);
224 if (matches.empty()) {
225 QListWidgetItem * db = new QListWidgetItem(f);
226 db->setFlags(db->flags() | Qt::ItemIsSelectable
227 | Qt::ItemIsUserCheckable);
228 db->setCheckState(Qt::Checked);
229 databaseLW->addItem(db);
238 void GuiBibtex::deletePressed()
240 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
249 void GuiBibtex::upPressed()
251 int row = databaseLW->currentRow();
252 QListWidgetItem *cur = databaseLW->takeItem(row);
253 databaseLW->insertItem(row - 1, cur);
254 databaseLW->setCurrentItem(cur);
259 void GuiBibtex::downPressed()
261 int row = databaseLW->currentRow();
262 QListWidgetItem *cur = databaseLW->takeItem(row);
263 databaseLW->insertItem(row + 1, cur);
264 databaseLW->setCurrentItem(cur);
269 void GuiBibtex::databaseChanged()
271 bool readOnly = isBufferReadonly();
272 int count = databaseLW->count();
273 int row = databaseLW->currentRow();
274 deletePB->setEnabled(!readOnly && row != -1);
275 upPB->setEnabled(!readOnly && count > 1 && row > 0);
276 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
280 void GuiBibtex::availableChanged()
282 add_bc_.setValid(true);
286 void GuiBibtex::updateContents()
288 bool bibtopic = usingBibtopic();
292 docstring bibs = params_["bibfiles"];
293 docstring embs = params_["embed"];
297 while (!bibs.empty()) {
298 bibs = split(bibs, bib, ',');
299 embs = split(embs, emb, ',');
302 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
303 db->setFlags(db->flags() | Qt::ItemIsSelectable
304 | Qt::ItemIsUserCheckable);
305 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
306 databaseLW->addItem(db);
310 add_->bibLW->clear();
312 QStringList bibfiles = bibFiles();
313 for (int i = 0; i != bibfiles.count(); ++i)
314 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
316 QString bibstyle = styleFile();
318 bibtocCB->setChecked(bibtotoc() && !bibtopic);
319 bibtocCB->setEnabled(!bibtopic);
321 if (!bibtopic && btPrintCO->count() == 3)
322 btPrintCO->removeItem(1);
323 else if (bibtopic && btPrintCO->count() < 3)
324 btPrintCO->insertItem(1, qt_("all uncited references", 0));
326 docstring btprint = params_["btprint"];
328 if ((bibtopic && btprint == "btPrintNotCited") ||
329 (!bibtopic && btprint == "btPrintAll"))
331 else if (bibtopic && btprint == "btPrintAll")
334 btPrintCO->setCurrentIndex(btp);
340 QStringList str = bibStyles();
341 for (int i = 0; i != str.count(); ++i) {
342 QString item = changeExtension(str[i], "");
343 if (item == bibstyle)
345 styleCB->addItem(item);
348 if (item_nr == -1 && !bibstyle.isEmpty()) {
349 styleCB->addItem(bibstyle);
350 item_nr = styleCB->count() - 1;
354 styleCB->setCurrentIndex(item_nr);
356 styleCB->clearEditText();
360 void GuiBibtex::applyView()
365 unsigned int maxCount = databaseLW->count();
366 Buffer & buf = buffer();
367 for (unsigned int i = 0; i < maxCount; i++) {
372 QString item = databaseLW->item(i)->text();
373 docstring bibfile = qstring_to_ucs4(item);
375 if (databaseLW->item(i)->checkState() == Qt::Checked)
379 params_["bibfiles"] = dbs;
380 params_["embed"] = emb;
382 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
383 bool const bibtotoc = bibtocCB->isChecked();
385 if (bibtotoc && !bibstyle.empty()) {
386 // both bibtotoc and style
387 params_["options"] = "bibtotoc," + bibstyle;
388 } else if (bibtotoc) {
389 // bibtotoc and no style
390 params_["options"] = from_ascii("bibtotoc");
392 // only style. An empty one is valid, because some
393 // documentclasses have an own \bibliographystyle{}
395 params_["options"] = bibstyle;
398 int btp = btPrintCO->currentIndex();
400 if (usingBibtopic()) {
401 // bibtopic allows three kinds of sections:
402 // 1. sections that include all cited references of the database(s)
403 // 2. sections that include all uncited references of the database(s)
404 // 3. sections that include all references of the database(s), cited or not
407 params_["btprint"] = from_ascii("btPrintCited");
410 params_["btprint"] = from_ascii("btPrintNotCited");
413 params_["btprint"] = from_ascii("btPrintAll");
419 params_["btprint"] = docstring();
423 params_["btprint"] = from_ascii("btPrintAll");
430 bool GuiBibtex::isValid()
432 return databaseLW->count() != 0;
436 QString GuiBibtex::browseBib(QString const & in_name) const
438 QString const label1 = qt_("Documents|#o#O");
439 QString const dir1 = toqstr(lyxrc.document_path);
440 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
441 return browseRelFile(in_name, bufferFilepath(),
442 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
446 QString GuiBibtex::browseBst(QString const & in_name) const
448 QString const label1 = qt_("Documents|#o#O");
449 QString const dir1 = toqstr(lyxrc.document_path);
450 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
451 return browseRelFile(in_name, bufferFilepath(),
452 qt_("Select a BibTeX style"), filter, false, label1, dir1);
456 QStringList GuiBibtex::bibStyles() const
458 QStringList data = texFileList("bstFiles.lst");
459 // test whether we have a valid list, otherwise run rescan
460 if (data.isEmpty()) {
462 data = texFileList("bstFiles.lst");
464 for (int i = 0; i != data.size(); ++i)
465 data[i] = onlyFilename(data[i]);
466 // sort on filename only (no path)
472 QStringList GuiBibtex::bibFiles() const
474 QStringList data = texFileList("bibFiles.lst");
475 // test whether we have a valid list, otherwise run rescan
476 if (data.isEmpty()) {
478 data = texFileList("bibFiles.lst");
480 for (int i = 0; i != data.size(); ++i)
481 data[i] = onlyFilename(data[i]);
482 // sort on filename only (no path)
488 void GuiBibtex::rescanBibStyles() const
494 bool GuiBibtex::usingBibtopic() const
496 return buffer().params().use_bibtopic;
500 bool GuiBibtex::bibtotoc() const
502 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
506 QString GuiBibtex::styleFile() const
508 // the different bibtex packages have (and need) their
509 // own "plain" stylefiles
510 biblio::CiteEngine const engine = buffer().params().citeEngine();
511 QString defaultstyle;
513 case biblio::ENGINE_BASIC:
514 defaultstyle = "plain";
516 case biblio::ENGINE_NATBIB_AUTHORYEAR:
517 defaultstyle = "plainnat";
519 case biblio::ENGINE_NATBIB_NUMERICAL:
520 defaultstyle = "plainnat";
522 case biblio::ENGINE_JURABIB:
523 defaultstyle = "jurabib";
527 QString bst = toqstr(params_["options"]);
530 int pos = bst.indexOf(',');
533 // docstring bibtotoc = from_ascii("bibtotoc");
534 // bst = split(bst, bibtotoc, ',');
541 // propose default style file for new insets
542 // existing insets might have (legally) no bst files
543 // (if the class already provides a style)
544 if (bst.isEmpty() && params_["bibfiles"].empty())
551 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
554 } // namespace frontend
557 #include "GuiBibtex_moc.cpp"