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) {
376 FileName bibfilepath = InsetBibtex::getBibTeXPath(bibfile, buf);
378 EmbeddedFile::calcInzipName(bibfilepath.absFilename(), buf.filePath());
379 emb += from_utf8(inzipName);
383 params_["bibfiles"] = dbs;
384 params_["embed"] = emb;
386 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
387 bool const bibtotoc = bibtocCB->isChecked();
389 if (bibtotoc && !bibstyle.empty()) {
390 // both bibtotoc and style
391 params_["options"] = "bibtotoc," + bibstyle;
392 } else if (bibtotoc) {
393 // bibtotoc and no style
394 params_["options"] = from_ascii("bibtotoc");
396 // only style. An empty one is valid, because some
397 // documentclasses have an own \bibliographystyle{}
399 params_["options"] = bibstyle;
402 int btp = btPrintCO->currentIndex();
404 if (usingBibtopic()) {
405 // bibtopic allows three kinds of sections:
406 // 1. sections that include all cited references of the database(s)
407 // 2. sections that include all uncited references of the database(s)
408 // 3. sections that include all references of the database(s), cited or not
411 params_["btprint"] = from_ascii("btPrintCited");
414 params_["btprint"] = from_ascii("btPrintNotCited");
417 params_["btprint"] = from_ascii("btPrintAll");
423 params_["btprint"] = docstring();
427 params_["btprint"] = from_ascii("btPrintAll");
434 bool GuiBibtex::isValid()
436 return databaseLW->count() != 0;
440 QString GuiBibtex::browseBib(QString const & in_name) const
442 QString const label1 = qt_("Documents|#o#O");
443 QString const dir1 = toqstr(lyxrc.document_path);
444 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
445 return browseRelFile(in_name, bufferFilepath(),
446 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
450 QString GuiBibtex::browseBst(QString const & in_name) const
452 QString const label1 = qt_("Documents|#o#O");
453 QString const dir1 = toqstr(lyxrc.document_path);
454 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
455 return browseRelFile(in_name, bufferFilepath(),
456 qt_("Select a BibTeX style"), filter, false, label1, dir1);
460 QStringList GuiBibtex::bibStyles() const
462 QStringList data = texFileList("bstFiles.lst");
463 // test whether we have a valid list, otherwise run rescan
464 if (data.isEmpty()) {
466 data = texFileList("bstFiles.lst");
468 for (int i = 0; i != data.size(); ++i)
469 data[i] = onlyFilename(data[i]);
470 // sort on filename only (no path)
476 QStringList GuiBibtex::bibFiles() const
478 QStringList data = texFileList("bibFiles.lst");
479 // test whether we have a valid list, otherwise run rescan
480 if (data.isEmpty()) {
482 data = texFileList("bibFiles.lst");
484 for (int i = 0; i != data.size(); ++i)
485 data[i] = onlyFilename(data[i]);
486 // sort on filename only (no path)
492 void GuiBibtex::rescanBibStyles() const
498 bool GuiBibtex::usingBibtopic() const
500 return buffer().params().use_bibtopic;
504 bool GuiBibtex::bibtotoc() const
506 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
510 QString GuiBibtex::styleFile() const
512 // the different bibtex packages have (and need) their
513 // own "plain" stylefiles
514 biblio::CiteEngine const engine = buffer().params().citeEngine();
515 QString defaultstyle;
517 case biblio::ENGINE_BASIC:
518 defaultstyle = "plain";
520 case biblio::ENGINE_NATBIB_AUTHORYEAR:
521 defaultstyle = "plainnat";
523 case biblio::ENGINE_NATBIB_NUMERICAL:
524 defaultstyle = "plainnat";
526 case biblio::ENGINE_JURABIB:
527 defaultstyle = "jurabib";
531 QString bst = toqstr(params_["options"]);
534 int pos = bst.indexOf(',');
537 // docstring bibtotoc = from_ascii("bibtotoc");
538 // bst = split(bst, bibtotoc, ',');
545 // propose default style file for new insets
546 // existing insets might have (legally) no bst files
547 // (if the class already provides a style)
548 if (bst.isEmpty() && params_["bibfiles"].empty())
555 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
558 } // namespace frontend
561 #include "GuiBibtex_moc.cpp"