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::updateContents()
283 bool bibtopic = usingBibtopic();
287 docstring bibs = params_["bibfiles"];
288 docstring embs = params_["embed"];
292 while (!bibs.empty()) {
293 bibs = split(bibs, bib, ',');
294 embs = split(embs, emb, ',');
297 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
298 db->setFlags(db->flags() | Qt::ItemIsSelectable
299 | Qt::ItemIsUserCheckable);
300 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
301 databaseLW->addItem(db);
305 add_->bibLW->clear();
307 vector<string> bib_str;
308 getBibFiles(bib_str);
309 for (vector<string>::const_iterator it = bib_str.begin();
310 it != bib_str.end(); ++it) {
311 string bibItem(changeExtension(*it, ""));
312 add_->bibLW->addItem(toqstr(bibItem));
315 string bibstyle = getStylefile();
317 bibtocCB->setChecked(bibtotoc() && !bibtopic);
318 bibtocCB->setEnabled(!bibtopic);
320 if (!bibtopic && btPrintCO->count() == 3)
321 btPrintCO->removeItem(1);
322 else if (bibtopic && btPrintCO->count() < 3)
323 btPrintCO->insertItem(1, qt_("all uncited references", 0));
325 docstring btprint = params_["btprint"];
327 if ((bibtopic && btprint == "btPrintNotCited") ||
328 (!bibtopic && btprint == "btPrintAll"))
330 else if (bibtopic && btprint == "btPrintAll")
333 btPrintCO->setCurrentIndex(btp);
341 for (vector<string>::const_iterator it = str.begin();
342 it != str.end(); ++it) {
343 string item(changeExtension(*it, ""));
344 if (item == bibstyle)
345 item_nr = int(it - str.begin());
346 styleCB->addItem(toqstr(item));
349 if (item_nr == -1 && !bibstyle.empty()) {
350 styleCB->addItem(toqstr(bibstyle));
351 item_nr = styleCB->count() - 1;
355 styleCB->setCurrentIndex(item_nr);
357 styleCB->clearEditText();
361 void GuiBibtex::applyView()
363 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
364 docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
366 unsigned int maxCount = databaseLW->count();
367 for (unsigned int i = 1; i < maxCount; i++) {
369 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
371 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
374 params_["bibfiles"] = dbs;
375 params_["embed"] = emb;
377 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
378 bool const bibtotoc = bibtocCB->isChecked();
380 if (bibtotoc && (!bibstyle.empty())) {
381 // both bibtotoc and style
382 params_["options"] = "bibtotoc," + bibstyle;
383 } else if (bibtotoc) {
384 // bibtotoc and no style
385 params_["options"] = from_ascii("bibtotoc");
387 // only style. An empty one is valid, because some
388 // documentclasses have an own \bibliographystyle{}
390 params_["options"] = bibstyle;
393 int btp = btPrintCO->currentIndex();
395 if (usingBibtopic()) {
396 // bibtopic allows three kinds of sections:
397 // 1. sections that include all cited references of the database(s)
398 // 2. sections that include all uncited references of the database(s)
399 // 3. sections that include all references of the database(s), cited or not
402 params_["btprint"] = from_ascii("btPrintCited");
405 params_["btprint"] = from_ascii("btPrintNotCited");
408 params_["btprint"] = from_ascii("btPrintAll");
414 params_["btprint"] = docstring();
418 params_["btprint"] = from_ascii("btPrintAll");
425 bool GuiBibtex::isValid()
427 return databaseLW->count() != 0;
431 docstring const GuiBibtex::browseBib(docstring const & in_name) const
434 docstring const label1 = _("Documents|#o#O");
435 docstring const dir1 = from_utf8(lyxrc.document_path);
436 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
437 return browseRelFile(in_name, from_utf8(bufferFilepath()),
438 _("Select a BibTeX database to add"), filter, false, label1, dir1);
442 docstring const GuiBibtex::browseBst(docstring const & in_name) const
445 docstring const label1 = _("Documents|#o#O");
446 docstring const dir1 = from_utf8(lyxrc.document_path);
447 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
448 return browseRelFile(in_name, from_utf8(bufferFilepath()),
449 _("Select a BibTeX style"), filter, false, label1, dir1);
453 void GuiBibtex::getBibStyles(vector<string> & data) const
457 getTexFileList("bstFiles.lst", data);
458 // test, if we have a valid list, otherwise run rescan
461 getTexFileList("bstFiles.lst", data);
463 vector<string>::iterator it = data.begin();
464 vector<string>::iterator end = data.end();
465 for (; it != end; ++it) {
466 *it = onlyFilename(*it);
468 // sort on filename only (no path)
469 sort(data.begin(), data.end());
473 void GuiBibtex::getBibFiles(vector<string> & data) const
477 getTexFileList("bibFiles.lst", data);
478 // test, if we have a valid list, otherwise run rescan
481 getTexFileList("bibFiles.lst", data);
483 vector<string>::iterator it = data.begin();
484 vector<string>::iterator end = data.end();
485 for (; it != end; ++it) {
486 *it = onlyFilename(*it);
488 // sort on filename only (no path)
489 sort(data.begin(), data.end());
493 void GuiBibtex::rescanBibStyles() const
499 bool GuiBibtex::usingBibtopic() const
501 return buffer().params().use_bibtopic;
505 bool GuiBibtex::bibtotoc() const
507 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
511 string const GuiBibtex::getStylefile() const
513 // the different bibtex packages have (and need) their
514 // own "plain" stylefiles
515 biblio::CiteEngine const engine = buffer().params().getEngine();
516 docstring defaultstyle;
518 case biblio::ENGINE_BASIC:
519 defaultstyle = from_ascii("plain");
521 case biblio::ENGINE_NATBIB_AUTHORYEAR:
522 defaultstyle = from_ascii("plainnat");
524 case biblio::ENGINE_NATBIB_NUMERICAL:
525 defaultstyle = from_ascii("plainnat");
527 case biblio::ENGINE_JURABIB:
528 defaultstyle = from_ascii("jurabib");
532 docstring bst = params_["options"];
535 if (contains(bst, ',')) {
536 docstring bibtotoc = from_ascii("bibtotoc");
537 bst = split(bst, bibtotoc, ',');
542 // propose default style file for new insets
543 // existing insets might have (legally) no bst files
544 // (if the class already provides a style)
545 if (bst.empty() && params_["bibfiles"].empty())
553 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
556 } // namespace frontend
559 #include "GuiBibtex_moc.cpp"