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