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