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 "EmbeddedFiles.h"
21 #include "ui_BibtexAddUi.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
26 #include "ButtonPolicy.h"
28 #include "frontends/alert.h"
30 #include "support/debug.h"
31 #include "support/ExceptionMessage.h"
32 #include "support/FileFilterList.h"
33 #include "support/filetools.h" // changeExtension
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
37 #include <QPushButton>
38 #include <QListWidget>
43 using namespace lyx::support;
49 GuiBibtex::GuiBibtex(GuiView & lv)
50 : GuiCommand(lv, "bibtex", qt_("BibTeX Bibliography"))
54 QDialog::setModal(true);
56 connect(okPB, SIGNAL(clicked()),
57 this, SLOT(slotOK()));
58 connect(closePB, SIGNAL(clicked()),
59 this, SLOT(slotClose()));
60 connect(stylePB, SIGNAL(clicked()),
61 this, SLOT(browsePressed()));
62 connect(deletePB, SIGNAL(clicked()),
63 this, SLOT(deletePressed()));
64 connect(upPB, SIGNAL(clicked()),
65 this, SLOT(upPressed()));
66 connect(downPB, SIGNAL(clicked()),
67 this, SLOT(downPressed()));
68 connect(styleCB, SIGNAL(editTextChanged(QString)),
69 this, SLOT(change_adaptor()));
70 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
71 this, SLOT(databaseChanged()));
72 connect(bibtocCB, SIGNAL(clicked()),
73 this, SLOT(change_adaptor()));
74 connect(btPrintCO, SIGNAL(activated(int)),
75 this, SLOT(change_adaptor()));
76 connect(addBibPB, SIGNAL(clicked()),
77 this, SLOT(addPressed()));
79 add_ = new GuiBibtexAddDialog(this);
80 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
81 add_bc_.setOK(add_->addPB);
82 add_bc_.setCancel(add_->closePB);
83 add_bc_.addCheckedLineEdit(add_->bibED, 0);
85 connect(add_->bibED, SIGNAL(textChanged(QString)),
86 this, SLOT(bibEDChanged()));
87 connect(add_->addPB, SIGNAL(clicked()),
88 this, SLOT(addDatabase()));
89 connect(add_->addPB, SIGNAL(clicked()),
90 add_, SLOT(accept()));
91 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
92 this, SLOT(addDatabase()));
93 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
94 add_, SLOT(accept()));
95 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
96 this, SLOT(availableChanged()));
97 connect(add_->browsePB, SIGNAL(clicked()),
98 this, SLOT(browseBibPressed()));
99 connect(add_->closePB, SIGNAL(clicked()),
100 add_, SLOT(reject()));
102 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
104 bc().setCancel(closePB);
105 bc().addReadOnly(databaseLW);
106 bc().addReadOnly(stylePB);
107 bc().addReadOnly(styleCB);
108 bc().addReadOnly(bibtocCB);
109 bc().addReadOnly(addBibPB);
110 // Delete/Up/Down are handled with more conditions in
111 // databaseChanged().
113 // Make sure the delete/up/down buttons are disabled if necessary.
118 void GuiBibtex::bibEDChanged()
120 // Indicate to the button controller that the contents have
121 // changed. The actual test of validity is carried out by
122 // the checkedLineEdit.
123 add_bc_.setValid(true);
127 void GuiBibtex::change_adaptor()
133 void GuiBibtex::browsePressed()
135 QString const file = browseBst(QString());
140 QString const filen = changeExtension(file, "");
141 bool present = false;
142 unsigned int pres = 0;
144 for (int i = 0; i != styleCB->count(); ++i) {
145 if (styleCB->itemText(i) == filen) {
152 styleCB->insertItem(0, filen);
154 styleCB->setCurrentIndex(pres);
159 void GuiBibtex::browseBibPressed()
161 QString const file = browseBib(QString()).trimmed();
166 QString const f = changeExtension(file, "");
167 bool present = false;
169 for (int i = 0; i < add_->bibLW->count(); ++i) {
170 if (add_->bibLW->item(i)->text() == f)
175 add_->bibLW->addItem(f);
179 add_->bibED->setText(f);
183 void GuiBibtex::addPressed()
185 add_bc_.setValid(false);
190 void GuiBibtex::addDatabase()
192 int const sel = add_->bibLW->currentRow();
193 QString const file = add_->bibED->text().trimmed();
195 if (sel < 0 && file.isEmpty())
198 // Add the selected browser_bib keys to browser_database
199 // multiple selections are possible
200 for (int i = 0; i != add_->bibLW->count(); ++i) {
201 QListWidgetItem * const item = add_->bibLW->item(i);
202 if (add_->bibLW->isItemSelected(item)) {
203 add_->bibLW->setItemSelected(item, false);
204 QList<QListWidgetItem *> matches =
205 databaseLW->findItems(item->text(), Qt::MatchExactly);
206 if (matches.empty()) {
207 QString label = item->text();
208 QListWidgetItem * db = new QListWidgetItem(label);
209 db->setFlags(db->flags() | Qt::ItemIsSelectable
210 | Qt::ItemIsUserCheckable);
211 db->setCheckState(Qt::Checked);
212 databaseLW->addItem(db);
217 if (!file.isEmpty()) {
218 add_->bibED->clear();
219 QString const f = changeExtension(file, "");
220 QList<QListWidgetItem *> matches =
221 databaseLW->findItems(f, Qt::MatchExactly);
222 if (matches.empty()) {
223 QListWidgetItem * db = new QListWidgetItem(f);
224 db->setFlags(db->flags() | Qt::ItemIsSelectable
225 | Qt::ItemIsUserCheckable);
226 db->setCheckState(Qt::Checked);
227 databaseLW->addItem(db);
236 void GuiBibtex::deletePressed()
238 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
247 void GuiBibtex::upPressed()
249 int row = databaseLW->currentRow();
250 QListWidgetItem *cur = databaseLW->takeItem(row);
251 databaseLW->insertItem(row - 1, cur);
252 databaseLW->setCurrentItem(cur);
257 void GuiBibtex::downPressed()
259 int row = databaseLW->currentRow();
260 QListWidgetItem *cur = databaseLW->takeItem(row);
261 databaseLW->insertItem(row + 1, cur);
262 databaseLW->setCurrentItem(cur);
267 void GuiBibtex::databaseChanged()
269 bool readOnly = isBufferReadonly();
270 int count = databaseLW->count();
271 int row = databaseLW->currentRow();
272 deletePB->setEnabled(!readOnly && row != -1);
273 upPB->setEnabled(!readOnly && count > 1 && row > 0);
274 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
278 void GuiBibtex::availableChanged()
280 add_bc_.setValid(true);
284 void GuiBibtex::updateContents()
286 bool bibtopic = usingBibtopic();
290 docstring bibs = params_["bibfiles"];
291 docstring embs = params_["embed"];
295 while (!bibs.empty()) {
296 bibs = split(bibs, bib, ',');
297 embs = split(embs, emb, ',');
300 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
301 db->setFlags(db->flags() | Qt::ItemIsSelectable
302 | Qt::ItemIsUserCheckable);
303 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
304 databaseLW->addItem(db);
308 add_->bibLW->clear();
310 QStringList bibfiles = bibFiles();
311 for (int i = 0; i != bibfiles.count(); ++i)
312 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
314 QString bibstyle = styleFile();
316 bibtocCB->setChecked(bibtotoc() && !bibtopic);
317 bibtocCB->setEnabled(!bibtopic);
319 if (!bibtopic && btPrintCO->count() == 3)
320 btPrintCO->removeItem(1);
321 else if (bibtopic && btPrintCO->count() < 3)
322 btPrintCO->insertItem(1, qt_("all uncited references", 0));
324 docstring btprint = params_["btprint"];
326 if ((bibtopic && btprint == "btPrintNotCited") ||
327 (!bibtopic && btprint == "btPrintAll"))
329 else if (bibtopic && btprint == "btPrintAll")
332 btPrintCO->setCurrentIndex(btp);
338 QStringList str = bibStyles();
339 for (int i = 0; i != str.count(); ++i) {
340 QString item = changeExtension(str[i], "");
341 if (item == bibstyle)
343 styleCB->addItem(item);
346 if (item_nr == -1 && !bibstyle.isEmpty()) {
347 styleCB->addItem(bibstyle);
348 item_nr = styleCB->count() - 1;
352 styleCB->setCurrentIndex(item_nr);
354 styleCB->clearEditText();
358 void GuiBibtex::applyView()
363 unsigned int maxCount = databaseLW->count();
364 Buffer & buf = buffer();
365 for (unsigned int i = 0; i < maxCount; i++) {
370 QString filename = databaseLW->item(i)->text();
371 dbs += qstring_to_ucs4(filename);
373 EmbeddedFile file(fromqstr(changeExtension(filename, "bib")),
375 file.setEmbed(databaseLW->item(i)->checkState() == Qt::Checked);
376 // move file around if needed, an exception may be raised.
377 file.enable(buf.embedded(), &buf, true);
378 // if things are OK...,
380 emb += from_utf8(file.inzipName());
381 } catch (ExceptionMessage const & message) {
382 Alert::error(message.title_, message.details_);
387 params_["bibfiles"] = dbs;
388 params_["embed"] = emb;
390 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
391 bool const bibtotoc = bibtocCB->isChecked();
393 if (bibtotoc && !bibstyle.empty()) {
394 // both bibtotoc and style
395 params_["options"] = "bibtotoc," + bibstyle;
396 } else if (bibtotoc) {
397 // bibtotoc and no style
398 params_["options"] = from_ascii("bibtotoc");
400 // only style. An empty one is valid, because some
401 // documentclasses have an own \bibliographystyle{}
403 params_["options"] = bibstyle;
406 int btp = btPrintCO->currentIndex();
408 if (usingBibtopic()) {
409 // bibtopic allows three kinds of sections:
410 // 1. sections that include all cited references of the database(s)
411 // 2. sections that include all uncited references of the database(s)
412 // 3. sections that include all references of the database(s), cited or not
415 params_["btprint"] = from_ascii("btPrintCited");
418 params_["btprint"] = from_ascii("btPrintNotCited");
421 params_["btprint"] = from_ascii("btPrintAll");
427 params_["btprint"] = docstring();
431 params_["btprint"] = from_ascii("btPrintAll");
438 bool GuiBibtex::isValid()
440 return databaseLW->count() != 0;
444 QString GuiBibtex::browseBib(QString const & in_name) const
446 QString const label1 = qt_("Documents|#o#O");
447 QString const dir1 = toqstr(lyxrc.document_path);
448 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
449 return browseRelFile(in_name, bufferFilepath(),
450 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
454 QString GuiBibtex::browseBst(QString const & in_name) const
456 QString const label1 = qt_("Documents|#o#O");
457 QString const dir1 = toqstr(lyxrc.document_path);
458 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
459 return browseRelFile(in_name, bufferFilepath(),
460 qt_("Select a BibTeX style"), filter, false, label1, dir1);
464 QStringList GuiBibtex::bibStyles() const
466 QStringList data = texFileList("bstFiles.lst");
467 // test whether we have a valid list, otherwise run rescan
468 if (data.isEmpty()) {
470 data = texFileList("bstFiles.lst");
472 for (int i = 0; i != data.size(); ++i)
473 data[i] = onlyFilename(data[i]);
474 // sort on filename only (no path)
480 QStringList GuiBibtex::bibFiles() const
482 QStringList data = texFileList("bibFiles.lst");
483 // test whether we have a valid list, otherwise run rescan
484 if (data.isEmpty()) {
486 data = texFileList("bibFiles.lst");
488 for (int i = 0; i != data.size(); ++i)
489 data[i] = onlyFilename(data[i]);
490 // sort on filename only (no path)
496 void GuiBibtex::rescanBibStyles() const
502 bool GuiBibtex::usingBibtopic() const
504 return buffer().params().use_bibtopic;
508 bool GuiBibtex::bibtotoc() const
510 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
514 QString GuiBibtex::styleFile() const
516 // the different bibtex packages have (and need) their
517 // own "plain" stylefiles
518 biblio::CiteEngine const engine = buffer().params().getEngine();
519 QString defaultstyle;
521 case biblio::ENGINE_BASIC:
522 defaultstyle = "plain";
524 case biblio::ENGINE_NATBIB_AUTHORYEAR:
525 defaultstyle = "plainnat";
527 case biblio::ENGINE_NATBIB_NUMERICAL:
528 defaultstyle = "plainnat";
530 case biblio::ENGINE_JURABIB:
531 defaultstyle = "jurabib";
535 QString bst = toqstr(params_["options"]);
538 int pos = bst.indexOf(',');
541 // docstring bibtotoc = from_ascii("bibtotoc");
542 // bst = split(bst, bibtotoc, ',');
549 // propose default style file for new insets
550 // existing insets might have (legally) no bst files
551 // (if the class already provides a style)
552 if (bst.isEmpty() && params_["bibfiles"].empty())
559 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
562 } // namespace frontend
565 #include "GuiBibtex_moc.cpp"