]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QBibtex.cpp
* src/frontends/qt4/ui/TextLayoutUi.ui:
[lyx.git] / src / frontends / qt4 / QBibtex.cpp
1 /**
2  * \file QBibtex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Herbert Voß
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "QBibtex.h"
16
17 #include "ui/BibtexAddUi.h"
18 #include "Qt2BC.h"
19 #include "qt_helpers.h"
20 #include "Validator.h"
21 #include "LyXRC.h"
22 #include "CheckedLineEdit.h"
23
24 #include "controllers/ControlBibtex.h"
25 #include "controllers/ButtonPolicy.h"
26
27 #include "controllers/ControlBibtex.h"
28
29 #include "support/filetools.h" // changeExtension
30 #include "support/lstrings.h"
31
32 #include <QPushButton>
33 #include <QListWidget>
34 #include <QCheckBox>
35 #include <QCloseEvent>
36 #include <QLineEdit>
37
38 using lyx::support::changeExtension;
39 using lyx::support::split;
40 using lyx::support::trim;
41
42 using std::vector;
43 using std::string;
44
45 #include "debug.h"
46 #include "support/filetools.h"
47 #include "support/lstrings.h"
48
49
50 namespace lyx {
51 namespace frontend {
52
53
54 /////////////////////////////////////////////////////////////////////
55 //
56 // QBibtexDialog
57 //
58 /////////////////////////////////////////////////////////////////////
59
60 QBibtexDialog::QBibtexDialog(QBibtex * form)
61         : form_(form)
62 {
63         setupUi(this);
64         QDialog::setModal(true);
65
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()));
84
85         add_ = new UiDialog<Ui::QBibtexAddUi>(this, true);
86
87         Qt2BC * bcview = new Qt2BC(add_bc_);
88         add_bc_.view(bcview);
89         add_bc_.bp(new OkCancelPolicy);
90
91         bcview->setOK(add_->addPB);
92         bcview->setCancel(add_->closePB);
93
94         add_->bibED->setValidator(new PathValidator(true, add_->bibED));
95         addCheckedLineEdit(add_bc_.view(), add_->bibED, 0);
96
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()));
113
114 }
115
116
117 void QBibtexDialog::bibEDChanged()
118 {
119         // Indicate to the button controller that the contents have
120         // changed. The actual test of validity is carried out by
121         // the checkedLineEdit.
122         add_bc_.valid(true);
123 }
124
125
126 void QBibtexDialog::change_adaptor()
127 {
128         form_->changed();
129 }
130
131
132 void QBibtexDialog::browsePressed()
133 {
134         docstring const file = form_->controller().browseBst(docstring());
135
136         if (!file.empty()) {
137                 // FIXME UNICODE
138                 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
139                 bool present = false;
140                 unsigned int pres = 0;
141
142                 for (int i = 0; i != styleCB->count(); ++i) {
143                         if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
144                                 present = true;
145                                 pres = i;
146                         }
147                 }
148
149                 if (!present)
150                         styleCB->insertItem(0, toqstr(filen));
151
152                 styleCB->setCurrentIndex(pres);
153                 form_->changed();
154         }
155 }
156
157
158 void QBibtexDialog::browseBibPressed()
159 {
160         docstring const file = trim(form_->controller().browseBib(docstring()));
161
162         if (!file.empty()) {
163                 // FIXME UNICODE
164                 QString const f = toqstr(changeExtension(to_utf8(file), ""));
165                 bool present = false;
166
167                 for (int i = 0; i < add_->bibLW->count(); ++i) {
168                         if (add_->bibLW->item(i)->text() == f)
169                                 present = true;
170                 }
171
172                 if (!present) {
173                         add_->bibLW->addItem(f);
174                         form_->changed();
175                 }
176
177                 add_->bibED->setText(f);
178         }
179 }
180
181
182 void QBibtexDialog::addPressed()
183 {
184         add_bc_.valid(false);
185         add_->exec();
186 }
187
188
189 void QBibtexDialog::addDatabase()
190 {
191         int const sel = add_->bibLW->currentRow();
192         docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
193
194         if (sel < 0 && file.empty())
195                 return;
196
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);
205                         if (matches.empty())
206                                 databaseLW->addItem(item->text());
207                 }
208         }
209
210         if (!file.empty()) {
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);
215                 if (matches.empty())
216                         databaseLW->addItem(f);
217         }
218
219         form_->changed();
220 }
221
222
223 void QBibtexDialog::deletePressed()
224 {
225         databaseLW->takeItem(databaseLW->currentRow());
226         form_->changed();
227 }
228
229
230
231 void QBibtexDialog::databaseChanged()
232 {
233         deletePB->setEnabled(!form_->readOnly() && databaseLW->currentRow() != -1);
234 }
235
236
237 void QBibtexDialog::availableChanged()
238 {
239         add_bc_.valid(true);
240 }
241
242
243 void QBibtexDialog::closeEvent(QCloseEvent *e)
244 {
245         form_->slotWMHide();
246         e->accept();
247 }
248
249
250 /////////////////////////////////////////////////////////////////////
251 //
252 // QBibTex
253 //
254 /////////////////////////////////////////////////////////////////////
255
256
257 typedef QController<ControlBibtex, QView<QBibtexDialog> > BibtexBase;
258
259 QBibtex::QBibtex(Dialog & parent)
260         : BibtexBase(parent, _("BibTeX Bibliography"))
261 {
262 }
263
264
265 void QBibtex::build_dialog()
266 {
267         dialog_.reset(new QBibtexDialog(this));
268
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);
277 }
278
279
280 void QBibtex::update_contents()
281 {
282         PathValidator * path_validator =
283                 getPathValidator(dialog_->add_->bibED);
284         if (path_validator)
285                 path_validator->setChecker(kernel().docType(), lyxrc);
286
287         bool bibtopic = controller().usingBibtopic();
288
289         dialog_->databaseLW->clear();
290
291         docstring bibs(controller().params()["bibfiles"]);
292         docstring bib;
293
294         while (!bibs.empty()) {
295                 bibs = split(bibs, bib, ',');
296                 bib = trim(bib);
297                 if (!bib.empty())
298                         dialog_->databaseLW->addItem(toqstr(bib));
299         }
300
301         dialog_->add_->bibLW->clear();
302
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));
309         }
310
311         string bibstyle(controller().getStylefile());
312
313         dialog_->bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
314         dialog_->bibtocCB->setEnabled(!bibtopic);
315
316         docstring btprint(controller().params()["btprint"]);
317         int btp = 0;
318         if (btprint == "btPrintNotCited")
319                 btp = 1;
320         else if (btprint == "btPrintAll")
321                 btp = 2;
322
323         dialog_->btPrintCO->setCurrentIndex(btp);
324         dialog_->btPrintCO->setEnabled(bibtopic);
325
326         dialog_->styleCB->clear();
327
328         int item_nr(-1);
329
330         vector<string> str;
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));
338         }
339
340         if (item_nr == -1 && !bibstyle.empty()) {
341                 dialog_->styleCB->addItem(toqstr(bibstyle));
342                 item_nr = dialog_->styleCB->count() - 1;
343         }
344
345         if (item_nr != -1)
346                 dialog_->styleCB->setCurrentIndex(item_nr);
347         else
348                 dialog_->styleCB->clearEditText();
349 }
350
351
352 void QBibtex::apply()
353 {
354         docstring dbs(qstring_to_ucs4(dialog_->databaseLW->item(0)->text()));
355
356         unsigned int maxCount = dialog_->databaseLW->count();
357         for (unsigned int i = 1; i < maxCount; i++) {
358                 dbs += ',';
359                 dbs += qstring_to_ucs4(dialog_->databaseLW->item(i)->text());
360         }
361
362         controller().params()["bibfiles"] = dbs;
363
364         docstring const bibstyle(qstring_to_ucs4(dialog_->styleCB->currentText()));
365         bool const bibtotoc(dialog_->bibtocCB->isChecked());
366
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"] = from_ascii("bibtotoc");
373         } else {
374                 // only style. An empty one is valid, because some
375                 // documentclasses have an own \bibliographystyle{}
376                 // command!
377                 controller().params()["options"] = bibstyle;
378         }
379
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();
385
386         switch (btp) {
387         case 0:
388                 controller().params()["btprint"] = from_ascii("btPrintCited");
389                 break;
390         case 1:
391                 controller().params()["btprint"] = from_ascii("btPrintNotCited");
392                 break;
393         case 2:
394                 controller().params()["btprint"] = from_ascii("btPrintAll");
395                 break;
396         }
397
398         if (!controller().usingBibtopic())
399                 controller().params()["btprint"] = docstring();
400 }
401
402
403 bool QBibtex::isValid()
404 {
405         return dialog_->databaseLW->count() != 0;
406 }
407
408 } // namespace frontend
409 } // namespace lyx
410
411 #include "QBibtex_moc.cpp"