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