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