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"
21 #include "ui_BibtexAddUi.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
27 #include "ButtonPolicy.h"
29 #include "support/filetools.h" // changeExtension
30 #include "support/lstrings.h"
31 #include "support/FileFilterList.h"
33 #include "frontend_helpers.h"
36 #include <QPushButton>
37 #include <QListWidget>
39 #include <QCloseEvent>
50 using support::changeExtension;
51 using support::contains;
52 using support::FileFilterList;
53 using support::onlyFilename;
54 using support::prefixIs;
59 GuiBibtex::GuiBibtex(LyXView & lv)
60 : GuiDialog(lv, "bibtex"), ControlCommand(*this, "bibtex")
64 setViewTitle( _("BibTeX Bibliography"));
65 setController(this, false);
67 QDialog::setModal(true);
69 connect(okPB, SIGNAL(clicked()),
70 this, SLOT(slotOK()));
71 connect(closePB, SIGNAL(clicked()),
72 this, SLOT(slotClose()));
73 connect(stylePB, SIGNAL(clicked()),
74 this, SLOT(browsePressed()));
75 connect(deletePB, SIGNAL(clicked()),
76 this, SLOT(deletePressed()));
77 connect(styleCB, SIGNAL(editTextChanged(QString)),
78 this, SLOT(change_adaptor()));
79 connect(databaseLW, SIGNAL(itemSelectionChanged()),
80 this, SLOT(databaseChanged()));
81 connect(bibtocCB, SIGNAL(clicked()),
82 this, SLOT(change_adaptor()));
83 connect(btPrintCO, SIGNAL(activated(int)),
84 this, SLOT(change_adaptor()));
85 connect(addBibPB, SIGNAL(clicked()),
86 this, SLOT(addPressed()));
88 add_ = new GuiBibtexAddDialog(this);
89 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
90 add_bc_.setOK(add_->addPB);
91 add_bc_.setCancel(add_->closePB);
92 add_bc_.addCheckedLineEdit(add_->bibED, 0);
94 connect(add_->bibED, SIGNAL(textChanged(const QString &)),
95 this, SLOT(bibEDChanged()));
96 connect(add_->addPB, SIGNAL(clicked()),
97 this, SLOT(addDatabase()));
98 connect(add_->addPB, SIGNAL(clicked()),
99 add_, SLOT(accept()));
100 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
101 this, SLOT(addDatabase()));
102 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
103 add_, SLOT(accept()));
104 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
105 this, SLOT(availableChanged()));
106 connect(add_->browsePB, SIGNAL(clicked()),
107 this, SLOT(browseBibPressed()));
108 connect(add_->closePB, SIGNAL(clicked()),
109 add_, SLOT(reject()));
111 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
113 bc().setCancel(closePB);
114 bc().addReadOnly(databaseLW);
115 bc().addReadOnly(stylePB);
116 bc().addReadOnly(styleCB);
117 bc().addReadOnly(bibtocCB);
118 bc().addReadOnly(addBibPB);
119 bc().addReadOnly(deletePB);
123 void GuiBibtex::bibEDChanged()
125 // Indicate to the button controller that the contents have
126 // changed. The actual test of validity is carried out by
127 // the checkedLineEdit.
128 add_bc_.setValid(true);
132 void GuiBibtex::change_adaptor()
138 void GuiBibtex::browsePressed()
140 docstring const file = browseBst(docstring());
144 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
145 bool present = false;
146 unsigned int pres = 0;
148 for (int i = 0; i != styleCB->count(); ++i) {
149 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
156 styleCB->insertItem(0, toqstr(filen));
158 styleCB->setCurrentIndex(pres);
164 void GuiBibtex::browseBibPressed()
166 docstring const file = trim(browseBib(docstring()));
170 QString const f = toqstr(changeExtension(to_utf8(file), ""));
171 bool present = false;
173 for (int i = 0; i < add_->bibLW->count(); ++i) {
174 if (add_->bibLW->item(i)->text() == f)
179 add_->bibLW->addItem(f);
183 add_->bibED->setText(f);
188 void GuiBibtex::addPressed()
190 add_bc_.setValid(false);
195 void GuiBibtex::addDatabase()
197 int const sel = add_->bibLW->currentRow();
198 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
200 if (sel < 0 && file.empty())
203 // Add the selected browser_bib keys to browser_database
204 // multiple selections are possible
205 for (int i = 0; i != add_->bibLW->count(); ++i) {
206 QListWidgetItem * const item = add_->bibLW->item(i);
207 if (add_->bibLW->isItemSelected(item)) {
208 add_->bibLW->setItemSelected(item, false);
209 QList<QListWidgetItem *> matches =
210 databaseLW->findItems(item->text(), Qt::MatchExactly);
212 databaseLW->addItem(item->text());
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);
222 databaseLW->addItem(f);
229 void GuiBibtex::deletePressed()
231 databaseLW->takeItem(databaseLW->currentRow());
237 void GuiBibtex::databaseChanged()
239 deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
243 void GuiBibtex::availableChanged()
245 add_bc_.setValid(true);
249 void GuiBibtex::closeEvent(QCloseEvent *e)
256 void GuiBibtex::updateContents()
258 bool bibtopic = usingBibtopic();
262 docstring bibs = params()["bibfiles"];
265 while (!bibs.empty()) {
266 bibs = split(bibs, bib, ',');
269 databaseLW->addItem(toqstr(bib));
272 add_->bibLW->clear();
274 vector<string> bib_str;
275 getBibFiles(bib_str);
276 for (vector<string>::const_iterator it = bib_str.begin();
277 it != bib_str.end(); ++it) {
278 string bibItem(changeExtension(*it, ""));
279 add_->bibLW->addItem(toqstr(bibItem));
282 string bibstyle = getStylefile();
284 bibtocCB->setChecked(bibtotoc() && !bibtopic);
285 bibtocCB->setEnabled(!bibtopic);
287 docstring btprint(params()["btprint"]);
289 if (btprint == "btPrintNotCited")
291 else if (btprint == "btPrintAll")
294 btPrintCO->setCurrentIndex(btp);
295 btPrintCO->setEnabled(bibtopic);
303 for (vector<string>::const_iterator it = str.begin();
304 it != str.end(); ++it) {
305 string item(changeExtension(*it, ""));
306 if (item == bibstyle)
307 item_nr = int(it - str.begin());
308 styleCB->addItem(toqstr(item));
311 if (item_nr == -1 && !bibstyle.empty()) {
312 styleCB->addItem(toqstr(bibstyle));
313 item_nr = styleCB->count() - 1;
317 styleCB->setCurrentIndex(item_nr);
319 styleCB->clearEditText();
323 void GuiBibtex::applyView()
325 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
327 unsigned int maxCount = databaseLW->count();
328 for (unsigned int i = 1; i < maxCount; i++) {
330 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
333 params()["bibfiles"] = dbs;
335 docstring const bibstyle(qstring_to_ucs4(styleCB->currentText()));
336 bool const bibtotoc(bibtocCB->isChecked());
338 if (bibtotoc && (!bibstyle.empty())) {
339 // both bibtotoc and style
340 params()["options"] = "bibtotoc," + bibstyle;
341 } else if (bibtotoc) {
342 // bibtotoc and no style
343 params()["options"] = from_ascii("bibtotoc");
345 // only style. An empty one is valid, because some
346 // documentclasses have an own \bibliographystyle{}
348 params()["options"] = bibstyle;
351 // bibtopic allows three kinds of sections:
352 // 1. sections that include all cited references of the database(s)
353 // 2. sections that include all uncited references of the database(s)
354 // 3. sections that include all references of the database(s), cited or not
355 int btp = btPrintCO->currentIndex();
359 params()["btprint"] = from_ascii("btPrintCited");
362 params()["btprint"] = from_ascii("btPrintNotCited");
365 params()["btprint"] = from_ascii("btPrintAll");
369 if (!usingBibtopic())
370 params()["btprint"] = docstring();
374 bool GuiBibtex::isValid()
376 return databaseLW->count() != 0;
380 docstring const GuiBibtex::browseBib(docstring const & in_name) const
383 pair<docstring, docstring> dir1(_("Documents|#o#O"),
384 from_utf8(lyxrc.document_path));
385 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
386 return browseRelFile(in_name, from_utf8(bufferFilepath()),
387 _("Select a BibTeX database to add"),
388 filter, false, dir1);
392 docstring const GuiBibtex::browseBst(docstring const & in_name) const
395 pair<docstring, docstring> dir1(_("Documents|#o#O"),
396 from_utf8(lyxrc.document_path));
397 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
398 return browseRelFile(in_name, from_utf8(bufferFilepath()),
399 _("Select a BibTeX style"), filter, false, dir1);
403 void GuiBibtex::getBibStyles(vector<string> & data) const
407 getTexFileList("bstFiles.lst", data);
408 // test, if we have a valid list, otherwise run rescan
411 getTexFileList("bstFiles.lst", data);
413 vector<string>::iterator it = data.begin();
414 vector<string>::iterator end = data.end();
415 for (; it != end; ++it) {
416 *it = onlyFilename(*it);
418 // sort on filename only (no path)
419 std::sort(data.begin(), data.end());
423 void GuiBibtex::getBibFiles(vector<string> & data) const
427 getTexFileList("bibFiles.lst", data);
428 // test, if we have a valid list, otherwise run rescan
431 getTexFileList("bibFiles.lst", data);
433 vector<string>::iterator it = data.begin();
434 vector<string>::iterator end = data.end();
435 for (; it != end; ++it) {
436 *it = onlyFilename(*it);
438 // sort on filename only (no path)
439 std::sort(data.begin(), data.end());
443 void GuiBibtex::rescanBibStyles() const
449 bool GuiBibtex::usingBibtopic() const
451 return buffer().params().use_bibtopic;
455 bool GuiBibtex::bibtotoc() const
457 return prefixIs(to_utf8(params()["options"]), "bibtotoc");
461 string const GuiBibtex::getStylefile() const
463 // the different bibtex packages have (and need) their
464 // own "plain" stylefiles
465 biblio::CiteEngine const engine = buffer().params().getEngine();
466 docstring defaultstyle;
468 case biblio::ENGINE_BASIC:
469 defaultstyle = from_ascii("plain");
471 case biblio::ENGINE_NATBIB_AUTHORYEAR:
472 defaultstyle = from_ascii("plainnat");
474 case biblio::ENGINE_NATBIB_NUMERICAL:
475 defaultstyle = from_ascii("plainnat");
477 case biblio::ENGINE_JURABIB:
478 defaultstyle = from_ascii("jurabib");
482 docstring bst = params()["options"];
485 if (contains(bst, ',')) {
486 docstring bibtotoc = from_ascii("bibtotoc");
487 bst = split(bst, bibtotoc, ',');
492 // propose default style file for new insets
493 // existing insets might have (legally) no bst files
494 // (if the class already provides a style)
495 if (bst.empty() && params()["bibfiles"].empty())
503 Dialog * createGuiBibtex(LyXView & lv) { return new GuiBibtex(lv); }
506 } // namespace frontend
509 #include "GuiBibtex_moc.cpp"