]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBibtex.cpp
simplify ButtonPolicy machinery
[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 "support/filetools.h" // changeExtension
28 #include "support/lstrings.h"
29
30 #include <QPushButton>
31 #include <QListWidget>
32 #include <QCheckBox>
33 #include <QCloseEvent>
34 #include <QLineEdit>
35
36 #include "debug.h"
37 #include "support/filetools.h"
38 #include "support/lstrings.h"
39
40 using std::vector;
41 using std::string;
42
43
44 namespace lyx {
45 namespace frontend {
46
47 using support::changeExtension;
48 using support::split;
49 using support::trim;
50
51
52 /////////////////////////////////////////////////////////////////////
53 //
54 // GuiBibtexDialog
55 //
56 /////////////////////////////////////////////////////////////////////
57
58 GuiBibtexDialog::GuiBibtexDialog(GuiBibtex * form)
59         : form_(form)
60 {
61         setupUi(this);
62         QDialog::setModal(true);
63
64         connect(okPB, SIGNAL(clicked()),
65                 form, SLOT(slotOK()));
66         connect(closePB, SIGNAL(clicked()),
67                 form, SLOT(slotClose()));
68         connect(stylePB, SIGNAL(clicked()),
69                 this, SLOT(browsePressed()));
70         connect(deletePB, SIGNAL(clicked()),
71                 this, SLOT(deletePressed()));
72         connect(styleCB, SIGNAL(editTextChanged (const QString &)),
73                 this, SLOT(change_adaptor()));
74         connect(databaseLW, SIGNAL(itemSelectionChanged()),
75                 this, SLOT(databaseChanged()));
76         connect(bibtocCB, SIGNAL(clicked()),
77                 this, SLOT(change_adaptor()));
78         connect(btPrintCO, SIGNAL(activated(int)),
79                 this, SLOT(change_adaptor()));
80         connect(addBibPB, SIGNAL(clicked()),
81                 this, SLOT(addPressed()));
82
83         add_ = new UiDialog<Ui::BibtexAddUi>(this, true);
84
85         Qt2BC * bcview = new Qt2BC(add_bc_);
86         add_bc_.view(bcview);
87         add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
88
89         bcview->setOK(add_->addPB);
90         bcview->setCancel(add_->closePB);
91
92         addCheckedLineEdit(add_bc_.view(), add_->bibED, 0);
93
94         connect(add_->bibED, SIGNAL(textChanged(const QString &)),
95                 this, SLOT(bibEDChanged()));
96         connect(add_->addPB, SIGNAL(clicked()),
97                 this, SLOT(addDatabase()));
98         connect(add_->addPB, SIGNAL(clicked()),
99                 add_, SLOT(accept()));
100         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
101                 this, SLOT(addDatabase()));
102         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
103                 add_, SLOT(accept()));
104         connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
105                 this, SLOT(availableChanged()));
106         connect(add_->browsePB, SIGNAL(clicked()),
107                 this, SLOT(browseBibPressed()));
108         connect(add_->closePB, SIGNAL(clicked()),
109                 add_, SLOT(reject()));
110
111 }
112
113
114 void GuiBibtexDialog::bibEDChanged()
115 {
116         // Indicate to the button controller that the contents have
117         // changed. The actual test of validity is carried out by
118         // the checkedLineEdit.
119         add_bc_.valid(true);
120 }
121
122
123 void GuiBibtexDialog::change_adaptor()
124 {
125         form_->changed();
126 }
127
128
129 void GuiBibtexDialog::browsePressed()
130 {
131         docstring const file = form_->controller().browseBst(docstring());
132
133         if (!file.empty()) {
134                 // FIXME UNICODE
135                 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
136                 bool present = false;
137                 unsigned int pres = 0;
138
139                 for (int i = 0; i != styleCB->count(); ++i) {
140                         if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
141                                 present = true;
142                                 pres = i;
143                         }
144                 }
145
146                 if (!present)
147                         styleCB->insertItem(0, toqstr(filen));
148
149                 styleCB->setCurrentIndex(pres);
150                 form_->changed();
151         }
152 }
153
154
155 void GuiBibtexDialog::browseBibPressed()
156 {
157         docstring const file = trim(form_->controller().browseBib(docstring()));
158
159         if (!file.empty()) {
160                 // FIXME UNICODE
161                 QString const f = toqstr(changeExtension(to_utf8(file), ""));
162                 bool present = false;
163
164                 for (int i = 0; i < add_->bibLW->count(); ++i) {
165                         if (add_->bibLW->item(i)->text() == f)
166                                 present = true;
167                 }
168
169                 if (!present) {
170                         add_->bibLW->addItem(f);
171                         form_->changed();
172                 }
173
174                 add_->bibED->setText(f);
175         }
176 }
177
178
179 void GuiBibtexDialog::addPressed()
180 {
181         add_bc_.valid(false);
182         add_->exec();
183 }
184
185
186 void GuiBibtexDialog::addDatabase()
187 {
188         int const sel = add_->bibLW->currentRow();
189         docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
190
191         if (sel < 0 && file.empty())
192                 return;
193
194         // Add the selected browser_bib keys to browser_database
195         // multiple selections are possible
196         for (int i = 0; i != add_->bibLW->count(); ++i) {
197                 QListWidgetItem * const item = add_->bibLW->item(i);
198                 if (add_->bibLW->isItemSelected(item)) {
199                         add_->bibLW->setItemSelected(item, false);
200                         QList<QListWidgetItem *> matches =
201                                 databaseLW->findItems(item->text(), Qt::MatchExactly);
202                         if (matches.empty())
203                                 databaseLW->addItem(item->text());
204                 }
205         }
206
207         if (!file.empty()) {
208                 add_->bibED->clear();
209                 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
210                 QList<QListWidgetItem *> matches =
211                         databaseLW->findItems(f, Qt::MatchExactly);
212                 if (matches.empty())
213                         databaseLW->addItem(f);
214         }
215
216         form_->changed();
217 }
218
219
220 void GuiBibtexDialog::deletePressed()
221 {
222         databaseLW->takeItem(databaseLW->currentRow());
223         form_->changed();
224 }
225
226
227
228 void GuiBibtexDialog::databaseChanged()
229 {
230         deletePB->setEnabled(!form_->readOnly() && databaseLW->currentRow() != -1);
231 }
232
233
234 void GuiBibtexDialog::availableChanged()
235 {
236         add_bc_.valid(true);
237 }
238
239
240 void GuiBibtexDialog::closeEvent(QCloseEvent *e)
241 {
242         form_->slotWMHide();
243         e->accept();
244 }
245
246
247 /////////////////////////////////////////////////////////////////////
248 //
249 // QBibTex
250 //
251 /////////////////////////////////////////////////////////////////////
252
253
254 GuiBibtex::GuiBibtex(Dialog & parent)
255         : GuiView<GuiBibtexDialog>(parent, _("BibTeX Bibliography"))
256 {
257 }
258
259
260 void GuiBibtex::build_dialog()
261 {
262         dialog_.reset(new GuiBibtexDialog(this));
263
264         bcview().setOK(dialog_->okPB);
265         bcview().setCancel(dialog_->closePB);
266         bcview().addReadOnly(dialog_->databaseLW);
267         bcview().addReadOnly(dialog_->stylePB);
268         bcview().addReadOnly(dialog_->styleCB);
269         bcview().addReadOnly(dialog_->bibtocCB);
270         bcview().addReadOnly(dialog_->addBibPB);
271         bcview().addReadOnly(dialog_->deletePB);
272 }
273
274
275 void GuiBibtex::update_contents()
276 {
277         bool bibtopic = controller().usingBibtopic();
278
279         dialog_->databaseLW->clear();
280
281         docstring bibs(controller().params()["bibfiles"]);
282         docstring bib;
283
284         while (!bibs.empty()) {
285                 bibs = split(bibs, bib, ',');
286                 bib = trim(bib);
287                 if (!bib.empty())
288                         dialog_->databaseLW->addItem(toqstr(bib));
289         }
290
291         dialog_->add_->bibLW->clear();
292
293         vector<string> bib_str;
294         controller().getBibFiles(bib_str);
295         for (vector<string>::const_iterator it = bib_str.begin();
296                 it != bib_str.end(); ++it) {
297                 string bibItem(changeExtension(*it, ""));
298                 dialog_->add_->bibLW->addItem(toqstr(bibItem));
299         }
300
301         string bibstyle(controller().getStylefile());
302
303         dialog_->bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
304         dialog_->bibtocCB->setEnabled(!bibtopic);
305
306         docstring btprint(controller().params()["btprint"]);
307         int btp = 0;
308         if (btprint == "btPrintNotCited")
309                 btp = 1;
310         else if (btprint == "btPrintAll")
311                 btp = 2;
312
313         dialog_->btPrintCO->setCurrentIndex(btp);
314         dialog_->btPrintCO->setEnabled(bibtopic);
315
316         dialog_->styleCB->clear();
317
318         int item_nr(-1);
319
320         vector<string> str;
321         controller().getBibStyles(str);
322         for (vector<string>::const_iterator it = str.begin();
323                 it != str.end(); ++it) {
324                 string item(changeExtension(*it, ""));
325                 if (item == bibstyle)
326                         item_nr = int(it - str.begin());
327                 dialog_->styleCB->addItem(toqstr(item));
328         }
329
330         if (item_nr == -1 && !bibstyle.empty()) {
331                 dialog_->styleCB->addItem(toqstr(bibstyle));
332                 item_nr = dialog_->styleCB->count() - 1;
333         }
334
335         if (item_nr != -1)
336                 dialog_->styleCB->setCurrentIndex(item_nr);
337         else
338                 dialog_->styleCB->clearEditText();
339 }
340
341
342 void GuiBibtex::apply()
343 {
344         docstring dbs(qstring_to_ucs4(dialog_->databaseLW->item(0)->text()));
345
346         unsigned int maxCount = dialog_->databaseLW->count();
347         for (unsigned int i = 1; i < maxCount; i++) {
348                 dbs += ',';
349                 dbs += qstring_to_ucs4(dialog_->databaseLW->item(i)->text());
350         }
351
352         controller().params()["bibfiles"] = dbs;
353
354         docstring const bibstyle(qstring_to_ucs4(dialog_->styleCB->currentText()));
355         bool const bibtotoc(dialog_->bibtocCB->isChecked());
356
357         if (bibtotoc && (!bibstyle.empty())) {
358                 // both bibtotoc and style
359                 controller().params()["options"] = "bibtotoc," + bibstyle;
360         } else if (bibtotoc) {
361                 // bibtotoc and no style
362                 controller().params()["options"] = from_ascii("bibtotoc");
363         } else {
364                 // only style. An empty one is valid, because some
365                 // documentclasses have an own \bibliographystyle{}
366                 // command!
367                 controller().params()["options"] = bibstyle;
368         }
369
370         // bibtopic allows three kinds of sections:
371         // 1. sections that include all cited references of the database(s)
372         // 2. sections that include all uncited references of the database(s)
373         // 3. sections that include all references of the database(s), cited or not
374         int btp = dialog_->btPrintCO->currentIndex();
375
376         switch (btp) {
377         case 0:
378                 controller().params()["btprint"] = from_ascii("btPrintCited");
379                 break;
380         case 1:
381                 controller().params()["btprint"] = from_ascii("btPrintNotCited");
382                 break;
383         case 2:
384                 controller().params()["btprint"] = from_ascii("btPrintAll");
385                 break;
386         }
387
388         if (!controller().usingBibtopic())
389                 controller().params()["btprint"] = docstring();
390 }
391
392
393 bool GuiBibtex::isValid()
394 {
395         return dialog_->databaseLW->count() != 0;
396 }
397
398 } // namespace frontend
399 } // namespace lyx
400
401 #include "GuiBibtex_moc.cpp"