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