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/debug.h"
34 #include "support/ExceptionMessage.h"
35 #include "support/FileFilterList.h"
36 #include "support/FileName.h"
37 #include "support/filetools.h" // changeExtension
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
41 #include <QPushButton>
42 #include <QListWidget>
47 using namespace lyx::support;
53 GuiBibtex::GuiBibtex(GuiView & lv)
54 : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
55 params_(insetCode("bibtex"))
59 QDialog::setModal(true);
61 connect(okPB, SIGNAL(clicked()),
62 this, SLOT(slotOK()));
63 connect(closePB, SIGNAL(clicked()),
64 this, SLOT(slotClose()));
65 connect(stylePB, SIGNAL(clicked()),
66 this, SLOT(browsePressed()));
67 connect(deletePB, SIGNAL(clicked()),
68 this, SLOT(deletePressed()));
69 connect(upPB, SIGNAL(clicked()),
70 this, SLOT(upPressed()));
71 connect(downPB, SIGNAL(clicked()),
72 this, SLOT(downPressed()));
73 connect(styleCB, SIGNAL(editTextChanged(QString)),
74 this, SLOT(change_adaptor()));
75 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
76 this, SLOT(databaseChanged()));
77 connect(bibtocCB, SIGNAL(clicked()),
78 this, SLOT(change_adaptor()));
79 connect(btPrintCO, SIGNAL(activated(int)),
80 this, SLOT(change_adaptor()));
81 connect(addBibPB, SIGNAL(clicked()),
82 this, SLOT(addPressed()));
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_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
97 this, SLOT(addDatabase()));
98 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
99 add_, SLOT(accept()));
100 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
101 this, SLOT(availableChanged()));
102 connect(add_->browsePB, SIGNAL(clicked()),
103 this, SLOT(browseBibPressed()));
104 connect(add_->closePB, SIGNAL(clicked()),
105 add_, SLOT(reject()));
107 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
109 bc().setCancel(closePB);
110 bc().addReadOnly(databaseLW);
111 bc().addReadOnly(stylePB);
112 bc().addReadOnly(styleCB);
113 bc().addReadOnly(bibtocCB);
114 bc().addReadOnly(addBibPB);
115 // Delete/Up/Down are handled with more conditions in
116 // databaseChanged().
118 // Make sure the delete/up/down buttons are disabled if necessary.
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 QString const file = browseBst(QString());
145 QString const filen = changeExtension(file, "");
146 bool present = false;
147 unsigned int pres = 0;
149 for (int i = 0; i != styleCB->count(); ++i) {
150 if (styleCB->itemText(i) == filen) {
157 styleCB->insertItem(0, filen);
159 styleCB->setCurrentIndex(pres);
164 void GuiBibtex::browseBibPressed()
166 QString const file = browseBib(QString()).trimmed();
171 QString const f = changeExtension(file, "");
172 bool present = false;
174 for (int i = 0; i < add_->bibLW->count(); ++i) {
175 if (add_->bibLW->item(i)->text() == f)
180 add_->bibLW->addItem(f);
184 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 QString const file = add_->bibED->text().trimmed();
200 if (sel < 0 && file.isEmpty())
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);
211 if (matches.empty()) {
212 QString label = item->text();
213 QListWidgetItem * db = new QListWidgetItem(label);
214 db->setFlags(db->flags() | Qt::ItemIsSelectable);
215 databaseLW->addItem(db);
220 if (!file.isEmpty()) {
221 add_->bibED->clear();
222 QString const f = changeExtension(file, "");
223 QList<QListWidgetItem *> matches =
224 databaseLW->findItems(f, Qt::MatchExactly);
225 if (matches.empty()) {
226 QListWidgetItem * db = new QListWidgetItem(f);
227 db->setFlags(db->flags() | Qt::ItemIsSelectable);
228 databaseLW->addItem(db);
237 void GuiBibtex::deletePressed()
239 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
248 void GuiBibtex::upPressed()
250 int row = databaseLW->currentRow();
251 QListWidgetItem *cur = databaseLW->takeItem(row);
252 databaseLW->insertItem(row - 1, cur);
253 databaseLW->setCurrentItem(cur);
258 void GuiBibtex::downPressed()
260 int row = databaseLW->currentRow();
261 QListWidgetItem *cur = databaseLW->takeItem(row);
262 databaseLW->insertItem(row + 1, cur);
263 databaseLW->setCurrentItem(cur);
268 void GuiBibtex::databaseChanged()
270 bool readOnly = isBufferReadonly();
271 int count = databaseLW->count();
272 int row = databaseLW->currentRow();
273 deletePB->setEnabled(!readOnly && row != -1);
274 upPB->setEnabled(!readOnly && count > 1 && row > 0);
275 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
279 void GuiBibtex::availableChanged()
281 add_bc_.setValid(true);
285 void GuiBibtex::updateContents()
287 bool bibtopic = usingBibtopic();
291 docstring bibs = params_["bibfiles"];
294 while (!bibs.empty()) {
295 bibs = split(bibs, bib, ',');
298 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
299 db->setFlags(db->flags() | Qt::ItemIsSelectable);
300 databaseLW->addItem(db);
304 add_->bibLW->clear();
306 QStringList bibfiles = bibFiles();
307 for (int i = 0; i != bibfiles.count(); ++i)
308 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
310 QString bibstyle = styleFile();
312 bibtocCB->setChecked(bibtotoc() && !bibtopic);
313 bibtocCB->setEnabled(!bibtopic);
315 if (!bibtopic && btPrintCO->count() == 3)
316 btPrintCO->removeItem(1);
317 else if (bibtopic && btPrintCO->count() < 3)
318 btPrintCO->insertItem(1, qt_("all uncited references", 0));
320 docstring btprint = params_["btprint"];
322 if ((bibtopic && btprint == "btPrintNotCited") ||
323 (!bibtopic && btprint == "btPrintAll"))
325 else if (bibtopic && btprint == "btPrintAll")
328 btPrintCO->setCurrentIndex(btp);
334 QStringList str = bibStyles();
335 for (int i = 0; i != str.count(); ++i) {
336 QString item = changeExtension(str[i], "");
337 if (item == bibstyle)
339 styleCB->addItem(item);
342 if (item_nr == -1 && !bibstyle.isEmpty()) {
343 styleCB->addItem(bibstyle);
344 item_nr = styleCB->count() - 1;
348 styleCB->setCurrentIndex(item_nr);
350 styleCB->clearEditText();
354 void GuiBibtex::applyView()
358 unsigned int maxCount = databaseLW->count();
359 for (unsigned int i = 0; i < maxCount; i++) {
362 QString item = databaseLW->item(i)->text();
363 docstring bibfile = qstring_to_ucs4(item);
367 params_["bibfiles"] = dbs;
369 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
370 bool const bibtotoc = bibtocCB->isChecked();
372 if (bibtotoc && !bibstyle.empty()) {
373 // both bibtotoc and style
374 params_["options"] = "bibtotoc," + bibstyle;
375 } else if (bibtotoc) {
376 // bibtotoc and no style
377 params_["options"] = from_ascii("bibtotoc");
379 // only style. An empty one is valid, because some
380 // documentclasses have an own \bibliographystyle{}
382 params_["options"] = bibstyle;
385 int btp = btPrintCO->currentIndex();
387 if (usingBibtopic()) {
388 // bibtopic allows three kinds of sections:
389 // 1. sections that include all cited references of the database(s)
390 // 2. sections that include all uncited references of the database(s)
391 // 3. sections that include all references of the database(s), cited or not
394 params_["btprint"] = from_ascii("btPrintCited");
397 params_["btprint"] = from_ascii("btPrintNotCited");
400 params_["btprint"] = from_ascii("btPrintAll");
406 params_["btprint"] = docstring();
410 params_["btprint"] = from_ascii("btPrintAll");
417 bool GuiBibtex::isValid()
419 return databaseLW->count() != 0;
423 QString GuiBibtex::browseBib(QString const & in_name) const
425 QString const label1 = qt_("Documents|#o#O");
426 QString const dir1 = toqstr(lyxrc.document_path);
427 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
428 return browseRelFile(in_name, bufferFilepath(),
429 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
433 QString GuiBibtex::browseBst(QString const & in_name) const
435 QString const label1 = qt_("Documents|#o#O");
436 QString const dir1 = toqstr(lyxrc.document_path);
437 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
438 return browseRelFile(in_name, bufferFilepath(),
439 qt_("Select a BibTeX style"), filter, false, label1, dir1);
443 QStringList GuiBibtex::bibStyles() const
445 QStringList data = texFileList("bstFiles.lst");
446 // test whether we have a valid list, otherwise run rescan
447 if (data.isEmpty()) {
449 data = texFileList("bstFiles.lst");
451 for (int i = 0; i != data.size(); ++i)
452 data[i] = onlyFilename(data[i]);
453 // sort on filename only (no path)
459 QStringList GuiBibtex::bibFiles() const
461 QStringList data = texFileList("bibFiles.lst");
462 // test whether we have a valid list, otherwise run rescan
463 if (data.isEmpty()) {
465 data = texFileList("bibFiles.lst");
467 for (int i = 0; i != data.size(); ++i)
468 data[i] = onlyFilename(data[i]);
469 // sort on filename only (no path)
475 void GuiBibtex::rescanBibStyles() const
481 bool GuiBibtex::usingBibtopic() const
483 return buffer().params().use_bibtopic;
487 bool GuiBibtex::bibtotoc() const
489 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
493 QString GuiBibtex::styleFile() const
495 // the different bibtex packages have (and need) their
496 // own "plain" stylefiles
497 CiteEngine const engine = buffer().params().citeEngine();
498 QString defaultstyle;
501 defaultstyle = "plain";
503 case ENGINE_NATBIB_AUTHORYEAR:
504 defaultstyle = "plainnat";
506 case ENGINE_NATBIB_NUMERICAL:
507 defaultstyle = "plainnat";
510 defaultstyle = "jurabib";
514 QString bst = toqstr(params_["options"]);
517 int pos = bst.indexOf(',');
520 // docstring bibtotoc = from_ascii("bibtotoc");
521 // bst = split(bst, bibtotoc, ',');
528 // propose default style file for new insets
529 // existing insets might have (legally) no bst files
530 // (if the class already provides a style)
531 if (bst.isEmpty() && params_["bibfiles"].empty())
538 bool GuiBibtex::initialiseParams(std::string const & data)
540 InsetCommand::string2params("bibtex", data, params_);
545 void GuiBibtex::dispatchParams()
547 std::string const lfun = InsetCommand::params2string("bibtex", params_);
548 dispatch(FuncRequest(getLfun(), lfun));
553 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
556 } // namespace frontend
559 #include "GuiBibtex_moc.cpp"