2 * \file QCitationDialog.cpp
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
9 * \author Abdelrazak Younes
11 * Full author contact details are available in file CREDITS.
16 #include "QCitationDialog.h"
18 #include "QCitation.h"
20 #include "frontends/controllers/frontend_helpers.h"
21 #include "frontends/controllers/ControlCitation.h"
30 #include <QCloseEvent>
40 QCitationDialog::QCitationDialog(Dialog & dialog, QCitation * form)
41 : Dialog::View(dialog, _("Citation")), form_(form)
45 setWindowTitle(toqstr("LyX: " + getTitle()));
47 selectedLV->setModel(form_->selected());
48 availableLV->setModel(form_->available());
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 &)));
71 QCitationDialog::~QCitationDialog()
76 void QCitationDialog::keyPressEvent(QKeyEvent * event)
78 if (event->key() == Qt::Key_Escape) {
79 form_->clearSelection();
88 void QCitationDialog::closeEvent(QCloseEvent * e)
90 form_->clearSelection();
96 void QCitationDialog::apply()
98 int const choice = std::max(0, citationStyleCO->currentIndex());
100 bool const full = fulllistCB->isChecked();
101 bool const force = forceuppercaseCB->isChecked();
103 QString const before = textBeforeED->text();
104 QString const after = textAfterED->text();
106 form_->apply(choice, full, force, before, after);
110 void QCitationDialog::hide()
112 form_->clearParams();
117 void QCitationDialog::show()
120 availableLV->setFocus();
125 bool QCitationDialog::isVisible() const
127 return QDialog::isVisible();
131 void QCitationDialog::on_okPB_clicked()
134 form_->clearSelection();
139 void QCitationDialog::on_cancelPB_clicked()
141 form_->clearSelection();
146 void QCitationDialog::on_applyPB_clicked()
152 void QCitationDialog::on_restorePB_clicked()
159 void QCitationDialog::update()
161 if (selectedLV->selectionModel()->selectedIndexes().isEmpty()) {
162 if (availableLV->selectionModel()->selectedIndexes().isEmpty()
163 && availableLV->model()->rowCount() > 0)
164 availableLV->setCurrentIndex(availableLV->model()->index(0,0));
165 updateInfo(availableLV->currentIndex());
167 updateInfo(selectedLV->currentIndex());
171 textBeforeED->setText(form_->textBefore());
172 textAfterED->setText(form_->textAfter());
179 void QCitationDialog::updateStyle()
181 biblio::CiteEngine const engine = form_->getEngine();
182 bool const natbib_engine =
183 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
184 engine == biblio::ENGINE_NATBIB_NUMERICAL;
185 bool const basic_engine = engine == biblio::ENGINE_BASIC;
187 fulllistCB->setEnabled(natbib_engine);
188 forceuppercaseCB->setEnabled(natbib_engine);
189 textBeforeED->setEnabled(!basic_engine);
190 textBeforeLA->setEnabled(!basic_engine);
192 string const & command = form_->params().getCmdName();
194 // Find the style of the citekeys
195 vector<biblio::CiteStyle> const & styles =
196 ControlCitation::getCiteStyles();
197 biblio::CitationStyle const cs(command);
199 vector<biblio::CiteStyle>::const_iterator cit =
200 std::find(styles.begin(), styles.end(), cs.style);
202 // restore the latest natbib style
203 if (style_ >= 0 && style_ < citationStyleCO->count())
204 citationStyleCO->setCurrentIndex(style_);
206 citationStyleCO->setCurrentIndex(0);
208 fulllistCB->setChecked(false);
209 forceuppercaseCB->setChecked(false);
211 if (cit != styles.end()) {
212 int const i = int(cit - styles.begin());
213 citationStyleCO->setCurrentIndex(i);
214 fulllistCB->setChecked(cs.full);
215 forceuppercaseCB->setChecked(cs.forceUCase);
220 void QCitationDialog::fillStyles()
222 int const orig = citationStyleCO->currentIndex();
224 citationStyleCO->clear();
226 QStringList selected_keys = form_->selected()->stringList();
227 if (selected_keys.empty()) {
228 citationStyleCO->setEnabled(false);
229 citationStyleLA->setEnabled(false);
233 int curr = selectedLV->model()->rowCount() - 1;
237 if (!selectedLV->selectionModel()->selectedIndexes().empty())
238 curr = selectedLV->selectionModel()->selectedIndexes()[0].row();
240 QStringList sty = form_->citationStyles(curr);
242 bool const basic_engine =
243 (form_->getEngine() == biblio::ENGINE_BASIC);
245 citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
246 citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
248 if (sty.isEmpty() || basic_engine)
251 citationStyleCO->insertItems(0, sty);
253 if (orig != -1 && orig < citationStyleCO->count())
254 citationStyleCO->setCurrentIndex(orig);
258 bool QCitationDialog::isSelected(const QModelIndex & idx)
260 QString const str = idx.data().toString();
261 return form_->selected()->stringList().contains(str);
265 void QCitationDialog::setButtons()
267 int const arows = availableLV->model()->rowCount();
268 addPB->setEnabled(arows > 0 &&
269 availableLV->currentIndex().isValid() &&
270 !isSelected(availableLV->currentIndex()));
272 int const srows = selectedLV->model()->rowCount();
273 int const sel_nr = selectedLV->currentIndex().row();
274 deletePB->setEnabled(sel_nr >= 0);
275 upPB->setEnabled(sel_nr > 0);
276 downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
277 applyPB->setEnabled(srows > 0);
278 okPB->setEnabled(srows > 0);
282 void QCitationDialog::updateInfo(const QModelIndex & idx)
285 QString const keytxt = form_->getKeyInfo(idx.data().toString());
286 infoML->document()->setPlainText(keytxt);
288 infoML->document()->clear();
292 void QCitationDialog::on_selectedLV_clicked(const QModelIndex &)
294 availableLV->selectionModel()->reset();
299 void QCitationDialog::selectedChanged(const QModelIndex & idx, const QModelIndex &)
304 availableLV->selectionModel()->reset();
309 void QCitationDialog::on_availableLV_clicked(const QModelIndex &)
311 selectedLV->selectionModel()->reset();
316 void QCitationDialog::availableChanged(const QModelIndex & idx, const QModelIndex &)
321 selectedLV->selectionModel()->reset();
326 void QCitationDialog::on_availableLV_activated(const QModelIndex & idx)
331 selectedLV->selectionModel()->reset();
333 if (selectedLV->model()->rowCount() == 1)
338 void QCitationDialog::on_availableLV_entered(const QModelIndex &)
343 void QCitationDialog::on_addPB_clicked()
345 QModelIndex idx = selectedLV->currentIndex();
346 form_->addKey(availableLV->currentIndex());
348 selectedLV->setCurrentIndex(idx);
349 selectedLV->selectionModel()->reset();
354 void QCitationDialog::on_deletePB_clicked()
356 QModelIndex idx = selectedLV->currentIndex();
357 int nrows = selectedLV->model()->rowCount();
359 form_->deleteKey(idx);
361 if (idx.row() == nrows - 1)
362 idx = idx.sibling(idx.row() - 1, idx.column());
365 selectedLV->setCurrentIndex(idx);
367 availableLV->selectionModel()->reset();
372 void QCitationDialog::on_upPB_clicked()
374 QModelIndex idx = selectedLV->currentIndex();
376 selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
377 availableLV->selectionModel()->reset();
382 void QCitationDialog::on_downPB_clicked()
384 QModelIndex idx = selectedLV->currentIndex();
386 selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
387 availableLV->selectionModel()->reset();
392 void QCitationDialog::findText(QString const & text)
394 bool const case_sentitive = caseCB->checkState();
395 bool const reg_exp = regexCB->checkState();
396 form_->findKey(text, false, case_sentitive, reg_exp);
397 selectedLV->selectionModel()->reset();
402 void QCitationDialog::on_findLE_textChanged(const QString & text)
404 clearPB->setDisabled(text.isEmpty());
411 void QCitationDialog::on_caseCB_stateChanged(int)
413 findText(findLE->text());
417 void QCitationDialog::on_regexCB_stateChanged(int)
419 findText(findLE->text());
423 void QCitationDialog::changed()
430 } // namespace frontend
433 #include "QCitationDialog_moc.cpp"