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