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