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"
23 #include "controllers/ControlBibtex.h"
24 #include "controllers/ButtonPolicy.h"
26 #include "support/filetools.h" // changeExtension
27 #include "support/lstrings.h"
29 #include <QPushButton>
30 #include <QListWidget>
32 #include <QCloseEvent>
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
46 using support::changeExtension;
51 /////////////////////////////////////////////////////////////////////
55 /////////////////////////////////////////////////////////////////////
57 GuiBibtexDialog::GuiBibtexDialog(GuiBibtex * form)
61 QDialog::setModal(true);
63 connect(okPB, SIGNAL(clicked()),
64 form, SLOT(slotOK()));
65 connect(closePB, SIGNAL(clicked()),
66 form, SLOT(slotClose()));
67 connect(stylePB, SIGNAL(clicked()),
68 this, SLOT(browsePressed()));
69 connect(deletePB, SIGNAL(clicked()),
70 this, SLOT(deletePressed()));
71 connect(styleCB, SIGNAL(editTextChanged (const QString &)),
72 this, SLOT(change_adaptor()));
73 connect(databaseLW, SIGNAL(itemSelectionChanged()),
74 this, SLOT(databaseChanged()));
75 connect(bibtocCB, SIGNAL(clicked()),
76 this, SLOT(change_adaptor()));
77 connect(btPrintCO, SIGNAL(activated(int)),
78 this, SLOT(change_adaptor()));
79 connect(addBibPB, SIGNAL(clicked()),
80 this, SLOT(addPressed()));
82 add_ = new UiDialog<Ui::BibtexAddUi>(this, true);
84 Qt2BC * bcview = new Qt2BC(add_bc_);
86 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
88 bcview->setOK(add_->addPB);
89 bcview->setCancel(add_->closePB);
90 bcview->addCheckedLineEdit(add_->bibED, 0);
92 connect(add_->bibED, SIGNAL(textChanged(const QString &)),
93 this, SLOT(bibEDChanged()));
94 connect(add_->addPB, SIGNAL(clicked()),
95 this, SLOT(addDatabase()));
96 connect(add_->addPB, SIGNAL(clicked()),
97 add_, SLOT(accept()));
98 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
99 this, SLOT(addDatabase()));
100 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
101 add_, SLOT(accept()));
102 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
103 this, SLOT(availableChanged()));
104 connect(add_->browsePB, SIGNAL(clicked()),
105 this, SLOT(browseBibPressed()));
106 connect(add_->closePB, SIGNAL(clicked()),
107 add_, SLOT(reject()));
112 void GuiBibtexDialog::bibEDChanged()
114 // Indicate to the button controller that the contents have
115 // changed. The actual test of validity is carried out by
116 // the checkedLineEdit.
121 void GuiBibtexDialog::change_adaptor()
127 void GuiBibtexDialog::browsePressed()
129 docstring const file = form_->controller().browseBst(docstring());
133 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
134 bool present = false;
135 unsigned int pres = 0;
137 for (int i = 0; i != styleCB->count(); ++i) {
138 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
145 styleCB->insertItem(0, toqstr(filen));
147 styleCB->setCurrentIndex(pres);
153 void GuiBibtexDialog::browseBibPressed()
155 docstring const file = trim(form_->controller().browseBib(docstring()));
159 QString const f = toqstr(changeExtension(to_utf8(file), ""));
160 bool present = false;
162 for (int i = 0; i < add_->bibLW->count(); ++i) {
163 if (add_->bibLW->item(i)->text() == f)
168 add_->bibLW->addItem(f);
172 add_->bibED->setText(f);
177 void GuiBibtexDialog::addPressed()
179 add_bc_.valid(false);
184 void GuiBibtexDialog::addDatabase()
186 int const sel = add_->bibLW->currentRow();
187 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
189 if (sel < 0 && file.empty())
192 // Add the selected browser_bib keys to browser_database
193 // multiple selections are possible
194 for (int i = 0; i != add_->bibLW->count(); ++i) {
195 QListWidgetItem * const item = add_->bibLW->item(i);
196 if (add_->bibLW->isItemSelected(item)) {
197 add_->bibLW->setItemSelected(item, false);
198 QList<QListWidgetItem *> matches =
199 databaseLW->findItems(item->text(), Qt::MatchExactly);
201 databaseLW->addItem(item->text());
206 add_->bibED->clear();
207 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
208 QList<QListWidgetItem *> matches =
209 databaseLW->findItems(f, Qt::MatchExactly);
211 databaseLW->addItem(f);
218 void GuiBibtexDialog::deletePressed()
220 databaseLW->takeItem(databaseLW->currentRow());
226 void GuiBibtexDialog::databaseChanged()
228 deletePB->setEnabled(!form_->readOnly() && databaseLW->currentRow() != -1);
232 void GuiBibtexDialog::availableChanged()
238 void GuiBibtexDialog::closeEvent(QCloseEvent *e)
245 /////////////////////////////////////////////////////////////////////
249 /////////////////////////////////////////////////////////////////////
252 GuiBibtex::GuiBibtex(Dialog & parent)
253 : GuiView<GuiBibtexDialog>(parent, _("BibTeX Bibliography"))
258 void GuiBibtex::build_dialog()
260 dialog_.reset(new GuiBibtexDialog(this));
262 bcview().setOK(dialog_->okPB);
263 bcview().setCancel(dialog_->closePB);
264 bcview().addReadOnly(dialog_->databaseLW);
265 bcview().addReadOnly(dialog_->stylePB);
266 bcview().addReadOnly(dialog_->styleCB);
267 bcview().addReadOnly(dialog_->bibtocCB);
268 bcview().addReadOnly(dialog_->addBibPB);
269 bcview().addReadOnly(dialog_->deletePB);
273 void GuiBibtex::update_contents()
275 bool bibtopic = controller().usingBibtopic();
277 dialog_->databaseLW->clear();
279 docstring bibs(controller().params()["bibfiles"]);
282 while (!bibs.empty()) {
283 bibs = split(bibs, bib, ',');
286 dialog_->databaseLW->addItem(toqstr(bib));
289 dialog_->add_->bibLW->clear();
291 vector<string> bib_str;
292 controller().getBibFiles(bib_str);
293 for (vector<string>::const_iterator it = bib_str.begin();
294 it != bib_str.end(); ++it) {
295 string bibItem(changeExtension(*it, ""));
296 dialog_->add_->bibLW->addItem(toqstr(bibItem));
299 string bibstyle(controller().getStylefile());
301 dialog_->bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
302 dialog_->bibtocCB->setEnabled(!bibtopic);
304 docstring btprint(controller().params()["btprint"]);
306 if (btprint == "btPrintNotCited")
308 else if (btprint == "btPrintAll")
311 dialog_->btPrintCO->setCurrentIndex(btp);
312 dialog_->btPrintCO->setEnabled(bibtopic);
314 dialog_->styleCB->clear();
319 controller().getBibStyles(str);
320 for (vector<string>::const_iterator it = str.begin();
321 it != str.end(); ++it) {
322 string item(changeExtension(*it, ""));
323 if (item == bibstyle)
324 item_nr = int(it - str.begin());
325 dialog_->styleCB->addItem(toqstr(item));
328 if (item_nr == -1 && !bibstyle.empty()) {
329 dialog_->styleCB->addItem(toqstr(bibstyle));
330 item_nr = dialog_->styleCB->count() - 1;
334 dialog_->styleCB->setCurrentIndex(item_nr);
336 dialog_->styleCB->clearEditText();
340 void GuiBibtex::apply()
342 docstring dbs(qstring_to_ucs4(dialog_->databaseLW->item(0)->text()));
344 unsigned int maxCount = dialog_->databaseLW->count();
345 for (unsigned int i = 1; i < maxCount; i++) {
347 dbs += qstring_to_ucs4(dialog_->databaseLW->item(i)->text());
350 controller().params()["bibfiles"] = dbs;
352 docstring const bibstyle(qstring_to_ucs4(dialog_->styleCB->currentText()));
353 bool const bibtotoc(dialog_->bibtocCB->isChecked());
355 if (bibtotoc && (!bibstyle.empty())) {
356 // both bibtotoc and style
357 controller().params()["options"] = "bibtotoc," + bibstyle;
358 } else if (bibtotoc) {
359 // bibtotoc and no style
360 controller().params()["options"] = from_ascii("bibtotoc");
362 // only style. An empty one is valid, because some
363 // documentclasses have an own \bibliographystyle{}
365 controller().params()["options"] = bibstyle;
368 // bibtopic allows three kinds of sections:
369 // 1. sections that include all cited references of the database(s)
370 // 2. sections that include all uncited references of the database(s)
371 // 3. sections that include all references of the database(s), cited or not
372 int btp = dialog_->btPrintCO->currentIndex();
376 controller().params()["btprint"] = from_ascii("btPrintCited");
379 controller().params()["btprint"] = from_ascii("btPrintNotCited");
382 controller().params()["btprint"] = from_ascii("btPrintAll");
386 if (!controller().usingBibtopic())
387 controller().params()["btprint"] = docstring();
391 bool GuiBibtex::isValid()
393 return dialog_->databaseLW->count() != 0;
396 } // namespace frontend
399 #include "GuiBibtex_moc.cpp"