]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBibtex.cpp
Factorize out the way window titles are handled.
[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::closeEvent(QCloseEvent *e)
282 {
283         slotClose();
284         e->accept();
285 }
286
287
288 void GuiBibtex::updateContents()
289 {
290         bool bibtopic = usingBibtopic();
291
292         databaseLW->clear();
293
294         docstring bibs = params_["bibfiles"];
295         docstring embs = params_["embed"];
296         docstring bib;
297         docstring emb;
298
299         while (!bibs.empty()) {
300                 bibs = split(bibs, bib, ',');
301                 embs = split(embs, emb, ',');
302                 bib = trim(bib);
303                 if (!bib.empty()) {
304                         QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
305                         db->setFlags(db->flags() | Qt::ItemIsSelectable
306                                 | Qt::ItemIsUserCheckable);
307                         db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
308                         databaseLW->addItem(db);
309                 }
310         }
311
312         add_->bibLW->clear();
313
314         vector<string> bib_str;
315         getBibFiles(bib_str);
316         for (vector<string>::const_iterator it = bib_str.begin();
317                 it != bib_str.end(); ++it) {
318                 string bibItem(changeExtension(*it, ""));
319                 add_->bibLW->addItem(toqstr(bibItem));
320         }
321
322         string bibstyle = getStylefile();
323
324         bibtocCB->setChecked(bibtotoc() && !bibtopic);
325         bibtocCB->setEnabled(!bibtopic);
326
327         if (!bibtopic && btPrintCO->count() == 3)
328                 btPrintCO->removeItem(1);
329         else if (bibtopic && btPrintCO->count() < 3)
330                 btPrintCO->insertItem(1, qt_("all uncited references", 0));
331
332         docstring btprint = params_["btprint"];
333         int btp = 0;
334         if ((bibtopic && btprint == "btPrintNotCited") ||
335            (!bibtopic && btprint == "btPrintAll"))
336                 btp = 1;
337         else if (bibtopic && btprint == "btPrintAll")
338                 btp = 2;
339
340         btPrintCO->setCurrentIndex(btp);
341
342         styleCB->clear();
343
344         int item_nr(-1);
345
346         vector<string> str;
347         getBibStyles(str);
348         for (vector<string>::const_iterator it = str.begin();
349                 it != str.end(); ++it) {
350                 string item(changeExtension(*it, ""));
351                 if (item == bibstyle)
352                         item_nr = int(it - str.begin());
353                 styleCB->addItem(toqstr(item));
354         }
355
356         if (item_nr == -1 && !bibstyle.empty()) {
357                 styleCB->addItem(toqstr(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 = qstring_to_ucs4(databaseLW->item(0)->text());
371         docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
372
373         unsigned int maxCount = databaseLW->count();
374         for (unsigned int i = 1; i < maxCount; i++) {
375                 dbs += ',';
376                 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
377                 emb += ',';
378                 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
379         }
380
381         params_["bibfiles"] = dbs;
382         params_["embed"] = emb;
383
384         docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
385         bool const bibtotoc = bibtocCB->isChecked();
386
387         if (bibtotoc && (!bibstyle.empty())) {
388                 // both bibtotoc and style
389                 params_["options"] = "bibtotoc," + bibstyle;
390         } else if (bibtotoc) {
391                 // bibtotoc and no style
392                 params_["options"] = from_ascii("bibtotoc");
393         } else {
394                 // only style. An empty one is valid, because some
395                 // documentclasses have an own \bibliographystyle{}
396                 // command!
397                 params_["options"] = bibstyle;
398         }
399
400         int btp = btPrintCO->currentIndex();
401
402         if (usingBibtopic()) {
403                 // bibtopic allows three kinds of sections:
404                 // 1. sections that include all cited references of the database(s)
405                 // 2. sections that include all uncited references of the database(s)
406                 // 3. sections that include all references of the database(s), cited or not
407                 switch (btp) {
408                 case 0:
409                         params_["btprint"] = from_ascii("btPrintCited");
410                         break;
411                 case 1:
412                         params_["btprint"] = from_ascii("btPrintNotCited");
413                         break;
414                 case 2:
415                         params_["btprint"] = from_ascii("btPrintAll");
416                         break;
417                 }
418         } else {
419                 switch (btp) {
420                 case 0:
421                         params_["btprint"] = docstring();
422                         break;
423                 case 1:
424                         // use \nocite{*}
425                         params_["btprint"] = from_ascii("btPrintAll");
426                         break;
427                 }               
428         }
429 }
430
431
432 bool GuiBibtex::isValid()
433 {
434         return databaseLW->count() != 0;
435 }
436
437
438 docstring const GuiBibtex::browseBib(docstring const & in_name) const
439 {
440         // FIXME UNICODE
441         docstring const label1 = _("Documents|#o#O");
442         docstring const dir1 = from_utf8(lyxrc.document_path);
443         FileFilterList const filter(_("BibTeX Databases (*.bib)"));
444         return browseRelFile(in_name, from_utf8(bufferFilepath()),
445                 _("Select a BibTeX database to add"), filter, false, label1, dir1);
446 }
447
448
449 docstring const GuiBibtex::browseBst(docstring const & in_name) const
450 {
451         // FIXME UNICODE
452         docstring const label1 = _("Documents|#o#O");
453         docstring const dir1 = from_utf8(lyxrc.document_path);
454         FileFilterList const filter(_("BibTeX Styles (*.bst)"));
455         return browseRelFile(in_name, from_utf8(bufferFilepath()),
456                 _("Select a BibTeX style"), filter, false, label1, dir1);
457 }
458
459
460 void GuiBibtex::getBibStyles(vector<string> & data) const
461 {
462         data.clear();
463
464         getTexFileList("bstFiles.lst", data);
465         // test, if we have a valid list, otherwise run rescan
466         if (data.empty()) {
467                 rescanBibStyles();
468                 getTexFileList("bstFiles.lst", data);
469         }
470         vector<string>::iterator it  = data.begin();
471         vector<string>::iterator end = data.end();
472         for (; it != end; ++it) {
473                 *it = onlyFilename(*it);
474         }
475         // sort on filename only (no path)
476         sort(data.begin(), data.end());
477 }
478
479
480 void GuiBibtex::getBibFiles(vector<string> & data) const
481 {
482         data.clear();
483
484         getTexFileList("bibFiles.lst", data);
485         // test, if we have a valid list, otherwise run rescan
486         if (data.empty()) {
487                 rescanBibStyles();
488                 getTexFileList("bibFiles.lst", data);
489         }
490         vector<string>::iterator it  = data.begin();
491         vector<string>::iterator end = data.end();
492         for (; it != end; ++it) {
493                 *it = onlyFilename(*it);
494         }
495         // sort on filename only (no path)
496         sort(data.begin(), data.end());
497 }
498
499
500 void GuiBibtex::rescanBibStyles() const
501 {
502         rescanTexStyles();
503 }
504
505
506 bool GuiBibtex::usingBibtopic() const
507 {
508         return buffer().params().use_bibtopic;
509 }
510
511
512 bool GuiBibtex::bibtotoc() const
513 {
514         return prefixIs(to_utf8(params_["options"]), "bibtotoc");
515 }
516
517
518 string const GuiBibtex::getStylefile() const
519 {
520         // the different bibtex packages have (and need) their
521         // own "plain" stylefiles
522         biblio::CiteEngine const engine = buffer().params().getEngine();
523         docstring defaultstyle;
524         switch (engine) {
525         case biblio::ENGINE_BASIC:
526                 defaultstyle = from_ascii("plain");
527                 break;
528         case biblio::ENGINE_NATBIB_AUTHORYEAR:
529                 defaultstyle = from_ascii("plainnat");
530                 break;
531         case biblio::ENGINE_NATBIB_NUMERICAL:
532                 defaultstyle = from_ascii("plainnat");
533                 break;
534         case biblio::ENGINE_JURABIB:
535                 defaultstyle = from_ascii("jurabib");
536                 break;
537         }
538
539         docstring bst = params_["options"];
540         if (bibtotoc()){
541                 // bibstyle exists?
542                 if (contains(bst, ',')) {
543                         docstring bibtotoc = from_ascii("bibtotoc");
544                         bst = split(bst, bibtotoc, ',');
545                 } else
546                         bst.erase();
547         }
548
549         // propose default style file for new insets
550         // existing insets might have (legally) no bst files
551         // (if the class already provides a style)
552         if (bst.empty() && params_["bibfiles"].empty())
553                 bst = defaultstyle;
554
555         // FIXME UNICODE
556         return to_utf8(bst);
557 }
558
559
560 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
561
562
563 } // namespace frontend
564 } // namespace lyx
565
566 #include "GuiBibtex_moc.cpp"