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