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