]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBibtex.cpp
More GuiInfo usability work
[lyx.git] / src / frontends / qt4 / GuiBibtex.cpp
1 /**
2  * \file GuiBibtex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Herbert Voß
8  * \author Angus Leeming
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiBibtex.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "CiteEnginesList.h"
21 #include "Encoding.h"
22 #include "FuncRequest.h"
23 #include "LyXRC.h"
24 #include "qt_helpers.h"
25 #include "Validator.h"
26
27 #include "ui_BibtexAddUi.h"
28
29 #include "ButtonPolicy.h"
30
31 #include "frontends/alert.h"
32
33 #include "insets/InsetBibtex.h"
34
35 #include "support/debug.h"
36 #include "support/ExceptionMessage.h"
37 #include "support/FileName.h"
38 #include "support/filetools.h" // changeExtension
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
41
42 #include <QDialogButtonBox>
43 #include <QPushButton>
44 #include <QListWidget>
45 #include <QCheckBox>
46 #include <QLineEdit>
47
48 using namespace std;
49 using namespace lyx::support;
50
51 namespace lyx {
52 namespace frontend {
53
54
55 GuiBibtex::GuiBibtex(GuiView & lv)
56         : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
57           params_(insetCode("bibtex"))
58 {
59         setupUi(this);
60
61         QDialog::setModal(true);
62         setWindowModality(Qt::WindowModal);
63
64         connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
65                 this, SLOT(slotButtonBox(QAbstractButton *)));
66         connect(stylePB, SIGNAL(clicked()),
67                 this, SLOT(browsePressed()));
68         connect(deletePB, SIGNAL(clicked()),
69                 this, SLOT(deletePressed()));
70         connect(upPB, SIGNAL(clicked()),
71                 this, SLOT(upPressed()));
72         connect(downPB, SIGNAL(clicked()),
73                 this, SLOT(downPressed()));
74         connect(styleCB, SIGNAL(editTextChanged(QString)),
75                 this, SLOT(change_adaptor()));
76         connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
77                 this, SLOT(databaseChanged()));
78         connect(bibtocCB, SIGNAL(clicked()),
79                 this, SLOT(change_adaptor()));
80         connect(btPrintCO, SIGNAL(activated(int)),
81                 this, SLOT(change_adaptor()));
82         connect(addBibPB, SIGNAL(clicked()),
83                 this, SLOT(addPressed()));
84         connect(rescanPB, SIGNAL(clicked()),
85                 this, SLOT(rescanClicked()));
86         connect(biblatexOptsLE, SIGNAL(textChanged(QString)),
87                 this, SLOT(change_adaptor()));
88         connect(bibEncodingCO, SIGNAL(activated(int)),
89                 this, SLOT(change_adaptor()));
90
91         add_ = new GuiBibtexAddDialog(this);
92         add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
93         add_bc_.setOK(add_->buttonBox->button(QDialogButtonBox::Ok));
94         add_bc_.setCancel(add_->buttonBox->button(QDialogButtonBox::Cancel));
95         add_bc_.addCheckedLineEdit(add_->bibED, 0);
96
97         connect(add_->bibED, SIGNAL(textChanged(QString)),
98                 this, SLOT(bibEDChanged()));
99         connect(add_->buttonBox, SIGNAL(clicked(QAbstractButton *)),
100                 this, SLOT(addBBClicked(QAbstractButton *)));
101         connect(add_->rescanPB, SIGNAL(clicked()),
102                 this, SLOT(rescanClicked()));
103         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
104                 this, SLOT(addDatabase()));
105         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
106                 add_, SLOT(accept()));
107         connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
108                 this, SLOT(availableChanged()));
109         connect(add_->browsePB, SIGNAL(clicked()),
110                 this, SLOT(browseBibPressed()));
111
112         add_->bibLW->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
113                                     "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
114                                     "If you want to reuse your own database, this is the place you should store it.")));
115
116         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
117         bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
118         bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
119         bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
120         bc().addReadOnly(databaseLW);
121         bc().addReadOnly(stylePB);
122         bc().addReadOnly(styleCB);
123         bc().addReadOnly(bibtocCB);
124         bc().addReadOnly(addBibPB);
125         bc().addReadOnly(bibEncodingCO);
126         // Delete/Up/Down are handled with more conditions in
127         // databaseChanged().
128
129         // Always put the default encoding in the first position.
130         bibEncodingCO->addItem(qt_("Document Encoding"), "default");
131         QMap<QString, QString> encodinglist;
132         for (auto const & encvar : encodings) {
133                 if (!encvar.unsafe() && !encvar.guiName().empty())
134                         encodinglist.insert(qt_(encvar.guiName()), toqstr(encvar.name()));
135         }
136         QMap<QString, QString>::const_iterator it = encodinglist.constBegin();
137         while (it != encodinglist.constEnd()) {
138                 bibEncodingCO->addItem(it.key(), it.value());
139                 ++it;
140         }
141
142         // Make sure the delete/up/down buttons are disabled if necessary.
143         databaseChanged();
144 }
145
146
147 void GuiBibtex::addBBClicked(QAbstractButton * button)
148 {
149         switch (add_->buttonBox->standardButton(button)) {
150         case QDialogButtonBox::Ok:
151                 addDatabase();
152                 add_->accept();
153                 break;
154         case QDialogButtonBox::Cancel:
155                 add_->reject();
156                 break;
157         default:
158                 break;
159         }
160 }
161
162
163 void GuiBibtex::bibEDChanged()
164 {
165         // Indicate to the button controller that the contents have
166         // changed. The actual test of validity is carried out by
167         // the checkedLineEdit.
168         add_bc_.setValid(true);
169 }
170
171
172 void GuiBibtex::change_adaptor()
173 {
174         changed();
175 }
176
177
178 void GuiBibtex::browsePressed()
179 {
180         QString const file = browseBst(QString());
181
182         if (file.isEmpty())
183                 return;
184
185         QString const filen = changeExtension(file, "");
186         bool present = false;
187         unsigned int pres = 0;
188
189         for (int i = 0; i != styleCB->count(); ++i) {
190                 if (styleCB->itemText(i) == filen) {
191                         present = true;
192                         pres = i;
193                 }
194         }
195
196         if (!present)
197                 styleCB->insertItem(0, filen);
198
199         styleCB->setCurrentIndex(pres);
200         changed();
201 }
202
203
204 void GuiBibtex::browseBibPressed()
205 {
206         QString const file = browseBib(QString()).trimmed();
207
208         if (file.isEmpty())
209                 return;
210
211         QString const f = changeExtension(file, "");
212         bool present = false;
213
214         for (int i = 0; i < add_->bibLW->count(); ++i) {
215                 if (add_->bibLW->item(i)->text() == f)
216                         present = true;
217         }
218
219         if (!present) {
220                 add_->bibLW->addItem(f);
221                 changed();
222         }
223
224         add_->bibED->setText(f);
225 }
226
227
228 void GuiBibtex::addPressed()
229 {
230         add_bc_.setValid(false);
231         add_->exec();
232 }
233
234
235 void GuiBibtex::addDatabase()
236 {
237         int const sel = add_->bibLW->currentRow();
238         QString const file = add_->bibED->text().trimmed();
239
240         if (sel < 0 && file.isEmpty())
241                 return;
242
243         // Add the selected browser_bib keys to browser_database
244         // multiple selections are possible
245         for (int i = 0; i != add_->bibLW->count(); ++i) {
246                 QListWidgetItem * const item = add_->bibLW->item(i);
247                 if (add_->bibLW->isItemSelected(item)) {
248                         add_->bibLW->setItemSelected(item, false);
249                         QList<QListWidgetItem *> matches =
250                                 databaseLW->findItems(item->text(), Qt::MatchExactly);
251                         if (matches.empty()) {
252                                 QString label = item->text();
253                                 QListWidgetItem * db = new QListWidgetItem(label);
254                                 db->setFlags(db->flags() | Qt::ItemIsSelectable);
255                                 databaseLW->addItem(db);
256                         }
257                 }
258         }
259
260         if (!file.isEmpty()) {
261                 add_->bibED->clear();
262                 QString const f = changeExtension(file, "");
263                 QList<QListWidgetItem *> matches =
264                         databaseLW->findItems(f, Qt::MatchExactly);
265                 if (matches.empty()) {
266                         QListWidgetItem * db = new QListWidgetItem(f);
267                         db->setFlags(db->flags() | Qt::ItemIsSelectable);
268                         databaseLW->addItem(db);
269                 }
270         }
271
272         databaseChanged();
273         changed();
274 }
275
276
277 void GuiBibtex::deletePressed()
278 {
279         QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
280         if (cur) {
281                 delete cur;
282                 databaseChanged();
283                 changed();
284         }
285 }
286
287
288 void GuiBibtex::upPressed()
289 {
290         int row = databaseLW->currentRow();
291         QListWidgetItem *cur = databaseLW->takeItem(row);
292         databaseLW->insertItem(row - 1, cur);
293         databaseLW->setCurrentItem(cur);
294         changed();
295 }
296
297
298 void GuiBibtex::downPressed()
299 {
300         int row = databaseLW->currentRow();
301         QListWidgetItem *cur = databaseLW->takeItem(row);
302         databaseLW->insertItem(row + 1, cur);
303         databaseLW->setCurrentItem(cur);
304         changed();
305 }
306
307
308 void GuiBibtex::rescanClicked()
309 {
310         rescanBibStyles();
311         updateContents();
312 }
313
314
315 void GuiBibtex::databaseChanged()
316 {
317         bool readOnly = isBufferReadonly();
318         int count = databaseLW->count();
319         int row = databaseLW->currentRow();
320         deletePB->setEnabled(!readOnly && row != -1);
321         upPB->setEnabled(!readOnly && count > 1 && row > 0);
322         downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
323 }
324
325
326 void GuiBibtex::availableChanged()
327 {
328         add_bc_.setValid(true);
329 }
330
331
332 void GuiBibtex::updateContents()
333 {
334         bool bibtopic = usingBibtopic();
335         bool biblatex = usingBiblatex();
336
337         if (biblatex)
338                 setTitle(qt_("Biblatex Bibliography"));
339         else
340                 setTitle(qt_("BibTeX Bibliography"));
341
342         databaseLW->clear();
343
344         docstring bibs = params_["bibfiles"];
345         docstring bib;
346
347         while (!bibs.empty()) {
348                 bibs = split(bibs, bib, ',');
349                 bib = trim(bib);
350                 if (!bib.empty()) {
351                         QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
352                         db->setFlags(db->flags() | Qt::ItemIsSelectable);
353                         databaseLW->addItem(db);
354                 }
355         }
356
357         add_->bibLW->clear();
358
359         QStringList bibfiles = bibFiles();
360         for (int i = 0; i != bibfiles.count(); ++i)
361                 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
362
363         QString const bibstyle = styleFile();
364
365         bibtocCB->setChecked(bibtotoc() && !bibtopic);
366         bibtocCB->setEnabled(!bibtopic);
367
368         btPrintCO->clear();
369         btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
370         if (bibtopic)
371                 btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
372         btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
373         if (usingBiblatex() && !buffer().masterParams().multibib.empty())
374                 btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
375
376         docstring btprint = params_["btprint"];
377         if (btprint.empty())
378                 // default
379                 btprint = from_ascii("btPrintCited");
380         btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
381
382         docstring encoding = params_["encoding"];
383         if (encoding.empty())
384                 // default
385                 encoding = from_ascii("default");
386         bibEncodingCO->setCurrentIndex(bibEncodingCO->findData(toqstr(encoding)));
387
388         // Only useful for biblatex
389         biblatexOptsLA->setVisible(biblatex);
390         biblatexOptsLE->setVisible(biblatex);
391
392         // only useful for BibTeX
393         bstGB->setVisible(!biblatex);
394
395         if (!biblatex) {
396                 styleCB->clear();
397
398                 int item_nr = -1;
399
400                 QStringList const str = bibStyles();
401                 for (int i = 0; i != str.count(); ++i) {
402                         QString item = changeExtension(str[i], "");
403                         if (item == bibstyle)
404                                 item_nr = i;
405                         styleCB->addItem(item);
406                 }
407
408                 if (item_nr == -1 && !bibstyle.isEmpty()) {
409                         styleCB->addItem(bibstyle);
410                         item_nr = styleCB->count() - 1;
411                 }
412
413
414                 if (item_nr != -1)
415                         styleCB->setCurrentIndex(item_nr);
416                 else
417                         styleCB->clearEditText();
418         } else
419                 biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
420 }
421
422
423 void GuiBibtex::applyView()
424 {
425         docstring dbs;
426
427         unsigned int maxCount = databaseLW->count();
428         for (unsigned int i = 0; i < maxCount; i++) {
429                 if (i != 0)
430                         dbs += ',';
431                 QString item = databaseLW->item(i)->text();
432                 docstring bibfile = qstring_to_ucs4(item);
433                 dbs += bibfile;
434         }
435
436         params_["bibfiles"] = dbs;
437
438         docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
439         bool const bibtotoc = bibtocCB->isChecked();
440
441         if (bibtotoc && !bibstyle.empty()) {
442                 // both bibtotoc and style
443                 params_["options"] = "bibtotoc," + bibstyle;
444         } else if (bibtotoc) {
445                 // bibtotoc and no style
446                 params_["options"] = from_ascii("bibtotoc");
447         } else {
448                 // only style. An empty one is valid, because some
449                 // documentclasses have an own \bibliographystyle{}
450                 // command!
451                 params_["options"] = bibstyle;
452         }
453
454         params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
455
456         params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
457
458         params_["encoding"] = qstring_to_ucs4(bibEncodingCO->itemData(bibEncodingCO->currentIndex()).toString());
459 }
460
461
462 bool GuiBibtex::isValid()
463 {
464         return databaseLW->count() != 0;
465 }
466
467
468 QString GuiBibtex::browseBib(QString const & in_name) const
469 {
470         QString const label1 = qt_("D&ocuments");
471         QString const dir1 = toqstr(lyxrc.document_path);
472         QStringList const filter(qt_("BibTeX Databases (*.bib)"));
473         return browseRelToParent(in_name, bufferFilePath(),
474                 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
475 }
476
477
478 QString GuiBibtex::browseBst(QString const & in_name) const
479 {
480         QString const label1 = qt_("D&ocuments");
481         QString const dir1 = toqstr(lyxrc.document_path);
482         QStringList const filter(qt_("BibTeX Styles (*.bst)"));
483         return browseRelToParent(in_name, bufferFilePath(),
484                 qt_("Select a BibTeX style"), filter, false, label1, dir1);
485 }
486
487
488 QStringList GuiBibtex::bibStyles() const
489 {
490         QStringList sdata = texFileList("bstFiles.lst");
491         // test whether we have a valid list, otherwise run rescan
492         if (sdata.isEmpty()) {
493                 rescanBibStyles();
494                 sdata = texFileList("bstFiles.lst");
495         }
496         for (int i = 0; i != sdata.size(); ++i)
497                 sdata[i] = onlyFileName(sdata[i]);
498         // sort on filename only (no path)
499         sdata.sort();
500         return sdata;
501 }
502
503
504 QStringList GuiBibtex::bibFiles() const
505 {
506         QStringList sdata = texFileList("bibFiles.lst");
507         // test whether we have a valid list, otherwise run rescan
508         if (sdata.isEmpty()) {
509                 rescanBibStyles();
510                 sdata = texFileList("bibFiles.lst");
511         }
512         for (int i = 0; i != sdata.size(); ++i)
513                 sdata[i] = onlyFileName(sdata[i]);
514         // sort on filename only (no path)
515         sdata.sort();
516         return sdata;
517 }
518
519
520 void GuiBibtex::rescanBibStyles() const
521 {
522         if (usingBiblatex())
523                 rescanTexStyles("bib");
524         else
525                 rescanTexStyles("bst bib");
526 }
527
528
529 bool GuiBibtex::usingBibtopic() const
530 {
531         return buffer().params().useBibtopic();
532 }
533
534
535 bool GuiBibtex::bibtotoc() const
536 {
537         return prefixIs(to_utf8(params_["options"]), "bibtotoc");
538 }
539
540
541 bool GuiBibtex::usingBiblatex() const
542 {
543         return buffer().masterBuffer()->params().useBiblatex();
544 }
545
546
547 QString GuiBibtex::styleFile() const
548 {
549         // the different bibtex packages have (and need) their
550         // own "plain" stylefiles
551         QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
552
553         QString bst = toqstr(params_["options"]);
554         if (bibtotoc()){
555                 // bibstyle exists?
556                 int pos = bst.indexOf(',');
557                 if (pos != -1) {
558                         // FIXME: check
559                         // docstring bibtotoc = from_ascii("bibtotoc");
560                         // bst = split(bst, bibtotoc, ',');
561                         bst = bst.mid(pos + 1);
562                 } else {
563                         bst.clear();
564                 }
565         }
566
567         // propose default style file for new insets
568         // existing insets might have (legally) no bst files
569         // (if the class already provides a style)
570         if (bst.isEmpty() && params_["bibfiles"].empty())
571                 bst = defaultstyle;
572
573         return bst;
574 }
575
576
577 bool GuiBibtex::initialiseParams(std::string const & sdata)
578 {
579         InsetCommand::string2params(sdata, params_);
580         return true;
581 }
582
583
584 void GuiBibtex::dispatchParams()
585 {
586         std::string const lfun = InsetCommand::params2string(params_);
587         dispatch(FuncRequest(getLfun(), lfun));
588 }
589
590
591
592 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
593
594
595 } // namespace frontend
596 } // namespace lyx
597
598 #include "moc_GuiBibtex.cpp"