]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QBibtexDialog.C
clean up the function renaming cleanup
[lyx.git] / src / frontends / qt4 / QBibtexDialog.C
1 /**
2  * \file QBibtexDialog.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "QBibtexDialog.h"
14
15 #include <QCloseEvent>
16
17 #include "checkedwidgets.h"
18 #include "Qt2BC.h"
19 #include "qt_helpers.h"
20 #include "validators.h"
21
22 #include "controllers/ControlBibtex.h"
23 #include "controllers/ButtonPolicies.h"
24
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27
28 #include <qpushbutton.h>
29 #include <qlineedit.h>
30 #include <q3listbox.h>
31
32 using lyx::support::changeExtension;
33 using lyx::support::trim;
34
35 using std::string;
36
37 namespace lyx {
38 namespace frontend {
39
40
41 QBibtexDialog::QBibtexDialog(QBibtex * form)
42         : form_(form)
43 {
44         setupUi(this);
45
46         connect(okPB, SIGNAL(clicked()),
47                 form, SLOT(slotOK()));
48         connect(closePB, SIGNAL(clicked()),
49                 form, SLOT(slotClose()));
50
51     connect( stylePB, SIGNAL( clicked() ), this, SLOT( browsePressed() ) );
52     connect( deletePB, SIGNAL( clicked() ), this, SLOT( deletePressed() ) );
53     connect( styleCB, SIGNAL( textChanged(const QString&) ), this, SLOT( change_adaptor() ) );
54     connect( databaseLB, SIGNAL( selectionChanged() ), this, SLOT( databaseChanged() ) );
55     connect( bibtocCB, SIGNAL( toggled(bool) ), this, SLOT( change_adaptor() ) );
56     connect( btPrintCO, SIGNAL( activated(int) ), this, SLOT( change_adaptor() ) );
57     connect( addBibPB, SIGNAL( clicked() ), this, SLOT( addPressed() ) );
58
59         add_ = new UiDialog<Ui::QBibtexAddUi>(this, true);
60
61         Qt2BC * bcview = new Qt2BC(add_bc_);
62         add_bc_.view(bcview);
63         add_bc_.bp(new OkCancelPolicy);
64
65         bcview->setOK(add_->addPB);
66         bcview->setCancel(add_->closePB);
67
68         add_->bibED->setValidator(new PathValidator(false, add_->bibED));
69         addCheckedLineEdit(add_bc_.view(), add_->bibED, 0);
70
71         connect(add_->bibED, SIGNAL(textChanged(const QString&)),
72                 this, SLOT(bibEDChanged()));
73         connect(add_->addPB, SIGNAL(clicked()),
74                 this, SLOT(addDatabase()));
75         connect(add_->bibLB, SIGNAL(selected(Q3ListBoxItem *)),
76                 this, SLOT(addDatabase()));
77         connect(add_->bibLB, SIGNAL(selected(Q3ListBoxItem *)),
78                 add_, SLOT(accept()));
79         connect(add_->bibLB, SIGNAL(currentChanged(Q3ListBoxItem *)),
80                 this, SLOT(availableChanged()));
81         connect(add_->browsePB, SIGNAL(clicked()),
82                 this, SLOT(browseBibPressed()));
83
84     connect( add_->addPB, SIGNAL( clicked() ), this, SLOT( accept() ) );
85     connect( add_->closePB, SIGNAL( clicked() ), this, SLOT( reject() ) );
86
87 }
88
89
90 QBibtexDialog::~QBibtexDialog()
91 {}
92
93
94 void QBibtexDialog::bibEDChanged()
95 {
96         // Indicate to the button controller that the contents have
97         // changed. The actual test of validity is carried out by
98         // the checkedLineEdit.
99         add_bc_.valid(true);
100 }
101
102
103 void QBibtexDialog::change_adaptor()
104 {
105         form_->changed();
106 }
107
108
109 void QBibtexDialog::browsePressed()
110 {
111         string const file = form_->controller().browseBst("");
112
113         if (!file.empty()) {
114                 string const filen = changeExtension(file, "");
115                 bool present = false;
116                 unsigned int pres = 0;
117
118                 for (int i = 0; i != styleCB->count(); ++i) {
119                         if (fromqstr(styleCB->text(i)) == filen) {
120                                 present = true;
121                                 pres = i;
122                         }
123                 }
124
125                 if (!present)
126                         styleCB->insertItem(toqstr(filen),0);
127
128                 styleCB->setCurrentItem(pres);
129                 form_->changed();
130         }
131 }
132
133
134 void QBibtexDialog::browseBibPressed()
135 {
136         string const file = trim(form_->controller().browseBib(""));
137
138         if (!file.empty()) {
139                 string const f = changeExtension(file, "");
140                 bool present = false;
141
142                 for (unsigned int i = 0; i != add_->bibLB->count(); i++) {
143                         if (fromqstr(add_->bibLB->text(i)) == f)
144                                 present = true;
145                 }
146
147                 if (!present) {
148                         add_->bibLB->insertItem(toqstr(f));
149                         form_->changed();
150                 }
151
152                 add_->bibED->setText(toqstr(f));
153         }
154 }
155
156
157 void QBibtexDialog::addPressed()
158 {
159         add_->exec();
160         add_bc_.valid(false);
161 }
162
163
164 void QBibtexDialog::addDatabase()
165 {
166         int const sel = add_->bibLB->currentItem();
167         string const file = trim(fromqstr(add_->bibED->text()));
168
169         if (sel < 0 && file.empty())
170                 return;
171
172         // Add the selected browser_bib keys to browser_database
173         // multiple selections are possible
174         for (unsigned int i = 0; i != add_->bibLB->count(); i++) {
175                 if (add_->bibLB->isSelected(i)) {
176                         // do not allow duplicates
177                         if ((databaseLB->findItem(add_->bibLB->text(i))) == 0)
178                                 databaseLB->insertItem(add_->bibLB->text(i));
179                 }
180         }
181
182         if (!file.empty()) {
183                 QString const f = toqstr(changeExtension(file, ""));
184                 if ((databaseLB->findItem(f)) == 0)
185                         databaseLB->insertItem(f);
186         }
187
188         form_->changed();
189 }
190
191
192 void QBibtexDialog::deletePressed()
193 {
194         databaseLB->removeItem(databaseLB->currentItem());
195 }
196
197
198
199 void QBibtexDialog::databaseChanged()
200 {
201         deletePB->setEnabled(!form_->readOnly() && databaseLB->currentItem() != -1);
202 }
203
204
205 void QBibtexDialog::availableChanged()
206 {
207         form_->changed();
208 }
209
210
211 void QBibtexDialog::closeEvent(QCloseEvent *e)
212 {
213         form_->slotWMHide();
214         e->accept();
215 }
216
217 } // namespace frontend
218 } // namespace lyx