]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QBibtexDialog.C
This commit saves the need to check for lyx::use_gui in a number of places.
[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 "debug.h"
26 #include "support/filetools.h"
27 #include "support/lstrings.h"
28
29 #include <QPushButton>
30 #include <QListWidget>
31 #include <QLineEdit>
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
42 QBibtexDialog::QBibtexDialog(QBibtex * form)
43         : form_(form)
44 {
45         setupUi(this);
46         QDialog::setModal(true);
47
48         connect(okPB, SIGNAL(clicked()),
49                 form, SLOT(slotOK()));
50         connect(closePB, SIGNAL(clicked()),
51                 form, SLOT(slotClose()));
52         connect(stylePB, SIGNAL( clicked() ),
53                 this, SLOT( browsePressed() ) );
54         connect(deletePB, SIGNAL( clicked() ),
55                 this, SLOT( deletePressed() ) );
56         connect(styleCB, SIGNAL( textChanged(const QString&) ),
57                 this, SLOT( change_adaptor() ) );
58         connect(databaseLW, SIGNAL( selectionChanged() ),
59                 this, SLOT( databaseChanged() ) );
60         connect(bibtocCB, SIGNAL( toggled(bool) ),
61                 this, SLOT( change_adaptor() ) );
62         connect(btPrintCO, SIGNAL( activated(int) ),
63                 this, SLOT( change_adaptor() ) );
64         connect(addBibPB, SIGNAL( clicked() ),
65                 this, SLOT( addPressed() ) );
66
67         add_ = new UiDialog<Ui::QBibtexAddUi>(this, true);
68
69         Qt2BC * bcview = new Qt2BC(add_bc_);
70         add_bc_.view(bcview);
71         add_bc_.bp(new OkCancelPolicy);
72
73         bcview->setOK(add_->addPB);
74         bcview->setCancel(add_->closePB);
75
76         add_->bibED->setValidator(new PathValidator(false, add_->bibED));
77         addCheckedLineEdit(add_bc_.view(), add_->bibED, 0);
78
79         connect(add_->bibED, SIGNAL(textChanged(const QString&)),
80                 this, SLOT(bibEDChanged()));
81         connect(add_->addPB, SIGNAL(clicked()),
82                 this, SLOT(addDatabase()));
83         connect(add_->addPB, SIGNAL(clicked()),
84                 add_, SLOT(accept()) );
85         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
86                 this, SLOT(addDatabase()));
87         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
88                 add_, SLOT(accept()));
89         connect(add_->bibLW, SIGNAL(itemChanged(QListWidgetItem *)),
90                 this, SLOT(availableChanged()));
91         connect(add_->browsePB, SIGNAL(clicked()),
92                 this, SLOT(browseBibPressed()));
93         connect(add_->closePB, SIGNAL( clicked() ),
94                 add_, SLOT( reject() ) );
95
96 }
97
98
99 QBibtexDialog::~QBibtexDialog()
100 {}
101
102
103 void QBibtexDialog::bibEDChanged()
104 {
105         // Indicate to the button controller that the contents have
106         // changed. The actual test of validity is carried out by
107         // the checkedLineEdit.
108         add_bc_.valid(true);
109 }
110
111
112 void QBibtexDialog::change_adaptor()
113 {
114         form_->changed();
115 }
116
117
118 void QBibtexDialog::browsePressed()
119 {
120         docstring const file = form_->controller().browseBst(docstring());
121
122         if (!file.empty()) {
123                 // FIXME UNICODE
124                 docstring const filen = lyx::from_utf8(changeExtension(lyx::to_utf8(file), ""));
125                 bool present = false;
126                 unsigned int pres = 0;
127
128                 for (int i = 0; i != styleCB->count(); ++i) {
129                         if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
130                                 present = true;
131                                 pres = i;
132                         }
133                 }
134
135                 if (!present)
136                         styleCB->insertItem(0, toqstr(filen));
137
138                 styleCB->setCurrentIndex(pres);
139                 form_->changed();
140         }
141 }
142
143
144 void QBibtexDialog::browseBibPressed()
145 {
146         docstring const file = trim(form_->controller().browseBib(docstring()));
147
148         if (!file.empty()) {
149                 // FIXME UNICODE
150                 QString const f = toqstr(lyx::from_utf8(changeExtension(lyx::to_utf8(file), "")));
151                 bool present = false;
152
153                 for (int i = 0; i < add_->bibLW->count(); ++i) {
154                         if (add_->bibLW->item(i)->text() == f)
155                                 present = true;
156                 }
157
158                 if (!present) {
159                         add_->bibLW->addItem(f);
160                         form_->changed();
161                 }
162
163                 add_->bibED->setText(f);
164         }
165 }
166
167
168 void QBibtexDialog::addPressed()
169 {
170         add_->exec();
171         add_bc_.valid(false);
172 }
173
174
175 void QBibtexDialog::addDatabase()
176 {
177         int const sel = add_->bibLW->currentRow();
178         docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
179
180         if (sel < 0 && file.empty())
181                 return;
182
183         // Add the selected browser_bib keys to browser_database
184         // multiple selections are possible
185         for (int i = 0; i != add_->bibLW->count(); ++i) {
186                 QListWidgetItem * const item = add_->bibLW->item(i);
187                 if (add_->bibLW->isItemSelected(item)) {
188                         add_->bibLW->setItemSelected(item, false);
189                         QList<QListWidgetItem *> matches =
190                                 databaseLW->findItems(item->text(), Qt::MatchExactly);
191                         if (matches.empty())
192                                 databaseLW->addItem(item->text());
193                 }
194         }
195
196         if (!file.empty()) {
197                 add_->bibED->clear();
198                 QString const f = toqstr(lyx::from_utf8(changeExtension(lyx::to_utf8(file), "")));
199                 QList<QListWidgetItem *> matches =
200                         databaseLW->findItems(f, Qt::MatchExactly);
201                 if (matches.empty())
202                         databaseLW->addItem(f);
203         }
204
205         form_->changed();
206 }
207
208
209 void QBibtexDialog::deletePressed()
210 {
211         databaseLW->takeItem(databaseLW->currentRow());
212 }
213
214
215
216 void QBibtexDialog::databaseChanged()
217 {
218         deletePB->setEnabled(!form_->readOnly() && databaseLW->currentRow() != -1);
219 }
220
221
222 void QBibtexDialog::availableChanged()
223 {
224         form_->changed();
225 }
226
227
228 void QBibtexDialog::closeEvent(QCloseEvent *e)
229 {
230         form_->slotWMHide();
231         e->accept();
232 }
233
234
235 } // namespace frontend
236 } // namespace lyx
237
238 #include "QBibtexDialog_moc.cpp"