]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBibtex.cpp
Introducing table templates
[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 "GuiApplication.h"
24 #include "LyXRC.h"
25 #include "qt_helpers.h"
26 #include "Validator.h"
27
28 #include "ButtonPolicy.h"
29
30 #include "frontends/alert.h"
31
32 #include "insets/InsetBibtex.h"
33
34 #include "support/debug.h"
35 #include "support/ExceptionMessage.h"
36 #include "support/FileName.h"
37 #include "support/filetools.h" // changeExtension
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40
41 #include <QDialogButtonBox>
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         // The filter bar
64         filter_ = new FancyLineEdit(this);
65         filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
66         filter_->setButtonVisible(FancyLineEdit::Right, true);
67         filter_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
68         filter_->setAutoHideButton(FancyLineEdit::Right, true);
69         filter_->setPlaceholderText(qt_("All avail. databases"));
70
71         filterBarL->addWidget(filter_, 0);
72         findKeysLA->setBuddy(filter_);
73
74         connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
75                 this, SLOT(slotButtonBox(QAbstractButton *)));
76         connect(stylePB, SIGNAL(clicked()),
77                 this, SLOT(browseBstPressed()));
78         connect(styleCB, SIGNAL(editTextChanged(QString)),
79                 this, SLOT(change_adaptor()));
80         connect(bibtocCB, SIGNAL(clicked()),
81                 this, SLOT(change_adaptor()));
82         connect(btPrintCO, SIGNAL(activated(int)),
83                 this, SLOT(change_adaptor()));
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         connect(browseBibPB, SIGNAL(clicked()),
91                 this, SLOT(browseBibPressed()));
92
93         selectionManager = new GuiSelectionManager(this, availableLV, selectedLV,
94                         addBibPB, deletePB, upPB, downPB, &available_model_, &selected_model_);
95         connect(selectionManager, SIGNAL(selectionChanged()),
96                 this, SLOT(databaseChanged()));
97         connect(selectionManager, SIGNAL(updateHook()),
98                 this, SLOT(selUpdated()));
99         connect(selectionManager, SIGNAL(okHook()),
100                 this, SLOT(on_buttonBox_accepted()));
101
102         connect(filter_, SIGNAL(rightButtonClicked()),
103                 this, SLOT(resetFilter()));
104         connect(filter_, SIGNAL(textEdited(QString)),
105                 this, SLOT(filterChanged(QString)));
106         connect(filter_, SIGNAL(returnPressed()),
107                 this, SLOT(filterPressed()));
108 #if (QT_VERSION < 0x050000)
109         connect(filter_, SIGNAL(downPressed()),
110                 availableLV, SLOT(setFocus()));
111 #else
112         connect(filter_, &FancyLineEdit::downPressed,
113                 availableLV, [=](){ focusAndHighlight(availableLV); });
114 #endif
115
116         availableLV->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
117                                     "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
118                                     "If you want to reuse your own database, this is the place you should store it.")));
119
120         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
121         bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
122         bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
123         bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
124         bc().addReadOnly(stylePB);
125         bc().addReadOnly(styleCB);
126         bc().addReadOnly(bibtocCB);
127         bc().addReadOnly(bibEncodingCO);
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         setFocusProxy(filter_);
143 }
144
145
146 void GuiBibtex::init()
147 {
148         all_bibs_ = bibFiles(false);
149         available_model_.setStringList(all_bibs_);
150
151         QString bibs = toqstr(params_["bibfiles"]);
152         if (bibs.isEmpty())
153                 selected_bibs_.clear();
154         else
155                 selected_bibs_ = bibs.split(",");
156         setSelectedBibs(selected_bibs_);
157
158         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
159         buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
160         selectionManager->update();
161 }
162
163
164 void GuiBibtex::change_adaptor()
165 {
166         setButtons();
167         changed();
168 }
169
170
171 void GuiBibtex::setButtons()
172 {
173         int const srows = selectedLV->model()->rowCount();
174         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(srows > 0);
175         buttonBox->button(QDialogButtonBox::Ok)->setEnabled(srows > 0);
176 }
177
178
179 void GuiBibtex::selUpdated()
180 {
181         selectionManager->update();
182         changed();
183 }
184
185
186 void GuiBibtex::on_buttonBox_accepted()
187 {
188         applyView();
189         clearSelection();
190         hide();
191 }
192
193
194 void GuiBibtex::browseBstPressed()
195 {
196         QString const file = browseBst(QString());
197
198         if (file.isEmpty())
199                 return;
200
201         QString const filen = changeExtension(file, "");
202         bool present = false;
203         unsigned int pres = 0;
204
205         for (int i = 0; i != styleCB->count(); ++i) {
206                 if (styleCB->itemText(i) == filen) {
207                         present = true;
208                         pres = i;
209                 }
210         }
211
212         if (!present)
213                 styleCB->insertItem(0, filen);
214
215         styleCB->setCurrentIndex(pres);
216         changed();
217 }
218
219
220 void GuiBibtex::browseBibPressed()
221 {
222         QString const file = browseBib(QString()).trimmed();
223
224         if (file.isEmpty())
225                 return;
226
227         QString const f = changeExtension(file, "");
228
229         if (!selected_bibs_.contains(f)) {
230                 selected_bibs_.append(f);
231                 setSelectedBibs(selected_bibs_);
232                 changed();
233         }
234 }
235
236
237 void GuiBibtex::rescanClicked()
238 {
239         rescanBibStyles();
240         updateContents();
241 }
242
243
244 void GuiBibtex::clearSelection()
245 {
246         selected_bibs_.clear();
247         setSelectedBibs(selected_bibs_);
248 }
249
250
251 void GuiBibtex::setSelectedBibs(QStringList const sl)
252 {
253         selected_model_.clear();
254         QStringList::const_iterator it  = sl.begin();
255         QStringList::const_iterator end = sl.end();
256         for (int i = 0; it != end; ++it, ++i) {
257                 QStandardItem * si = new QStandardItem();
258                 si->setData(*it);
259                 si->setText(*it);
260                 si->setToolTip(*it);
261                 si->setEditable(false);
262                 selected_model_.insertRow(i, si);
263         }
264 }
265
266
267 QStringList GuiBibtex::selectedBibs()
268 {
269         QStringList res;
270         for (int i = 0; i != selected_model_.rowCount(); ++i) {
271                 QStandardItem const * item = selected_model_.item(i);
272                 if (item)
273                         res.append(item->text());
274         }
275         return res;
276 }
277
278
279 void GuiBibtex::databaseChanged()
280 {
281         QString const item = selectionManager->getSelectedIndex().data().toString();
282         if (!selected_bibs_.contains(item)) {
283                 selected_bibs_.append(item);
284         } else
285                 selected_bibs_ = selectedBibs();
286         setSelectedBibs(selected_bibs_);
287 }
288
289
290 void GuiBibtex::updateContents()
291 {
292         bool bibtopic = usingBibtopic();
293         bool biblatex = usingBiblatex();
294
295         if (biblatex)
296                 setTitle(qt_("Biblatex Bibliography"));
297         else
298                 setTitle(qt_("BibTeX Bibliography"));
299
300         QString const bibstyle = styleFile();
301
302         bibtocCB->setChecked(bibtotoc() && !bibtopic);
303         bibtocCB->setEnabled(!bibtopic);
304
305         btPrintCO->clear();
306         btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
307         if (bibtopic)
308                 btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
309         btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
310         if (usingBiblatex() && !buffer().masterParams().multibib.empty())
311                 btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
312
313         docstring btprint = params_["btprint"];
314         if (btprint.empty())
315                 // default
316                 btprint = from_ascii("btPrintCited");
317         btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
318
319         docstring encoding = params_["encoding"];
320         if (encoding.empty())
321                 // default
322                 encoding = from_ascii("default");
323         bibEncodingCO->setCurrentIndex(bibEncodingCO->findData(toqstr(encoding)));
324
325         // Only useful for biblatex
326         biblatexOptsLA->setVisible(biblatex);
327         biblatexOptsLE->setVisible(biblatex);
328
329         // only useful for BibTeX
330         bstGB->setVisible(!biblatex);
331
332         if (!biblatex) {
333                 styleCB->clear();
334
335                 int item_nr = -1;
336
337                 QStringList const str = bibStyles();
338                 for (int i = 0; i != str.count(); ++i) {
339                         QString item = changeExtension(str[i], "");
340                         if (item == bibstyle)
341                                 item_nr = i;
342                         styleCB->addItem(item);
343                 }
344
345                 if (item_nr == -1 && !bibstyle.isEmpty()) {
346                         styleCB->addItem(bibstyle);
347                         item_nr = styleCB->count() - 1;
348                 }
349
350
351                 if (item_nr != -1)
352                         styleCB->setCurrentIndex(item_nr);
353                 else
354                         styleCB->clearEditText();
355         } else
356                 biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
357 }
358
359
360 void GuiBibtex::applyView()
361 {
362         docstring dbs;
363
364         unsigned int maxCount = selected_bibs_.count();
365         for (unsigned int i = 0; i < maxCount; i++) {
366                 if (i != 0)
367                         dbs += ',';
368                 QString item = selected_bibs_.at(i);
369                 docstring bibfile = qstring_to_ucs4(item);
370                 dbs += bibfile;
371         }
372
373         params_["bibfiles"] = dbs;
374
375         docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
376         bool const bibtotoc = bibtocCB->isChecked();
377
378         if (bibtotoc && !bibstyle.empty()) {
379                 // both bibtotoc and style
380                 params_["options"] = "bibtotoc," + bibstyle;
381         } else if (bibtotoc) {
382                 // bibtotoc and no style
383                 params_["options"] = from_ascii("bibtotoc");
384         } else {
385                 // only style. An empty one is valid, because some
386                 // documentclasses have an own \bibliographystyle{}
387                 // command!
388                 params_["options"] = bibstyle;
389         }
390
391         params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
392
393         params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
394
395         params_["encoding"] = qstring_to_ucs4(bibEncodingCO->itemData(bibEncodingCO->currentIndex()).toString());
396 }
397
398
399 QString GuiBibtex::browseBib(QString const & in_name) const
400 {
401         QString const label1 = qt_("D&ocuments");
402         QString const dir1 = toqstr(lyxrc.document_path);
403         QStringList const filter(qt_("BibTeX Databases (*.bib)"));
404         return browseRelToParent(in_name, bufferFilePath(),
405                 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
406 }
407
408
409 QString GuiBibtex::browseBst(QString const & in_name) const
410 {
411         QString const label1 = qt_("D&ocuments");
412         QString const dir1 = toqstr(lyxrc.document_path);
413         QStringList const filter(qt_("BibTeX Styles (*.bst)"));
414         return browseRelToParent(in_name, bufferFilePath(),
415                 qt_("Select a BibTeX style"), filter, false, label1, dir1);
416 }
417
418
419 QStringList GuiBibtex::bibStyles() const
420 {
421         QStringList sdata = texFileList("bstFiles.lst");
422         // test whether we have a valid list, otherwise run rescan
423         if (sdata.isEmpty()) {
424                 rescanBibStyles();
425                 sdata = texFileList("bstFiles.lst");
426         }
427         for (int i = 0; i != sdata.size(); ++i)
428                 sdata[i] = onlyFileName(sdata[i]);
429         // sort on filename only (no path)
430         sdata.sort();
431         return sdata;
432 }
433
434
435 QStringList GuiBibtex::bibFiles(bool const extension) const
436 {
437         QStringList sdata = texFileList("bibFiles.lst");
438         // test whether we have a valid list, otherwise run rescan
439         if (sdata.isEmpty()) {
440                 rescanBibStyles();
441                 sdata = texFileList("bibFiles.lst");
442         }
443         for (int i = 0; i != sdata.size(); ++i)
444                 sdata[i] = extension ? onlyFileName(sdata[i])
445                                      : changeExtension(onlyFileName(sdata[i]), "");
446         // sort on filename only (no path)
447         sdata.sort();
448         return sdata;
449 }
450
451
452 void GuiBibtex::rescanBibStyles() const
453 {
454         if (usingBiblatex())
455                 rescanTexStyles("bib");
456         else
457                 rescanTexStyles("bst bib");
458 }
459
460
461 void GuiBibtex::findText(QString const & text)
462 {
463         QStringList const result = bibFiles(false).filter(text);
464         available_model_.setStringList(result);
465 }
466
467
468 void GuiBibtex::filterChanged(const QString & text)
469 {
470         if (!text.isEmpty()) {
471                 findText(filter_->text());
472                 return;
473         }
474         findText(filter_->text());
475         filter_->setFocus();
476 }
477
478
479 void GuiBibtex::filterPressed()
480 {
481         findText(filter_->text());
482 }
483
484
485 void GuiBibtex::resetFilter()
486 {
487         filter_->setText(QString());
488         findText(filter_->text());
489 }
490
491
492
493 bool GuiBibtex::usingBibtopic() const
494 {
495         return buffer().params().useBibtopic();
496 }
497
498
499 bool GuiBibtex::bibtotoc() const
500 {
501         return prefixIs(to_utf8(params_["options"]), "bibtotoc");
502 }
503
504
505 bool GuiBibtex::usingBiblatex() const
506 {
507         return buffer().masterBuffer()->params().useBiblatex();
508 }
509
510
511 QString GuiBibtex::styleFile() const
512 {
513         // the different bibtex packages have (and need) their
514         // own "plain" stylefiles
515         QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
516
517         QString bst = toqstr(params_["options"]);
518         if (bibtotoc()){
519                 // bibstyle exists?
520                 int pos = bst.indexOf(',');
521                 if (pos != -1) {
522                         // FIXME: check
523                         // docstring bibtotoc = from_ascii("bibtotoc");
524                         // bst = split(bst, bibtotoc, ',');
525                         bst = bst.mid(pos + 1);
526                 } else {
527                         bst.clear();
528                 }
529         }
530
531         // propose default style file for new insets
532         // existing insets might have (legally) no bst files
533         // (if the class already provides a style)
534         if (bst.isEmpty() && params_["bibfiles"].empty())
535                 bst = defaultstyle;
536
537         return bst;
538 }
539
540
541 bool GuiBibtex::initialiseParams(std::string const & sdata)
542 {
543         InsetCommand::string2params(sdata, params_);
544         init();
545         return true;
546 }
547
548
549 void GuiBibtex::dispatchParams()
550 {
551         std::string const lfun = InsetCommand::params2string(params_);
552         dispatch(FuncRequest(getLfun(), lfun));
553 }
554
555
556 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
557
558
559 } // namespace frontend
560 } // namespace lyx
561
562 #include "moc_GuiBibtex.cpp"