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.
17 #include "ui/BibtexAddUi.h"
19 #include "qt_helpers.h"
20 #include "validators.h"
22 #include "checkedwidgets.h"
24 #include "controllers/ControlBibtex.h"
25 #include "controllers/ButtonPolicies.h"
27 #include "controllers/ControlBibtex.h"
29 #include "support/filetools.h" // changeExtension
30 #include "support/lstrings.h"
32 #include <QPushButton>
33 #include <QListWidget>
35 #include <QCloseEvent>
38 using lyx::support::changeExtension;
39 using lyx::support::split;
40 using lyx::support::trim;
46 #include "support/filetools.h"
47 #include "support/lstrings.h"
54 /////////////////////////////////////////////////////////////////////
58 /////////////////////////////////////////////////////////////////////
60 QBibtexDialog::QBibtexDialog(QBibtex * form)
64 QDialog::setModal(true);
66 connect(okPB, SIGNAL(clicked()),
67 form, SLOT(slotOK()));
68 connect(closePB, SIGNAL(clicked()),
69 form, SLOT(slotClose()));
70 connect(stylePB, SIGNAL(clicked()),
71 this, SLOT( browsePressed()));
72 connect(deletePB, SIGNAL(clicked()),
73 this, SLOT( deletePressed()));
74 connect(styleCB, SIGNAL(editTextChanged (const QString &)),
75 this, SLOT( change_adaptor()));
76 connect(databaseLW, SIGNAL(itemSelectionChanged()),
77 this, SLOT( databaseChanged()));
78 connect(bibtocCB, SIGNAL(clicked()),
79 this, SLOT( change_adaptor()));
80 connect(btPrintCO, SIGNAL(activated(int)),
81 this, SLOT( change_adaptor()));
82 connect(addBibPB, SIGNAL(clicked()),
83 this, SLOT( addPressed()));
85 add_ = new UiDialog<Ui::QBibtexAddUi>(this, true);
87 Qt2BC * bcview = new Qt2BC(add_bc_);
89 add_bc_.bp(new OkCancelPolicy);
91 bcview->setOK(add_->addPB);
92 bcview->setCancel(add_->closePB);
94 add_->bibED->setValidator(new PathValidator(true, add_->bibED));
95 addCheckedLineEdit(add_bc_.view(), add_->bibED, 0);
97 connect(add_->bibED, SIGNAL(textChanged(const QString &)),
98 this, SLOT(bibEDChanged()));
99 connect(add_->addPB, SIGNAL(clicked()),
100 this, SLOT(addDatabase()));
101 connect(add_->addPB, SIGNAL(clicked()),
102 add_, SLOT(accept()));
103 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
104 this, SLOT(addDatabase()));
105 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
106 add_, SLOT(accept()));
107 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
108 this, SLOT(availableChanged()));
109 connect(add_->browsePB, SIGNAL(clicked()),
110 this, SLOT(browseBibPressed()));
111 connect(add_->closePB, SIGNAL(clicked()),
112 add_, SLOT(reject()));
117 void QBibtexDialog::bibEDChanged()
119 // Indicate to the button controller that the contents have
120 // changed. The actual test of validity is carried out by
121 // the checkedLineEdit.
126 void QBibtexDialog::change_adaptor()
132 void QBibtexDialog::browsePressed()
134 docstring const file = form_->controller().browseBst(docstring());
138 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
139 bool present = false;
140 unsigned int pres = 0;
142 for (int i = 0; i != styleCB->count(); ++i) {
143 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
150 styleCB->insertItem(0, toqstr(filen));
152 styleCB->setCurrentIndex(pres);
158 void QBibtexDialog::browseBibPressed()
160 docstring const file = trim(form_->controller().browseBib(docstring()));
164 QString const f = toqstr(changeExtension(to_utf8(file), ""));
165 bool present = false;
167 for (int i = 0; i < add_->bibLW->count(); ++i) {
168 if (add_->bibLW->item(i)->text() == f)
173 add_->bibLW->addItem(f);
177 add_->bibED->setText(f);
182 void QBibtexDialog::addPressed()
184 add_bc_.valid(false);
189 void QBibtexDialog::addDatabase()
191 int const sel = add_->bibLW->currentRow();
192 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
194 if (sel < 0 && file.empty())
197 // Add the selected browser_bib keys to browser_database
198 // multiple selections are possible
199 for (int i = 0; i != add_->bibLW->count(); ++i) {
200 QListWidgetItem * const item = add_->bibLW->item(i);
201 if (add_->bibLW->isItemSelected(item)) {
202 add_->bibLW->setItemSelected(item, false);
203 QList<QListWidgetItem *> matches =
204 databaseLW->findItems(item->text(), Qt::MatchExactly);
206 databaseLW->addItem(item->text());
211 add_->bibED->clear();
212 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
213 QList<QListWidgetItem *> matches =
214 databaseLW->findItems(f, Qt::MatchExactly);
216 databaseLW->addItem(f);
223 void QBibtexDialog::deletePressed()
225 databaseLW->takeItem(databaseLW->currentRow());
231 void QBibtexDialog::databaseChanged()
233 deletePB->setEnabled(!form_->readOnly() && databaseLW->currentRow() != -1);
237 void QBibtexDialog::availableChanged()
243 void QBibtexDialog::closeEvent(QCloseEvent *e)
250 /////////////////////////////////////////////////////////////////////
254 /////////////////////////////////////////////////////////////////////
257 typedef QController<ControlBibtex, QView<QBibtexDialog> > BibtexBase;
259 QBibtex::QBibtex(Dialog & parent)
260 : BibtexBase(parent, _("BibTeX Bibliography"))
265 void QBibtex::build_dialog()
267 dialog_.reset(new QBibtexDialog(this));
269 bcview().setOK(dialog_->okPB);
270 bcview().setCancel(dialog_->closePB);
271 bcview().addReadOnly(dialog_->databaseLW);
272 bcview().addReadOnly(dialog_->stylePB);
273 bcview().addReadOnly(dialog_->styleCB);
274 bcview().addReadOnly(dialog_->bibtocCB);
275 bcview().addReadOnly(dialog_->addBibPB);
276 bcview().addReadOnly(dialog_->deletePB);
280 void QBibtex::update_contents()
282 PathValidator * path_validator =
283 getPathValidator(dialog_->add_->bibED);
285 path_validator->setChecker(kernel().docType(), lyxrc);
287 bool bibtopic = controller().usingBibtopic();
289 dialog_->databaseLW->clear();
291 docstring bibs(controller().params()["bibfiles"]);
294 while (!bibs.empty()) {
295 bibs = split(bibs, bib, ',');
298 dialog_->databaseLW->addItem(toqstr(bib));
301 dialog_->add_->bibLW->clear();
303 vector<string> bib_str;
304 controller().getBibFiles(bib_str);
305 for (vector<string>::const_iterator it = bib_str.begin();
306 it != bib_str.end(); ++it) {
307 string bibItem(changeExtension(*it, ""));
308 dialog_->add_->bibLW->addItem(toqstr(bibItem));
311 string bibstyle(controller().getStylefile());
313 dialog_->bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
314 dialog_->bibtocCB->setEnabled(!bibtopic);
316 docstring btprint(controller().params()["btprint"]);
318 if (btprint == "btPrintNotCited")
320 else if (btprint == "btPrintAll")
323 dialog_->btPrintCO->setCurrentIndex(btp);
324 dialog_->btPrintCO->setEnabled(bibtopic);
326 dialog_->styleCB->clear();
331 controller().getBibStyles(str);
332 for (vector<string>::const_iterator it = str.begin();
333 it != str.end(); ++it) {
334 string item(changeExtension(*it, ""));
335 if (item == bibstyle)
336 item_nr = int(it - str.begin());
337 dialog_->styleCB->addItem(toqstr(item));
340 if (item_nr == -1 && !bibstyle.empty()) {
341 dialog_->styleCB->addItem(toqstr(bibstyle));
342 item_nr = dialog_->styleCB->count() - 1;
346 dialog_->styleCB->setCurrentIndex(item_nr);
348 dialog_->styleCB->clearEditText();
352 void QBibtex::apply()
354 docstring dbs(qstring_to_ucs4(dialog_->databaseLW->item(0)->text()));
356 unsigned int maxCount = dialog_->databaseLW->count();
357 for (unsigned int i = 1; i < maxCount; i++) {
359 dbs += qstring_to_ucs4(dialog_->databaseLW->item(i)->text());
362 controller().params()["bibfiles"] = dbs;
364 docstring const bibstyle(qstring_to_ucs4(dialog_->styleCB->currentText()));
365 bool const bibtotoc(dialog_->bibtocCB->isChecked());
367 if (bibtotoc && (!bibstyle.empty())) {
368 // both bibtotoc and style
369 controller().params()["options"] = "bibtotoc," + bibstyle;
370 } else if (bibtotoc) {
371 // bibtotoc and no style
372 controller().params()["options"] = lyx::from_ascii("bibtotoc");
374 // only style. An empty one is valid, because some
375 // documentclasses have an own \bibliographystyle{}
377 controller().params()["options"] = bibstyle;
380 // bibtopic allows three kinds of sections:
381 // 1. sections that include all cited references of the database(s)
382 // 2. sections that include all uncited references of the database(s)
383 // 3. sections that include all references of the database(s), cited or not
384 int btp = dialog_->btPrintCO->currentIndex();
388 controller().params()["btprint"] = lyx::from_ascii("btPrintCited");
391 controller().params()["btprint"] = lyx::from_ascii("btPrintNotCited");
394 controller().params()["btprint"] = lyx::from_ascii("btPrintAll");
398 if (!controller().usingBibtopic())
399 controller().params()["btprint"] = docstring();
403 bool QBibtex::isValid()
405 return dialog_->databaseLW->count() != 0;
408 } // namespace frontend
411 #include "QBibtex_moc.cpp"