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