]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBibtex.cpp
Set a maximum value to zoom level
[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 "CiteEnginesList.h"
21 #include "FuncRequest.h"
22 #include "LyXRC.h"
23 #include "qt_helpers.h"
24 #include "Validator.h"
25
26 #include "ui_BibtexAddUi.h"
27
28 #include "ButtonPolicy.h"
29
30 #include "frontends/alert.h"
31
32 #include "insets/InsetBibtex.h"
33
34 #include "support/debug.h"
35 #include "support/ExceptionMessage.h"
36 #include "support/FileName.h"
37 #include "support/filetools.h" // changeExtension
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40
41 #include <QPushButton>
42 #include <QListWidget>
43 #include <QCheckBox>
44 #include <QLineEdit>
45
46 using namespace std;
47 using namespace lyx::support;
48
49 namespace lyx {
50 namespace frontend {
51
52
53 GuiBibtex::GuiBibtex(GuiView & lv)
54         : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
55           params_(insetCode("bibtex"))
56 {
57         setupUi(this);
58
59         QDialog::setModal(true);
60         setWindowModality(Qt::WindowModal);
61
62         connect(okPB, SIGNAL(clicked()),
63                 this, SLOT(slotOK()));
64         connect(closePB, SIGNAL(clicked()),
65                 this, SLOT(slotClose()));
66         connect(stylePB, SIGNAL(clicked()),
67                 this, SLOT(browsePressed()));
68         connect(deletePB, SIGNAL(clicked()),
69                 this, SLOT(deletePressed()));
70         connect(upPB, SIGNAL(clicked()),
71                 this, SLOT(upPressed()));
72         connect(downPB, SIGNAL(clicked()),
73                 this, SLOT(downPressed()));
74         connect(styleCB, SIGNAL(editTextChanged(QString)),
75                 this, SLOT(change_adaptor()));
76         connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
77                 this, SLOT(databaseChanged()));
78         connect(bibtocCB, SIGNAL(clicked()),
79                 this, SLOT(change_adaptor()));
80         connect(btPrintCO, SIGNAL(activated(int)),
81                 this, SLOT(change_adaptor()));
82         connect(addBibPB, SIGNAL(clicked()),
83                 this, SLOT(addPressed()));
84         connect(rescanPB, SIGNAL(clicked()),
85                 this, SLOT(rescanClicked()));
86         connect(biblatexOptsLE, SIGNAL(textChanged(QString)),
87                 this, SLOT(change_adaptor()));
88
89         add_ = new GuiBibtexAddDialog(this);
90         add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
91         add_bc_.setOK(add_->addPB);
92         add_bc_.setCancel(add_->closePB);
93         add_bc_.addCheckedLineEdit(add_->bibED, 0);
94
95         connect(add_->bibED, SIGNAL(textChanged(QString)),
96                 this, SLOT(bibEDChanged()));
97         connect(add_->addPB, SIGNAL(clicked()),
98                 this, SLOT(addDatabase()));
99         connect(add_->addPB, SIGNAL(clicked()),
100                 add_, SLOT(accept()));
101         connect(add_->rescanPB, SIGNAL(clicked()),
102                 this, SLOT(rescanClicked()));
103         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
104                 this, SLOT(addDatabase()));
105         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
106                 add_, SLOT(accept()));
107         connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
108                 this, SLOT(availableChanged()));
109         connect(add_->browsePB, SIGNAL(clicked()),
110                 this, SLOT(browseBibPressed()));
111         connect(add_->closePB, SIGNAL(clicked()),
112                 add_, SLOT(reject()));
113
114         add_->bibLW->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
115                                     "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
116                                     "If you want to reuse your own database, this is the place you should store it.")));
117
118         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
119         bc().setOK(okPB);
120         bc().setCancel(closePB);
121         bc().addReadOnly(databaseLW);
122         bc().addReadOnly(stylePB);
123         bc().addReadOnly(styleCB);
124         bc().addReadOnly(bibtocCB);
125         bc().addReadOnly(addBibPB);
126         // Delete/Up/Down are handled with more conditions in
127         // databaseChanged().
128
129         // Make sure the delete/up/down buttons are disabled if necessary.
130         databaseChanged();
131 }
132
133
134 void GuiBibtex::bibEDChanged()
135 {
136         // Indicate to the button controller that the contents have
137         // changed. The actual test of validity is carried out by
138         // the checkedLineEdit.
139         add_bc_.setValid(true);
140 }
141
142
143 void GuiBibtex::change_adaptor()
144 {
145         changed();
146 }
147
148
149 void GuiBibtex::browsePressed()
150 {
151         QString const file = browseBst(QString());
152
153         if (file.isEmpty())
154                 return;
155
156         QString const filen = changeExtension(file, "");
157         bool present = false;
158         unsigned int pres = 0;
159
160         for (int i = 0; i != styleCB->count(); ++i) {
161                 if (styleCB->itemText(i) == filen) {
162                         present = true;
163                         pres = i;
164                 }
165         }
166
167         if (!present)
168                 styleCB->insertItem(0, filen);
169
170         styleCB->setCurrentIndex(pres);
171         changed();
172 }
173
174
175 void GuiBibtex::browseBibPressed()
176 {
177         QString const file = browseBib(QString()).trimmed();
178
179         if (file.isEmpty())
180                 return;
181
182         QString const f = changeExtension(file, "");
183         bool present = false;
184
185         for (int i = 0; i < add_->bibLW->count(); ++i) {
186                 if (add_->bibLW->item(i)->text() == f)
187                         present = true;
188         }
189
190         if (!present) {
191                 add_->bibLW->addItem(f);
192                 changed();
193         }
194
195         add_->bibED->setText(f);
196 }
197
198
199 void GuiBibtex::addPressed()
200 {
201         add_bc_.setValid(false);
202         add_->exec();
203 }
204
205
206 void GuiBibtex::addDatabase()
207 {
208         int const sel = add_->bibLW->currentRow();
209         QString const file = add_->bibED->text().trimmed();
210
211         if (sel < 0 && file.isEmpty())
212                 return;
213
214         // Add the selected browser_bib keys to browser_database
215         // multiple selections are possible
216         for (int i = 0; i != add_->bibLW->count(); ++i) {
217                 QListWidgetItem * const item = add_->bibLW->item(i);
218                 if (item->isSelected()) {
219                         item->setSelected(false);
220                         QList<QListWidgetItem *> matches =
221                                 databaseLW->findItems(item->text(), Qt::MatchExactly);
222                         if (matches.empty()) {
223                                 QString label = item->text();
224                                 QListWidgetItem * db = new QListWidgetItem(label);
225                                 db->setFlags(db->flags() | Qt::ItemIsSelectable);
226                                 databaseLW->addItem(db);
227                         }
228                 }
229         }
230
231         if (!file.isEmpty()) {
232                 add_->bibED->clear();
233                 QString const f = changeExtension(file, "");
234                 QList<QListWidgetItem *> matches =
235                         databaseLW->findItems(f, Qt::MatchExactly);
236                 if (matches.empty()) {
237                         QListWidgetItem * db = new QListWidgetItem(f);
238                         db->setFlags(db->flags() | Qt::ItemIsSelectable);
239                         databaseLW->addItem(db);
240                 }
241         }
242
243         databaseChanged();
244         changed();
245 }
246
247
248 void GuiBibtex::deletePressed()
249 {
250         QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
251         if (cur) {
252                 delete cur;
253                 databaseChanged();
254                 changed();
255         }
256 }
257
258
259 void GuiBibtex::upPressed()
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::downPressed()
270 {
271         int row = databaseLW->currentRow();
272         QListWidgetItem *cur = databaseLW->takeItem(row);
273         databaseLW->insertItem(row + 1, cur);
274         databaseLW->setCurrentItem(cur);
275         changed();
276 }
277
278
279 void GuiBibtex::rescanClicked()
280 {
281         rescanBibStyles();
282         updateContents();
283 }
284
285
286 void GuiBibtex::databaseChanged()
287 {
288         bool readOnly = isBufferReadonly();
289         int count = databaseLW->count();
290         int row = databaseLW->currentRow();
291         deletePB->setEnabled(!readOnly && row != -1);
292         upPB->setEnabled(!readOnly && count > 1 && row > 0);
293         downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
294 }
295
296
297 void GuiBibtex::availableChanged()
298 {
299         add_bc_.setValid(true);
300 }
301
302
303 void GuiBibtex::updateContents()
304 {
305         bool bibtopic = usingBibtopic();
306         bool biblatex = usingBiblatex();
307
308         if (biblatex)
309                 setTitle(qt_("Biblatex Bibliography"));
310         else
311                 setTitle(qt_("BibTeX Bibliography"));
312
313         databaseLW->clear();
314
315         docstring bibs = params_["bibfiles"];
316         docstring bib;
317
318         while (!bibs.empty()) {
319                 bibs = split(bibs, bib, ',');
320                 bib = trim(bib);
321                 if (!bib.empty()) {
322                         QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
323                         db->setFlags(db->flags() | Qt::ItemIsSelectable);
324                         databaseLW->addItem(db);
325                 }
326         }
327
328         add_->bibLW->clear();
329
330         QStringList bibfiles = bibFiles();
331         for (int i = 0; i != bibfiles.count(); ++i)
332                 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
333
334         QString const bibstyle = styleFile();
335
336         bibtocCB->setChecked(bibtotoc() && !bibtopic);
337         bibtocCB->setEnabled(!bibtopic);
338
339         btPrintCO->clear();
340         btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
341         if (bibtopic)
342                 btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
343         btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
344         if (usingBiblatex() && !buffer().masterParams().multibib.empty())
345                 btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
346
347         docstring btprint = params_["btprint"];
348         if (btprint.empty())
349                 // default
350                 btprint = from_ascii("btPrintCited");
351         btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
352
353         // Only useful for biblatex
354         biblatexOptsLA->setVisible(biblatex);
355         biblatexOptsLE->setVisible(biblatex);
356
357         // only useful for BibTeX
358         styleCB->setVisible(!biblatex);
359         styleLA->setVisible(!biblatex);
360         stylePB->setVisible(!biblatex);
361
362         if (!biblatex) {
363                 styleCB->clear();
364
365                 int item_nr = -1;
366
367                 QStringList const str = bibStyles();
368                 for (int i = 0; i != str.count(); ++i) {
369                         QString item = changeExtension(str[i], "");
370                         if (item == bibstyle)
371                                 item_nr = i;
372                         styleCB->addItem(item);
373                 }
374
375                 if (item_nr == -1 && !bibstyle.isEmpty()) {
376                         styleCB->addItem(bibstyle);
377                         item_nr = styleCB->count() - 1;
378                 }
379
380
381                 if (item_nr != -1)
382                         styleCB->setCurrentIndex(item_nr);
383                 else
384                         styleCB->clearEditText();
385         } else
386                 biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
387 }
388
389
390 void GuiBibtex::applyView()
391 {
392         docstring dbs;
393
394         unsigned int maxCount = databaseLW->count();
395         for (unsigned int i = 0; i < maxCount; i++) {
396                 if (i != 0)
397                         dbs += ',';
398                 QString item = databaseLW->item(i)->text();
399                 docstring bibfile = qstring_to_ucs4(item);
400                 dbs += bibfile;
401         }
402
403         params_["bibfiles"] = dbs;
404
405         docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
406         bool const bibtotoc = bibtocCB->isChecked();
407
408         if (bibtotoc && !bibstyle.empty()) {
409                 // both bibtotoc and style
410                 params_["options"] = "bibtotoc," + bibstyle;
411         } else if (bibtotoc) {
412                 // bibtotoc and no style
413                 params_["options"] = from_ascii("bibtotoc");
414         } else {
415                 // only style. An empty one is valid, because some
416                 // documentclasses have an own \bibliographystyle{}
417                 // command!
418                 params_["options"] = bibstyle;
419         }
420
421         params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
422
423         params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
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 browseRelToParent(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 browseRelToParent(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         if (usingBiblatex())
488                 rescanTexStyles("bib");
489         else
490                 rescanTexStyles("bst bib");
491 }
492
493
494 bool GuiBibtex::usingBibtopic() const
495 {
496         return buffer().params().useBibtopic();
497 }
498
499
500 bool GuiBibtex::bibtotoc() const
501 {
502         return prefixIs(to_utf8(params_["options"]), "bibtotoc");
503 }
504
505
506 bool GuiBibtex::usingBiblatex() const
507 {
508         return buffer().masterBuffer()->params().useBiblatex();
509 }
510
511
512 QString GuiBibtex::styleFile() const
513 {
514         // the different bibtex packages have (and need) their
515         // own "plain" stylefiles
516         QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
517
518         QString bst = toqstr(params_["options"]);
519         if (bibtotoc()){
520                 // bibstyle exists?
521                 int pos = bst.indexOf(',');
522                 if (pos != -1) {
523                         // FIXME: check
524                         // docstring bibtotoc = from_ascii("bibtotoc");
525                         // bst = split(bst, bibtotoc, ',');
526                         bst = bst.mid(pos + 1);
527                 } else {
528                         bst.clear();
529                 }
530         }
531
532         // propose default style file for new insets
533         // existing insets might have (legally) no bst files
534         // (if the class already provides a style)
535         if (bst.isEmpty() && params_["bibfiles"].empty())
536                 bst = defaultstyle;
537
538         return bst;
539 }
540
541
542 bool GuiBibtex::initialiseParams(std::string const & data)
543 {
544         InsetCommand::string2params(data, params_);
545         return true;
546 }
547
548
549 void GuiBibtex::dispatchParams()
550 {
551         std::string const lfun = InsetCommand::params2string(params_);
552         dispatch(FuncRequest(getLfun(), lfun));
553 }
554
555
556
557 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
558
559
560 } // namespace frontend
561 } // namespace lyx
562
563 #include "moc_GuiBibtex.cpp"