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