]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QBibtex.C
Some string(widget->text()) fixes. Weirdness
[lyx.git] / src / frontends / qt2 / QBibtex.C
1 /**
2  * \file QBibtex.C
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 Voss
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "support/lstrings.h"
19
20 #include "ControlBibtex.h"
21 #include "gettext.h"
22 #include "debug.h"
23
24 #include "support/filetools.h" // ChangeExtension
25 #include "support/lstrings.h" // getVectorFromString
26
27 #include <qlineedit.h>
28 #include <qcombobox.h>
29 #include <qpushbutton.h>
30 #include <qlistbox.h>
31 #include <qcheckbox.h>
32
33 #include "QBibtexDialog.h"
34 #include "QBibtex.h"
35 #include "Qt2BC.h"
36
37 using std::vector;
38
39 typedef Qt2CB<ControlBibtex, Qt2DB<QBibtexDialog> > base_class;
40
41
42 QBibtex::QBibtex()
43         : base_class(_("BibTeX"))
44 {
45 }
46
47
48 void QBibtex::build_dialog()
49 {
50         dialog_.reset(new QBibtexDialog(this));
51
52         bc().setOK(dialog_->okPB);
53         bc().setCancel(dialog_->closePB);
54         bc().addReadOnly(dialog_->databaseLB);
55         bc().addReadOnly(dialog_->databasePB);
56         bc().addReadOnly(dialog_->stylePB);
57         bc().addReadOnly(dialog_->styleCB);
58         bc().addReadOnly(dialog_->bibtocCB);
59         bc().addReadOnly(dialog_->databasePB);
60         bc().addReadOnly(dialog_->deletePB);
61 }
62
63
64 void QBibtex::update_contents()
65 {
66         dialog_->databaseLB->clear();
67
68         string bibs(controller().params().getContents());
69         string bib;
70
71         while (!bibs.empty()) {
72                 bibs = split(bibs, bib, ',');
73                 bib = trim(bib);
74                 if (!bib.empty())
75                         dialog_->databaseLB->insertItem(bib.c_str());
76         }
77
78         string bibtotoc = "bibtotoc";
79         string bibstyle(controller().params().getOptions().c_str());
80
81         // bibtotoc exists?
82         if (prefixIs(bibstyle, bibtotoc)) {
83                 dialog_->bibtocCB->setChecked(true);
84
85                 // bibstyle exists?
86                 if (contains(bibstyle,','))
87                         bibstyle = split(bibstyle, bibtotoc, ',');
88                 else
89                         bibstyle.erase();
90         } else
91                 dialog_->bibtocCB->setChecked(false);
92
93
94         dialog_->styleCB->clear();
95  
96         int item_nr(-1);
97  
98         vector<string> const str = getVectorFromString(
99                 controller().getBibStyles(), "\n");
100         for (vector<string>::const_iterator it = str.begin();
101                 it != str.end(); ++it) {
102                 string item(ChangeExtension(*it, "")); 
103                 if (item == bibstyle)
104                         item_nr = int(it - str.begin());
105                 dialog_->styleCB->insertItem(item.c_str());
106         }
107
108         if (item_nr == -1) {
109                 dialog_->styleCB->insertItem(bibstyle.c_str());
110                 item_nr = dialog_->styleCB->count() - 1;
111         }
112
113         dialog_->styleCB->setCurrentItem(item_nr);
114 }
115
116
117 void QBibtex::apply()
118 {
119         string dbs(dialog_->databaseLB->text(0).latin1());
120
121         unsigned int maxCount = dialog_->databaseLB->count();
122         for (unsigned int i = 1; i < maxCount; i++) {
123                 dbs += ',';
124                 dbs += dialog_->databaseLB->text(i).latin1();
125         }
126
127         controller().params().setContents(dbs);
128
129         string const bibstyle(dialog_->styleCB->currentText().latin1());
130         bool const bibtotoc(dialog_->bibtocCB->isChecked());
131
132         if (bibtotoc && (!bibstyle.empty())) {
133                 // both bibtotoc and style
134                 controller().params().setOptions("bibtotoc," + bibstyle);
135         } else if (bibtotoc) {
136                 // bibtotoc and no style
137                 controller().params().setOptions("bibtotoc");
138         } else {
139                 // only style. An empty one is valid, because some
140                 // documentclasses have an own \bibliographystyle{}
141                 // command!
142                 controller().params().setOptions(bibstyle);
143         }
144 }
145
146
147 bool QBibtex::isValid()
148 {
149         return dialog_->databaseLB->count() != 0;
150 }