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