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()));
82 connect(rescanPB, SIGNAL(clicked()),
83 this, SLOT(rescanClicked()));
85 add_ = new GuiBibtexAddDialog(this);
86 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
87 add_bc_.setOK(add_->addPB);
88 add_bc_.setCancel(add_->closePB);
89 add_bc_.addCheckedLineEdit(add_->bibED, 0);
91 connect(add_->bibED, SIGNAL(textChanged(QString)),
92 this, SLOT(bibEDChanged()));
93 connect(add_->addPB, SIGNAL(clicked()),
94 this, SLOT(addDatabase()));
95 connect(add_->addPB, SIGNAL(clicked()),
96 add_, SLOT(accept()));
97 connect(add_->rescanPB, SIGNAL(clicked()),
98 this, SLOT(rescanClicked()));
99 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
100 this, SLOT(addDatabase()));
101 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
102 add_, SLOT(accept()));
103 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
104 this, SLOT(availableChanged()));
105 connect(add_->browsePB, SIGNAL(clicked()),
106 this, SLOT(browseBibPressed()));
107 connect(add_->closePB, SIGNAL(clicked()),
108 add_, SLOT(reject()));
110 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
112 bc().setCancel(closePB);
113 bc().addReadOnly(databaseLW);
114 bc().addReadOnly(stylePB);
115 bc().addReadOnly(styleCB);
116 bc().addReadOnly(bibtocCB);
117 bc().addReadOnly(addBibPB);
118 // Delete/Up/Down are handled with more conditions in
119 // databaseChanged().
121 // Make sure the delete/up/down buttons are disabled if necessary.
126 void GuiBibtex::bibEDChanged()
128 // Indicate to the button controller that the contents have
129 // changed. The actual test of validity is carried out by
130 // the checkedLineEdit.
131 add_bc_.setValid(true);
135 void GuiBibtex::change_adaptor()
141 void GuiBibtex::browsePressed()
143 QString const file = browseBst(QString());
148 QString const filen = changeExtension(file, "");
149 bool present = false;
150 unsigned int pres = 0;
152 for (int i = 0; i != styleCB->count(); ++i) {
153 if (styleCB->itemText(i) == filen) {
160 styleCB->insertItem(0, filen);
162 styleCB->setCurrentIndex(pres);
167 void GuiBibtex::browseBibPressed()
169 QString const file = browseBib(QString()).trimmed();
174 QString const f = changeExtension(file, "");
175 bool present = false;
177 for (int i = 0; i < add_->bibLW->count(); ++i) {
178 if (add_->bibLW->item(i)->text() == f)
183 add_->bibLW->addItem(f);
187 add_->bibED->setText(f);
191 void GuiBibtex::addPressed()
193 add_bc_.setValid(false);
198 void GuiBibtex::addDatabase()
200 int const sel = add_->bibLW->currentRow();
201 QString const file = add_->bibED->text().trimmed();
203 if (sel < 0 && file.isEmpty())
206 // Add the selected browser_bib keys to browser_database
207 // multiple selections are possible
208 for (int i = 0; i != add_->bibLW->count(); ++i) {
209 QListWidgetItem * const item = add_->bibLW->item(i);
210 if (add_->bibLW->isItemSelected(item)) {
211 add_->bibLW->setItemSelected(item, false);
212 QList<QListWidgetItem *> matches =
213 databaseLW->findItems(item->text(), Qt::MatchExactly);
214 if (matches.empty()) {
215 QString label = item->text();
216 QListWidgetItem * db = new QListWidgetItem(label);
217 db->setFlags(db->flags() | Qt::ItemIsSelectable);
218 databaseLW->addItem(db);
223 if (!file.isEmpty()) {
224 add_->bibED->clear();
225 QString const f = changeExtension(file, "");
226 QList<QListWidgetItem *> matches =
227 databaseLW->findItems(f, Qt::MatchExactly);
228 if (matches.empty()) {
229 QListWidgetItem * db = new QListWidgetItem(f);
230 db->setFlags(db->flags() | Qt::ItemIsSelectable);
231 databaseLW->addItem(db);
240 void GuiBibtex::deletePressed()
242 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
251 void GuiBibtex::upPressed()
253 int row = databaseLW->currentRow();
254 QListWidgetItem *cur = databaseLW->takeItem(row);
255 databaseLW->insertItem(row - 1, cur);
256 databaseLW->setCurrentItem(cur);
261 void GuiBibtex::downPressed()
263 int row = databaseLW->currentRow();
264 QListWidgetItem *cur = databaseLW->takeItem(row);
265 databaseLW->insertItem(row + 1, cur);
266 databaseLW->setCurrentItem(cur);
271 void GuiBibtex::rescanClicked()
278 void GuiBibtex::databaseChanged()
280 bool readOnly = isBufferReadonly();
281 int count = databaseLW->count();
282 int row = databaseLW->currentRow();
283 deletePB->setEnabled(!readOnly && row != -1);
284 upPB->setEnabled(!readOnly && count > 1 && row > 0);
285 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
289 void GuiBibtex::availableChanged()
291 add_bc_.setValid(true);
295 void GuiBibtex::updateContents()
297 bool bibtopic = usingBibtopic();
301 docstring bibs = params_["bibfiles"];
304 while (!bibs.empty()) {
305 bibs = split(bibs, bib, ',');
308 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
309 db->setFlags(db->flags() | Qt::ItemIsSelectable);
310 databaseLW->addItem(db);
314 add_->bibLW->clear();
316 QStringList bibfiles = bibFiles();
317 for (int i = 0; i != bibfiles.count(); ++i)
318 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
320 QString bibstyle = styleFile();
322 bibtocCB->setChecked(bibtotoc() && !bibtopic);
323 bibtocCB->setEnabled(!bibtopic);
325 if (!bibtopic && btPrintCO->count() == 3)
326 btPrintCO->removeItem(1);
327 else if (bibtopic && btPrintCO->count() < 3)
328 btPrintCO->insertItem(1, qt_("all uncited references", 0));
330 docstring btprint = params_["btprint"];
332 if ((bibtopic && btprint == "btPrintNotCited") ||
333 (!bibtopic && btprint == "btPrintAll"))
335 else if (bibtopic && btprint == "btPrintAll")
338 btPrintCO->setCurrentIndex(btp);
344 QStringList str = bibStyles();
345 for (int i = 0; i != str.count(); ++i) {
346 QString item = changeExtension(str[i], "");
347 if (item == bibstyle)
349 styleCB->addItem(item);
352 if (item_nr == -1 && !bibstyle.isEmpty()) {
353 styleCB->addItem(bibstyle);
354 item_nr = styleCB->count() - 1;
358 styleCB->setCurrentIndex(item_nr);
360 styleCB->clearEditText();
364 void GuiBibtex::applyView()
368 unsigned int maxCount = databaseLW->count();
369 for (unsigned int i = 0; i < maxCount; i++) {
372 QString item = databaseLW->item(i)->text();
373 docstring bibfile = qstring_to_ucs4(item);
377 params_["bibfiles"] = dbs;
379 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
380 bool const bibtotoc = bibtocCB->isChecked();
382 if (bibtotoc && !bibstyle.empty()) {
383 // both bibtotoc and style
384 params_["options"] = "bibtotoc," + bibstyle;
385 } else if (bibtotoc) {
386 // bibtotoc and no style
387 params_["options"] = from_ascii("bibtotoc");
389 // only style. An empty one is valid, because some
390 // documentclasses have an own \bibliographystyle{}
392 params_["options"] = bibstyle;
395 int btp = btPrintCO->currentIndex();
397 if (usingBibtopic()) {
398 // bibtopic allows three kinds of sections:
399 // 1. sections that include all cited references of the database(s)
400 // 2. sections that include all uncited references of the database(s)
401 // 3. sections that include all references of the database(s), cited or not
404 params_["btprint"] = from_ascii("btPrintCited");
407 params_["btprint"] = from_ascii("btPrintNotCited");
410 params_["btprint"] = from_ascii("btPrintAll");
416 params_["btprint"] = docstring();
420 params_["btprint"] = from_ascii("btPrintAll");
427 bool GuiBibtex::isValid()
429 return databaseLW->count() != 0;
433 QString GuiBibtex::browseBib(QString const & in_name) const
435 QString const label1 = qt_("Documents|#o#O");
436 QString const dir1 = toqstr(lyxrc.document_path);
437 QStringList const filter(qt_("BibTeX Databases (*.bib)"));
438 return browseRelFile(in_name, bufferFilepath(),
439 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
443 QString GuiBibtex::browseBst(QString const & in_name) const
445 QString const label1 = qt_("Documents|#o#O");
446 QString const dir1 = toqstr(lyxrc.document_path);
447 QStringList const filter(qt_("BibTeX Styles (*.bst)"));
448 return browseRelFile(in_name, bufferFilepath(),
449 qt_("Select a BibTeX style"), filter, false, label1, dir1);
453 QStringList GuiBibtex::bibStyles() const
455 QStringList data = texFileList("bstFiles.lst");
456 // test whether we have a valid list, otherwise run rescan
457 if (data.isEmpty()) {
459 data = texFileList("bstFiles.lst");
461 for (int i = 0; i != data.size(); ++i)
462 data[i] = onlyFilename(data[i]);
463 // sort on filename only (no path)
469 QStringList GuiBibtex::bibFiles() const
471 QStringList data = texFileList("bibFiles.lst");
472 // test whether we have a valid list, otherwise run rescan
473 if (data.isEmpty()) {
475 data = texFileList("bibFiles.lst");
477 for (int i = 0; i != data.size(); ++i)
478 data[i] = onlyFilename(data[i]);
479 // sort on filename only (no path)
485 void GuiBibtex::rescanBibStyles() const
491 bool GuiBibtex::usingBibtopic() const
493 return buffer().params().use_bibtopic;
497 bool GuiBibtex::bibtotoc() const
499 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
503 QString GuiBibtex::styleFile() const
505 // the different bibtex packages have (and need) their
506 // own "plain" stylefiles
507 CiteEngine const engine = buffer().params().citeEngine();
508 QString defaultstyle;
511 defaultstyle = "plain";
513 case ENGINE_NATBIB_AUTHORYEAR:
514 defaultstyle = "plainnat";
516 case ENGINE_NATBIB_NUMERICAL:
517 defaultstyle = "plainnat";
520 defaultstyle = "jurabib";
524 QString bst = toqstr(params_["options"]);
527 int pos = bst.indexOf(',');
530 // docstring bibtotoc = from_ascii("bibtotoc");
531 // bst = split(bst, bibtotoc, ',');
532 bst = bst.mid(pos + 1);
538 // propose default style file for new insets
539 // existing insets might have (legally) no bst files
540 // (if the class already provides a style)
541 if (bst.isEmpty() && params_["bibfiles"].empty())
548 bool GuiBibtex::initialiseParams(std::string const & data)
550 InsetCommand::string2params("bibtex", data, params_);
555 void GuiBibtex::dispatchParams()
557 std::string const lfun = InsetCommand::params2string("bibtex", params_);
558 dispatch(FuncRequest(getLfun(), lfun));
563 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
566 } // namespace frontend
569 #include "moc_GuiBibtex.cpp"