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