2 * \file GuiCitationDialog.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
10 * \author Richard Heck
12 * Full author contact details are available in file CREDITS.
17 #include "GuiCitationDialog.h"
19 #include "GuiCitation.h"
21 #include "frontends/controllers/frontend_helpers.h"
22 #include "frontends/controllers/ControlCitation.h"
23 #include "qt_helpers.h"
25 #include "support/docstring.h"
34 #include <QCloseEvent>
46 GuiCitationDialog::GuiCitationDialog(Dialog & dialog, GuiCitation * form)
47 : Dialog::View(dialog, _("Citation")), form_(form)
51 setWindowTitle(toqstr("LyX: " + getTitle()));
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(this, SIGNAL(rejected()), this, SLOT(cleanUp()));
68 new GuiSelectionManager(availableLV, selectedLV,
69 addPB, deletePB, upPB, downPB,
70 form_->available(), form_->selected());
71 connect(selectionManager, SIGNAL(selectionChanged()),
72 this, SLOT(setCitedKeys()));
73 connect(selectionManager, SIGNAL(updateHook()),
74 this, SLOT(updateDialog()));
75 connect(selectionManager, SIGNAL(okHook()),
76 this, SLOT(on_okPB_clicked()));
81 GuiCitationDialog::~GuiCitationDialog()
85 void GuiCitationDialog::cleanUp()
87 form_->clearSelection();
93 void GuiCitationDialog::closeEvent(QCloseEvent * e)
95 form_->clearSelection();
101 void GuiCitationDialog::apply()
103 int const choice = std::max(0, citationStyleCO->currentIndex());
105 bool const full = fulllistCB->isChecked();
106 bool const force = forceuppercaseCB->isChecked();
108 QString const before = textBeforeED->text();
109 QString const after = textAfterED->text();
111 form_->apply(choice, full, force, before, after);
115 void GuiCitationDialog::hide()
117 form_->clearParams();
122 void GuiCitationDialog::show()
125 availableLV->setFocus();
132 bool GuiCitationDialog::isVisible() const
134 return QDialog::isVisible();
138 void GuiCitationDialog::on_okPB_clicked()
141 form_->clearSelection();
146 void GuiCitationDialog::on_cancelPB_clicked()
148 form_->clearSelection();
153 void GuiCitationDialog::on_applyPB_clicked()
159 void GuiCitationDialog::on_restorePB_clicked()
166 void GuiCitationDialog::update()
174 //The main point of separating this out is that the fill*() methods
175 //called in update() do not need to be called for INTERNAL updates,
176 //such as when addPB is pressed, as the list of fields, entries, etc,
177 //will not have changed. At the moment, however, the division between
178 //fillStyles() and updateStyles() doesn't lend itself to dividing the
179 //two methods, though they should be divisible.
180 void GuiCitationDialog::updateDialog()
182 if (selectionManager->selectedFocused()) {
183 if (selectedLV->selectionModel()->selectedIndexes().isEmpty())
184 updateInfo(availableLV->currentIndex());
186 updateInfo(selectedLV->currentIndex());
188 if (availableLV->selectionModel()->selectedIndexes().isEmpty())
189 updateInfo(QModelIndex());
191 updateInfo(availableLV->currentIndex());
195 textBeforeED->setText(form_->textBefore());
196 textAfterED->setText(form_->textAfter());
202 void GuiCitationDialog::updateStyle()
204 biblio::CiteEngine const engine = form_->getEngine();
205 bool const natbib_engine =
206 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
207 engine == biblio::ENGINE_NATBIB_NUMERICAL;
208 bool const basic_engine = engine == biblio::ENGINE_BASIC;
210 bool const haveSelection =
211 selectedLV->model()->rowCount() > 0;
212 fulllistCB->setEnabled(natbib_engine && haveSelection);
213 forceuppercaseCB->setEnabled(natbib_engine && haveSelection);
214 textBeforeED->setEnabled(!basic_engine && haveSelection);
215 textBeforeLA->setEnabled(!basic_engine && haveSelection);
216 textAfterED->setEnabled(haveSelection);
217 textAfterLA->setEnabled(haveSelection);
218 citationStyleCO->setEnabled(!basic_engine && haveSelection);
219 citationStyleLA->setEnabled(!basic_engine && haveSelection);
221 string const & command = form_->params().getCmdName();
223 // Find the style of the citekeys
224 vector<biblio::CiteStyle> const & styles =
225 ControlCitation::getCiteStyles();
226 biblio::CitationStyle const cs(command);
228 vector<biblio::CiteStyle>::const_iterator cit =
229 std::find(styles.begin(), styles.end(), cs.style);
231 // restore the latest natbib style
232 if (style_ >= 0 && style_ < citationStyleCO->count())
233 citationStyleCO->setCurrentIndex(style_);
235 citationStyleCO->setCurrentIndex(0);
237 if (cit != styles.end()) {
238 int const i = int(cit - styles.begin());
239 citationStyleCO->setCurrentIndex(i);
240 fulllistCB->setChecked(cs.full);
241 forceuppercaseCB->setChecked(cs.forceUCase);
243 fulllistCB->setChecked(false);
244 forceuppercaseCB->setChecked(false);
249 //This one needs to be called whenever citationStyleCO needs
250 //to be updated---and this would be on anything that changes the
251 //selection in selectedLV, or on a general update.
252 void GuiCitationDialog::fillStyles()
254 int const oldIndex = citationStyleCO->currentIndex();
256 citationStyleCO->clear();
258 QStringList selected_keys = form_->selected()->stringList();
259 if (selected_keys.empty()) {
260 citationStyleCO->setEnabled(false);
261 citationStyleLA->setEnabled(false);
265 int curr = selectedLV->model()->rowCount() - 1;
269 if (!selectedLV->selectionModel()->selectedIndexes().empty())
270 curr = selectedLV->selectionModel()->selectedIndexes()[0].row();
272 QStringList sty = form_->citationStyles(curr);
274 bool const basic_engine =
275 (form_->getEngine() == biblio::ENGINE_BASIC);
277 citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
278 citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
280 if (sty.isEmpty() || basic_engine)
283 citationStyleCO->insertItems(0, sty);
285 if (oldIndex != -1 && oldIndex < citationStyleCO->count())
286 citationStyleCO->setCurrentIndex(oldIndex);
290 void GuiCitationDialog::fillFields()
292 fieldsCO->blockSignals(true);
293 int const oldIndex = fieldsCO->currentIndex();
295 QStringList const & fields = form_->getFieldsAsQStringList();
296 fieldsCO->insertItem(0, qt_("All Fields"));
297 fieldsCO->insertItem(1, qt_("Keys"));
298 fieldsCO->insertItems(2, fields);
299 if (oldIndex != -1 && oldIndex < fieldsCO->count())
300 fieldsCO->setCurrentIndex(oldIndex);
301 fieldsCO->blockSignals(false);
305 void GuiCitationDialog::fillEntries()
307 entriesCO->blockSignals(true);
308 int const oldIndex = entriesCO->currentIndex();
310 QStringList const & entries = form_->getEntriesAsQStringList();
311 entriesCO->insertItem(0, qt_("All Entry Types"));
312 entriesCO->insertItems(1, entries);
313 if (oldIndex != -1 && oldIndex < entriesCO->count())
314 entriesCO->setCurrentIndex(oldIndex);
315 entriesCO->blockSignals(false);
319 bool GuiCitationDialog::isSelected(const QModelIndex & idx)
321 QString const str = idx.data().toString();
322 return form_->selected()->stringList().contains(str);
326 void GuiCitationDialog::setButtons()
328 selectionManager->update();
329 int const srows = selectedLV->model()->rowCount();
330 applyPB->setEnabled(srows > 0);
331 okPB->setEnabled(srows > 0);
335 void GuiCitationDialog::updateInfo(QModelIndex const & idx)
338 QString const keytxt = form_->getKeyInfo(idx.data().toString());
339 infoML->document()->setPlainText(keytxt);
341 infoML->document()->clear();
345 void GuiCitationDialog::setCitedKeys()
347 form_->setCitedKeys();
351 void GuiCitationDialog::findText(QString const & text, bool reset)
353 //"All Fields" and "Keys" are the first two
354 int index = fieldsCO->currentIndex() - 2;
355 vector<docstring> const & fields = form_->availableFields();
358 if (index <= -1 || index >= int(fields.size()))
359 //either "All Fields" or "Keys" or an invalid value
360 field = from_ascii("");
362 field = fields[index];
365 bool const onlyKeys = index == -1;
367 //"All Entry Types" is first.
368 index = entriesCO->currentIndex() - 1;
369 vector<docstring> const & entries = form_->availableEntries();
371 if (index < 0 || index >= int(entries.size()))
372 entryType = from_ascii("");
374 entryType = entries[index];
376 bool const case_sentitive = caseCB->checkState();
377 bool const reg_exp = regexCB->checkState();
378 form_->findKey(text, onlyKeys, field, entryType,
379 case_sentitive, reg_exp, reset);
381 //It'd be nice to save and restore the current selection in
382 //availableLV. Currently, we get an automatic reset, since the
389 void GuiCitationDialog::on_fieldsCO_currentIndexChanged(int /*index*/)
391 findText(findLE->text(), true);
395 void GuiCitationDialog::on_entriesCO_currentIndexChanged(int /*index*/)
397 findText(findLE->text(), true);
401 void GuiCitationDialog::on_findLE_textChanged(const QString & text)
403 clearPB->setDisabled(text.isEmpty());
410 void GuiCitationDialog::on_caseCB_stateChanged(int)
412 findText(findLE->text());
416 void GuiCitationDialog::on_regexCB_stateChanged(int)
418 findText(findLE->text());
422 void GuiCitationDialog::changed()
429 } // namespace frontend
432 #include "GuiCitationDialog_moc.cpp"