]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCitationDialog.C
more cleanup:
[lyx.git] / src / frontends / qt4 / QCitationDialog.C
1 /**
2  * \file QCitationDialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Kalle Dalheimer
7  * \author John Levon
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "QCitationDialog.h"
16 #include "QCitation.h"
17 #include "qt_helpers.h"
18
19 #include "bufferparams.h"
20 #include "gettext.h"
21
22 #include "controllers/ControlCitation.h"
23
24 #include "support/lstrings.h"
25
26 #include <vector>
27 #include <string>
28
29 using std::vector;
30 using std::string;
31
32 namespace lyx {
33
34 using support::getStringFromVector;
35 using support::getVectorFromString;
36
37 namespace frontend {
38
39
40 QCitationDialog::QCitationDialog(Dialog & dialog, QCitation * form)
41         : Dialog::View(dialog, _("Citation")), form_(form)
42 {
43         setupUi(this);
44
45         setWindowTitle(toqstr("LyX: " + getTitle()));
46
47         selectedLV->setModel(form_->selected());
48         availableLV->setModel(form_->available());
49
50     connect(citationStyleCO, SIGNAL(activated(int)),
51                 this, SLOT(changed()));
52     connect(fulllistCB, SIGNAL(clicked()),
53                 this, SLOT(changed()));
54     connect(forceuppercaseCB, SIGNAL(clicked()),
55                 this, SLOT(changed()));
56     connect(textBeforeED, SIGNAL(textChanged(const QString&)),
57                 this, SLOT(changed()));
58     connect(textAfterED, SIGNAL(textChanged(const QString&)),
59                 this, SLOT(changed()));
60     connect(clearPB, SIGNAL(clicked()),
61                 findLE, SLOT(clear()));
62         connect(availableLV->selectionModel(),
63                 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
64                 this, SLOT(availableChanged(const QModelIndex &, const QModelIndex &)));
65         connect(selectedLV->selectionModel(),
66                 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
67                 this, SLOT(selectedChanged(const QModelIndex &, const QModelIndex &)));
68 }
69
70
71 QCitationDialog::~QCitationDialog()
72 {
73 }
74
75
76 void QCitationDialog::apply()
77 {
78         int  const choice = std::max(0, citationStyleCO->currentIndex());
79         style_ = choice;
80         bool const full  = fulllistCB->isChecked();
81         bool const force = forceuppercaseCB->isChecked();
82
83         QString const before = textBeforeED->text();
84         QString const after = textAfterED->text();
85
86         form_->apply(choice, full, force, before, after);
87 }
88
89
90 void QCitationDialog::hide()
91 {
92         accept();
93 }
94
95
96 void QCitationDialog::show()
97 {
98         QDialog::show();
99 }
100
101
102 bool QCitationDialog::isVisible() const
103 {
104         return QDialog::isVisible();
105 }
106  
107
108 void QCitationDialog::on_okPB_clicked()
109 {
110         apply();
111         form_->clearSelection();
112         accept();
113 }
114
115
116 void QCitationDialog::on_cancelPB_clicked()
117 {
118         form_->clearSelection();
119         accept();
120 }
121
122
123 void QCitationDialog::on_applyPB_clicked()
124 {
125         apply();
126 }
127
128
129 void QCitationDialog::on_restorePB_clicked()
130 {
131         update();
132 }
133
134
135 void QCitationDialog::update()
136 {
137         form_->updateModel();
138
139         QModelIndex const idxa = availableLV->currentIndex();
140         if (form_->available()->rowCount() > 0 && !idxa.isValid())
141                 availableLV->setCurrentIndex(availableLV->model()->index(0,0));
142
143         QModelIndex const idx = selectedLV->currentIndex();
144         if (form_->selected()->rowCount() > 0 && !idx.isValid()) {
145                 selectedLV->setCurrentIndex(selectedLV->model()->index(0,0));
146                 updateInfo(selectedLV->currentIndex());
147         } else
148                 updateInfo(availableLV->currentIndex());
149
150         setButtons();
151
152         textBeforeED->setText(form_->textBefore());
153         textAfterED->setText(form_->textAfter());
154
155         fillStyles();
156         updateStyle();
157 }
158
159
160 void QCitationDialog::updateStyle()
161 {
162         biblio::CiteEngine const engine = form_->getEngine();
163         bool const natbib_engine =
164                 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
165                 engine == biblio::ENGINE_NATBIB_NUMERICAL;
166         bool const basic_engine = engine == biblio::ENGINE_BASIC;
167
168         fulllistCB->setEnabled(natbib_engine);
169         forceuppercaseCB->setEnabled(natbib_engine);
170         textBeforeED->setEnabled(!basic_engine);
171         textBeforeLA->setEnabled(!basic_engine);
172
173         string const & command = form_->params().getCmdName();
174
175         // Find the style of the citekeys
176         vector<biblio::CiteStyle> const & styles =
177                 ControlCitation::getCiteStyles();
178         biblio::CitationStyle const cs(command);
179
180         vector<biblio::CiteStyle>::const_iterator cit =
181                 std::find(styles.begin(), styles.end(), cs.style);
182
183         // restore the latest natbib style
184         if (style_ >= 0 && style_ < citationStyleCO->count())
185                 citationStyleCO->setCurrentIndex(style_);
186         else
187                 citationStyleCO->setCurrentIndex(0);
188
189         fulllistCB->setChecked(false);
190         forceuppercaseCB->setChecked(false);
191
192         if (cit != styles.end()) {
193                 int const i = int(cit - styles.begin());
194                 citationStyleCO->setCurrentIndex(i);
195                 fulllistCB->setChecked(cs.full);
196                 forceuppercaseCB->setChecked(cs.forceUCase);
197         }
198 }
199
200
201 void QCitationDialog::fillStyles()
202 {
203         int const orig = citationStyleCO->currentIndex();
204
205         citationStyleCO->clear();
206
207         QStringList selected_keys = form_->selected()->stringList();
208         if (selected_keys.empty()) {
209                 citationStyleCO->setEnabled(false);
210                 citationStyleLA->setEnabled(false);
211                 return;
212         }
213
214         int curr = selectedLV->model()->rowCount() - 1;
215         if (curr < 0)
216                 return;
217
218         if (!selectedLV->selectionModel()->selectedIndexes().empty())
219                 curr = selectedLV->selectionModel()->selectedIndexes()[0].row();
220
221         QStringList sty = form_->citationStyles(curr);
222
223         bool const basic_engine = 
224                 (form_->getEngine() == biblio::ENGINE_BASIC);
225
226         citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
227         citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
228
229         if (sty.isEmpty() || basic_engine)
230                 return;
231
232         citationStyleCO->insertItems(0, sty);
233
234         if (orig != -1 && orig < citationStyleCO->count())
235                 citationStyleCO->setCurrentIndex(orig);
236 }
237
238
239 bool QCitationDialog::isSelected(const QModelIndex & idx)
240 {
241         QString const str = idx.data().toString();
242         return !form_->selected()->stringList().filter(str).isEmpty();
243 }
244
245
246 void QCitationDialog::setButtons()
247 {
248         int const arows = availableLV->model()->rowCount();
249         addPB->setEnabled(arows>0 && !isSelected(availableLV->currentIndex()));
250
251         int const srows = selectedLV->model()->rowCount();
252         int const sel_nr = selectedLV->currentIndex().row();
253         deletePB->setEnabled(sel_nr >= 0);
254         upPB->setEnabled(sel_nr > 0);
255         downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
256         applyPB->setEnabled(srows>0);
257         okPB->setEnabled(srows>0);
258 }
259
260
261 void QCitationDialog::updateInfo(const QModelIndex & idx)
262 {
263         if (idx.isValid()) {
264                 QString const keytxt = form_->getKeyInfo(idx.data().toString());
265                 infoML->document()->setPlainText(keytxt);
266         } else
267                 infoML->document()->clear();
268 }
269
270
271 void QCitationDialog::on_selectedLV_clicked(const QModelIndex & idx)
272 {
273         availableLV->selectionModel()->clear();
274
275         updateInfo(idx);
276         changed();
277 }
278
279
280 void QCitationDialog::selectedChanged(const QModelIndex & idx, const QModelIndex &)
281 {
282         if (!idx.isValid())
283                 return;
284
285         updateInfo(idx);
286         changed();
287 }
288
289
290 void QCitationDialog::on_availableLV_clicked(const QModelIndex & idx)
291 {
292         selectedLV->selectionModel()->clear();
293
294         updateInfo(idx);
295         setButtons();
296 }
297
298
299 void QCitationDialog::availableChanged(const QModelIndex & idx, const QModelIndex &)
300 {
301         if (!idx.isValid())
302                 return;
303                 
304         updateInfo(idx);
305         setButtons();
306 }
307
308
309 void QCitationDialog::on_availableLV_activated(const QModelIndex & idx)
310 {
311         if (isSelected(idx))
312                 return;
313
314         on_addPB_clicked();             
315 }
316
317
318 void QCitationDialog::on_addPB_clicked()
319 {
320         QModelIndex idx = selectedLV->currentIndex();
321         form_->addKey(availableLV->currentIndex());
322         if (idx.isValid())
323                 selectedLV->setCurrentIndex(idx);
324         changed();
325 }
326
327
328 void QCitationDialog::on_deletePB_clicked()
329 {
330         QModelIndex idx = selectedLV->currentIndex();
331         int nrows = selectedLV->model()->rowCount();
332         
333         form_->deleteKey(idx);
334
335         if (idx.row() == nrows - 1)     
336                 idx = idx.sibling(idx.row() - 1, idx.column());
337
338         if (nrows>1)
339                 selectedLV->setCurrentIndex(idx);
340
341         updateInfo(selectedLV->currentIndex());
342         changed();
343 }
344
345
346 void QCitationDialog::on_upPB_clicked()
347 {
348         QModelIndex idx = selectedLV->currentIndex();
349         form_->upKey(idx);
350         selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
351         changed();
352 }
353
354
355 void QCitationDialog::on_downPB_clicked()
356 {
357         QModelIndex idx = selectedLV->currentIndex();
358         form_->downKey(idx);
359         selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
360         changed();
361 }
362
363
364 void QCitationDialog::on_findLE_textChanged(const QString & text)
365 {
366         clearPB->setDisabled(text.isEmpty());
367         if (text.isEmpty())
368                 findLE->setFocus();
369
370         form_->findKey(text);
371         if (form_->found()->rowCount() == 0) {
372                 findLE->backspace();
373                 return;
374         }
375         availableLV->setModel(form_->found());
376         changed();
377 }
378
379
380 void QCitationDialog::changed()
381 {
382         fillStyles();
383         setButtons();
384 }
385
386
387 } // namespace frontend
388 } // namespace lyx
389
390 #include "QCitationDialog_moc.cpp"