2 * \file QCitationDialog.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Kalle Dalheimer
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "QCitationDialog.h"
16 #include "ui/QCitationFindUi.h"
17 #include "QCitation.h"
19 #include "qt_helpers.h"
21 #include "bufferparams.h"
23 #include "controllers/ControlCitation.h"
24 #include "controllers/ButtonController.h"
26 #include "support/lstrings.h"
39 using support::getStringFromVector;
40 using support::getVectorFromString;
46 QCitationDialog::QCitationDialog(Dialog & dialog, QCitation * form)
47 : Dialog::View(dialog, "Citation"), form_(form)
51 setCaption(toqstr("LyX: " + getTitle()));
55 // Manage the ok, apply, restore and cancel/close buttons
57 bcview().setApply(applyPB);
58 bcview().setCancel(closePB);
59 bcview().setRestore(restorePB);
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);
72 selectedLV->setModel(form_->selected());
73 availableLV->setModel(form_->available());
75 // foundLV.setModel(form_->found());
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() ) );
84 // find_ = new QCitationFind(form_, this);
86 // connect(selectedLV, SIGNAL(doubleClicked(const QModelIndex & index)),
87 // form_, SLOT(on_okPB_clicked()));//SLOT(slotOK()));
90 QCitationDialog::~QCitationDialog()
94 void QCitationDialog::apply()
96 int const choice = std::max(0, citationStyleCO->currentItem());
97 bool const full = fulllistCB->isChecked();
98 bool const force = forceuppercaseCB->isChecked();
100 QString const before = textBeforeED->text();
101 QString const after = textAfterED->text();
103 form_->apply(choice, full, force, before, after);
107 void QCitationDialog::hide()
113 void QCitationDialog::show()
119 bool QCitationDialog::isVisible() const
121 return QDialog::isVisible();
125 void QCitationDialog::on_okPB_clicked()
131 void QCitationDialog::on_cancelPB_clicked()
137 void QCitationDialog::on_applyPB_clicked()
142 void QCitationDialog::on_restorePB_clicked()
145 bc().valid(form_->isValid() );
148 void QCitationDialog::update()
150 form_->updateModel();
152 // No keys have been selected yet, so...
153 infoML->document()->clear();
156 textBeforeED->setText(form_->textBefore());
157 textAfterED->setText(form_->textAfter());
163 void QCitationDialog::updateStyle()
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;
171 fulllistCB->setEnabled(natbib_engine);
172 forceuppercaseCB->setEnabled(natbib_engine);
173 textBeforeED->setEnabled(!basic_engine);
175 string const & command = form_->params().getCmdName();
177 // Find the style of the citekeys
178 vector<biblio::CiteStyle> const & styles =
179 ControlCitation::getCiteStyles();
180 biblio::CitationStyle const cs(command);
182 vector<biblio::CiteStyle>::const_iterator cit =
183 std::find(styles.begin(), styles.end(), cs.style);
185 // restore the latest natbib style
186 if (style_ >= 0 && style_ < citationStyleCO->count())
187 citationStyleCO->setCurrentItem(style_);
189 citationStyleCO->setCurrentItem(0);
191 fulllistCB->setChecked(false);
192 forceuppercaseCB->setChecked(false);
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);
203 void QCitationDialog::fillStyles()
205 int const orig = citationStyleCO->currentItem();
207 citationStyleCO->clear();
209 QStringList selected_keys = form_->selected()->stringList();
210 if (selected_keys.empty()) {
211 citationStyleCO->setEnabled(false);
212 citationStyleLA->setEnabled(false);
216 if (selectedLV->selectionModel()->selectedIndexes().empty())
219 int curr = selectedLV->selectionModel()->selectedIndexes()[0].row();//selectedLV->currentItem();
221 QStringList sty = form_->citationStyles(curr);
223 bool const basic_engine =
224 (form_->getEngine() == biblio::ENGINE_BASIC);
226 citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
227 citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
229 citationStyleCO->insertItems(0, sty);
231 if (orig != -1 && orig < citationStyleCO->count())
232 citationStyleCO->setCurrentItem(orig);
236 void QCitationDialog::setButtons()
238 // if (form_->readOnly())
241 int const row_count = selectedLV->model()->rowCount();
244 if (! selectedLV->selectionModel()->selectedIndexes().empty()) {
246 selectedLV->selectionModel()->selectedIndexes()[0].row();
249 deletePB->setEnabled(sel_nr >= 0);
250 upPB->setEnabled(sel_nr > 0);
251 downPB->setEnabled(sel_nr >= 0 && sel_nr < row_count - 1);
255 void QCitationDialog::on_selectedLV_currentChanged(Q3ListBoxItem*)
258 infoML->document()->clear();
260 int const sel = selectedLB->currentItem();
266 infoML->document()->setPlainText(form_->getKeyInfo(sel));
273 void QCitationDialog::on_addPB_clicked()
275 form_->addKeys(availableLV->selectionModel()->selectedIndexes());
279 void QCitationDialog::on_deletePB_clicked()
281 form_->deleteKeys(selectedLV->selectionModel()->selectedIndexes());
286 void QCitationDialog::on_upPB_clicked()
288 form_->upKey(selectedLV->selectionModel()->selectedIndexes());
293 void QCitationDialog::on_downPB_clicked()
295 form_->downKey(selectedLV->selectionModel()->selectedIndexes());
299 void QCitationDialog::on_findLE_textChanged(const QString & text)
301 QModelIndex const index = form_->findKey(text);
302 if (! index.isValid())
305 // QItemSelection selection(index, index);
306 availableLV->selectionModel()->select(index, QItemSelectionModel::Select);
310 void QCitationDialog::on_advancedSearchPB_clicked()
317 void QCitationDialog::changed()
324 void updateBrowser(Q3ListBox * browser,
325 vector<string> const & keys)
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 ?
334 browser->insertItem(toqstr(key));
339 QCitationFind::QCitationFind(QCitation * form, QWidget * parent, Qt::WFlags f)
340 : form_(form), QDialog(parent, f)
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()));
349 void QCitationFind::update()
351 // updateBrowser(availableLB, form_->availableKeys());
354 void QCitationFind::on_availableLB_currentChanged(Q3ListBoxItem *)
356 infoML->document()->clear();
358 int const sel = availableLB->currentItem();
360 addPB->setEnabled(false);
364 addPB->setEnabled(true);
365 // infoML->document()->setPlainText(form_->getKeyInfo(sel));
369 void QCitationFind::on_availableLB_selected(Q3ListBoxItem *)
371 int const sel = availableLB->currentItem();
373 // foundkeys.push_back(form_->availableKeys()[sel]);
378 void QCitationFind::on_addPB_clicked()
380 // form_->addKeys(availableLB->selectionModel()->selectedIndexes());
382 int const sel = availableLB->currentItem();
387 QStringList bibkeys = form_->available()->stringList();
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]));
402 void QCitationFind::previous()
404 find(biblio::BACKWARD);
408 void QCitationFind::next()
410 find(biblio::FORWARD);
414 void QCitationFind::find(biblio::Direction dir)
416 /* QStringList bibkeys = form_->available()->stringList();
418 biblio::InfoMap const & theMap = form_->bibkeysInfo();
420 biblio::Search const type = searchTypeCB->isChecked()
421 ? biblio::REGEX : biblio::SIMPLE;
423 vector<string>::const_iterator start = bibkeys.begin();
424 int const sel = availableLB->currentItem();
425 if (sel >= 0 && sel <= int(bibkeys.size()-1))
428 // Find the NEXT instance...
429 if (dir == biblio::FORWARD)
432 bool const casesens = searchCaseCB->isChecked();
433 string const str = fromqstr(searchED->text());
435 vector<string>::const_iterator cit =
436 biblio::searchKeys(theMap, bibkeys, str,
437 start, type, dir, casesens);
439 // not found. let's loop round
440 if (cit == bibkeys.end()) {
441 if (dir == biblio::FORWARD) {
442 start = bibkeys.begin();
444 else start = bibkeys.end() - 1;
446 cit = biblio::searchKeys(theMap, bibkeys, str,
447 start, type, dir, casesens);
449 if (cit == bibkeys.end())
453 int const found = int(cit - bibkeys.begin());
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();
467 } // namespace frontend