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 "qt_helpers.h"
16 #include "controllers/ControlCitation.h"
17 #include "ButtonController.h"
19 #include <qcheckbox.h>
20 #include <qlineedit.h>
22 #include <qmultilineedit.h>
23 #include <qpushbutton.h>
25 #include "ui/QCitationFindDialogBase.h"
26 #include "QCitationDialog.h"
27 #include "QCitation.h"
33 QCitationDialog::QCitationDialog(QCitation * form)
34 : QCitationDialogBase(0, 0, false, 0),
37 connect(restorePB, SIGNAL(clicked()),
38 form, SLOT(slotRestore()));
39 connect(okPB, SIGNAL(clicked()),
40 form, SLOT(slotOK()));
41 connect(applyPB, SIGNAL(clicked()),
42 form, SLOT(slotApply()));
43 connect(closePB, SIGNAL(clicked()),
44 form, SLOT(slotClose()));
46 add_ = new QCitationFindDialogBase(this, "", true);
47 connect(add_->previousPB, SIGNAL(clicked()), this, SLOT(previous()));
48 connect(add_->nextPB, SIGNAL(clicked()), this, SLOT(next()));
49 connect(add_->availableLB, SIGNAL(currentChanged(QListBoxItem *)), this, SLOT(availableChanged()));
50 connect(add_->availableLB, SIGNAL(selected(QListBoxItem *)), this, SLOT(addCitation()));
51 connect(add_->availableLB, SIGNAL(selected(QListBoxItem *)), add_, SLOT(accept()));
52 connect(add_->addPB, SIGNAL(clicked()), this, SLOT(addCitation()));
53 connect(selectedLB, SIGNAL(returnPressed(QListBoxItem *)), form, SLOT(slotOK()));
57 QCitationDialog::~QCitationDialog()
62 void QCitationDialog::setButtons()
64 if (form_->readOnly())
67 int const sel_nr = selectedLB->currentItem();
68 int const avail_nr = add_->availableLB->currentItem();
70 add_->addPB->setEnabled(avail_nr >= 0);
71 deletePB->setEnabled(sel_nr >= 0);
72 upPB->setEnabled(sel_nr > 0);
73 downPB->setEnabled(sel_nr >= 0 && sel_nr < int(selectedLB->count() - 1));
77 void QCitationDialog::openFind()
79 if (form_->readOnly())
82 if (selectedLB->count() == 0 && add_->availableLB->count() != 0){
83 // open the find dialog
85 // and let the user press ok after a selection
86 if (selectedLB->count() != 0)
92 void QCitationDialog::selectedChanged()
95 biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
98 int const sel = selectedLB->currentItem();
106 toqstr(biblio::getInfo(theMap, form_->citekeys[sel])));
111 void QCitationDialog::previous()
113 find(biblio::BACKWARD);
117 void QCitationDialog::next()
119 find(biblio::FORWARD);
123 void QCitationDialog::availableChanged()
125 biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
126 add_->infoML->clear();
128 int const sel = add_->availableLB->currentItem();
135 add_->infoML->setText(
136 toqstr(biblio::getInfo(theMap, form_->bibkeys[sel])));
141 void QCitationDialog::addCitation()
143 int const sel = add_->availableLB->currentItem();
148 // Add the selected browser_bib keys to browser_cite
149 // multiple selections are possible
150 for (unsigned int i = 0; i != add_->availableLB->count(); i++) {
151 if (add_->availableLB->isSelected(i)) {
152 // do not allow duplicates
153 if ((selectedLB->findItem(add_->availableLB->text(i))) == 0) {
154 selectedLB->insertItem(toqstr(form_->bibkeys[i]));
155 form_->citekeys.push_back(form_->bibkeys[i]);
160 int const n = int(form_->citekeys.size());
161 selectedLB->setSelected(n - 1, true);
169 void QCitationDialog::del()
171 int const sel = selectedLB->currentItem();
173 // Remove the selected key from browser_cite
174 selectedLB->removeItem(sel);
175 form_->citekeys.erase(form_->citekeys.begin() + sel);
183 void QCitationDialog::up()
185 int const sel = selectedLB->currentItem();
187 // Move the selected key up one line
188 vector<string>::iterator it = form_->citekeys.begin() + sel;
189 string const tmp = *it;
191 selectedLB->removeItem(sel);
192 form_->citekeys.erase(it);
194 selectedLB->insertItem(toqstr(tmp), sel - 1);
195 selectedLB->setSelected(sel - 1, true);
196 form_->citekeys.insert(it - 1, tmp);
204 void QCitationDialog::down()
206 int const sel = selectedLB->currentItem();
208 // Move the selected key down one line
209 vector<string>::iterator it = form_->citekeys.begin() + sel;
210 string const tmp = *it;
212 selectedLB->removeItem(sel);
213 form_->citekeys.erase(it);
215 selectedLB->insertItem(toqstr(tmp), sel + 1);
216 selectedLB->setSelected(sel + 1, true);
217 form_->citekeys.insert(it + 1, tmp);
225 void QCitationDialog::add()
231 void QCitationDialog::changed_adaptor()
237 void QCitationDialog::find(biblio::Direction dir)
239 biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
241 biblio::Search const type = add_->searchTypeCB->isChecked()
242 ? biblio::REGEX : biblio::SIMPLE;
244 vector<string>::const_iterator start = form_->bibkeys.begin();
245 int const sel = add_->availableLB->currentItem();
246 if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
249 // Find the NEXT instance...
250 if (dir == biblio::FORWARD)
255 bool const casesens = add_->searchCaseCB->isChecked();
256 string const str = fromqstr(add_->searchED->text());
258 vector<string>::const_iterator cit =
259 biblio::searchKeys(theMap, form_->bibkeys, str,
260 start, type, dir, casesens);
262 // not found. let's loop round
263 if (cit == form_->bibkeys.end()) {
264 if (dir == biblio::FORWARD) {
265 start = form_->bibkeys.begin();
267 else start = form_->bibkeys.end() - 1;
269 cit = biblio::searchKeys(theMap, form_->bibkeys, str,
270 start, type, dir, casesens);
272 if (cit == form_->bibkeys.end())
276 int const found = int(cit - form_->bibkeys.begin());
281 // Update the display
282 add_->availableLB->setSelected(found, true);
283 add_->availableLB->ensureCurrentVisible();