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