]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitationDialog.C
Minipage is no more (long live the box inset)
[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         if (!theMap.empty())
87                 infoML->setText(
88                         toqstr(biblio::getInfo(theMap, form_->citekeys[sel])));
89         setButtons();
90 }
91
92
93 void QCitationDialog::previous()
94 {
95         find(biblio::BACKWARD);
96 }
97
98
99 void QCitationDialog::next()
100 {
101         find(biblio::FORWARD);
102 }
103
104
105 void QCitationDialog::availableChanged()
106 {
107         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
108         add_->infoML->clear();
109
110         int const sel = add_->availableLB->currentItem();
111         if (sel < 0) {
112                 setButtons();
113                 return;
114         }
115
116         if (!theMap.empty())
117                 add_->infoML->setText(
118                         toqstr(biblio::getInfo(theMap, form_->bibkeys[sel])));
119         setButtons();
120 }
121
122
123 void QCitationDialog::addCitation()
124 {
125         int const sel = add_->availableLB->currentItem();
126
127         if (sel < 0)
128                 return;
129
130         // Add the selected browser_bib keys to browser_cite
131         // multiple selections are possible
132         for (unsigned int i = 0; i != add_->availableLB->count(); i++) {
133                 if (add_->availableLB->isSelected(i)) {
134                         // do not allow duplicates
135                         if ((selectedLB->findItem(add_->availableLB->text(i))) == 0) {
136                                 selectedLB->insertItem(toqstr(form_->bibkeys[i]));
137                                 form_->citekeys.push_back(form_->bibkeys[i]);
138                         }
139                 }
140         }
141
142         int const n = int(form_->citekeys.size());
143         selectedLB->setSelected(n - 1, true);
144
145         form_->changed();
146         form_->fillStyles();
147         setButtons();
148 }
149
150
151 void QCitationDialog::del()
152 {
153         int const sel = selectedLB->currentItem();
154
155         // Remove the selected key from browser_cite
156         selectedLB->removeItem(sel);
157         form_->citekeys.erase(form_->citekeys.begin() + sel);
158
159         form_->changed();
160         form_->fillStyles();
161         setButtons();
162 }
163
164
165 void QCitationDialog::up()
166 {
167         int const sel = selectedLB->currentItem();
168
169         // Move the selected key up one line
170         vector<string>::iterator it = form_->citekeys.begin() + sel;
171         string const tmp = *it;
172
173         selectedLB->removeItem(sel);
174         form_->citekeys.erase(it);
175
176         selectedLB->insertItem(toqstr(tmp), sel - 1);
177         selectedLB->setSelected(sel - 1, true);
178         form_->citekeys.insert(it - 1, tmp);
179
180         form_->changed();
181         form_->fillStyles();
182         setButtons();
183 }
184
185
186 void QCitationDialog::down()
187 {
188         int const sel = selectedLB->currentItem();
189
190         // Move the selected key down one line
191         vector<string>::iterator it = form_->citekeys.begin() + sel;
192         string const tmp = *it;
193
194         selectedLB->removeItem(sel);
195         form_->citekeys.erase(it);
196
197         selectedLB->insertItem(toqstr(tmp), sel + 1);
198         selectedLB->setSelected(sel + 1, true);
199         form_->citekeys.insert(it + 1, tmp);
200
201         form_->changed();
202         form_->fillStyles();
203         setButtons();
204 }
205
206
207 void QCitationDialog::add()
208 {
209         add_->exec();
210 }
211
212
213 void QCitationDialog::changed_adaptor()
214 {
215         form_->changed();
216 }
217
218
219 void QCitationDialog::find(biblio::Direction dir)
220 {
221         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
222
223         biblio::Search const type = add_->searchTypeCB->isChecked()
224                 ? biblio::REGEX : biblio::SIMPLE;
225
226         vector<string>::const_iterator start = form_->bibkeys.begin();
227         int const sel = add_->availableLB->currentItem();
228         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
229                 start += sel;
230
231         // Find the NEXT instance...
232         if (dir == biblio::FORWARD)
233                 start += 1;
234         else
235                 start -= 1;
236
237         bool const casesens = add_->searchCaseCB->isChecked();
238         string const str = fromqstr(add_->searchED->text());
239
240         vector<string>::const_iterator cit =
241                 biblio::searchKeys(theMap, form_->bibkeys, str,
242                                    start, type, dir, casesens);
243
244         // not found. let's loop round
245         if (cit == form_->bibkeys.end()) {
246                 if (dir == biblio::FORWARD) {
247                         start = form_->bibkeys.begin();
248                 }
249                 else start = form_->bibkeys.end() - 1;
250
251                 cit = biblio::searchKeys(theMap, form_->bibkeys, str,
252                                          start, type, dir, casesens);
253
254                 if (cit == form_->bibkeys.end())
255                         return;
256         }
257
258         int const found = int(cit - form_->bibkeys.begin());
259         if (found == sel) {
260                 return;
261         }
262
263         // Update the display
264         add_->availableLB->setSelected(found, true);
265         add_->availableLB->ensureCurrentVisible();
266 }