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