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