]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBibtex.cpp
less string conversions as long as we stay in the frontend
[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 "ui_BibtexAddUi.h"
21 #include "qt_helpers.h"
22 #include "Validator.h"
23 #include "LyXRC.h"
24
25 #include "ButtonPolicy.h"
26
27 #include "support/debug.h"
28 #include "support/FileFilterList.h"
29 #include "support/filetools.h" // changeExtension
30 #include "support/gettext.h"
31 #include "support/lstrings.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         QString const file = browseBst(QString());
132
133         if (file.isEmpty())
134                 return;
135
136         QString const filen = changeExtension(file, "");
137         bool present = false;
138         unsigned int pres = 0;
139
140         for (int i = 0; i != styleCB->count(); ++i) {
141                 if (styleCB->itemText(i) == filen) {
142                         present = true;
143                         pres = i;
144                 }
145         }
146
147         if (!present)
148                 styleCB->insertItem(0, filen);
149
150         styleCB->setCurrentIndex(pres);
151         changed();
152 }
153
154
155 void GuiBibtex::browseBibPressed()
156 {
157         QString const file = browseBib(QString()).trimmed();
158
159         if (file.isEmpty())
160                 return;
161
162         QString const f = changeExtension(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 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         QString const file = add_->bibED->text().trimmed();
190
191         if (sel < 0 && file.isEmpty())
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.isEmpty()) {
214                 add_->bibED->clear();
215                 QString const f = changeExtension(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         QStringList bibfiles = bibFiles();
307         for (int i = 0; i != bibfiles.count(); ++i)
308                 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
309
310         QString bibstyle = styleFile();
311
312         bibtocCB->setChecked(bibtotoc() && !bibtopic);
313         bibtocCB->setEnabled(!bibtopic);
314
315         if (!bibtopic && btPrintCO->count() == 3)
316                 btPrintCO->removeItem(1);
317         else if (bibtopic && btPrintCO->count() < 3)
318                 btPrintCO->insertItem(1, qt_("all uncited references", 0));
319
320         docstring btprint = params_["btprint"];
321         int btp = 0;
322         if ((bibtopic && btprint == "btPrintNotCited") ||
323            (!bibtopic && btprint == "btPrintAll"))
324                 btp = 1;
325         else if (bibtopic && btprint == "btPrintAll")
326                 btp = 2;
327
328         btPrintCO->setCurrentIndex(btp);
329
330         styleCB->clear();
331
332         int item_nr = -1;
333
334         QStringList str = bibStyles();
335         for (int i = 0; i != str.count(); ++i) {
336                 QString item = changeExtension(str[i], "");
337                 if (item == bibstyle)
338                         item_nr = i;
339                 styleCB->addItem(item);
340         }
341
342         if (item_nr == -1 && !bibstyle.isEmpty()) {
343                 styleCB->addItem(bibstyle);
344                 item_nr = styleCB->count() - 1;
345         }
346
347         if (item_nr != -1)
348                 styleCB->setCurrentIndex(item_nr);
349         else
350                 styleCB->clearEditText();
351 }
352
353
354 void GuiBibtex::applyView()
355 {
356         QString dbs = databaseLW->item(0)->text();
357         docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
358
359         unsigned int maxCount = databaseLW->count();
360         for (unsigned int i = 1; i < maxCount; i++) {
361                 dbs += ',';
362                 dbs += databaseLW->item(i)->text();
363                 emb += ',';
364                 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
365         }
366
367         params_["bibfiles"] = qstring_to_ucs4(dbs);
368         params_["embed"] = emb;
369
370         docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
371         bool const bibtotoc = bibtocCB->isChecked();
372
373         if (bibtotoc && !bibstyle.empty()) {
374                 // both bibtotoc and style
375                 params_["options"] = "bibtotoc," + bibstyle;
376         } else if (bibtotoc) {
377                 // bibtotoc and no style
378                 params_["options"] = from_ascii("bibtotoc");
379         } else {
380                 // only style. An empty one is valid, because some
381                 // documentclasses have an own \bibliographystyle{}
382                 // command!
383                 params_["options"] = bibstyle;
384         }
385
386         int btp = btPrintCO->currentIndex();
387
388         if (usingBibtopic()) {
389                 // bibtopic allows three kinds of sections:
390                 // 1. sections that include all cited references of the database(s)
391                 // 2. sections that include all uncited references of the database(s)
392                 // 3. sections that include all references of the database(s), cited or not
393                 switch (btp) {
394                 case 0:
395                         params_["btprint"] = from_ascii("btPrintCited");
396                         break;
397                 case 1:
398                         params_["btprint"] = from_ascii("btPrintNotCited");
399                         break;
400                 case 2:
401                         params_["btprint"] = from_ascii("btPrintAll");
402                         break;
403                 }
404         } else {
405                 switch (btp) {
406                 case 0:
407                         params_["btprint"] = docstring();
408                         break;
409                 case 1:
410                         // use \nocite{*}
411                         params_["btprint"] = from_ascii("btPrintAll");
412                         break;
413                 }               
414         }
415 }
416
417
418 bool GuiBibtex::isValid()
419 {
420         return databaseLW->count() != 0;
421 }
422
423
424 QString GuiBibtex::browseBib(QString const & in_name) const
425 {
426         QString const label1 = qt_("Documents|#o#O");
427         QString const dir1 = toqstr(lyxrc.document_path);
428         FileFilterList const filter(_("BibTeX Databases (*.bib)"));
429         return browseRelFile(in_name, bufferFilepath(),
430                 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
431 }
432
433
434 QString GuiBibtex::browseBst(QString const & in_name) const
435 {
436         QString const label1 = qt_("Documents|#o#O");
437         QString const dir1 = toqstr(lyxrc.document_path);
438         FileFilterList const filter(_("BibTeX Styles (*.bst)"));
439         return browseRelFile(in_name, bufferFilepath(),
440                 qt_("Select a BibTeX style"), filter, false, label1, dir1);
441 }
442
443
444 QStringList GuiBibtex::bibStyles() const
445 {
446         QStringList data = texFileList("bstFiles.lst");
447         // test whether we have a valid list, otherwise run rescan
448         if (data.isEmpty()) {
449                 rescanBibStyles();
450                 data = texFileList("bstFiles.lst");
451         }
452         for (int i = 0; i != data.size(); ++i)
453                 data[i] = onlyFilename(data[i]);
454         // sort on filename only (no path)
455         data.sort();
456         return data;
457 }
458
459
460 QStringList GuiBibtex::bibFiles() const
461 {
462         QStringList data = texFileList("bibFiles.lst");
463         // test whether we have a valid list, otherwise run rescan
464         if (data.isEmpty()) {
465                 rescanBibStyles();
466                 data = texFileList("bibFiles.lst");
467         }
468         for (int i = 0; i != data.size(); ++i)
469                 data[i] = onlyFilename(data[i]);
470         // sort on filename only (no path)
471         data.sort();
472         return data;
473 }
474
475
476 void GuiBibtex::rescanBibStyles() const
477 {
478         rescanTexStyles();
479 }
480
481
482 bool GuiBibtex::usingBibtopic() const
483 {
484         return buffer().params().use_bibtopic;
485 }
486
487
488 bool GuiBibtex::bibtotoc() const
489 {
490         return prefixIs(to_utf8(params_["options"]), "bibtotoc");
491 }
492
493
494 QString GuiBibtex::styleFile() const
495 {
496         // the different bibtex packages have (and need) their
497         // own "plain" stylefiles
498         biblio::CiteEngine const engine = buffer().params().getEngine();
499         QString defaultstyle;
500         switch (engine) {
501         case biblio::ENGINE_BASIC:
502                 defaultstyle = "plain";
503                 break;
504         case biblio::ENGINE_NATBIB_AUTHORYEAR:
505                 defaultstyle = "plainnat";
506                 break;
507         case biblio::ENGINE_NATBIB_NUMERICAL:
508                 defaultstyle = "plainnat";
509                 break;
510         case biblio::ENGINE_JURABIB:
511                 defaultstyle = "jurabib";
512                 break;
513         }
514
515         QString bst = toqstr(params_["options"]);
516         if (bibtotoc()){
517                 // bibstyle exists?
518                 int pos = bst.indexOf(',');
519                 if (pos != -1) {
520                         // FIXME: check
521                         // docstring bibtotoc = from_ascii("bibtotoc");
522                         // bst = split(bst, bibtotoc, ',');
523                         bst = bst.mid(pos);     
524                 } else {
525                         bst.clear();
526                 }
527         }
528
529         // propose default style file for new insets
530         // existing insets might have (legally) no bst files
531         // (if the class already provides a style)
532         if (bst.isEmpty() && params_["bibfiles"].empty())
533                 bst = defaultstyle;
534
535         return bst;
536 }
537
538
539 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
540
541
542 } // namespace frontend
543 } // namespace lyx
544
545 #include "GuiBibtex_moc.cpp"