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"
25 #include "support/gettext.h"
27 #include "ButtonPolicy.h"
29 #include "support/filetools.h" // changeExtension
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")
51 setViewTitle( _("BibTeX Bibliography"));
53 QDialog::setModal(true);
55 connect(okPB, SIGNAL(clicked()),
56 this, SLOT(slotOK()));
57 connect(closePB, SIGNAL(clicked()),
58 this, SLOT(slotClose()));
59 connect(stylePB, SIGNAL(clicked()),
60 this, SLOT(browsePressed()));
61 connect(deletePB, SIGNAL(clicked()),
62 this, SLOT(deletePressed()));
63 connect(upPB, SIGNAL(clicked()),
64 this, SLOT(upPressed()));
65 connect(downPB, SIGNAL(clicked()),
66 this, SLOT(downPressed()));
67 connect(styleCB, SIGNAL(editTextChanged(QString)),
68 this, SLOT(change_adaptor()));
69 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
70 this, SLOT(databaseChanged()));
71 connect(bibtocCB, SIGNAL(clicked()),
72 this, SLOT(change_adaptor()));
73 connect(btPrintCO, SIGNAL(activated(int)),
74 this, SLOT(change_adaptor()));
75 connect(addBibPB, SIGNAL(clicked()),
76 this, SLOT(addPressed()));
78 add_ = new GuiBibtexAddDialog(this);
79 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
80 add_bc_.setOK(add_->addPB);
81 add_bc_.setCancel(add_->closePB);
82 add_bc_.addCheckedLineEdit(add_->bibED, 0);
84 connect(add_->bibED, SIGNAL(textChanged(QString)),
85 this, SLOT(bibEDChanged()));
86 connect(add_->addPB, SIGNAL(clicked()),
87 this, SLOT(addDatabase()));
88 connect(add_->addPB, SIGNAL(clicked()),
89 add_, SLOT(accept()));
90 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
91 this, SLOT(addDatabase()));
92 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
93 add_, SLOT(accept()));
94 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
95 this, SLOT(availableChanged()));
96 connect(add_->browsePB, SIGNAL(clicked()),
97 this, SLOT(browseBibPressed()));
98 connect(add_->closePB, SIGNAL(clicked()),
99 add_, SLOT(reject()));
101 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
103 bc().setCancel(closePB);
104 bc().addReadOnly(databaseLW);
105 bc().addReadOnly(stylePB);
106 bc().addReadOnly(styleCB);
107 bc().addReadOnly(bibtocCB);
108 bc().addReadOnly(addBibPB);
109 // Delete/Up/Down are handled with more conditions in
110 // databaseChanged().
112 // Make sure the delete/up/down buttons are disabled if necessary.
117 void GuiBibtex::bibEDChanged()
119 // Indicate to the button controller that the contents have
120 // changed. The actual test of validity is carried out by
121 // the checkedLineEdit.
122 add_bc_.setValid(true);
126 void GuiBibtex::change_adaptor()
132 void GuiBibtex::browsePressed()
134 docstring const file = browseBst(docstring());
138 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
139 bool present = false;
140 unsigned int pres = 0;
142 for (int i = 0; i != styleCB->count(); ++i) {
143 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
150 styleCB->insertItem(0, toqstr(filen));
152 styleCB->setCurrentIndex(pres);
158 void GuiBibtex::browseBibPressed()
160 docstring const file = trim(browseBib(docstring()));
164 QString const f = toqstr(changeExtension(to_utf8(file), ""));
165 bool present = false;
167 for (int i = 0; i < add_->bibLW->count(); ++i) {
168 if (add_->bibLW->item(i)->text() == f)
173 add_->bibLW->addItem(f);
177 add_->bibED->setText(f);
182 void GuiBibtex::addPressed()
184 add_bc_.setValid(false);
189 void GuiBibtex::addDatabase()
191 int const sel = add_->bibLW->currentRow();
192 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
194 if (sel < 0 && file.empty())
197 // Add the selected browser_bib keys to browser_database
198 // multiple selections are possible
199 for (int i = 0; i != add_->bibLW->count(); ++i) {
200 QListWidgetItem * const item = add_->bibLW->item(i);
201 if (add_->bibLW->isItemSelected(item)) {
202 add_->bibLW->setItemSelected(item, false);
203 QList<QListWidgetItem *> matches =
204 databaseLW->findItems(item->text(), Qt::MatchExactly);
205 if (matches.empty()) {
206 QString label = item->text();
207 QListWidgetItem * db = new QListWidgetItem(label);
208 db->setFlags(db->flags() | Qt::ItemIsSelectable
209 | Qt::ItemIsUserCheckable);
210 db->setCheckState(Qt::Checked);
211 databaseLW->addItem(db);
217 add_->bibED->clear();
218 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
219 QList<QListWidgetItem *> matches =
220 databaseLW->findItems(f, Qt::MatchExactly);
221 if (matches.empty()) {
222 QListWidgetItem * db = new QListWidgetItem(f);
223 db->setFlags(db->flags() | Qt::ItemIsSelectable
224 | Qt::ItemIsUserCheckable);
225 db->setCheckState(Qt::Checked);
226 databaseLW->addItem(db);
235 void GuiBibtex::deletePressed()
237 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
246 void GuiBibtex::upPressed()
248 int row = databaseLW->currentRow();
249 QListWidgetItem *cur = databaseLW->takeItem(row);
250 databaseLW->insertItem(row - 1, cur);
251 databaseLW->setCurrentItem(cur);
256 void GuiBibtex::downPressed()
258 int row = databaseLW->currentRow();
259 QListWidgetItem *cur = databaseLW->takeItem(row);
260 databaseLW->insertItem(row + 1, cur);
261 databaseLW->setCurrentItem(cur);
266 void GuiBibtex::databaseChanged()
268 bool readOnly = isBufferReadonly();
269 int count = databaseLW->count();
270 int row = databaseLW->currentRow();
271 deletePB->setEnabled(!readOnly && row != -1);
272 upPB->setEnabled(!readOnly && count > 1 && row > 0);
273 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
277 void GuiBibtex::availableChanged()
279 add_bc_.setValid(true);
283 void GuiBibtex::closeEvent(QCloseEvent *e)
290 void GuiBibtex::updateContents()
292 bool bibtopic = usingBibtopic();
296 docstring bibs = params_["bibfiles"];
297 docstring embs = params_["embed"];
301 while (!bibs.empty()) {
302 bibs = split(bibs, bib, ',');
303 embs = split(embs, emb, ',');
306 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
307 db->setFlags(db->flags() | Qt::ItemIsSelectable
308 | Qt::ItemIsUserCheckable);
309 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
310 databaseLW->addItem(db);
314 add_->bibLW->clear();
316 vector<string> bib_str;
317 getBibFiles(bib_str);
318 for (vector<string>::const_iterator it = bib_str.begin();
319 it != bib_str.end(); ++it) {
320 string bibItem(changeExtension(*it, ""));
321 add_->bibLW->addItem(toqstr(bibItem));
324 string bibstyle = getStylefile();
326 bibtocCB->setChecked(bibtotoc() && !bibtopic);
327 bibtocCB->setEnabled(!bibtopic);
329 if (!bibtopic && btPrintCO->count() == 3)
330 btPrintCO->removeItem(1);
331 else if (bibtopic && btPrintCO->count() < 3)
332 btPrintCO->insertItem(1, qt_("all uncited references", 0));
334 docstring btprint = params_["btprint"];
336 if ((bibtopic && btprint == "btPrintNotCited") ||
337 (!bibtopic && btprint == "btPrintAll"))
339 else if (bibtopic && btprint == "btPrintAll")
342 btPrintCO->setCurrentIndex(btp);
350 for (vector<string>::const_iterator it = str.begin();
351 it != str.end(); ++it) {
352 string item(changeExtension(*it, ""));
353 if (item == bibstyle)
354 item_nr = int(it - str.begin());
355 styleCB->addItem(toqstr(item));
358 if (item_nr == -1 && !bibstyle.empty()) {
359 styleCB->addItem(toqstr(bibstyle));
360 item_nr = styleCB->count() - 1;
364 styleCB->setCurrentIndex(item_nr);
366 styleCB->clearEditText();
370 void GuiBibtex::applyView()
372 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
373 docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
375 unsigned int maxCount = databaseLW->count();
376 for (unsigned int i = 1; i < maxCount; i++) {
378 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
380 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
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 docstring const GuiBibtex::browseBib(docstring const & in_name) const
443 docstring const label1 = _("Documents|#o#O");
444 docstring const dir1 = from_utf8(lyxrc.document_path);
445 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
446 return browseRelFile(in_name, from_utf8(bufferFilepath()),
447 _("Select a BibTeX database to add"), filter, false, label1, dir1);
451 docstring const GuiBibtex::browseBst(docstring const & in_name) const
454 docstring const label1 = _("Documents|#o#O");
455 docstring const dir1 = from_utf8(lyxrc.document_path);
456 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
457 return browseRelFile(in_name, from_utf8(bufferFilepath()),
458 _("Select a BibTeX style"), filter, false, label1, dir1);
462 void GuiBibtex::getBibStyles(vector<string> & data) const
466 getTexFileList("bstFiles.lst", data);
467 // test, if we have a valid list, otherwise run rescan
470 getTexFileList("bstFiles.lst", data);
472 vector<string>::iterator it = data.begin();
473 vector<string>::iterator end = data.end();
474 for (; it != end; ++it) {
475 *it = onlyFilename(*it);
477 // sort on filename only (no path)
478 sort(data.begin(), data.end());
482 void GuiBibtex::getBibFiles(vector<string> & data) const
486 getTexFileList("bibFiles.lst", data);
487 // test, if we have a valid list, otherwise run rescan
490 getTexFileList("bibFiles.lst", data);
492 vector<string>::iterator it = data.begin();
493 vector<string>::iterator end = data.end();
494 for (; it != end; ++it) {
495 *it = onlyFilename(*it);
497 // sort on filename only (no path)
498 sort(data.begin(), data.end());
502 void GuiBibtex::rescanBibStyles() const
508 bool GuiBibtex::usingBibtopic() const
510 return buffer().params().use_bibtopic;
514 bool GuiBibtex::bibtotoc() const
516 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
520 string const GuiBibtex::getStylefile() const
522 // the different bibtex packages have (and need) their
523 // own "plain" stylefiles
524 biblio::CiteEngine const engine = buffer().params().getEngine();
525 docstring defaultstyle;
527 case biblio::ENGINE_BASIC:
528 defaultstyle = from_ascii("plain");
530 case biblio::ENGINE_NATBIB_AUTHORYEAR:
531 defaultstyle = from_ascii("plainnat");
533 case biblio::ENGINE_NATBIB_NUMERICAL:
534 defaultstyle = from_ascii("plainnat");
536 case biblio::ENGINE_JURABIB:
537 defaultstyle = from_ascii("jurabib");
541 docstring bst = params_["options"];
544 if (contains(bst, ',')) {
545 docstring bibtotoc = from_ascii("bibtotoc");
546 bst = split(bst, bibtotoc, ',');
551 // propose default style file for new insets
552 // existing insets might have (legally) no bst files
553 // (if the class already provides a style)
554 if (bst.empty() && params_["bibfiles"].empty())
562 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
565 } // namespace frontend
568 #include "GuiBibtex_moc.cpp"