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"
19 #include "qt_helpers.h"
20 #include "Validator.h"
22 #include "CheckedLineEdit.h"
24 #include "controllers/ControlBibtex.h"
25 #include "controllers/ButtonPolicy.h"
27 #include "support/filetools.h" // changeExtension
28 #include "support/lstrings.h"
30 #include <QPushButton>
31 #include <QListWidget>
33 #include <QCloseEvent>
37 #include "support/filetools.h"
38 #include "support/lstrings.h"
47 using support::changeExtension;
52 /////////////////////////////////////////////////////////////////////
56 /////////////////////////////////////////////////////////////////////
58 GuiBibtexDialog::GuiBibtexDialog(GuiBibtex * form)
62 QDialog::setModal(true);
64 connect(okPB, SIGNAL(clicked()),
65 form, SLOT(slotOK()));
66 connect(closePB, SIGNAL(clicked()),
67 form, SLOT(slotClose()));
68 connect(stylePB, SIGNAL(clicked()),
69 this, SLOT(browsePressed()));
70 connect(deletePB, SIGNAL(clicked()),
71 this, SLOT(deletePressed()));
72 connect(styleCB, SIGNAL(editTextChanged (const QString &)),
73 this, SLOT(change_adaptor()));
74 connect(databaseLW, SIGNAL(itemSelectionChanged()),
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()));
83 add_ = new UiDialog<Ui::BibtexAddUi>(this, true);
85 Qt2BC * bcview = new Qt2BC(add_bc_);
87 add_bc_.bp(new OkCancelPolicy);
89 bcview->setOK(add_->addPB);
90 bcview->setCancel(add_->closePB);
92 addCheckedLineEdit(add_bc_.view(), add_->bibED, 0);
94 connect(add_->bibED, SIGNAL(textChanged(const QString &)),
95 this, SLOT(bibEDChanged()));
96 connect(add_->addPB, SIGNAL(clicked()),
97 this, SLOT(addDatabase()));
98 connect(add_->addPB, SIGNAL(clicked()),
99 add_, SLOT(accept()));
100 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
101 this, SLOT(addDatabase()));
102 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
103 add_, SLOT(accept()));
104 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
105 this, SLOT(availableChanged()));
106 connect(add_->browsePB, SIGNAL(clicked()),
107 this, SLOT(browseBibPressed()));
108 connect(add_->closePB, SIGNAL(clicked()),
109 add_, SLOT(reject()));
114 void GuiBibtexDialog::bibEDChanged()
116 // Indicate to the button controller that the contents have
117 // changed. The actual test of validity is carried out by
118 // the checkedLineEdit.
123 void GuiBibtexDialog::change_adaptor()
129 void GuiBibtexDialog::browsePressed()
131 docstring const file = form_->controller().browseBst(docstring());
135 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
136 bool present = false;
137 unsigned int pres = 0;
139 for (int i = 0; i != styleCB->count(); ++i) {
140 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
147 styleCB->insertItem(0, toqstr(filen));
149 styleCB->setCurrentIndex(pres);
155 void GuiBibtexDialog::browseBibPressed()
157 docstring const file = trim(form_->controller().browseBib(docstring()));
161 QString const f = toqstr(changeExtension(to_utf8(file), ""));
162 bool present = false;
164 for (int i = 0; i < add_->bibLW->count(); ++i) {
165 if (add_->bibLW->item(i)->text() == f)
170 add_->bibLW->addItem(f);
174 add_->bibED->setText(f);
179 void GuiBibtexDialog::addPressed()
181 add_bc_.valid(false);
186 void GuiBibtexDialog::addDatabase()
188 int const sel = add_->bibLW->currentRow();
189 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
191 if (sel < 0 && file.empty())
194 // Add the selected browser_bib keys to browser_database
195 // multiple selections are possible
196 for (int i = 0; i != add_->bibLW->count(); ++i) {
197 QListWidgetItem * const item = add_->bibLW->item(i);
198 if (add_->bibLW->isItemSelected(item)) {
199 add_->bibLW->setItemSelected(item, false);
200 QList<QListWidgetItem *> matches =
201 databaseLW->findItems(item->text(), Qt::MatchExactly);
203 databaseLW->addItem(item->text());
208 add_->bibED->clear();
209 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
210 QList<QListWidgetItem *> matches =
211 databaseLW->findItems(f, Qt::MatchExactly);
213 databaseLW->addItem(f);
220 void GuiBibtexDialog::deletePressed()
222 databaseLW->takeItem(databaseLW->currentRow());
228 void GuiBibtexDialog::databaseChanged()
230 deletePB->setEnabled(!form_->readOnly() && databaseLW->currentRow() != -1);
234 void GuiBibtexDialog::availableChanged()
240 void GuiBibtexDialog::closeEvent(QCloseEvent *e)
247 /////////////////////////////////////////////////////////////////////
251 /////////////////////////////////////////////////////////////////////
254 GuiBibtex::GuiBibtex(Dialog & parent)
255 : GuiView<GuiBibtexDialog>(parent, _("BibTeX Bibliography"))
260 void GuiBibtex::build_dialog()
262 dialog_.reset(new GuiBibtexDialog(this));
264 bcview().setOK(dialog_->okPB);
265 bcview().setCancel(dialog_->closePB);
266 bcview().addReadOnly(dialog_->databaseLW);
267 bcview().addReadOnly(dialog_->stylePB);
268 bcview().addReadOnly(dialog_->styleCB);
269 bcview().addReadOnly(dialog_->bibtocCB);
270 bcview().addReadOnly(dialog_->addBibPB);
271 bcview().addReadOnly(dialog_->deletePB);
275 void GuiBibtex::update_contents()
277 bool bibtopic = controller().usingBibtopic();
279 dialog_->databaseLW->clear();
281 docstring bibs(controller().params()["bibfiles"]);
284 while (!bibs.empty()) {
285 bibs = split(bibs, bib, ',');
288 dialog_->databaseLW->addItem(toqstr(bib));
291 dialog_->add_->bibLW->clear();
293 vector<string> bib_str;
294 controller().getBibFiles(bib_str);
295 for (vector<string>::const_iterator it = bib_str.begin();
296 it != bib_str.end(); ++it) {
297 string bibItem(changeExtension(*it, ""));
298 dialog_->add_->bibLW->addItem(toqstr(bibItem));
301 string bibstyle(controller().getStylefile());
303 dialog_->bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
304 dialog_->bibtocCB->setEnabled(!bibtopic);
306 docstring btprint(controller().params()["btprint"]);
308 if (btprint == "btPrintNotCited")
310 else if (btprint == "btPrintAll")
313 dialog_->btPrintCO->setCurrentIndex(btp);
314 dialog_->btPrintCO->setEnabled(bibtopic);
316 dialog_->styleCB->clear();
321 controller().getBibStyles(str);
322 for (vector<string>::const_iterator it = str.begin();
323 it != str.end(); ++it) {
324 string item(changeExtension(*it, ""));
325 if (item == bibstyle)
326 item_nr = int(it - str.begin());
327 dialog_->styleCB->addItem(toqstr(item));
330 if (item_nr == -1 && !bibstyle.empty()) {
331 dialog_->styleCB->addItem(toqstr(bibstyle));
332 item_nr = dialog_->styleCB->count() - 1;
336 dialog_->styleCB->setCurrentIndex(item_nr);
338 dialog_->styleCB->clearEditText();
342 void GuiBibtex::apply()
344 docstring dbs(qstring_to_ucs4(dialog_->databaseLW->item(0)->text()));
346 unsigned int maxCount = dialog_->databaseLW->count();
347 for (unsigned int i = 1; i < maxCount; i++) {
349 dbs += qstring_to_ucs4(dialog_->databaseLW->item(i)->text());
352 controller().params()["bibfiles"] = dbs;
354 docstring const bibstyle(qstring_to_ucs4(dialog_->styleCB->currentText()));
355 bool const bibtotoc(dialog_->bibtocCB->isChecked());
357 if (bibtotoc && (!bibstyle.empty())) {
358 // both bibtotoc and style
359 controller().params()["options"] = "bibtotoc," + bibstyle;
360 } else if (bibtotoc) {
361 // bibtotoc and no style
362 controller().params()["options"] = from_ascii("bibtotoc");
364 // only style. An empty one is valid, because some
365 // documentclasses have an own \bibliographystyle{}
367 controller().params()["options"] = bibstyle;
370 // bibtopic allows three kinds of sections:
371 // 1. sections that include all cited references of the database(s)
372 // 2. sections that include all uncited references of the database(s)
373 // 3. sections that include all references of the database(s), cited or not
374 int btp = dialog_->btPrintCO->currentIndex();
378 controller().params()["btprint"] = from_ascii("btPrintCited");
381 controller().params()["btprint"] = from_ascii("btPrintNotCited");
384 controller().params()["btprint"] = from_ascii("btPrintAll");
388 if (!controller().usingBibtopic())
389 controller().params()["btprint"] = docstring();
393 bool GuiBibtex::isValid()
395 return dialog_->databaseLW->count() != 0;
398 } // namespace frontend
401 #include "GuiBibtex_moc.cpp"