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 "FuncRequest.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
25 #include "ui_BibtexAddUi.h"
27 #include "ButtonPolicy.h"
29 #include "frontends/alert.h"
31 #include "insets/InsetBibtex.h"
33 #include "support/ExceptionMessage.h"
34 #include "support/FileName.h"
35 #include "support/filetools.h" // changeExtension
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
39 #include <QPushButton>
40 #include <QListWidget>
45 using namespace lyx::support;
51 GuiBibtex::GuiBibtex(GuiView & lv)
52 : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
53 params_(insetCode("bibtex"))
57 QDialog::setModal(true);
59 connect(okPB, SIGNAL(clicked()),
60 this, SLOT(slotOK()));
61 connect(closePB, SIGNAL(clicked()),
62 this, SLOT(slotClose()));
63 connect(stylePB, SIGNAL(clicked()),
64 this, SLOT(browsePressed()));
65 connect(deletePB, SIGNAL(clicked()),
66 this, SLOT(deletePressed()));
67 connect(upPB, SIGNAL(clicked()),
68 this, SLOT(upPressed()));
69 connect(downPB, SIGNAL(clicked()),
70 this, SLOT(downPressed()));
71 connect(styleCB, SIGNAL(editTextChanged(QString)),
72 this, SLOT(change_adaptor()));
73 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
74 this, SLOT(databaseChanged()));
75 connect(bibtocCB, SIGNAL(clicked()),
76 this, SLOT(change_adaptor()));
77 connect(btPrintCO, SIGNAL(activated(int)),
78 this, SLOT(change_adaptor()));
79 connect(addBibPB, SIGNAL(clicked()),
80 this, SLOT(addPressed()));
81 connect(rescanPB, SIGNAL(clicked()),
82 this, SLOT(rescanClicked()));
84 add_ = new GuiBibtexAddDialog(this);
85 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
86 add_bc_.setOK(add_->addPB);
87 add_bc_.setCancel(add_->closePB);
88 add_bc_.addCheckedLineEdit(add_->bibED, 0);
90 connect(add_->bibED, SIGNAL(textChanged(QString)),
91 this, SLOT(bibEDChanged()));
92 connect(add_->addPB, SIGNAL(clicked()),
93 this, SLOT(addDatabase()));
94 connect(add_->addPB, SIGNAL(clicked()),
95 add_, SLOT(accept()));
96 connect(add_->rescanPB, SIGNAL(clicked()),
97 this, SLOT(rescanClicked()));
98 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
99 this, SLOT(addDatabase()));
100 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
101 add_, SLOT(accept()));
102 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
103 this, SLOT(availableChanged()));
104 connect(add_->browsePB, SIGNAL(clicked()),
105 this, SLOT(browseBibPressed()));
106 connect(add_->closePB, SIGNAL(clicked()),
107 add_, SLOT(reject()));
109 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
111 bc().setCancel(closePB);
112 bc().addReadOnly(databaseLW);
113 bc().addReadOnly(stylePB);
114 bc().addReadOnly(styleCB);
115 bc().addReadOnly(bibtocCB);
116 bc().addReadOnly(addBibPB);
117 // Delete/Up/Down are handled with more conditions in
118 // databaseChanged().
120 // Make sure the delete/up/down buttons are disabled if necessary.
125 void GuiBibtex::bibEDChanged()
127 // Indicate to the button controller that the contents have
128 // changed. The actual test of validity is carried out by
129 // the checkedLineEdit.
130 add_bc_.setValid(true);
134 void GuiBibtex::change_adaptor()
140 void GuiBibtex::browsePressed()
142 QString const file = browseBst(QString());
147 QString const filen = changeExtension(file, "");
148 bool present = false;
149 unsigned int pres = 0;
151 for (int i = 0; i != styleCB->count(); ++i) {
152 if (styleCB->itemText(i) == filen) {
159 styleCB->insertItem(0, filen);
161 styleCB->setCurrentIndex(pres);
166 void GuiBibtex::browseBibPressed()
168 QString const file = browseBib(QString()).trimmed();
173 QString const f = changeExtension(file, "");
174 bool present = false;
176 for (int i = 0; i < add_->bibLW->count(); ++i) {
177 if (add_->bibLW->item(i)->text() == f)
182 add_->bibLW->addItem(f);
186 add_->bibED->setText(f);
190 void GuiBibtex::addPressed()
192 add_bc_.setValid(false);
197 void GuiBibtex::addDatabase()
199 int const sel = add_->bibLW->currentRow();
200 QString const file = add_->bibED->text().trimmed();
202 if (sel < 0 && file.isEmpty())
205 // Add the selected browser_bib keys to browser_database
206 // multiple selections are possible
207 for (int i = 0; i != add_->bibLW->count(); ++i) {
208 QListWidgetItem * const item = add_->bibLW->item(i);
209 if (add_->bibLW->isItemSelected(item)) {
210 add_->bibLW->setItemSelected(item, false);
211 QList<QListWidgetItem *> matches =
212 databaseLW->findItems(item->text(), Qt::MatchExactly);
213 if (matches.empty()) {
214 QString label = item->text();
215 QListWidgetItem * db = new QListWidgetItem(label);
216 db->setFlags(db->flags() | Qt::ItemIsSelectable);
217 databaseLW->addItem(db);
222 if (!file.isEmpty()) {
223 add_->bibED->clear();
224 QString const f = changeExtension(file, "");
225 QList<QListWidgetItem *> matches =
226 databaseLW->findItems(f, Qt::MatchExactly);
227 if (matches.empty()) {
228 QListWidgetItem * db = new QListWidgetItem(f);
229 db->setFlags(db->flags() | Qt::ItemIsSelectable);
230 databaseLW->addItem(db);
239 void GuiBibtex::deletePressed()
241 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
250 void GuiBibtex::upPressed()
252 int row = databaseLW->currentRow();
253 QListWidgetItem *cur = databaseLW->takeItem(row);
254 databaseLW->insertItem(row - 1, cur);
255 databaseLW->setCurrentItem(cur);
260 void GuiBibtex::downPressed()
262 int row = databaseLW->currentRow();
263 QListWidgetItem *cur = databaseLW->takeItem(row);
264 databaseLW->insertItem(row + 1, cur);
265 databaseLW->setCurrentItem(cur);
270 void GuiBibtex::rescanClicked()
277 void GuiBibtex::databaseChanged()
279 bool readOnly = isBufferReadonly();
280 int count = databaseLW->count();
281 int row = databaseLW->currentRow();
282 deletePB->setEnabled(!readOnly && row != -1);
283 upPB->setEnabled(!readOnly && count > 1 && row > 0);
284 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
288 void GuiBibtex::availableChanged()
290 add_bc_.setValid(true);
294 void GuiBibtex::updateContents()
296 bool bibtopic = usingBibtopic();
300 docstring bibs = params_["bibfiles"];
303 while (!bibs.empty()) {
304 bibs = split(bibs, bib, ',');
307 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
308 db->setFlags(db->flags() | Qt::ItemIsSelectable);
309 databaseLW->addItem(db);
313 add_->bibLW->clear();
315 QStringList bibfiles = bibFiles();
316 for (int i = 0; i != bibfiles.count(); ++i)
317 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
319 QString const bibstyle = styleFile();
321 bibtocCB->setChecked(bibtotoc() && !bibtopic);
322 bibtocCB->setEnabled(!bibtopic);
324 if (!bibtopic && btPrintCO->count() == 3)
325 btPrintCO->removeItem(1);
326 else if (bibtopic && btPrintCO->count() < 3)
327 btPrintCO->insertItem(1, qt_("all uncited references", 0));
329 docstring const & btprint = params_["btprint"];
331 if ((bibtopic && btprint == "btPrintNotCited") ||
332 (!bibtopic && btprint == "btPrintAll"))
334 else if (bibtopic && btprint == "btPrintAll")
337 btPrintCO->setCurrentIndex(btp);
343 QStringList const str = bibStyles();
344 for (int i = 0; i != str.count(); ++i) {
345 QString item = changeExtension(str[i], "");
346 if (item == bibstyle)
348 styleCB->addItem(item);
351 if (item_nr == -1 && !bibstyle.isEmpty()) {
352 styleCB->addItem(bibstyle);
353 item_nr = styleCB->count() - 1;
357 styleCB->setCurrentIndex(item_nr);
359 styleCB->clearEditText();
363 void GuiBibtex::applyView()
367 unsigned int maxCount = databaseLW->count();
368 for (unsigned int i = 0; i < maxCount; i++) {
371 QString item = databaseLW->item(i)->text();
372 docstring bibfile = qstring_to_ucs4(item);
376 params_["bibfiles"] = dbs;
378 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
379 bool const bibtotoc = bibtocCB->isChecked();
381 if (bibtotoc && !bibstyle.empty()) {
382 // both bibtotoc and style
383 params_["options"] = "bibtotoc," + bibstyle;
384 } else if (bibtotoc) {
385 // bibtotoc and no style
386 params_["options"] = from_ascii("bibtotoc");
388 // only style. An empty one is valid, because some
389 // documentclasses have an own \bibliographystyle{}
391 params_["options"] = bibstyle;
394 int btp = btPrintCO->currentIndex();
396 if (usingBibtopic()) {
397 // bibtopic allows three kinds of sections:
398 // 1. sections that include all cited references of the database(s)
399 // 2. sections that include all uncited references of the database(s)
400 // 3. sections that include all references of the database(s), cited or not
403 params_["btprint"] = from_ascii("btPrintCited");
406 params_["btprint"] = from_ascii("btPrintNotCited");
409 params_["btprint"] = from_ascii("btPrintAll");
415 params_["btprint"] = docstring();
419 params_["btprint"] = from_ascii("btPrintAll");
426 bool GuiBibtex::isValid()
428 return databaseLW->count() != 0;
432 QString GuiBibtex::browseBib(QString const & in_name) const
434 QString const label1 = qt_("Documents|#o#O");
435 QString const dir1 = toqstr(lyxrc.document_path);
436 QStringList const filter(qt_("BibTeX Databases (*.bib)"));
437 return browseRelToParent(in_name, bufferFilePath(),
438 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
442 QString GuiBibtex::browseBst(QString const & in_name) const
444 QString const label1 = qt_("Documents|#o#O");
445 QString const dir1 = toqstr(lyxrc.document_path);
446 QStringList const filter(qt_("BibTeX Styles (*.bst)"));
447 return browseRelToParent(in_name, bufferFilePath(),
448 qt_("Select a BibTeX style"), filter, false, label1, dir1);
452 QStringList GuiBibtex::bibStyles() const
454 QStringList data = texFileList("bstFiles.lst");
455 // test whether we have a valid list, otherwise run rescan
456 if (data.isEmpty()) {
458 data = texFileList("bstFiles.lst");
460 for (int i = 0; i != data.size(); ++i)
461 data[i] = onlyFileName(data[i]);
462 // sort on filename only (no path)
468 QStringList GuiBibtex::bibFiles() const
470 QStringList data = texFileList("bibFiles.lst");
471 // test whether we have a valid list, otherwise run rescan
472 if (data.isEmpty()) {
474 data = texFileList("bibFiles.lst");
476 for (int i = 0; i != data.size(); ++i)
477 data[i] = onlyFileName(data[i]);
478 // sort on filename only (no path)
484 void GuiBibtex::rescanBibStyles() const
486 rescanTexStyles("bst bib");
490 bool GuiBibtex::usingBibtopic() const
492 return buffer().params().use_bibtopic;
496 bool GuiBibtex::bibtotoc() const
498 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
502 QString GuiBibtex::styleFile() const
504 // the different bibtex packages have (and need) their
505 // own "plain" stylefiles
506 QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
508 QString bst = toqstr(params_["options"]);
511 int pos = bst.indexOf(',');
514 // docstring bibtotoc = from_ascii("bibtotoc");
515 // bst = split(bst, bibtotoc, ',');
516 bst = bst.mid(pos + 1);
522 // propose default style file for new insets
523 // existing insets might have (legally) no bst files
524 // (if the class already provides a style)
525 if (bst.isEmpty() && params_["bibfiles"].empty())
532 bool GuiBibtex::initialiseParams(std::string const & data)
534 InsetCommand::string2params(data, params_);
539 void GuiBibtex::dispatchParams()
541 std::string const lfun = InsetCommand::params2string(params_);
542 dispatch(FuncRequest(getLfun(), lfun));
547 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
550 } // namespace frontend
553 #include "moc_GuiBibtex.cpp"