]> git.lyx.org Git - features.git/blob - src/frontends/qt2/QCitationDialog.C
70e6e8254e29054d37e0e88d7af1aa74fc992451
[features.git] / src / frontends / qt2 / QCitationDialog.C
1 /**
2  * \file QCitationDialog.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Kalle Dalheimer <kalle@klaralvdalens-datakonsult.se>
7  */
8
9 #include <config.h>
10  
11 #include "QCitationDialog.h"
12 #include "Dialogs.h"
13 #include "QCitation.h"
14 #include "qt2BC.h"
15 #include "controllers/ControlCitation.h"
16
17 #include <qcheckbox.h>
18 #include <qlineedit.h>
19 #include <qlistbox.h>
20 #include <qmultilineedit.h>
21 #include <qpushbutton.h>
22  
23 #include "QtLyXView.h"
24
25 #include <algorithm>
26 #include "buffer.h"
27
28 using std::vector;
29 using std::find;
30 using std::max;
31
32 QCitationDialog::QCitationDialog(QCitation * form, QWidget * parent,  const char * name, bool modal, WFlags fl)
33         : QCitationDialogBase(parent, name, modal, fl),
34         form_(form)
35 {
36         connect(okPB, SIGNAL(clicked()),
37                 form, SLOT(slotOK()));
38         connect(cancelPB, SIGNAL(clicked()),
39                 form, SLOT(slotCancel()));
40         connect(restorePB, SIGNAL(clicked()),
41                 form, SLOT(slotRestore()));
42         connect(applyPB, SIGNAL(clicked()),
43                 form, SLOT(slotApply()));
44 }
45
46  
47 QCitationDialog::~QCitationDialog()
48 {
49 }
50
51
52 void QCitationDialog::slotBibSelected( int sel )
53 {
54         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
55
56         citeLB->clearSelection();
57
58         // FIXME: why would this happen ?
59         if (sel < 0 || sel >= (int)form_->bibkeys.size()) {
60                 return;
61         }
62
63         // Put into browser_info the additional info associated with
64         // the selected browser_bib key
65         infoML->clear();
66
67         infoML->setText(biblio::getInfo(theMap, form_->bibkeys[sel]).c_str());
68
69         // Highlight the selected browser_bib key in browser_cite if
70         // present
71         vector<string>::const_iterator cit =
72                 std::find(form_->citekeys.begin(), form_->citekeys.end(),
73                           form_->bibkeys[sel]);
74
75         if (cit != form_->citekeys.end()) {
76                 int const n = int(cit - form_->citekeys.begin());
77                 citeLB->setSelected(n, true);
78                 citeLB->setTopItem(n);
79         }
80
81         if (!form_->controller().isReadonly()) {
82                 if (cit != form_->citekeys.end()) {
83                         form_->setBibButtons(QCitation::OFF);
84                         form_->setCiteButtons(QCitation::ON);
85                 } else {
86                         form_->setBibButtons(QCitation::ON);
87                         form_->setCiteButtons(QCitation::OFF);
88                 }
89         }
90 }
91
92
93 void QCitationDialog::slotCiteSelected(int sel)
94 {
95         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
96
97         // FIXME: why would this happen ?
98         if (sel < 0 || sel >= (int)form_->citekeys.size()) {
99                 return;
100         }
101
102         if (!form_->controller().isReadonly()) {
103                 form_->setBibButtons(QCitation::OFF);
104                 form_->setCiteButtons(QCitation::ON);
105         }
106
107         // Highlight the selected browser_cite key in browser_bib
108         vector<string>::const_iterator cit =
109                 std::find(form_->bibkeys.begin(), 
110                 form_->bibkeys.end(), form_->citekeys[sel]);
111
112         if (cit != form_->bibkeys.end()) {
113                 int const n = int(cit - form_->bibkeys.begin());
114                 bibLB->setSelected(n, true);
115                 bibLB->setTopItem(n);
116
117                 // Put into browser_info the additional info associated
118                 // with the selected browser_cite key
119                 infoML->clear();
120                 infoML->setText(biblio::getInfo(theMap, form_->bibkeys[sel]).c_str());
121         }
122 }
123
124
125 void QCitationDialog::slotAddClicked()
126 {
127         int const sel = bibLB->currentItem();
128
129         // FIXME: why ?
130         if (sel < 0 || sel >= (int)form_->bibkeys.size()) {
131                 return;
132         }
133
134         // Add the selected browser_bib key to browser_cite
135         citeLB->insertItem(form_->bibkeys[sel].c_str());
136         form_->citekeys.push_back(form_->bibkeys[sel]);
137
138         int const n = int(form_->citekeys.size());
139         citeLB->setSelected(n - 1, true);
140
141         form_->setBibButtons(QCitation::OFF);
142         form_->setCiteButtons(QCitation::ON);
143         form_->changed();
144 }
145
146
147 void QCitationDialog::slotDelClicked()
148 {
149         int const sel = citeLB->currentItem();
150
151         // FIXME: why ? 
152         if (sel < 0 || sel >= (int)form_->citekeys.size()) {
153                 return;
154         }
155
156         // Remove the selected key from browser_cite
157         citeLB->removeItem(sel);
158         form_->citekeys.erase(form_->citekeys.begin() + sel);
159
160         form_->setBibButtons(QCitation::ON);
161         form_->setCiteButtons(QCitation::OFF);
162         form_->changed();
163 }
164
165
166 void QCitationDialog::slotUpClicked()
167 {
168         int const sel = citeLB->currentItem();
169
170         // FIXME: why ?
171         if (sel < 1 || sel >= (int)form_->citekeys.size()) {
172                 return;
173         }
174
175         // Move the selected key up one line
176         vector<string>::iterator it = form_->citekeys.begin() + sel;
177         string const tmp = *it;
178
179         citeLB->removeItem(sel);
180         form_->citekeys.erase(it);
181
182         citeLB->insertItem(tmp.c_str(), sel - 1);
183         citeLB->setSelected(sel - 1, true);
184         form_->citekeys.insert(it - 1, tmp);
185         form_->setCiteButtons(QCitation::ON);
186         form_->changed();
187 }
188
189
190 void QCitationDialog::slotDownClicked()
191 {
192         int const sel = citeLB->currentItem();
193
194         // FIXME: ? 
195         if (sel < 0 || sel >= (int)form_->citekeys.size() - 1) {
196                 return;
197         }
198
199         // Move the selected key down one line
200         vector<string>::iterator it = form_->citekeys.begin() + sel;
201         string const tmp = *it;
202
203         citeLB->removeItem(sel);
204         form_->citekeys.erase(it);
205
206         citeLB->insertItem(tmp.c_str(), sel + 1);
207         citeLB->setSelected(sel + 1, true);
208         form_->citekeys.insert(it + 1, tmp);
209         form_->setCiteButtons(QCitation::ON);
210         form_->changed();
211 }
212
213
214 void QCitationDialog::slotPreviousClicked()
215 {
216         doFind(biblio::BACKWARD);
217 }
218
219
220 void QCitationDialog::slotNextClicked()
221 {
222         doFind(biblio::FORWARD);
223 }
224
225
226 void QCitationDialog::doFind(biblio::Direction const dir)
227 {
228         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
229         string const str = searchED->text().latin1();
230
231         biblio::Search const type =
232                 searchTypeCB->isChecked() ?
233                 biblio::REGEX : biblio::SIMPLE;
234
235         vector<string>::const_iterator start = form_->bibkeys.begin();
236         int const sel = bibLB->currentItem();
237         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
238                 start += sel;
239
240         // Find the NEXT instance...
241         if (dir == biblio::FORWARD)
242                 start += 1;
243         else
244                 start -= 1;
245
246         bool const caseSensitive = searchCaseCB->isChecked();
247         
248         vector<string>::const_iterator const cit =
249                 biblio::searchKeys(theMap, form_->bibkeys, str,
250                            start, type, dir, caseSensitive);
251
252         if (cit == form_->bibkeys.end()) {
253                 return;
254         }
255
256         int const found = int(cit - form_->bibkeys.begin());
257         if (found == sel) {
258                 return;
259         }
260
261         // Update the display
262         int const top = max(found - 5, 1);
263         bibLB->setTopItem(top);
264         bibLB->setSelected(found, true);
265         slotBibSelected(0);
266 }
267
268
269 void QCitationDialog::slotCitationStyleSelected( int )
270 {
271         form_->changed();
272 }
273
274
275 void QCitationDialog::slotTextBeforeReturn()
276 {
277         form_->changed();
278 }
279
280
281 void QCitationDialog::slotTextAfterReturn()
282 {
283         form_->changed();
284 }