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 "ui_BibtexAddUi.h"
21 #include "qt_helpers.h"
22 #include "Validator.h"
25 #include "ButtonPolicy.h"
27 #include "support/debug.h"
28 #include "support/FileFilterList.h"
29 #include "support/filetools.h" // changeExtension
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
33 #include <QPushButton>
34 #include <QListWidget>
39 using namespace lyx::support;
45 GuiBibtex::GuiBibtex(GuiView & lv)
46 : GuiCommand(lv, "bibtex", qt_("BibTeX Bibliography"))
50 QDialog::setModal(true);
52 connect(okPB, SIGNAL(clicked()),
53 this, SLOT(slotOK()));
54 connect(closePB, SIGNAL(clicked()),
55 this, SLOT(slotClose()));
56 connect(stylePB, SIGNAL(clicked()),
57 this, SLOT(browsePressed()));
58 connect(deletePB, SIGNAL(clicked()),
59 this, SLOT(deletePressed()));
60 connect(upPB, SIGNAL(clicked()),
61 this, SLOT(upPressed()));
62 connect(downPB, SIGNAL(clicked()),
63 this, SLOT(downPressed()));
64 connect(styleCB, SIGNAL(editTextChanged(QString)),
65 this, SLOT(change_adaptor()));
66 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
67 this, SLOT(databaseChanged()));
68 connect(bibtocCB, SIGNAL(clicked()),
69 this, SLOT(change_adaptor()));
70 connect(btPrintCO, SIGNAL(activated(int)),
71 this, SLOT(change_adaptor()));
72 connect(addBibPB, SIGNAL(clicked()),
73 this, SLOT(addPressed()));
75 add_ = new GuiBibtexAddDialog(this);
76 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
77 add_bc_.setOK(add_->addPB);
78 add_bc_.setCancel(add_->closePB);
79 add_bc_.addCheckedLineEdit(add_->bibED, 0);
81 connect(add_->bibED, SIGNAL(textChanged(QString)),
82 this, SLOT(bibEDChanged()));
83 connect(add_->addPB, SIGNAL(clicked()),
84 this, SLOT(addDatabase()));
85 connect(add_->addPB, SIGNAL(clicked()),
86 add_, SLOT(accept()));
87 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
88 this, SLOT(addDatabase()));
89 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
90 add_, SLOT(accept()));
91 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
92 this, SLOT(availableChanged()));
93 connect(add_->browsePB, SIGNAL(clicked()),
94 this, SLOT(browseBibPressed()));
95 connect(add_->closePB, SIGNAL(clicked()),
96 add_, SLOT(reject()));
98 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
100 bc().setCancel(closePB);
101 bc().addReadOnly(databaseLW);
102 bc().addReadOnly(stylePB);
103 bc().addReadOnly(styleCB);
104 bc().addReadOnly(bibtocCB);
105 bc().addReadOnly(addBibPB);
106 // Delete/Up/Down are handled with more conditions in
107 // databaseChanged().
109 // Make sure the delete/up/down buttons are disabled if necessary.
114 void GuiBibtex::bibEDChanged()
116 // Indicate to the button controller that the contents have
117 // changed. The actual test of validity is carried out by
118 // the checkedLineEdit.
119 add_bc_.setValid(true);
123 void GuiBibtex::change_adaptor()
129 void GuiBibtex::browsePressed()
131 docstring const file = browseBst(docstring());
135 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
136 bool present = false;
137 unsigned int pres = 0;
139 for (int i = 0; i != styleCB->count(); ++i) {
140 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
147 styleCB->insertItem(0, toqstr(filen));
149 styleCB->setCurrentIndex(pres);
155 void GuiBibtex::browseBibPressed()
157 docstring const file = trim(browseBib(docstring()));
161 QString const f = toqstr(changeExtension(to_utf8(file), ""));
162 bool present = false;
164 for (int i = 0; i < add_->bibLW->count(); ++i) {
165 if (add_->bibLW->item(i)->text() == f)
170 add_->bibLW->addItem(f);
174 add_->bibED->setText(f);
179 void GuiBibtex::addPressed()
181 add_bc_.setValid(false);
186 void GuiBibtex::addDatabase()
188 int const sel = add_->bibLW->currentRow();
189 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
191 if (sel < 0 && file.empty())
194 // Add the selected browser_bib keys to browser_database
195 // multiple selections are possible
196 for (int i = 0; i != add_->bibLW->count(); ++i) {
197 QListWidgetItem * const item = add_->bibLW->item(i);
198 if (add_->bibLW->isItemSelected(item)) {
199 add_->bibLW->setItemSelected(item, false);
200 QList<QListWidgetItem *> matches =
201 databaseLW->findItems(item->text(), Qt::MatchExactly);
202 if (matches.empty()) {
203 QString label = item->text();
204 QListWidgetItem * db = new QListWidgetItem(label);
205 db->setFlags(db->flags() | Qt::ItemIsSelectable
206 | Qt::ItemIsUserCheckable);
207 db->setCheckState(Qt::Checked);
208 databaseLW->addItem(db);
214 add_->bibED->clear();
215 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
216 QList<QListWidgetItem *> matches =
217 databaseLW->findItems(f, Qt::MatchExactly);
218 if (matches.empty()) {
219 QListWidgetItem * db = new QListWidgetItem(f);
220 db->setFlags(db->flags() | Qt::ItemIsSelectable
221 | Qt::ItemIsUserCheckable);
222 db->setCheckState(Qt::Checked);
223 databaseLW->addItem(db);
232 void GuiBibtex::deletePressed()
234 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
243 void GuiBibtex::upPressed()
245 int row = databaseLW->currentRow();
246 QListWidgetItem *cur = databaseLW->takeItem(row);
247 databaseLW->insertItem(row - 1, cur);
248 databaseLW->setCurrentItem(cur);
253 void GuiBibtex::downPressed()
255 int row = databaseLW->currentRow();
256 QListWidgetItem *cur = databaseLW->takeItem(row);
257 databaseLW->insertItem(row + 1, cur);
258 databaseLW->setCurrentItem(cur);
263 void GuiBibtex::databaseChanged()
265 bool readOnly = isBufferReadonly();
266 int count = databaseLW->count();
267 int row = databaseLW->currentRow();
268 deletePB->setEnabled(!readOnly && row != -1);
269 upPB->setEnabled(!readOnly && count > 1 && row > 0);
270 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
274 void GuiBibtex::availableChanged()
276 add_bc_.setValid(true);
280 void GuiBibtex::updateContents()
282 bool bibtopic = usingBibtopic();
286 docstring bibs = params_["bibfiles"];
287 docstring embs = params_["embed"];
291 while (!bibs.empty()) {
292 bibs = split(bibs, bib, ',');
293 embs = split(embs, emb, ',');
296 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
297 db->setFlags(db->flags() | Qt::ItemIsSelectable
298 | Qt::ItemIsUserCheckable);
299 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
300 databaseLW->addItem(db);
304 add_->bibLW->clear();
306 vector<string> bib_str;
307 getBibFiles(bib_str);
308 for (vector<string>::const_iterator it = bib_str.begin();
309 it != bib_str.end(); ++it) {
310 string bibItem(changeExtension(*it, ""));
311 add_->bibLW->addItem(toqstr(bibItem));
314 string bibstyle = getStylefile();
316 bibtocCB->setChecked(bibtotoc() && !bibtopic);
317 bibtocCB->setEnabled(!bibtopic);
319 if (!bibtopic && btPrintCO->count() == 3)
320 btPrintCO->removeItem(1);
321 else if (bibtopic && btPrintCO->count() < 3)
322 btPrintCO->insertItem(1, qt_("all uncited references", 0));
324 docstring btprint = params_["btprint"];
326 if ((bibtopic && btprint == "btPrintNotCited") ||
327 (!bibtopic && btprint == "btPrintAll"))
329 else if (bibtopic && btprint == "btPrintAll")
332 btPrintCO->setCurrentIndex(btp);
340 for (vector<string>::const_iterator it = str.begin();
341 it != str.end(); ++it) {
342 string item(changeExtension(*it, ""));
343 if (item == bibstyle)
344 item_nr = int(it - str.begin());
345 styleCB->addItem(toqstr(item));
348 if (item_nr == -1 && !bibstyle.empty()) {
349 styleCB->addItem(toqstr(bibstyle));
350 item_nr = styleCB->count() - 1;
354 styleCB->setCurrentIndex(item_nr);
356 styleCB->clearEditText();
360 void GuiBibtex::applyView()
362 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
363 docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
365 unsigned int maxCount = databaseLW->count();
366 for (unsigned int i = 1; i < maxCount; i++) {
368 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
370 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
373 params_["bibfiles"] = dbs;
374 params_["embed"] = emb;
376 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
377 bool const bibtotoc = bibtocCB->isChecked();
379 if (bibtotoc && (!bibstyle.empty())) {
380 // both bibtotoc and style
381 params_["options"] = "bibtotoc," + bibstyle;
382 } else if (bibtotoc) {
383 // bibtotoc and no style
384 params_["options"] = from_ascii("bibtotoc");
386 // only style. An empty one is valid, because some
387 // documentclasses have an own \bibliographystyle{}
389 params_["options"] = bibstyle;
392 int btp = btPrintCO->currentIndex();
394 if (usingBibtopic()) {
395 // bibtopic allows three kinds of sections:
396 // 1. sections that include all cited references of the database(s)
397 // 2. sections that include all uncited references of the database(s)
398 // 3. sections that include all references of the database(s), cited or not
401 params_["btprint"] = from_ascii("btPrintCited");
404 params_["btprint"] = from_ascii("btPrintNotCited");
407 params_["btprint"] = from_ascii("btPrintAll");
413 params_["btprint"] = docstring();
417 params_["btprint"] = from_ascii("btPrintAll");
424 bool GuiBibtex::isValid()
426 return databaseLW->count() != 0;
430 docstring const GuiBibtex::browseBib(docstring const & in_name) const
433 docstring const label1 = _("Documents|#o#O");
434 docstring const dir1 = from_utf8(lyxrc.document_path);
435 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
436 return browseRelFile(in_name, from_utf8(bufferFilepath()),
437 _("Select a BibTeX database to add"), filter, false, label1, dir1);
441 docstring const GuiBibtex::browseBst(docstring const & in_name) const
444 docstring const label1 = _("Documents|#o#O");
445 docstring const dir1 = from_utf8(lyxrc.document_path);
446 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
447 return browseRelFile(in_name, from_utf8(bufferFilepath()),
448 _("Select a BibTeX style"), filter, false, label1, dir1);
452 void GuiBibtex::getBibStyles(vector<string> & data) const
456 getTexFileList("bstFiles.lst", data);
457 // test, if we have a valid list, otherwise run rescan
460 getTexFileList("bstFiles.lst", data);
462 vector<string>::iterator it = data.begin();
463 vector<string>::iterator end = data.end();
464 for (; it != end; ++it)
465 *it = support::onlyFilename(*it);
466 // sort on filename only (no path)
467 sort(data.begin(), data.end());
471 void GuiBibtex::getBibFiles(vector<string> & data) const
475 getTexFileList("bibFiles.lst", data);
476 // test, if we have a valid list, otherwise run rescan
479 getTexFileList("bibFiles.lst", data);
481 vector<string>::iterator it = data.begin();
482 vector<string>::iterator end = data.end();
483 for (; it != end; ++it)
484 *it = support::onlyFilename(*it);
485 // sort on filename only (no path)
486 sort(data.begin(), data.end());
490 void GuiBibtex::rescanBibStyles() const
496 bool GuiBibtex::usingBibtopic() const
498 return buffer().params().use_bibtopic;
502 bool GuiBibtex::bibtotoc() const
504 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
508 string const GuiBibtex::getStylefile() const
510 // the different bibtex packages have (and need) their
511 // own "plain" stylefiles
512 biblio::CiteEngine const engine = buffer().params().getEngine();
513 docstring defaultstyle;
515 case biblio::ENGINE_BASIC:
516 defaultstyle = from_ascii("plain");
518 case biblio::ENGINE_NATBIB_AUTHORYEAR:
519 defaultstyle = from_ascii("plainnat");
521 case biblio::ENGINE_NATBIB_NUMERICAL:
522 defaultstyle = from_ascii("plainnat");
524 case biblio::ENGINE_JURABIB:
525 defaultstyle = from_ascii("jurabib");
529 docstring bst = params_["options"];
532 if (contains(bst, ',')) {
533 docstring bibtotoc = from_ascii("bibtotoc");
534 bst = split(bst, bibtotoc, ',');
539 // propose default style file for new insets
540 // existing insets might have (legally) no bst files
541 // (if the class already provides a style)
542 if (bst.empty() && params_["bibfiles"].empty())
550 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
553 } // namespace frontend
556 #include "GuiBibtex_moc.cpp"