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