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