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