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 "QCitation.h"
17 #include "qt_helpers.h"
19 #include "bufferparams.h"
22 #include "controllers/ControlCitation.h"
24 #include "support/lstrings.h"
29 #include <QCloseEvent>
37 using support::getStringFromVector;
38 using support::getVectorFromString;
43 QCitationDialog::QCitationDialog(Dialog & dialog, QCitation * form)
44 : Dialog::View(dialog, _("Citation")), form_(form)
48 setWindowTitle(toqstr("LyX: " + getTitle()));
50 selectedLV->setModel(form_->selected());
51 availableLV->setModel(form_->available());
53 connect(citationStyleCO, SIGNAL(activated(int)),
54 this, SLOT(changed()));
55 connect(fulllistCB, SIGNAL(clicked()),
56 this, SLOT(changed()));
57 connect(forceuppercaseCB, SIGNAL(clicked()),
58 this, SLOT(changed()));
59 connect(textBeforeED, SIGNAL(textChanged(const QString&)),
60 this, SLOT(changed()));
61 connect(textAfterED, SIGNAL(textChanged(const QString&)),
62 this, SLOT(changed()));
63 connect(clearPB, SIGNAL(clicked()),
64 findLE, SLOT(clear()));
65 connect(availableLV->selectionModel(),
66 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
67 this, SLOT(availableChanged(const QModelIndex &, const QModelIndex &)));
68 connect(selectedLV->selectionModel(),
69 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
70 this, SLOT(selectedChanged(const QModelIndex &, const QModelIndex &)));
74 QCitationDialog::~QCitationDialog()
79 void QCitationDialog::keyPressEvent(QKeyEvent * event)
81 if (event->key() == Qt::Key_Escape) {
82 form_->clearSelection();
91 void QCitationDialog::closeEvent(QCloseEvent * e)
93 form_->clearSelection();
99 void QCitationDialog::apply()
101 int const choice = std::max(0, citationStyleCO->currentIndex());
103 bool const full = fulllistCB->isChecked();
104 bool const force = forceuppercaseCB->isChecked();
106 QString const before = textBeforeED->text();
107 QString const after = textAfterED->text();
109 form_->apply(choice, full, force, before, after);
113 void QCitationDialog::hide()
115 form_->clearParams();
120 void QCitationDialog::show()
126 bool QCitationDialog::isVisible() const
128 return QDialog::isVisible();
132 void QCitationDialog::on_okPB_clicked()
135 form_->clearSelection();
140 void QCitationDialog::on_cancelPB_clicked()
142 form_->clearSelection();
147 void QCitationDialog::on_applyPB_clicked()
153 void QCitationDialog::on_restorePB_clicked()
159 void QCitationDialog::update()
161 form_->updateModel();
163 QModelIndex const idxa = availableLV->currentIndex();
164 if (form_->available()->rowCount() > 0 && !idxa.isValid())
165 availableLV->setCurrentIndex(availableLV->model()->index(0,0));
167 QModelIndex const idx = selectedLV->currentIndex();
168 if (form_->selected()->rowCount() > 0 && !idx.isValid()) {
169 selectedLV->setCurrentIndex(selectedLV->model()->index(0,0));
170 updateInfo(selectedLV->currentIndex());
172 updateInfo(availableLV->currentIndex());
176 textBeforeED->setText(form_->textBefore());
177 textAfterED->setText(form_->textAfter());
184 void QCitationDialog::updateStyle()
186 biblio::CiteEngine const engine = form_->getEngine();
187 bool const natbib_engine =
188 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
189 engine == biblio::ENGINE_NATBIB_NUMERICAL;
190 bool const basic_engine = engine == biblio::ENGINE_BASIC;
192 fulllistCB->setEnabled(natbib_engine);
193 forceuppercaseCB->setEnabled(natbib_engine);
194 textBeforeED->setEnabled(!basic_engine);
195 textBeforeLA->setEnabled(!basic_engine);
197 string const & command = form_->params().getCmdName();
199 // Find the style of the citekeys
200 vector<biblio::CiteStyle> const & styles =
201 ControlCitation::getCiteStyles();
202 biblio::CitationStyle const cs(command);
204 vector<biblio::CiteStyle>::const_iterator cit =
205 std::find(styles.begin(), styles.end(), cs.style);
207 // restore the latest natbib style
208 if (style_ >= 0 && style_ < citationStyleCO->count())
209 citationStyleCO->setCurrentIndex(style_);
211 citationStyleCO->setCurrentIndex(0);
213 fulllistCB->setChecked(false);
214 forceuppercaseCB->setChecked(false);
216 if (cit != styles.end()) {
217 int const i = int(cit - styles.begin());
218 citationStyleCO->setCurrentIndex(i);
219 fulllistCB->setChecked(cs.full);
220 forceuppercaseCB->setChecked(cs.forceUCase);
225 void QCitationDialog::fillStyles()
227 int const orig = citationStyleCO->currentIndex();
229 citationStyleCO->clear();
231 QStringList selected_keys = form_->selected()->stringList();
232 if (selected_keys.empty()) {
233 citationStyleCO->setEnabled(false);
234 citationStyleLA->setEnabled(false);
238 int curr = selectedLV->model()->rowCount() - 1;
242 if (!selectedLV->selectionModel()->selectedIndexes().empty())
243 curr = selectedLV->selectionModel()->selectedIndexes()[0].row();
245 QStringList sty = form_->citationStyles(curr);
247 bool const basic_engine =
248 (form_->getEngine() == biblio::ENGINE_BASIC);
250 citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
251 citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
253 if (sty.isEmpty() || basic_engine)
256 citationStyleCO->insertItems(0, sty);
258 if (orig != -1 && orig < citationStyleCO->count())
259 citationStyleCO->setCurrentIndex(orig);
263 bool QCitationDialog::isSelected(const QModelIndex & idx)
265 QString const str = idx.data().toString();
266 return !form_->selected()->stringList().filter(str).isEmpty();
270 void QCitationDialog::setButtons()
272 int const arows = availableLV->model()->rowCount();
273 addPB->setEnabled(arows>0 && !isSelected(availableLV->currentIndex()));
275 int const srows = selectedLV->model()->rowCount();
276 int const sel_nr = selectedLV->currentIndex().row();
277 deletePB->setEnabled(sel_nr >= 0);
278 upPB->setEnabled(sel_nr > 0);
279 downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
280 applyPB->setEnabled(srows>0);
281 okPB->setEnabled(srows>0);
285 void QCitationDialog::updateInfo(const QModelIndex & idx)
288 QString const keytxt = form_->getKeyInfo(idx.data().toString());
289 infoML->document()->setPlainText(keytxt);
291 infoML->document()->clear();
295 void QCitationDialog::on_selectedLV_clicked(const QModelIndex & idx)
297 availableLV->selectionModel()->clear();
304 void QCitationDialog::selectedChanged(const QModelIndex & idx, const QModelIndex &)
314 void QCitationDialog::on_availableLV_clicked(const QModelIndex & idx)
316 selectedLV->selectionModel()->clear();
323 void QCitationDialog::availableChanged(const QModelIndex & idx, const QModelIndex &)
333 void QCitationDialog::on_availableLV_activated(const QModelIndex & idx)
342 void QCitationDialog::on_addPB_clicked()
344 QModelIndex idx = selectedLV->currentIndex();
345 form_->addKey(availableLV->currentIndex());
347 selectedLV->setCurrentIndex(idx);
352 void QCitationDialog::on_deletePB_clicked()
354 QModelIndex idx = selectedLV->currentIndex();
355 int nrows = selectedLV->model()->rowCount();
357 form_->deleteKey(idx);
359 if (idx.row() == nrows - 1)
360 idx = idx.sibling(idx.row() - 1, idx.column());
363 selectedLV->setCurrentIndex(idx);
365 updateInfo(selectedLV->currentIndex());
370 void QCitationDialog::on_upPB_clicked()
372 QModelIndex idx = selectedLV->currentIndex();
374 selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
379 void QCitationDialog::on_downPB_clicked()
381 QModelIndex idx = selectedLV->currentIndex();
383 selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
388 void QCitationDialog::on_findLE_textChanged(const QString & text)
390 clearPB->setDisabled(text.isEmpty());
394 form_->findKey(text);
395 if (form_->found()->rowCount() == 0) {
399 availableLV->setModel(form_->found());
404 void QCitationDialog::changed()
411 } // namespace frontend
414 #include "QCitationDialog_moc.cpp"