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