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"
34 using support::getStringFromVector;
35 using support::getVectorFromString;
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::apply()
78 int const choice = std::max(0, citationStyleCO->currentIndex());
80 bool const full = fulllistCB->isChecked();
81 bool const force = forceuppercaseCB->isChecked();
83 QString const before = textBeforeED->text();
84 QString const after = textAfterED->text();
86 form_->apply(choice, full, force, before, after);
90 void QCitationDialog::hide()
96 void QCitationDialog::show()
102 bool QCitationDialog::isVisible() const
104 return QDialog::isVisible();
108 void QCitationDialog::on_okPB_clicked()
111 form_->clearSelection();
116 void QCitationDialog::on_cancelPB_clicked()
118 form_->clearSelection();
123 void QCitationDialog::on_applyPB_clicked()
129 void QCitationDialog::on_restorePB_clicked()
135 void QCitationDialog::update()
137 form_->updateModel();
139 QModelIndex const idxa = availableLV->currentIndex();
140 if (form_->available()->rowCount() > 0 && !idxa.isValid())
141 availableLV->setCurrentIndex(availableLV->model()->index(0,0));
143 QModelIndex const idx = selectedLV->currentIndex();
144 if (form_->selected()->rowCount() > 0 && !idx.isValid()) {
145 selectedLV->setCurrentIndex(selectedLV->model()->index(0,0));
146 updateInfo(selectedLV->currentIndex());
148 updateInfo(availableLV->currentIndex());
152 textBeforeED->setText(form_->textBefore());
153 textAfterED->setText(form_->textAfter());
160 void QCitationDialog::updateStyle()
162 biblio::CiteEngine const engine = form_->getEngine();
163 bool const natbib_engine =
164 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
165 engine == biblio::ENGINE_NATBIB_NUMERICAL;
166 bool const basic_engine = engine == biblio::ENGINE_BASIC;
168 fulllistCB->setEnabled(natbib_engine);
169 forceuppercaseCB->setEnabled(natbib_engine);
170 textBeforeED->setEnabled(!basic_engine);
171 textBeforeLA->setEnabled(!basic_engine);
173 string const & command = form_->params().getCmdName();
175 // Find the style of the citekeys
176 vector<biblio::CiteStyle> const & styles =
177 ControlCitation::getCiteStyles();
178 biblio::CitationStyle const cs(command);
180 vector<biblio::CiteStyle>::const_iterator cit =
181 std::find(styles.begin(), styles.end(), cs.style);
183 // restore the latest natbib style
184 if (style_ >= 0 && style_ < citationStyleCO->count())
185 citationStyleCO->setCurrentIndex(style_);
187 citationStyleCO->setCurrentIndex(0);
189 fulllistCB->setChecked(false);
190 forceuppercaseCB->setChecked(false);
192 if (cit != styles.end()) {
193 int const i = int(cit - styles.begin());
194 citationStyleCO->setCurrentIndex(i);
195 fulllistCB->setChecked(cs.full);
196 forceuppercaseCB->setChecked(cs.forceUCase);
201 void QCitationDialog::fillStyles()
203 int const orig = citationStyleCO->currentIndex();
205 citationStyleCO->clear();
207 QStringList selected_keys = form_->selected()->stringList();
208 if (selected_keys.empty()) {
209 citationStyleCO->setEnabled(false);
210 citationStyleLA->setEnabled(false);
214 int curr = selectedLV->model()->rowCount() - 1;
218 if (!selectedLV->selectionModel()->selectedIndexes().empty())
219 curr = selectedLV->selectionModel()->selectedIndexes()[0].row();
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 if (sty.isEmpty() || basic_engine)
232 citationStyleCO->insertItems(0, sty);
234 if (orig != -1 && orig < citationStyleCO->count())
235 citationStyleCO->setCurrentIndex(orig);
239 bool QCitationDialog::isSelected(const QModelIndex & idx)
241 QString const str = idx.data().toString();
242 return !form_->selected()->stringList().filter(str).isEmpty();
246 void QCitationDialog::setButtons()
248 int const arows = availableLV->model()->rowCount();
249 addPB->setEnabled(arows>0 && !isSelected(availableLV->currentIndex()));
251 int const srows = selectedLV->model()->rowCount();
252 int const sel_nr = selectedLV->currentIndex().row();
253 deletePB->setEnabled(sel_nr >= 0);
254 upPB->setEnabled(sel_nr > 0);
255 downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
256 applyPB->setEnabled(srows>0);
257 okPB->setEnabled(srows>0);
261 void QCitationDialog::updateInfo(const QModelIndex & idx)
264 QString const keytxt = form_->getKeyInfo(idx.data().toString());
265 infoML->document()->setPlainText(keytxt);
267 infoML->document()->clear();
271 void QCitationDialog::on_selectedLV_clicked(const QModelIndex & idx)
273 availableLV->selectionModel()->clear();
280 void QCitationDialog::selectedChanged(const QModelIndex & idx, const QModelIndex &)
290 void QCitationDialog::on_availableLV_clicked(const QModelIndex & idx)
292 selectedLV->selectionModel()->clear();
299 void QCitationDialog::availableChanged(const QModelIndex & idx, const QModelIndex &)
309 void QCitationDialog::on_availableLV_activated(const QModelIndex & idx)
318 void QCitationDialog::on_addPB_clicked()
320 QModelIndex idx = selectedLV->currentIndex();
321 form_->addKey(availableLV->currentIndex());
323 selectedLV->setCurrentIndex(idx);
328 void QCitationDialog::on_deletePB_clicked()
330 QModelIndex idx = selectedLV->currentIndex();
331 int nrows = selectedLV->model()->rowCount();
333 form_->deleteKey(idx);
335 if (idx.row() == nrows - 1)
336 idx = idx.sibling(idx.row() - 1, idx.column());
339 selectedLV->setCurrentIndex(idx);
341 updateInfo(selectedLV->currentIndex());
346 void QCitationDialog::on_upPB_clicked()
348 QModelIndex idx = selectedLV->currentIndex();
350 selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
355 void QCitationDialog::on_downPB_clicked()
357 QModelIndex idx = selectedLV->currentIndex();
359 selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
364 void QCitationDialog::on_findLE_textChanged(const QString & text)
366 clearPB->setDisabled(text.isEmpty());
370 form_->findKey(text);
371 if (form_->found()->rowCount() == 0) {
375 availableLV->setModel(form_->found());
380 void QCitationDialog::changed()
387 } // namespace frontend
390 #include "QCitationDialog_moc.cpp"