]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBibtex.cpp
move our stuff off the Q* namespace
[lyx.git] / src / frontends / qt4 / GuiBibtex.cpp
1 /**
2  * \file GuiBibtex.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 "GuiBibtex.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 // GuiBibtexDialog
57 //
58 /////////////////////////////////////////////////////////////////////
59
60 GuiBibtexDialog::GuiBibtexDialog(GuiBibtex * 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::BibtexAddUi>(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         addCheckedLineEdit(add_bc_.view(), add_->bibED, 0);
95
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()));
112
113 }
114
115
116 void GuiBibtexDialog::bibEDChanged()
117 {
118         // Indicate to the button controller that the contents have
119         // changed. The actual test of validity is carried out by
120         // the checkedLineEdit.
121         add_bc_.valid(true);
122 }
123
124
125 void GuiBibtexDialog::change_adaptor()
126 {
127         form_->changed();
128 }
129
130
131 void GuiBibtexDialog::browsePressed()
132 {
133         docstring const file = form_->controller().browseBst(docstring());
134
135         if (!file.empty()) {
136                 // FIXME UNICODE
137                 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
138                 bool present = false;
139                 unsigned int pres = 0;
140
141                 for (int i = 0; i != styleCB->count(); ++i) {
142                         if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
143                                 present = true;
144                                 pres = i;
145                         }
146                 }
147
148                 if (!present)
149                         styleCB->insertItem(0, toqstr(filen));
150
151                 styleCB->setCurrentIndex(pres);
152                 form_->changed();
153         }
154 }
155
156
157 void GuiBibtexDialog::browseBibPressed()
158 {
159         docstring const file = trim(form_->controller().browseBib(docstring()));
160
161         if (!file.empty()) {
162                 // FIXME UNICODE
163                 QString const f = toqstr(changeExtension(to_utf8(file), ""));
164                 bool present = false;
165
166                 for (int i = 0; i < add_->bibLW->count(); ++i) {
167                         if (add_->bibLW->item(i)->text() == f)
168                                 present = true;
169                 }
170
171                 if (!present) {
172                         add_->bibLW->addItem(f);
173                         form_->changed();
174                 }
175
176                 add_->bibED->setText(f);
177         }
178 }
179
180
181 void GuiBibtexDialog::addPressed()
182 {
183         add_bc_.valid(false);
184         add_->exec();
185 }
186
187
188 void GuiBibtexDialog::addDatabase()
189 {
190         int const sel = add_->bibLW->currentRow();
191         docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
192
193         if (sel < 0 && file.empty())
194                 return;
195
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);
204                         if (matches.empty())
205                                 databaseLW->addItem(item->text());
206                 }
207         }
208
209         if (!file.empty()) {
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);
214                 if (matches.empty())
215                         databaseLW->addItem(f);
216         }
217
218         form_->changed();
219 }
220
221
222 void GuiBibtexDialog::deletePressed()
223 {
224         databaseLW->takeItem(databaseLW->currentRow());
225         form_->changed();
226 }
227
228
229
230 void GuiBibtexDialog::databaseChanged()
231 {
232         deletePB->setEnabled(!form_->readOnly() && databaseLW->currentRow() != -1);
233 }
234
235
236 void GuiBibtexDialog::availableChanged()
237 {
238         add_bc_.valid(true);
239 }
240
241
242 void GuiBibtexDialog::closeEvent(QCloseEvent *e)
243 {
244         form_->slotWMHide();
245         e->accept();
246 }
247
248
249 /////////////////////////////////////////////////////////////////////
250 //
251 // QBibTex
252 //
253 /////////////////////////////////////////////////////////////////////
254
255
256 typedef QController<ControlBibtex, GuiView<GuiBibtexDialog> > BibtexBase;
257
258 GuiBibtex::GuiBibtex(Dialog & parent)
259         : BibtexBase(parent, _("BibTeX Bibliography"))
260 {
261 }
262
263
264 void GuiBibtex::build_dialog()
265 {
266         dialog_.reset(new GuiBibtexDialog(this));
267
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);
276 }
277
278
279 void GuiBibtex::update_contents()
280 {
281         bool bibtopic = controller().usingBibtopic();
282
283         dialog_->databaseLW->clear();
284
285         docstring bibs(controller().params()["bibfiles"]);
286         docstring bib;
287
288         while (!bibs.empty()) {
289                 bibs = split(bibs, bib, ',');
290                 bib = trim(bib);
291                 if (!bib.empty())
292                         dialog_->databaseLW->addItem(toqstr(bib));
293         }
294
295         dialog_->add_->bibLW->clear();
296
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));
303         }
304
305         string bibstyle(controller().getStylefile());
306
307         dialog_->bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
308         dialog_->bibtocCB->setEnabled(!bibtopic);
309
310         docstring btprint(controller().params()["btprint"]);
311         int btp = 0;
312         if (btprint == "btPrintNotCited")
313                 btp = 1;
314         else if (btprint == "btPrintAll")
315                 btp = 2;
316
317         dialog_->btPrintCO->setCurrentIndex(btp);
318         dialog_->btPrintCO->setEnabled(bibtopic);
319
320         dialog_->styleCB->clear();
321
322         int item_nr(-1);
323
324         vector<string> str;
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));
332         }
333
334         if (item_nr == -1 && !bibstyle.empty()) {
335                 dialog_->styleCB->addItem(toqstr(bibstyle));
336                 item_nr = dialog_->styleCB->count() - 1;
337         }
338
339         if (item_nr != -1)
340                 dialog_->styleCB->setCurrentIndex(item_nr);
341         else
342                 dialog_->styleCB->clearEditText();
343 }
344
345
346 void GuiBibtex::apply()
347 {
348         docstring dbs(qstring_to_ucs4(dialog_->databaseLW->item(0)->text()));
349
350         unsigned int maxCount = dialog_->databaseLW->count();
351         for (unsigned int i = 1; i < maxCount; i++) {
352                 dbs += ',';
353                 dbs += qstring_to_ucs4(dialog_->databaseLW->item(i)->text());
354         }
355
356         controller().params()["bibfiles"] = dbs;
357
358         docstring const bibstyle(qstring_to_ucs4(dialog_->styleCB->currentText()));
359         bool const bibtotoc(dialog_->bibtocCB->isChecked());
360
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");
367         } else {
368                 // only style. An empty one is valid, because some
369                 // documentclasses have an own \bibliographystyle{}
370                 // command!
371                 controller().params()["options"] = bibstyle;
372         }
373
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();
379
380         switch (btp) {
381         case 0:
382                 controller().params()["btprint"] = from_ascii("btPrintCited");
383                 break;
384         case 1:
385                 controller().params()["btprint"] = from_ascii("btPrintNotCited");
386                 break;
387         case 2:
388                 controller().params()["btprint"] = from_ascii("btPrintAll");
389                 break;
390         }
391
392         if (!controller().usingBibtopic())
393                 controller().params()["btprint"] = docstring();
394 }
395
396
397 bool GuiBibtex::isValid()
398 {
399         return dialog_->databaseLW->count() != 0;
400 }
401
402 } // namespace frontend
403 } // namespace lyx
404
405 #include "GuiBibtex_moc.cpp"