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>
44 using support::changeExtension;
45 using support::contains;
46 using support::FileFilterList;
47 using support::onlyFilename;
48 using support::prefixIs;
53 GuiBibtex::GuiBibtex(GuiView & lv)
54 : GuiCommand(lv, "bibtex")
58 setViewTitle( _("BibTeX Bibliography"));
60 QDialog::setModal(true);
62 connect(okPB, SIGNAL(clicked()),
63 this, SLOT(slotOK()));
64 connect(closePB, SIGNAL(clicked()),
65 this, SLOT(slotClose()));
66 connect(stylePB, SIGNAL(clicked()),
67 this, SLOT(browsePressed()));
68 connect(deletePB, SIGNAL(clicked()),
69 this, SLOT(deletePressed()));
70 connect(styleCB, SIGNAL(editTextChanged(QString)),
71 this, SLOT(change_adaptor()));
72 connect(databaseLW, SIGNAL(itemSelectionChanged()),
73 this, SLOT(databaseChanged()));
74 connect(bibtocCB, SIGNAL(clicked()),
75 this, SLOT(change_adaptor()));
76 connect(btPrintCO, SIGNAL(activated(int)),
77 this, SLOT(change_adaptor()));
78 connect(addBibPB, SIGNAL(clicked()),
79 this, SLOT(addPressed()));
81 add_ = new GuiBibtexAddDialog(this);
82 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
83 add_bc_.setOK(add_->addPB);
84 add_bc_.setCancel(add_->closePB);
85 add_bc_.addCheckedLineEdit(add_->bibED, 0);
87 connect(add_->bibED, SIGNAL(textChanged(QString)),
88 this, SLOT(bibEDChanged()));
89 connect(add_->addPB, SIGNAL(clicked()),
90 this, SLOT(addDatabase()));
91 connect(add_->addPB, SIGNAL(clicked()),
92 add_, SLOT(accept()));
93 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
94 this, SLOT(addDatabase()));
95 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
96 add_, SLOT(accept()));
97 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
98 this, SLOT(availableChanged()));
99 connect(add_->browsePB, SIGNAL(clicked()),
100 this, SLOT(browseBibPressed()));
101 connect(add_->closePB, SIGNAL(clicked()),
102 add_, SLOT(reject()));
104 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
106 bc().setCancel(closePB);
107 bc().addReadOnly(databaseLW);
108 bc().addReadOnly(stylePB);
109 bc().addReadOnly(styleCB);
110 bc().addReadOnly(bibtocCB);
111 bc().addReadOnly(addBibPB);
112 bc().addReadOnly(deletePB);
116 void GuiBibtex::bibEDChanged()
118 // Indicate to the button controller that the contents have
119 // changed. The actual test of validity is carried out by
120 // the checkedLineEdit.
121 add_bc_.setValid(true);
125 void GuiBibtex::change_adaptor()
131 void GuiBibtex::browsePressed()
133 docstring const file = browseBst(docstring());
137 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
138 bool present = false;
139 unsigned int pres = 0;
141 for (int i = 0; i != styleCB->count(); ++i) {
142 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
149 styleCB->insertItem(0, toqstr(filen));
151 styleCB->setCurrentIndex(pres);
157 void GuiBibtex::browseBibPressed()
159 docstring const file = trim(browseBib(docstring()));
163 QString const f = toqstr(changeExtension(to_utf8(file), ""));
164 bool present = false;
166 for (int i = 0; i < add_->bibLW->count(); ++i) {
167 if (add_->bibLW->item(i)->text() == f)
172 add_->bibLW->addItem(f);
176 add_->bibED->setText(f);
181 void GuiBibtex::addPressed()
183 add_bc_.setValid(false);
188 void GuiBibtex::addDatabase()
190 int const sel = add_->bibLW->currentRow();
191 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
193 if (sel < 0 && file.empty())
196 // Add the selected browser_bib keys to browser_database
197 // multiple selections are possible
198 for (int i = 0; i != add_->bibLW->count(); ++i) {
199 QListWidgetItem * const item = add_->bibLW->item(i);
200 if (add_->bibLW->isItemSelected(item)) {
201 add_->bibLW->setItemSelected(item, false);
202 QList<QListWidgetItem *> matches =
203 databaseLW->findItems(item->text(), Qt::MatchExactly);
205 databaseLW->addItem(item->text());
210 add_->bibED->clear();
211 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
212 QList<QListWidgetItem *> matches =
213 databaseLW->findItems(f, Qt::MatchExactly);
215 databaseLW->addItem(f);
222 void GuiBibtex::deletePressed()
224 databaseLW->takeItem(databaseLW->currentRow());
230 void GuiBibtex::databaseChanged()
232 deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
236 void GuiBibtex::availableChanged()
238 add_bc_.setValid(true);
242 void GuiBibtex::closeEvent(QCloseEvent *e)
249 void GuiBibtex::updateContents()
251 bool bibtopic = usingBibtopic();
255 docstring bibs = params_["bibfiles"];
258 while (!bibs.empty()) {
259 bibs = split(bibs, bib, ',');
262 databaseLW->addItem(toqstr(bib));
265 add_->bibLW->clear();
267 vector<string> bib_str;
268 getBibFiles(bib_str);
269 for (vector<string>::const_iterator it = bib_str.begin();
270 it != bib_str.end(); ++it) {
271 string bibItem(changeExtension(*it, ""));
272 add_->bibLW->addItem(toqstr(bibItem));
275 string bibstyle = getStylefile();
277 bibtocCB->setChecked(bibtotoc() && !bibtopic);
278 bibtocCB->setEnabled(!bibtopic);
280 docstring btprint = params_["btprint"];
282 if (btprint == "btPrintNotCited")
284 else if (btprint == "btPrintAll")
287 btPrintCO->setCurrentIndex(btp);
288 btPrintCO->setEnabled(bibtopic);
296 for (vector<string>::const_iterator it = str.begin();
297 it != str.end(); ++it) {
298 string item(changeExtension(*it, ""));
299 if (item == bibstyle)
300 item_nr = int(it - str.begin());
301 styleCB->addItem(toqstr(item));
304 if (item_nr == -1 && !bibstyle.empty()) {
305 styleCB->addItem(toqstr(bibstyle));
306 item_nr = styleCB->count() - 1;
310 styleCB->setCurrentIndex(item_nr);
312 styleCB->clearEditText();
316 void GuiBibtex::applyView()
318 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
320 unsigned int maxCount = databaseLW->count();
321 for (unsigned int i = 1; i < maxCount; i++) {
323 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
326 params_["bibfiles"] = dbs;
328 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
329 bool const bibtotoc = bibtocCB->isChecked();
331 if (bibtotoc && (!bibstyle.empty())) {
332 // both bibtotoc and style
333 params_["options"] = "bibtotoc," + bibstyle;
334 } else if (bibtotoc) {
335 // bibtotoc and no style
336 params_["options"] = from_ascii("bibtotoc");
338 // only style. An empty one is valid, because some
339 // documentclasses have an own \bibliographystyle{}
341 params_["options"] = bibstyle;
344 // bibtopic allows three kinds of sections:
345 // 1. sections that include all cited references of the database(s)
346 // 2. sections that include all uncited references of the database(s)
347 // 3. sections that include all references of the database(s), cited or not
348 int btp = btPrintCO->currentIndex();
352 params_["btprint"] = from_ascii("btPrintCited");
355 params_["btprint"] = from_ascii("btPrintNotCited");
358 params_["btprint"] = from_ascii("btPrintAll");
362 if (!usingBibtopic())
363 params_["btprint"] = docstring();
367 bool GuiBibtex::isValid()
369 return databaseLW->count() != 0;
373 docstring const GuiBibtex::browseBib(docstring const & in_name) const
376 docstring const label1 = _("Documents|#o#O");
377 docstring const dir1 = from_utf8(lyxrc.document_path);
378 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
379 return browseRelFile(in_name, from_utf8(bufferFilepath()),
380 _("Select a BibTeX database to add"), filter, false, label1, dir1);
384 docstring const GuiBibtex::browseBst(docstring const & in_name) const
387 docstring const label1 = _("Documents|#o#O");
388 docstring const dir1 = from_utf8(lyxrc.document_path);
389 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
390 return browseRelFile(in_name, from_utf8(bufferFilepath()),
391 _("Select a BibTeX style"), filter, false, label1, dir1);
395 void GuiBibtex::getBibStyles(vector<string> & data) const
399 getTexFileList("bstFiles.lst", data);
400 // test, if we have a valid list, otherwise run rescan
403 getTexFileList("bstFiles.lst", data);
405 vector<string>::iterator it = data.begin();
406 vector<string>::iterator end = data.end();
407 for (; it != end; ++it) {
408 *it = onlyFilename(*it);
410 // sort on filename only (no path)
411 std::sort(data.begin(), data.end());
415 void GuiBibtex::getBibFiles(vector<string> & data) const
419 getTexFileList("bibFiles.lst", data);
420 // test, if we have a valid list, otherwise run rescan
423 getTexFileList("bibFiles.lst", data);
425 vector<string>::iterator it = data.begin();
426 vector<string>::iterator end = data.end();
427 for (; it != end; ++it) {
428 *it = onlyFilename(*it);
430 // sort on filename only (no path)
431 std::sort(data.begin(), data.end());
435 void GuiBibtex::rescanBibStyles() const
441 bool GuiBibtex::usingBibtopic() const
443 return buffer().params().use_bibtopic;
447 bool GuiBibtex::bibtotoc() const
449 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
453 string const GuiBibtex::getStylefile() const
455 // the different bibtex packages have (and need) their
456 // own "plain" stylefiles
457 biblio::CiteEngine const engine = buffer().params().getEngine();
458 docstring defaultstyle;
460 case biblio::ENGINE_BASIC:
461 defaultstyle = from_ascii("plain");
463 case biblio::ENGINE_NATBIB_AUTHORYEAR:
464 defaultstyle = from_ascii("plainnat");
466 case biblio::ENGINE_NATBIB_NUMERICAL:
467 defaultstyle = from_ascii("plainnat");
469 case biblio::ENGINE_JURABIB:
470 defaultstyle = from_ascii("jurabib");
474 docstring bst = params_["options"];
477 if (contains(bst, ',')) {
478 docstring bibtotoc = from_ascii("bibtotoc");
479 bst = split(bst, bibtotoc, ',');
484 // propose default style file for new insets
485 // existing insets might have (legally) no bst files
486 // (if the class already provides a style)
487 if (bst.empty() && params_["bibfiles"].empty())
495 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
498 } // namespace frontend
501 #include "GuiBibtex_moc.cpp"