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 "Validator.h"
22 #include "CheckedLineEdit.h"
24 #include "controllers/ControlBibtex.h"
25 #include "controllers/ButtonPolicy.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 addCheckedLineEdit(add_bc_.view(), add_->bibED, 0);
96 connect(add_->bibED, SIGNAL(textChanged(const QString &)),
97 this, SLOT(bibEDChanged()));
98 connect(add_->addPB, SIGNAL(clicked()),
99 this, SLOT(addDatabase()));
100 connect(add_->addPB, SIGNAL(clicked()),
101 add_, SLOT(accept()));
102 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
103 this, SLOT(addDatabase()));
104 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
105 add_, SLOT(accept()));
106 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
107 this, SLOT(availableChanged()));
108 connect(add_->browsePB, SIGNAL(clicked()),
109 this, SLOT(browseBibPressed()));
110 connect(add_->closePB, SIGNAL(clicked()),
111 add_, SLOT(reject()));
116 void QBibtexDialog::bibEDChanged()
118 // Indicate to the button controller that the contents have
119 // changed. The actual test of validity is carried out by
120 // the checkedLineEdit.
125 void QBibtexDialog::change_adaptor()
131 void QBibtexDialog::browsePressed()
133 docstring const file = form_->controller().browseBst(docstring());
137 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
138 bool present = false;
139 unsigned int pres = 0;
141 for (int i = 0; i != styleCB->count(); ++i) {
142 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
149 styleCB->insertItem(0, toqstr(filen));
151 styleCB->setCurrentIndex(pres);
157 void QBibtexDialog::browseBibPressed()
159 docstring const file = trim(form_->controller().browseBib(docstring()));
163 QString const f = toqstr(changeExtension(to_utf8(file), ""));
164 bool present = false;
166 for (int i = 0; i < add_->bibLW->count(); ++i) {
167 if (add_->bibLW->item(i)->text() == f)
172 add_->bibLW->addItem(f);
176 add_->bibED->setText(f);
181 void QBibtexDialog::addPressed()
183 add_bc_.valid(false);
188 void QBibtexDialog::addDatabase()
190 int const sel = add_->bibLW->currentRow();
191 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
193 if (sel < 0 && file.empty())
196 // Add the selected browser_bib keys to browser_database
197 // multiple selections are possible
198 for (int i = 0; i != add_->bibLW->count(); ++i) {
199 QListWidgetItem * const item = add_->bibLW->item(i);
200 if (add_->bibLW->isItemSelected(item)) {
201 add_->bibLW->setItemSelected(item, false);
202 QList<QListWidgetItem *> matches =
203 databaseLW->findItems(item->text(), Qt::MatchExactly);
205 databaseLW->addItem(item->text());
210 add_->bibED->clear();
211 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
212 QList<QListWidgetItem *> matches =
213 databaseLW->findItems(f, Qt::MatchExactly);
215 databaseLW->addItem(f);
222 void QBibtexDialog::deletePressed()
224 databaseLW->takeItem(databaseLW->currentRow());
230 void QBibtexDialog::databaseChanged()
232 deletePB->setEnabled(!form_->readOnly() && databaseLW->currentRow() != -1);
236 void QBibtexDialog::availableChanged()
242 void QBibtexDialog::closeEvent(QCloseEvent *e)
249 /////////////////////////////////////////////////////////////////////
253 /////////////////////////////////////////////////////////////////////
256 typedef QController<ControlBibtex, QView<QBibtexDialog> > BibtexBase;
258 QBibtex::QBibtex(Dialog & parent)
259 : BibtexBase(parent, _("BibTeX Bibliography"))
264 void QBibtex::build_dialog()
266 dialog_.reset(new QBibtexDialog(this));
268 bcview().setOK(dialog_->okPB);
269 bcview().setCancel(dialog_->closePB);
270 bcview().addReadOnly(dialog_->databaseLW);
271 bcview().addReadOnly(dialog_->stylePB);
272 bcview().addReadOnly(dialog_->styleCB);
273 bcview().addReadOnly(dialog_->bibtocCB);
274 bcview().addReadOnly(dialog_->addBibPB);
275 bcview().addReadOnly(dialog_->deletePB);
279 void QBibtex::update_contents()
281 bool bibtopic = controller().usingBibtopic();
283 dialog_->databaseLW->clear();
285 docstring bibs(controller().params()["bibfiles"]);
288 while (!bibs.empty()) {
289 bibs = split(bibs, bib, ',');
292 dialog_->databaseLW->addItem(toqstr(bib));
295 dialog_->add_->bibLW->clear();
297 vector<string> bib_str;
298 controller().getBibFiles(bib_str);
299 for (vector<string>::const_iterator it = bib_str.begin();
300 it != bib_str.end(); ++it) {
301 string bibItem(changeExtension(*it, ""));
302 dialog_->add_->bibLW->addItem(toqstr(bibItem));
305 string bibstyle(controller().getStylefile());
307 dialog_->bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
308 dialog_->bibtocCB->setEnabled(!bibtopic);
310 docstring btprint(controller().params()["btprint"]);
312 if (btprint == "btPrintNotCited")
314 else if (btprint == "btPrintAll")
317 dialog_->btPrintCO->setCurrentIndex(btp);
318 dialog_->btPrintCO->setEnabled(bibtopic);
320 dialog_->styleCB->clear();
325 controller().getBibStyles(str);
326 for (vector<string>::const_iterator it = str.begin();
327 it != str.end(); ++it) {
328 string item(changeExtension(*it, ""));
329 if (item == bibstyle)
330 item_nr = int(it - str.begin());
331 dialog_->styleCB->addItem(toqstr(item));
334 if (item_nr == -1 && !bibstyle.empty()) {
335 dialog_->styleCB->addItem(toqstr(bibstyle));
336 item_nr = dialog_->styleCB->count() - 1;
340 dialog_->styleCB->setCurrentIndex(item_nr);
342 dialog_->styleCB->clearEditText();
346 void QBibtex::apply()
348 docstring dbs(qstring_to_ucs4(dialog_->databaseLW->item(0)->text()));
350 unsigned int maxCount = dialog_->databaseLW->count();
351 for (unsigned int i = 1; i < maxCount; i++) {
353 dbs += qstring_to_ucs4(dialog_->databaseLW->item(i)->text());
356 controller().params()["bibfiles"] = dbs;
358 docstring const bibstyle(qstring_to_ucs4(dialog_->styleCB->currentText()));
359 bool const bibtotoc(dialog_->bibtocCB->isChecked());
361 if (bibtotoc && (!bibstyle.empty())) {
362 // both bibtotoc and style
363 controller().params()["options"] = "bibtotoc," + bibstyle;
364 } else if (bibtotoc) {
365 // bibtotoc and no style
366 controller().params()["options"] = from_ascii("bibtotoc");
368 // only style. An empty one is valid, because some
369 // documentclasses have an own \bibliographystyle{}
371 controller().params()["options"] = bibstyle;
374 // bibtopic allows three kinds of sections:
375 // 1. sections that include all cited references of the database(s)
376 // 2. sections that include all uncited references of the database(s)
377 // 3. sections that include all references of the database(s), cited or not
378 int btp = dialog_->btPrintCO->currentIndex();
382 controller().params()["btprint"] = from_ascii("btPrintCited");
385 controller().params()["btprint"] = from_ascii("btPrintNotCited");
388 controller().params()["btprint"] = from_ascii("btPrintAll");
392 if (!controller().usingBibtopic())
393 controller().params()["btprint"] = docstring();
397 bool QBibtex::isValid()
399 return dialog_->databaseLW->count() != 0;
402 } // namespace frontend
405 #include "QBibtex_moc.cpp"