]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QBibtexDialog.C
enable Font cache only for MacOSX and inline width() for other platform.
[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         string const file = form_->controller().browseBst("");
121
122         if (!file.empty()) {
123                 string const filen = changeExtension(file, "");
124                 bool present = false;
125                 unsigned int pres = 0;
126
127                 for (int i = 0; i != styleCB->count(); ++i) {
128                         if (fromqstr(styleCB->itemText(i)) == filen) {
129                                 present = true;
130                                 pres = i;
131                         }
132                 }
133
134                 if (!present)
135                         styleCB->insertItem(0, toqstr(filen));
136
137                 styleCB->setCurrentIndex(pres);
138                 form_->changed();
139         }
140 }
141
142
143 void QBibtexDialog::browseBibPressed()
144 {
145         string const file = trim(form_->controller().browseBib(""));
146
147         if (!file.empty()) {
148                 QString const f = toqstr(changeExtension(file, ""));
149                 bool present = false;
150
151                 for (int i = 0; i < add_->bibLW->count(); ++i) {
152                         if (add_->bibLW->item(i)->text() == f)
153                                 present = true;
154                 }
155
156                 if (!present) {
157                         add_->bibLW->addItem(f);
158                         form_->changed();
159                 }
160
161                 add_->bibED->setText(f);
162         }
163 }
164
165
166 void QBibtexDialog::addPressed()
167 {
168         add_->exec();
169         add_bc_.valid(false);
170 }
171
172
173 void QBibtexDialog::addDatabase()
174 {
175         int const sel = add_->bibLW->currentRow();
176         string const file = trim(fromqstr(add_->bibED->text()));
177
178         if (sel < 0 && file.empty())
179                 return;
180
181         // Add the selected browser_bib keys to browser_database
182         // multiple selections are possible
183         for (int i = 0; i != add_->bibLW->count(); ++i) {
184                 QListWidgetItem * const item = add_->bibLW->item(i);
185                 if (add_->bibLW->isItemSelected(item)) {
186                         add_->bibLW->setItemSelected(item, false);
187                         QList<QListWidgetItem *> matches =
188                                 databaseLW->findItems(item->text(), Qt::MatchExactly);
189                         if (matches.empty())
190                                 databaseLW->addItem(item->text());
191                 }
192         }
193
194         if (!file.empty()) {
195                 add_->bibED->clear();
196                 QString const f = toqstr(changeExtension(file, ""));
197                 QList<QListWidgetItem *> matches =
198                         databaseLW->findItems(f, Qt::MatchExactly);
199                 if (matches.empty())
200                         databaseLW->addItem(f);
201         }
202
203         form_->changed();
204 }
205
206
207 void QBibtexDialog::deletePressed()
208 {
209         databaseLW->takeItem(databaseLW->currentRow());
210 }
211
212
213
214 void QBibtexDialog::databaseChanged()
215 {
216         deletePB->setEnabled(!form_->readOnly() && databaseLW->currentRow() != -1);
217 }
218
219
220 void QBibtexDialog::availableChanged()
221 {
222         form_->changed();
223 }
224
225
226 void QBibtexDialog::closeEvent(QCloseEvent *e)
227 {
228         form_->slotWMHide();
229         e->accept();
230 }
231
232
233 } // namespace frontend
234 } // namespace lyx
235
236 #include "QBibtexDialog_moc.cpp"