]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitationDialog.C
bb0e07c516ec8f14f6275d22725b4fbec20f9899
[lyx.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 }
144
145
146 void QCitationDialog::slotDelClicked()
147 {
148         int const sel = citeLB->currentItem();
149
150         // FIXME: why ? 
151         if (sel < 0 || sel >= (int)form_->citekeys.size()) {
152                 return;
153         }
154
155         // Remove the selected key from browser_cite
156         citeLB->removeItem(sel);
157         form_->citekeys.erase(form_->citekeys.begin() + sel);
158
159         form_->setBibButtons(QCitation::ON);
160         form_->setCiteButtons(QCitation::OFF);
161 }
162
163
164 void QCitationDialog::slotUpClicked()
165 {
166         int const sel = citeLB->currentItem();
167
168         // FIXME: why ?
169         if (sel < 1 || sel >= (int)form_->citekeys.size()) {
170                 return;
171         }
172
173         // Move the selected key up one line
174         vector<string>::iterator it = form_->citekeys.begin() + sel;
175         string const tmp = *it;
176
177         citeLB->removeItem(sel);
178         form_->citekeys.erase(it);
179
180         citeLB->insertItem(tmp.c_str(), sel - 1);
181         citeLB->setSelected(sel - 1, true);
182         form_->citekeys.insert(it - 1, tmp);
183         form_->setCiteButtons(QCitation::ON);
184 }
185
186
187 void QCitationDialog::slotDownClicked()
188 {
189         int const sel = citeLB->currentItem();
190
191         // FIXME: ? 
192         if (sel < 0 || sel >= (int)form_->citekeys.size() - 1) {
193                 return;
194         }
195
196         // Move the selected key down one line
197         vector<string>::iterator it = form_->citekeys.begin() + sel;
198         string const tmp = *it;
199
200         citeLB->removeItem(sel);
201         form_->citekeys.erase(it);
202
203         citeLB->insertItem(tmp.c_str(), sel + 1);
204         citeLB->setSelected(sel + 1, true);
205         form_->citekeys.insert(it + 1, tmp);
206         form_->setCiteButtons(QCitation::ON);
207 }
208
209
210 void QCitationDialog::slotPreviousClicked()
211 {
212         doFind(biblio::BACKWARD);
213 }
214
215
216 void QCitationDialog::slotNextClicked()
217 {
218         doFind(biblio::FORWARD);
219 }
220
221
222 void QCitationDialog::doFind(biblio::Direction const dir)
223 {
224         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
225         string const str = searchED->text().latin1();
226
227         biblio::Search const type =
228                 searchTypeCB->isChecked() ?
229                 biblio::REGEX : biblio::SIMPLE;
230
231         vector<string>::const_iterator start = form_->bibkeys.begin();
232         int const sel = bibLB->currentItem();
233         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
234                 start += sel;
235
236         // Find the NEXT instance...
237         if (dir == biblio::FORWARD)
238                 start += 1;
239         else
240                 start -= 1;
241
242         bool const caseSensitive = searchCaseCB->isChecked();
243         
244         vector<string>::const_iterator const cit =
245                 biblio::searchKeys(theMap, form_->bibkeys, str,
246                            start, type, dir, caseSensitive);
247
248         if (cit == form_->bibkeys.end()) {
249                 return;
250         }
251
252         int const found = int(cit - form_->bibkeys.begin());
253         if (found == sel) {
254                 return;
255         }
256
257         // Update the display
258         int const top = max(found - 5, 1);
259         bibLB->setTopItem(top);
260         bibLB->setSelected(found, true);
261         slotBibSelected(0);
262 }
263
264
265 void QCitationDialog::slotCitationStyleSelected( int )
266 {
267 }
268
269
270 void QCitationDialog::slotTextBeforeReturn()
271 {
272 }
273
274
275 void QCitationDialog::slotTextAfterReturn()
276 {
277 }