]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCitationDialog.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[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 "ui/QCitationFindUi.h"
17 #include "QCitation.h"
18 #include "Qt2BC.h"
19 #include "qt_helpers.h"
20
21 #include "bufferparams.h"
22
23 #include "controllers/ControlCitation.h"
24 #include "controllers/ButtonController.h"
25
26 #include "support/lstrings.h"
27
28 #include <iostream>
29 using std::cout;
30 using std::endl;
31
32 using std::find;
33 using std::string;
34 using std::vector;
35
36
37 namespace lyx {
38
39 using support::getStringFromVector;
40 using support::getVectorFromString;
41 using support::trim;
42
43 namespace frontend {
44
45
46 QCitationDialog::QCitationDialog(Dialog & dialog, QCitation * form)
47         : Dialog::View(dialog, "Citation"), form_(form)
48 {
49         setupUi(this);
50
51         setCaption(toqstr("LyX: " + getTitle()));
52
53 /*
54
55         // Manage the ok, apply, restore and cancel/close buttons
56         bcview().setOK(okPB);
57         bcview().setApply(applyPB);
58         bcview().setCancel(closePB);
59         bcview().setRestore(restorePB);
60
61         bcview().addReadOnly(addPB);
62         bcview().addReadOnly(deletePB);
63         bcview().addReadOnly(upPB);
64         bcview().addReadOnly(downPB);
65         bcview().addReadOnly(citationStyleCO);
66         bcview().addReadOnly(forceuppercaseCB);
67         bcview().addReadOnly(fulllistCB);
68         bcview().addReadOnly(textBeforeED);
69         bcview().addReadOnly(textAfterED);
70 */
71
72         selectedLV->setModel(form_->selected());
73         availableLV->setModel(form_->available());
74
75 //      foundLV.setModel(form_->found());
76
77     connect( citationStyleCO, SIGNAL( activated(int) ), this, SLOT( changed() ) );
78     connect( fulllistCB, SIGNAL( clicked() ), this, SLOT( changed() ) );
79     connect( forceuppercaseCB, SIGNAL( clicked() ), this, SLOT( changed() ) );
80     connect( textBeforeED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed() ) );
81     connect( textAfterED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed() ) );
82
83
84 //      find_ = new QCitationFind(form_, this);
85         
86 //      connect(selectedLV, SIGNAL(doubleClicked(const QModelIndex & index)),
87 //              form_, SLOT(on_okPB_clicked()));//SLOT(slotOK()));
88 }
89
90 QCitationDialog::~QCitationDialog()
91 {
92 }
93
94 void QCitationDialog::apply()
95 {
96         int  const choice = std::max(0, citationStyleCO->currentItem());
97         bool const full  = fulllistCB->isChecked();
98         bool const force = forceuppercaseCB->isChecked();
99
100         QString const before = textBeforeED->text();
101         QString const after = textAfterED->text();
102
103         form_->apply(choice, full, force, before, after);
104 }
105
106
107 void QCitationDialog::hide()
108 {
109         accept();
110 }
111
112
113 void QCitationDialog::show()
114 {
115         QDialog::show();
116 }
117
118
119 bool QCitationDialog::isVisible() const
120 {
121         return QDialog::isVisible();
122 }
123  
124
125 void QCitationDialog::on_okPB_clicked()
126 {
127         apply();
128         accept();
129 }
130
131 void QCitationDialog::on_cancelPB_clicked()
132 {
133         accept();
134 //      reject();
135 }
136
137 void QCitationDialog::on_applyPB_clicked()
138 {
139         apply();
140 }
141
142 void QCitationDialog::on_restorePB_clicked()
143 {
144         update();
145         bc().valid(form_->isValid() );
146 }
147
148 void QCitationDialog::update()
149 {
150         form_->updateModel();
151
152         // No keys have been selected yet, so...
153         infoML->document()->clear();
154         setButtons();
155
156         textBeforeED->setText(form_->textBefore());
157         textAfterED->setText(form_->textAfter());
158
159         fillStyles();
160         updateStyle();
161 }
162
163 void QCitationDialog::updateStyle()
164 {
165         biblio::CiteEngine const engine = form_->getEngine();
166         bool const natbib_engine =
167                 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
168                 engine == biblio::ENGINE_NATBIB_NUMERICAL;
169         bool const basic_engine = engine == biblio::ENGINE_BASIC;
170
171         fulllistCB->setEnabled(natbib_engine);
172         forceuppercaseCB->setEnabled(natbib_engine);
173         textBeforeED->setEnabled(!basic_engine);
174
175         string const & command = form_->params().getCmdName();
176
177         // Find the style of the citekeys
178         vector<biblio::CiteStyle> const & styles =
179                 ControlCitation::getCiteStyles();
180         biblio::CitationStyle const cs(command);
181
182         vector<biblio::CiteStyle>::const_iterator cit =
183                 std::find(styles.begin(), styles.end(), cs.style);
184
185         // restore the latest natbib style
186         if (style_ >= 0 && style_ < citationStyleCO->count())
187                 citationStyleCO->setCurrentItem(style_);
188         else
189                 citationStyleCO->setCurrentItem(0);
190
191         fulllistCB->setChecked(false);
192         forceuppercaseCB->setChecked(false);
193
194         if (cit != styles.end()) {
195                 int const i = int(cit - styles.begin());
196                 citationStyleCO->setCurrentItem(i);
197                 fulllistCB->setChecked(cs.full);
198                 forceuppercaseCB->setChecked(cs.forceUCase);
199         }
200 }
201
202
203 void QCitationDialog::fillStyles()
204 {
205         int const orig = citationStyleCO->currentItem();
206
207         citationStyleCO->clear();
208
209         QStringList selected_keys = form_->selected()->stringList();
210         if (selected_keys.empty()) {
211                 citationStyleCO->setEnabled(false);
212                 citationStyleLA->setEnabled(false);
213                 return;
214         }
215
216         if (selectedLV->selectionModel()->selectedIndexes().empty())
217                 return;
218         
219         int curr = selectedLV->selectionModel()->selectedIndexes()[0].row();//selectedLV->currentItem();
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         citationStyleCO->insertItems(0, sty);
230
231         if (orig != -1 && orig < citationStyleCO->count())
232                 citationStyleCO->setCurrentItem(orig);
233 }
234
235
236 void QCitationDialog::setButtons()
237 {
238 //      if (form_->readOnly())
239 //              return;
240
241         int const row_count = selectedLV->model()->rowCount();
242
243         int sel_nr=-1;
244         if (! selectedLV->selectionModel()->selectedIndexes().empty()) {
245                 sel_nr = 
246                 selectedLV->selectionModel()->selectedIndexes()[0].row();
247         }
248
249         deletePB->setEnabled(sel_nr >= 0);
250         upPB->setEnabled(sel_nr > 0);
251         downPB->setEnabled(sel_nr >= 0 && sel_nr < row_count - 1);
252 }
253
254 /*
255 void QCitationDialog::on_selectedLV_currentChanged(Q3ListBoxItem*)
256 {
257         fillStyles();
258         infoML->document()->clear();
259
260         int const sel = selectedLB->currentItem();
261         if (sel < 0) {
262                 setButtons();
263                 return;
264         }
265
266         infoML->document()->setPlainText(form_->getKeyInfo(sel));
267
268         setButtons();
269 }
270 */
271
272
273 void QCitationDialog::on_addPB_clicked()
274 {
275         form_->addKeys(availableLV->selectionModel()->selectedIndexes());
276         changed();
277 }
278
279 void QCitationDialog::on_deletePB_clicked()
280 {
281         form_->deleteKeys(selectedLV->selectionModel()->selectedIndexes());
282         changed();
283 }
284
285
286 void QCitationDialog::on_upPB_clicked()
287 {
288         form_->upKey(selectedLV->selectionModel()->selectedIndexes());
289         changed();
290 }
291
292
293 void QCitationDialog::on_downPB_clicked()
294 {
295         form_->downKey(selectedLV->selectionModel()->selectedIndexes());
296         changed();
297 }
298
299 void QCitationDialog::on_findLE_textChanged(const QString & text)
300 {
301         QModelIndex const index = form_->findKey(text);
302         if (! index.isValid())
303                 return;
304
305 //      QItemSelection selection(index, index);
306         availableLV->selectionModel()->select(index, QItemSelectionModel::Select);
307         changed();
308 }
309
310 void QCitationDialog::on_advancedSearchPB_clicked()
311 {
312 //      find_->exec();
313         changed();
314 }
315
316
317 void QCitationDialog::changed()
318 {
319         fillStyles();
320         setButtons();
321 }
322
323
324 void updateBrowser(Q3ListBox * browser,
325                               vector<string> const & keys)
326 {
327         browser->clear();
328
329         for (vector<string>::const_iterator it = keys.begin();
330                 it < keys.end(); ++it) {
331                 string const key = trim(*it);
332                 // FIXME: why the .empty() test ?
333                 if (!key.empty())
334                         browser->insertItem(toqstr(key));
335         }
336 }
337
338
339 QCitationFind::QCitationFind(QCitation * form, QWidget * parent, Qt::WFlags f)
340 : form_(form), QDialog(parent, f)
341 {
342         setupUi(this);
343     connect(addPB, SIGNAL(clicked()), this, SLOT(accept()));
344     connect(closePB, SIGNAL(clicked()), this, SLOT(reject()));
345         connect(previousPB, SIGNAL(clicked()), this, SLOT(previous()));
346         connect(nextPB, SIGNAL(clicked()), this, SLOT(next()));
347 }
348
349 void QCitationFind::update()
350 {
351 //      updateBrowser(availableLB, form_->availableKeys());
352 }
353
354 void QCitationFind::on_availableLB_currentChanged(Q3ListBoxItem *)
355 {
356         infoML->document()->clear();
357
358         int const sel = availableLB->currentItem();
359         if (sel < 0) {
360                 addPB->setEnabled(false);
361                 return;
362         }
363
364         addPB->setEnabled(true);
365 //      infoML->document()->setPlainText(form_->getKeyInfo(sel));
366 }
367
368
369 void QCitationFind::on_availableLB_selected(Q3ListBoxItem *)
370 {
371         int const sel = availableLB->currentItem();
372         foundkeys.clear();
373 //      foundkeys.push_back(form_->availableKeys()[sel]);
374         emit newCitations();
375         accept();
376 }
377
378 void QCitationFind::on_addPB_clicked()
379 {
380 //      form_->addKeys(availableLB->selectionModel()->selectedIndexes());
381
382         int const sel = availableLB->currentItem();
383
384         if (sel < 0)
385                 return;
386
387         QStringList bibkeys = form_->available()->stringList();
388
389         // Add the selected browser_bib keys to browser_cite
390         // multiple selections are possible
391         for (unsigned int i = 0; i != availableLB->count(); i++) {
392                 if (availableLB->isSelected(i)) {
393                                 foundkeys.push_back(fromqstr(bibkeys[i]));
394                 }
395         }
396
397         emit newCitations();
398         accept();
399 }
400
401
402 void QCitationFind::previous()
403 {
404         find(biblio::BACKWARD);
405 }
406
407
408 void QCitationFind::next()
409 {
410         find(biblio::FORWARD);
411 }
412
413
414 void QCitationFind::find(biblio::Direction dir)
415 {
416 /*      QStringList bibkeys = form_->available()->stringList();
417
418         biblio::InfoMap const & theMap = form_->bibkeysInfo();
419
420         biblio::Search const type = searchTypeCB->isChecked()
421                 ? biblio::REGEX : biblio::SIMPLE;
422
423         vector<string>::const_iterator start = bibkeys.begin();
424         int const sel = availableLB->currentItem();
425         if (sel >= 0 && sel <= int(bibkeys.size()-1))
426                 start += sel;
427
428         // Find the NEXT instance...
429         if (dir == biblio::FORWARD)
430                 start += 1;
431
432         bool const casesens = searchCaseCB->isChecked();
433         string const str = fromqstr(searchED->text());
434
435         vector<string>::const_iterator cit =
436                 biblio::searchKeys(theMap, bibkeys, str,
437                                    start, type, dir, casesens);
438
439         // not found. let's loop round
440         if (cit == bibkeys.end()) {
441                 if (dir == biblio::FORWARD) {
442                         start = bibkeys.begin();
443                 }
444                 else start = bibkeys.end() - 1;
445
446                 cit = biblio::searchKeys(theMap, bibkeys, str,
447                                          start, type, dir, casesens);
448
449                 if (cit == bibkeys.end())
450                         return;
451         }
452
453         int const found = int(cit - bibkeys.begin());
454         if (found == sel) {
455                 return;
456         }
457
458         // Update the display
459         // note that we have multi selection mode!
460         availableLB->setSelected(sel, false);
461         availableLB->setSelected(found, true);
462         availableLB->setCurrentItem(found);
463         availableLB->ensureCurrentVisible();
464 */
465 }
466
467 } // namespace frontend
468 } // namespace lyx