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