]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitationDialog.C
Joao latest bits
[lyx.git] / src / frontends / qt2 / QCitationDialog.C
1 /**
2  * \file QCitationDialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Kalle Dalheimer
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "qt_helpers.h"
14 #include "controllers/ControlCitation.h"
15
16 #include <qcheckbox.h>
17 #include <qlineedit.h>
18 #include <qlistbox.h>
19 #include <qmultilineedit.h>
20 #include <qpushbutton.h>
21
22 #include "ui/QCitationFindDialogBase.h"
23 #include "QCitationDialog.h"
24 #include "QCitation.h"
25
26 using std::vector;
27 using std::string;
28
29
30 QCitationDialog::QCitationDialog(QCitation * form)
31         : QCitationDialogBase(0, 0, false, 0),
32         form_(form)
33 {
34         connect(restorePB, SIGNAL(clicked()),
35                 form, SLOT(slotRestore()));
36         connect(okPB, SIGNAL(clicked()),
37                 form, SLOT(slotOK()));
38         connect(applyPB, SIGNAL(clicked()),
39                 form, SLOT(slotApply()));
40         connect(closePB, SIGNAL(clicked()),
41                 form, SLOT(slotClose()));
42
43         add_ = new QCitationFindDialogBase(this, "", true);
44         connect(add_->previousPB, SIGNAL(clicked()), this, SLOT(previous()));
45         connect(add_->nextPB, SIGNAL(clicked()), this, SLOT(next()));
46         connect(add_->availableLB, SIGNAL(currentChanged(QListBoxItem *)), this, SLOT(availableChanged()));
47         connect(add_->availableLB, SIGNAL(selected(QListBoxItem *)), this, SLOT(addCitation()));
48         connect(add_->availableLB, SIGNAL(selected(QListBoxItem *)), add_, SLOT(accept()));
49         connect(add_->addPB, SIGNAL(clicked()), this, SLOT(addCitation()));
50         connect(selectedLB, SIGNAL(returnPressed(QListBoxItem *)), form, SLOT(slotOK()));
51 }
52
53
54 QCitationDialog::~QCitationDialog()
55 {
56 }
57
58
59 void QCitationDialog::setButtons()
60 {
61         if (form_->readOnly())
62                 return;
63
64         int const sel_nr = selectedLB->currentItem();
65         int const avail_nr = add_->availableLB->currentItem();
66
67         add_->addPB->setEnabled(avail_nr >= 0);
68         deletePB->setEnabled(sel_nr >= 0);
69         upPB->setEnabled(sel_nr > 0);
70         downPB->setEnabled(sel_nr >= 0 && sel_nr < int(selectedLB->count() - 1));
71 }
72
73
74 void QCitationDialog::selectedChanged()
75 {
76         form_->fillStyles();
77         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
78         infoML->clear();
79
80         int const sel = selectedLB->currentItem();
81         if (sel < 0) {
82                 setButtons();
83                 return;
84         }
85
86         infoML->setText(toqstr(biblio::getInfo(theMap, form_->citekeys[sel])));
87         setButtons();
88 }
89
90
91 void QCitationDialog::previous()
92 {
93         find(biblio::BACKWARD);
94 }
95
96
97 void QCitationDialog::next()
98 {
99         find(biblio::FORWARD);
100 }
101
102
103 void QCitationDialog::availableChanged()
104 {
105         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
106         add_->infoML->clear();
107
108         int const sel = add_->availableLB->currentItem();
109         if (sel < 0) {
110                 setButtons();
111                 return;
112         }
113
114         add_->infoML->setText(toqstr(biblio::getInfo(theMap, form_->bibkeys[sel])));
115         setButtons();
116 }
117
118
119 void QCitationDialog::addCitation()
120 {
121         int const sel = add_->availableLB->currentItem();
122
123         if (sel < 0)
124                 return;
125
126         // Add the selected browser_bib keys to browser_cite
127         // multiple selections are possible
128         for (unsigned int i = 0; i != add_->availableLB->count(); i++) {
129                 if (add_->availableLB->isSelected(i)) {
130                         // do not allow duplicates
131                         if ((selectedLB->findItem(add_->availableLB->text(i))) == 0) {
132                                 selectedLB->insertItem(toqstr(form_->bibkeys[i]));
133                                 form_->citekeys.push_back(form_->bibkeys[i]);
134                         }
135                 }
136         }
137
138         int const n = int(form_->citekeys.size());
139         selectedLB->setSelected(n - 1, true);
140
141         form_->changed();
142         form_->fillStyles();
143         setButtons();
144 }
145
146
147 void QCitationDialog::del()
148 {
149         int const sel = selectedLB->currentItem();
150
151         // Remove the selected key from browser_cite
152         selectedLB->removeItem(sel);
153         form_->citekeys.erase(form_->citekeys.begin() + sel);
154
155         form_->changed();
156         form_->fillStyles();
157         setButtons();
158 }
159
160
161 void QCitationDialog::up()
162 {
163         int const sel = selectedLB->currentItem();
164
165         // Move the selected key up one line
166         vector<string>::iterator it = form_->citekeys.begin() + sel;
167         string const tmp = *it;
168
169         selectedLB->removeItem(sel);
170         form_->citekeys.erase(it);
171
172         selectedLB->insertItem(toqstr(tmp), sel - 1);
173         selectedLB->setSelected(sel - 1, true);
174         form_->citekeys.insert(it - 1, tmp);
175
176         form_->changed();
177         form_->fillStyles();
178         setButtons();
179 }
180
181
182 void QCitationDialog::down()
183 {
184         int const sel = selectedLB->currentItem();
185
186         // Move the selected key down one line
187         vector<string>::iterator it = form_->citekeys.begin() + sel;
188         string const tmp = *it;
189
190         selectedLB->removeItem(sel);
191         form_->citekeys.erase(it);
192
193         selectedLB->insertItem(toqstr(tmp), sel + 1);
194         selectedLB->setSelected(sel + 1, true);
195         form_->citekeys.insert(it + 1, tmp);
196
197         form_->changed();
198         form_->fillStyles();
199         setButtons();
200 }
201
202
203 void QCitationDialog::add()
204 {
205         add_->exec();
206 }
207
208
209 void QCitationDialog::changed_adaptor()
210 {
211         form_->changed();
212 }
213
214
215 void QCitationDialog::find(biblio::Direction dir)
216 {
217         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
218
219         biblio::Search const type = add_->searchTypeCB->isChecked()
220                 ? biblio::REGEX : biblio::SIMPLE;
221
222         vector<string>::const_iterator start = form_->bibkeys.begin();
223         int const sel = add_->availableLB->currentItem();
224         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
225                 start += sel;
226
227         // Find the NEXT instance...
228         if (dir == biblio::FORWARD)
229                 start += 1;
230         else
231                 start -= 1;
232
233         bool const casesens = add_->searchCaseCB->isChecked();
234         string const str = fromqstr(add_->searchED->text());
235
236         vector<string>::const_iterator cit =
237                 biblio::searchKeys(theMap, form_->bibkeys, str,
238                                    start, type, dir, casesens);
239
240         // not found. let's loop round
241         if (cit == form_->bibkeys.end()) {
242                 if (dir == biblio::FORWARD) {
243                         start = form_->bibkeys.begin();
244                 }
245                 else start = form_->bibkeys.end() - 1;
246
247                 cit = biblio::searchKeys(theMap, form_->bibkeys, str,
248                                          start, type, dir, casesens);
249
250                 if (cit == form_->bibkeys.end())
251                         return;
252         }
253
254         int const found = int(cit - form_->bibkeys.begin());
255         if (found == sel) {
256                 return;
257         }
258
259         // Update the display
260         add_->availableLB->setSelected(found, true);
261         add_->availableLB->ensureCurrentVisible();
262 }