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