3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "GuiBibtex.h"
17 #include "ui_BibtexAddUi.h"
18 #include "qt_helpers.h"
19 #include "Validator.h"
22 #include "ControlBibtex.h"
23 #include "ButtonPolicy.h"
25 #include "support/filetools.h" // changeExtension
26 #include "support/lstrings.h"
28 #include <QPushButton>
29 #include <QListWidget>
31 #include <QCloseEvent>
35 #include "support/filetools.h"
36 #include "support/lstrings.h"
45 using support::changeExtension;
50 GuiBibtexDialog::GuiBibtexDialog(LyXView & lv)
51 : GuiDialog(lv, "bibtex")
55 setViewTitle( _("BibTeX Bibliography"));
56 setController(new ControlBibtex(*this));
58 QDialog::setModal(true);
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(styleCB, SIGNAL(editTextChanged(const QString &)),
69 this, SLOT(change_adaptor()));
70 connect(databaseLW, SIGNAL(itemSelectionChanged()),
71 this, SLOT(databaseChanged()));
72 connect(bibtocCB, SIGNAL(clicked()),
73 this, SLOT(change_adaptor()));
74 connect(btPrintCO, SIGNAL(activated(int)),
75 this, SLOT(change_adaptor()));
76 connect(addBibPB, SIGNAL(clicked()),
77 this, SLOT(addPressed()));
79 add_ = new GuiBibtexAddDialog(this);
80 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
81 add_bc_.setOK(add_->addPB);
82 add_bc_.setCancel(add_->closePB);
83 add_bc_.addCheckedLineEdit(add_->bibED, 0);
85 connect(add_->bibED, SIGNAL(textChanged(const QString &)),
86 this, SLOT(bibEDChanged()));
87 connect(add_->addPB, SIGNAL(clicked()),
88 this, SLOT(addDatabase()));
89 connect(add_->addPB, SIGNAL(clicked()),
90 add_, SLOT(accept()));
91 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
92 this, SLOT(addDatabase()));
93 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
94 add_, SLOT(accept()));
95 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
96 this, SLOT(availableChanged()));
97 connect(add_->browsePB, SIGNAL(clicked()),
98 this, SLOT(browseBibPressed()));
99 connect(add_->closePB, SIGNAL(clicked()),
100 add_, SLOT(reject()));
102 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
104 bc().setCancel(closePB);
105 bc().addReadOnly(databaseLW);
106 bc().addReadOnly(stylePB);
107 bc().addReadOnly(styleCB);
108 bc().addReadOnly(bibtocCB);
109 bc().addReadOnly(addBibPB);
110 bc().addReadOnly(deletePB);
114 ControlBibtex & GuiBibtexDialog::controller() const
116 return static_cast<ControlBibtex &>(GuiDialog::controller());
120 void GuiBibtexDialog::bibEDChanged()
122 // Indicate to the button controller that the contents have
123 // changed. The actual test of validity is carried out by
124 // the checkedLineEdit.
125 add_bc_.setValid(true);
129 void GuiBibtexDialog::change_adaptor()
135 void GuiBibtexDialog::browsePressed()
137 docstring const file = controller().browseBst(docstring());
141 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
142 bool present = false;
143 unsigned int pres = 0;
145 for (int i = 0; i != styleCB->count(); ++i) {
146 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
153 styleCB->insertItem(0, toqstr(filen));
155 styleCB->setCurrentIndex(pres);
161 void GuiBibtexDialog::browseBibPressed()
163 docstring const file = trim(controller().browseBib(docstring()));
167 QString const f = toqstr(changeExtension(to_utf8(file), ""));
168 bool present = false;
170 for (int i = 0; i < add_->bibLW->count(); ++i) {
171 if (add_->bibLW->item(i)->text() == f)
176 add_->bibLW->addItem(f);
180 add_->bibED->setText(f);
185 void GuiBibtexDialog::addPressed()
187 add_bc_.setValid(false);
192 void GuiBibtexDialog::addDatabase()
194 int const sel = add_->bibLW->currentRow();
195 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
197 if (sel < 0 && file.empty())
200 // Add the selected browser_bib keys to browser_database
201 // multiple selections are possible
202 for (int i = 0; i != add_->bibLW->count(); ++i) {
203 QListWidgetItem * const item = add_->bibLW->item(i);
204 if (add_->bibLW->isItemSelected(item)) {
205 add_->bibLW->setItemSelected(item, false);
206 QList<QListWidgetItem *> matches =
207 databaseLW->findItems(item->text(), Qt::MatchExactly);
209 databaseLW->addItem(item->text());
214 add_->bibED->clear();
215 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
216 QList<QListWidgetItem *> matches =
217 databaseLW->findItems(f, Qt::MatchExactly);
219 databaseLW->addItem(f);
226 void GuiBibtexDialog::deletePressed()
228 databaseLW->takeItem(databaseLW->currentRow());
234 void GuiBibtexDialog::databaseChanged()
236 deletePB->setEnabled(!readOnly() && databaseLW->currentRow() != -1);
240 void GuiBibtexDialog::availableChanged()
242 add_bc_.setValid(true);
246 void GuiBibtexDialog::closeEvent(QCloseEvent *e)
253 void GuiBibtexDialog::update_contents()
255 bool bibtopic = controller().usingBibtopic();
259 docstring bibs(controller().params()["bibfiles"]);
262 while (!bibs.empty()) {
263 bibs = split(bibs, bib, ',');
266 databaseLW->addItem(toqstr(bib));
269 add_->bibLW->clear();
271 vector<string> bib_str;
272 controller().getBibFiles(bib_str);
273 for (vector<string>::const_iterator it = bib_str.begin();
274 it != bib_str.end(); ++it) {
275 string bibItem(changeExtension(*it, ""));
276 add_->bibLW->addItem(toqstr(bibItem));
279 string bibstyle(controller().getStylefile());
281 bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
282 bibtocCB->setEnabled(!bibtopic);
284 docstring btprint(controller().params()["btprint"]);
286 if (btprint == "btPrintNotCited")
288 else if (btprint == "btPrintAll")
291 btPrintCO->setCurrentIndex(btp);
292 btPrintCO->setEnabled(bibtopic);
299 controller().getBibStyles(str);
300 for (vector<string>::const_iterator it = str.begin();
301 it != str.end(); ++it) {
302 string item(changeExtension(*it, ""));
303 if (item == bibstyle)
304 item_nr = int(it - str.begin());
305 styleCB->addItem(toqstr(item));
308 if (item_nr == -1 && !bibstyle.empty()) {
309 styleCB->addItem(toqstr(bibstyle));
310 item_nr = styleCB->count() - 1;
314 styleCB->setCurrentIndex(item_nr);
316 styleCB->clearEditText();
320 void GuiBibtexDialog::applyView()
322 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
324 unsigned int maxCount = databaseLW->count();
325 for (unsigned int i = 1; i < maxCount; i++) {
327 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
330 controller().params()["bibfiles"] = dbs;
332 docstring const bibstyle(qstring_to_ucs4(styleCB->currentText()));
333 bool const bibtotoc(bibtocCB->isChecked());
335 if (bibtotoc && (!bibstyle.empty())) {
336 // both bibtotoc and style
337 controller().params()["options"] = "bibtotoc," + bibstyle;
338 } else if (bibtotoc) {
339 // bibtotoc and no style
340 controller().params()["options"] = from_ascii("bibtotoc");
342 // only style. An empty one is valid, because some
343 // documentclasses have an own \bibliographystyle{}
345 controller().params()["options"] = bibstyle;
348 // bibtopic allows three kinds of sections:
349 // 1. sections that include all cited references of the database(s)
350 // 2. sections that include all uncited references of the database(s)
351 // 3. sections that include all references of the database(s), cited or not
352 int btp = btPrintCO->currentIndex();
356 controller().params()["btprint"] = from_ascii("btPrintCited");
359 controller().params()["btprint"] = from_ascii("btPrintNotCited");
362 controller().params()["btprint"] = from_ascii("btPrintAll");
366 if (!controller().usingBibtopic())
367 controller().params()["btprint"] = docstring();
371 bool GuiBibtexDialog::isValid()
373 return databaseLW->count() != 0;
376 } // namespace frontend
379 #include "GuiBibtex_moc.cpp"