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 "FuncRequest.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
25 #include "ui_BibtexAddUi.h"
27 #include "ButtonPolicy.h"
29 #include "frontends/alert.h"
31 #include "insets/InsetBibtex.h"
33 #include "support/debug.h"
34 #include "support/ExceptionMessage.h"
35 #include "support/FileName.h"
36 #include "support/filetools.h" // changeExtension
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
40 #include <QPushButton>
41 #include <QListWidget>
46 using namespace lyx::support;
52 GuiBibtex::GuiBibtex(GuiView & lv)
53 : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
54 params_(insetCode("bibtex"))
58 QDialog::setModal(true);
60 connect(okPB, SIGNAL(clicked()),
61 this, SLOT(slotOK()));
62 connect(closePB, SIGNAL(clicked()),
63 this, SLOT(slotClose()));
64 connect(stylePB, SIGNAL(clicked()),
65 this, SLOT(browsePressed()));
66 connect(deletePB, SIGNAL(clicked()),
67 this, SLOT(deletePressed()));
68 connect(upPB, SIGNAL(clicked()),
69 this, SLOT(upPressed()));
70 connect(downPB, SIGNAL(clicked()),
71 this, SLOT(downPressed()));
72 connect(styleCB, SIGNAL(editTextChanged(QString)),
73 this, SLOT(change_adaptor()));
74 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
75 this, SLOT(databaseChanged()));
76 connect(bibtocCB, SIGNAL(clicked()),
77 this, SLOT(change_adaptor()));
78 connect(btPrintCO, SIGNAL(activated(int)),
79 this, SLOT(change_adaptor()));
80 connect(addBibPB, SIGNAL(clicked()),
81 this, SLOT(addPressed()));
83 add_ = new GuiBibtexAddDialog(this);
84 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
85 add_bc_.setOK(add_->addPB);
86 add_bc_.setCancel(add_->closePB);
87 add_bc_.addCheckedLineEdit(add_->bibED, 0);
89 connect(add_->bibED, SIGNAL(textChanged(QString)),
90 this, SLOT(bibEDChanged()));
91 connect(add_->addPB, SIGNAL(clicked()),
92 this, SLOT(addDatabase()));
93 connect(add_->addPB, SIGNAL(clicked()),
94 add_, SLOT(accept()));
95 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
96 this, SLOT(addDatabase()));
97 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
98 add_, SLOT(accept()));
99 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
100 this, SLOT(availableChanged()));
101 connect(add_->browsePB, SIGNAL(clicked()),
102 this, SLOT(browseBibPressed()));
103 connect(add_->closePB, SIGNAL(clicked()),
104 add_, SLOT(reject()));
106 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
108 bc().setCancel(closePB);
109 bc().addReadOnly(databaseLW);
110 bc().addReadOnly(stylePB);
111 bc().addReadOnly(styleCB);
112 bc().addReadOnly(bibtocCB);
113 bc().addReadOnly(addBibPB);
114 // Delete/Up/Down are handled with more conditions in
115 // databaseChanged().
117 // Make sure the delete/up/down buttons are disabled if necessary.
122 void GuiBibtex::bibEDChanged()
124 // Indicate to the button controller that the contents have
125 // changed. The actual test of validity is carried out by
126 // the checkedLineEdit.
127 add_bc_.setValid(true);
131 void GuiBibtex::change_adaptor()
137 void GuiBibtex::browsePressed()
139 QString const file = browseBst(QString());
144 QString const filen = changeExtension(file, "");
145 bool present = false;
146 unsigned int pres = 0;
148 for (int i = 0; i != styleCB->count(); ++i) {
149 if (styleCB->itemText(i) == filen) {
156 styleCB->insertItem(0, filen);
158 styleCB->setCurrentIndex(pres);
163 void GuiBibtex::browseBibPressed()
165 QString const file = browseBib(QString()).trimmed();
170 QString const f = changeExtension(file, "");
171 bool present = false;
173 for (int i = 0; i < add_->bibLW->count(); ++i) {
174 if (add_->bibLW->item(i)->text() == f)
179 add_->bibLW->addItem(f);
183 add_->bibED->setText(f);
187 void GuiBibtex::addPressed()
189 add_bc_.setValid(false);
194 void GuiBibtex::addDatabase()
196 int const sel = add_->bibLW->currentRow();
197 QString const file = add_->bibED->text().trimmed();
199 if (sel < 0 && file.isEmpty())
202 // Add the selected browser_bib keys to browser_database
203 // multiple selections are possible
204 for (int i = 0; i != add_->bibLW->count(); ++i) {
205 QListWidgetItem * const item = add_->bibLW->item(i);
206 if (add_->bibLW->isItemSelected(item)) {
207 add_->bibLW->setItemSelected(item, false);
208 QList<QListWidgetItem *> matches =
209 databaseLW->findItems(item->text(), Qt::MatchExactly);
210 if (matches.empty()) {
211 QString label = item->text();
212 QListWidgetItem * db = new QListWidgetItem(label);
213 db->setFlags(db->flags() | Qt::ItemIsSelectable);
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 databaseLW->addItem(db);
236 void GuiBibtex::deletePressed()
238 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
247 void GuiBibtex::upPressed()
249 int row = databaseLW->currentRow();
250 QListWidgetItem *cur = databaseLW->takeItem(row);
251 databaseLW->insertItem(row - 1, cur);
252 databaseLW->setCurrentItem(cur);
257 void GuiBibtex::downPressed()
259 int row = databaseLW->currentRow();
260 QListWidgetItem *cur = databaseLW->takeItem(row);
261 databaseLW->insertItem(row + 1, cur);
262 databaseLW->setCurrentItem(cur);
267 void GuiBibtex::databaseChanged()
269 bool readOnly = isBufferReadonly();
270 int count = databaseLW->count();
271 int row = databaseLW->currentRow();
272 deletePB->setEnabled(!readOnly && row != -1);
273 upPB->setEnabled(!readOnly && count > 1 && row > 0);
274 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
278 void GuiBibtex::availableChanged()
280 add_bc_.setValid(true);
284 void GuiBibtex::updateContents()
286 bool bibtopic = usingBibtopic();
290 docstring bibs = params_["bibfiles"];
293 while (!bibs.empty()) {
294 bibs = split(bibs, bib, ',');
297 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
298 db->setFlags(db->flags() | Qt::ItemIsSelectable);
299 databaseLW->addItem(db);
303 add_->bibLW->clear();
305 QStringList bibfiles = bibFiles();
306 for (int i = 0; i != bibfiles.count(); ++i)
307 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
309 QString bibstyle = styleFile();
311 bibtocCB->setChecked(bibtotoc() && !bibtopic);
312 bibtocCB->setEnabled(!bibtopic);
314 if (!bibtopic && btPrintCO->count() == 3)
315 btPrintCO->removeItem(1);
316 else if (bibtopic && btPrintCO->count() < 3)
317 btPrintCO->insertItem(1, qt_("all uncited references", 0));
319 docstring btprint = params_["btprint"];
321 if ((bibtopic && btprint == "btPrintNotCited") ||
322 (!bibtopic && btprint == "btPrintAll"))
324 else if (bibtopic && btprint == "btPrintAll")
327 btPrintCO->setCurrentIndex(btp);
333 QStringList str = bibStyles();
334 for (int i = 0; i != str.count(); ++i) {
335 QString item = changeExtension(str[i], "");
336 if (item == bibstyle)
338 styleCB->addItem(item);
341 if (item_nr == -1 && !bibstyle.isEmpty()) {
342 styleCB->addItem(bibstyle);
343 item_nr = styleCB->count() - 1;
347 styleCB->setCurrentIndex(item_nr);
349 styleCB->clearEditText();
353 void GuiBibtex::applyView()
357 unsigned int maxCount = databaseLW->count();
358 for (unsigned int i = 0; i < maxCount; i++) {
361 QString item = databaseLW->item(i)->text();
362 docstring bibfile = qstring_to_ucs4(item);
366 params_["bibfiles"] = dbs;
368 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
369 bool const bibtotoc = bibtocCB->isChecked();
371 if (bibtotoc && !bibstyle.empty()) {
372 // both bibtotoc and style
373 params_["options"] = "bibtotoc," + bibstyle;
374 } else if (bibtotoc) {
375 // bibtotoc and no style
376 params_["options"] = from_ascii("bibtotoc");
378 // only style. An empty one is valid, because some
379 // documentclasses have an own \bibliographystyle{}
381 params_["options"] = bibstyle;
384 int btp = btPrintCO->currentIndex();
386 if (usingBibtopic()) {
387 // bibtopic allows three kinds of sections:
388 // 1. sections that include all cited references of the database(s)
389 // 2. sections that include all uncited references of the database(s)
390 // 3. sections that include all references of the database(s), cited or not
393 params_["btprint"] = from_ascii("btPrintCited");
396 params_["btprint"] = from_ascii("btPrintNotCited");
399 params_["btprint"] = from_ascii("btPrintAll");
405 params_["btprint"] = docstring();
409 params_["btprint"] = from_ascii("btPrintAll");
416 bool GuiBibtex::isValid()
418 return databaseLW->count() != 0;
422 QString GuiBibtex::browseBib(QString const & in_name) const
424 QString const label1 = qt_("Documents|#o#O");
425 QString const dir1 = toqstr(lyxrc.document_path);
426 QStringList const filter(qt_("BibTeX Databases (*.bib)"));
427 return browseRelFile(in_name, bufferFilepath(),
428 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
432 QString GuiBibtex::browseBst(QString const & in_name) const
434 QString const label1 = qt_("Documents|#o#O");
435 QString const dir1 = toqstr(lyxrc.document_path);
436 QStringList const filter(qt_("BibTeX Styles (*.bst)"));
437 return browseRelFile(in_name, bufferFilepath(),
438 qt_("Select a BibTeX style"), filter, false, label1, dir1);
442 QStringList GuiBibtex::bibStyles() const
444 QStringList data = texFileList("bstFiles.lst");
445 // test whether we have a valid list, otherwise run rescan
446 if (data.isEmpty()) {
448 data = texFileList("bstFiles.lst");
450 for (int i = 0; i != data.size(); ++i)
451 data[i] = onlyFilename(data[i]);
452 // sort on filename only (no path)
458 QStringList GuiBibtex::bibFiles() const
460 QStringList data = texFileList("bibFiles.lst");
461 // test whether we have a valid list, otherwise run rescan
462 if (data.isEmpty()) {
464 data = texFileList("bibFiles.lst");
466 for (int i = 0; i != data.size(); ++i)
467 data[i] = onlyFilename(data[i]);
468 // sort on filename only (no path)
474 void GuiBibtex::rescanBibStyles() const
480 bool GuiBibtex::usingBibtopic() const
482 return buffer().params().use_bibtopic;
486 bool GuiBibtex::bibtotoc() const
488 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
492 QString GuiBibtex::styleFile() const
494 // the different bibtex packages have (and need) their
495 // own "plain" stylefiles
496 CiteEngine const engine = buffer().params().citeEngine();
497 QString defaultstyle;
500 defaultstyle = "plain";
502 case ENGINE_NATBIB_AUTHORYEAR:
503 defaultstyle = "plainnat";
505 case ENGINE_NATBIB_NUMERICAL:
506 defaultstyle = "plainnat";
509 defaultstyle = "jurabib";
513 QString bst = toqstr(params_["options"]);
516 int pos = bst.indexOf(',');
519 // docstring bibtotoc = from_ascii("bibtotoc");
520 // bst = split(bst, bibtotoc, ',');
527 // propose default style file for new insets
528 // existing insets might have (legally) no bst files
529 // (if the class already provides a style)
530 if (bst.isEmpty() && params_["bibfiles"].empty())
537 bool GuiBibtex::initialiseParams(std::string const & data)
539 InsetCommand::string2params("bibtex", data, params_);
544 void GuiBibtex::dispatchParams()
546 std::string const lfun = InsetCommand::params2string("bibtex", params_);
547 dispatch(FuncRequest(getLfun(), lfun));
552 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
555 } // namespace frontend
558 #include "GuiBibtex_moc.cpp"