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 "support/debug.h"
21 #include "ui_BibtexAddUi.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
26 #include "ButtonPolicy.h"
28 #include "support/filetools.h" // changeExtension
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31 #include "support/FileFilterList.h"
33 #include <QPushButton>
34 #include <QListWidget>
36 #include <QCloseEvent>
40 using namespace lyx::support;
46 GuiBibtex::GuiBibtex(GuiView & lv)
47 : GuiCommand(lv, "bibtex", qt_("BibTeX Bibliography"))
51 QDialog::setModal(true);
53 connect(okPB, SIGNAL(clicked()),
54 this, SLOT(slotOK()));
55 connect(closePB, SIGNAL(clicked()),
56 this, SLOT(slotClose()));
57 connect(stylePB, SIGNAL(clicked()),
58 this, SLOT(browsePressed()));
59 connect(deletePB, SIGNAL(clicked()),
60 this, SLOT(deletePressed()));
61 connect(upPB, SIGNAL(clicked()),
62 this, SLOT(upPressed()));
63 connect(downPB, SIGNAL(clicked()),
64 this, SLOT(downPressed()));
65 connect(styleCB, SIGNAL(editTextChanged(QString)),
66 this, SLOT(change_adaptor()));
67 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
68 this, SLOT(databaseChanged()));
69 connect(bibtocCB, SIGNAL(clicked()),
70 this, SLOT(change_adaptor()));
71 connect(btPrintCO, SIGNAL(activated(int)),
72 this, SLOT(change_adaptor()));
73 connect(addBibPB, SIGNAL(clicked()),
74 this, SLOT(addPressed()));
76 add_ = new GuiBibtexAddDialog(this);
77 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
78 add_bc_.setOK(add_->addPB);
79 add_bc_.setCancel(add_->closePB);
80 add_bc_.addCheckedLineEdit(add_->bibED, 0);
82 connect(add_->bibED, SIGNAL(textChanged(QString)),
83 this, SLOT(bibEDChanged()));
84 connect(add_->addPB, SIGNAL(clicked()),
85 this, SLOT(addDatabase()));
86 connect(add_->addPB, SIGNAL(clicked()),
87 add_, SLOT(accept()));
88 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
89 this, SLOT(addDatabase()));
90 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
91 add_, SLOT(accept()));
92 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
93 this, SLOT(availableChanged()));
94 connect(add_->browsePB, SIGNAL(clicked()),
95 this, SLOT(browseBibPressed()));
96 connect(add_->closePB, SIGNAL(clicked()),
97 add_, SLOT(reject()));
99 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
101 bc().setCancel(closePB);
102 bc().addReadOnly(databaseLW);
103 bc().addReadOnly(stylePB);
104 bc().addReadOnly(styleCB);
105 bc().addReadOnly(bibtocCB);
106 bc().addReadOnly(addBibPB);
107 // Delete/Up/Down are handled with more conditions in
108 // databaseChanged().
110 // Make sure the delete/up/down buttons are disabled if necessary.
115 void GuiBibtex::bibEDChanged()
117 // Indicate to the button controller that the contents have
118 // changed. The actual test of validity is carried out by
119 // the checkedLineEdit.
120 add_bc_.setValid(true);
124 void GuiBibtex::change_adaptor()
130 void GuiBibtex::browsePressed()
132 docstring const file = browseBst(docstring());
136 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
137 bool present = false;
138 unsigned int pres = 0;
140 for (int i = 0; i != styleCB->count(); ++i) {
141 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
148 styleCB->insertItem(0, toqstr(filen));
150 styleCB->setCurrentIndex(pres);
156 void GuiBibtex::browseBibPressed()
158 docstring const file = trim(browseBib(docstring()));
162 QString const f = toqstr(changeExtension(to_utf8(file), ""));
163 bool present = false;
165 for (int i = 0; i < add_->bibLW->count(); ++i) {
166 if (add_->bibLW->item(i)->text() == f)
171 add_->bibLW->addItem(f);
175 add_->bibED->setText(f);
180 void GuiBibtex::addPressed()
182 add_bc_.setValid(false);
187 void GuiBibtex::addDatabase()
189 int const sel = add_->bibLW->currentRow();
190 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
192 if (sel < 0 && file.empty())
195 // Add the selected browser_bib keys to browser_database
196 // multiple selections are possible
197 for (int i = 0; i != add_->bibLW->count(); ++i) {
198 QListWidgetItem * const item = add_->bibLW->item(i);
199 if (add_->bibLW->isItemSelected(item)) {
200 add_->bibLW->setItemSelected(item, false);
201 QList<QListWidgetItem *> matches =
202 databaseLW->findItems(item->text(), Qt::MatchExactly);
203 if (matches.empty()) {
204 QString label = item->text();
205 QListWidgetItem * db = new QListWidgetItem(label);
206 db->setFlags(db->flags() | Qt::ItemIsSelectable
207 | Qt::ItemIsUserCheckable);
208 db->setCheckState(Qt::Checked);
209 databaseLW->addItem(db);
215 add_->bibED->clear();
216 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
217 QList<QListWidgetItem *> matches =
218 databaseLW->findItems(f, Qt::MatchExactly);
219 if (matches.empty()) {
220 QListWidgetItem * db = new QListWidgetItem(f);
221 db->setFlags(db->flags() | Qt::ItemIsSelectable
222 | Qt::ItemIsUserCheckable);
223 db->setCheckState(Qt::Checked);
224 databaseLW->addItem(db);
233 void GuiBibtex::deletePressed()
235 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
244 void GuiBibtex::upPressed()
246 int row = databaseLW->currentRow();
247 QListWidgetItem *cur = databaseLW->takeItem(row);
248 databaseLW->insertItem(row - 1, cur);
249 databaseLW->setCurrentItem(cur);
254 void GuiBibtex::downPressed()
256 int row = databaseLW->currentRow();
257 QListWidgetItem *cur = databaseLW->takeItem(row);
258 databaseLW->insertItem(row + 1, cur);
259 databaseLW->setCurrentItem(cur);
264 void GuiBibtex::databaseChanged()
266 bool readOnly = isBufferReadonly();
267 int count = databaseLW->count();
268 int row = databaseLW->currentRow();
269 deletePB->setEnabled(!readOnly && row != -1);
270 upPB->setEnabled(!readOnly && count > 1 && row > 0);
271 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
275 void GuiBibtex::availableChanged()
277 add_bc_.setValid(true);
281 void GuiBibtex::closeEvent(QCloseEvent *e)
288 void GuiBibtex::updateContents()
290 bool bibtopic = usingBibtopic();
294 docstring bibs = params_["bibfiles"];
295 docstring embs = params_["embed"];
299 while (!bibs.empty()) {
300 bibs = split(bibs, bib, ',');
301 embs = split(embs, emb, ',');
304 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
305 db->setFlags(db->flags() | Qt::ItemIsSelectable
306 | Qt::ItemIsUserCheckable);
307 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
308 databaseLW->addItem(db);
312 add_->bibLW->clear();
314 vector<string> bib_str;
315 getBibFiles(bib_str);
316 for (vector<string>::const_iterator it = bib_str.begin();
317 it != bib_str.end(); ++it) {
318 string bibItem(changeExtension(*it, ""));
319 add_->bibLW->addItem(toqstr(bibItem));
322 string bibstyle = getStylefile();
324 bibtocCB->setChecked(bibtotoc() && !bibtopic);
325 bibtocCB->setEnabled(!bibtopic);
327 if (!bibtopic && btPrintCO->count() == 3)
328 btPrintCO->removeItem(1);
329 else if (bibtopic && btPrintCO->count() < 3)
330 btPrintCO->insertItem(1, qt_("all uncited references", 0));
332 docstring btprint = params_["btprint"];
334 if ((bibtopic && btprint == "btPrintNotCited") ||
335 (!bibtopic && btprint == "btPrintAll"))
337 else if (bibtopic && btprint == "btPrintAll")
340 btPrintCO->setCurrentIndex(btp);
348 for (vector<string>::const_iterator it = str.begin();
349 it != str.end(); ++it) {
350 string item(changeExtension(*it, ""));
351 if (item == bibstyle)
352 item_nr = int(it - str.begin());
353 styleCB->addItem(toqstr(item));
356 if (item_nr == -1 && !bibstyle.empty()) {
357 styleCB->addItem(toqstr(bibstyle));
358 item_nr = styleCB->count() - 1;
362 styleCB->setCurrentIndex(item_nr);
364 styleCB->clearEditText();
368 void GuiBibtex::applyView()
370 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
371 docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
373 unsigned int maxCount = databaseLW->count();
374 for (unsigned int i = 1; i < maxCount; i++) {
376 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
378 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
381 params_["bibfiles"] = dbs;
382 params_["embed"] = emb;
384 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
385 bool const bibtotoc = bibtocCB->isChecked();
387 if (bibtotoc && (!bibstyle.empty())) {
388 // both bibtotoc and style
389 params_["options"] = "bibtotoc," + bibstyle;
390 } else if (bibtotoc) {
391 // bibtotoc and no style
392 params_["options"] = from_ascii("bibtotoc");
394 // only style. An empty one is valid, because some
395 // documentclasses have an own \bibliographystyle{}
397 params_["options"] = bibstyle;
400 int btp = btPrintCO->currentIndex();
402 if (usingBibtopic()) {
403 // bibtopic allows three kinds of sections:
404 // 1. sections that include all cited references of the database(s)
405 // 2. sections that include all uncited references of the database(s)
406 // 3. sections that include all references of the database(s), cited or not
409 params_["btprint"] = from_ascii("btPrintCited");
412 params_["btprint"] = from_ascii("btPrintNotCited");
415 params_["btprint"] = from_ascii("btPrintAll");
421 params_["btprint"] = docstring();
425 params_["btprint"] = from_ascii("btPrintAll");
432 bool GuiBibtex::isValid()
434 return databaseLW->count() != 0;
438 docstring const GuiBibtex::browseBib(docstring const & in_name) const
441 docstring const label1 = _("Documents|#o#O");
442 docstring const dir1 = from_utf8(lyxrc.document_path);
443 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
444 return browseRelFile(in_name, from_utf8(bufferFilepath()),
445 _("Select a BibTeX database to add"), filter, false, label1, dir1);
449 docstring const GuiBibtex::browseBst(docstring const & in_name) const
452 docstring const label1 = _("Documents|#o#O");
453 docstring const dir1 = from_utf8(lyxrc.document_path);
454 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
455 return browseRelFile(in_name, from_utf8(bufferFilepath()),
456 _("Select a BibTeX style"), filter, false, label1, dir1);
460 void GuiBibtex::getBibStyles(vector<string> & data) const
464 getTexFileList("bstFiles.lst", data);
465 // test, if we have a valid list, otherwise run rescan
468 getTexFileList("bstFiles.lst", data);
470 vector<string>::iterator it = data.begin();
471 vector<string>::iterator end = data.end();
472 for (; it != end; ++it) {
473 *it = onlyFilename(*it);
475 // sort on filename only (no path)
476 sort(data.begin(), data.end());
480 void GuiBibtex::getBibFiles(vector<string> & data) const
484 getTexFileList("bibFiles.lst", data);
485 // test, if we have a valid list, otherwise run rescan
488 getTexFileList("bibFiles.lst", data);
490 vector<string>::iterator it = data.begin();
491 vector<string>::iterator end = data.end();
492 for (; it != end; ++it) {
493 *it = onlyFilename(*it);
495 // sort on filename only (no path)
496 sort(data.begin(), data.end());
500 void GuiBibtex::rescanBibStyles() const
506 bool GuiBibtex::usingBibtopic() const
508 return buffer().params().use_bibtopic;
512 bool GuiBibtex::bibtotoc() const
514 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
518 string const GuiBibtex::getStylefile() const
520 // the different bibtex packages have (and need) their
521 // own "plain" stylefiles
522 biblio::CiteEngine const engine = buffer().params().getEngine();
523 docstring defaultstyle;
525 case biblio::ENGINE_BASIC:
526 defaultstyle = from_ascii("plain");
528 case biblio::ENGINE_NATBIB_AUTHORYEAR:
529 defaultstyle = from_ascii("plainnat");
531 case biblio::ENGINE_NATBIB_NUMERICAL:
532 defaultstyle = from_ascii("plainnat");
534 case biblio::ENGINE_JURABIB:
535 defaultstyle = from_ascii("jurabib");
539 docstring bst = params_["options"];
542 if (contains(bst, ',')) {
543 docstring bibtotoc = from_ascii("bibtotoc");
544 bst = split(bst, bibtotoc, ',');
549 // propose default style file for new insets
550 // existing insets might have (legally) no bst files
551 // (if the class already provides a style)
552 if (bst.empty() && params_["bibfiles"].empty())
560 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
563 } // namespace frontend
566 #include "GuiBibtex_moc.cpp"