]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/FormCitation.C
started moving citation dialog to MVC
[lyx.git] / src / frontends / qt2 / FormCitation.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  *
5  *           LyX, The Document Processor
6  *
7  *           Copyright 2000 The LyX Team.
8  *
9  * ======================================================
10  *
11  * \author Angus Leeming <a.leeming@ic.ac.uk>
12  */
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "FormCitationDialogImpl.h"
21 #include "FormCitation.h"
22
23 #include <qcombobox.h>
24 #include <qlineedit.h>
25 #include <qlistbox.h>
26 #include <qmultilineedit.h>
27 #include <qpushbutton.h>
28
29 #undef emit
30 #include "qt2BC.h"
31 #include "ControlCitation.h"
32 #include "gettext.h"
33 #include "support/lstrings.h"
34 #include "biblio.h"
35 #include "helper_funcs.h"
36
37
38 using std::find;
39 using std::max;
40 using std::min;
41 using std::pair;
42 using std::sort;
43 using std::vector;
44
45 typedef Qt2CB<ControlCitation, Qt2DB<FormCitationDialogImpl> > base_class;
46
47 FormCitation::FormCitation(ControlCitation & c)
48     : base_class(c, _("Citation"))
49 {}
50
51
52 void FormCitation::apply()
53 {
54     controller().params().setCmdName("cite");
55     controller().params().setContents(getStringFromVector(citekeys));
56     
57     string const after  = dialog_->textAfterED->text().latin1();
58     controller().params().setOptions(after);
59 }
60
61
62 void FormCitation::hide()
63 {
64     citekeys.clear();
65     bibkeys.clear();
66     
67     Qt2Base::hide();
68 }
69
70
71 void FormCitation::build()
72 {
73     // PENDING(kalle) Parent?
74     dialog_.reset( new FormCitationDialogImpl( this ));
75
76     dialog_->searchTypePB->setOn( false );
77     dialog_->searchTypePB->setText( _( "Simple" ) );
78     
79     // Manage the ok, apply, restore and cancel/close buttons
80     bc().setOK(dialog_->okPB);
81     bc().setApply(dialog_->applyPB);
82     bc().setCancel(dialog_->cancelPB);
83     bc().setUndoAll(dialog_->restorePB);
84
85     bc().addReadOnly(dialog_->addPB);
86     bc().addReadOnly(dialog_->delPB);
87     bc().addReadOnly(dialog_->upPB);
88     bc().addReadOnly(dialog_->downPB);
89     bc().addReadOnly(dialog_->citationStyleCO);
90     bc().addReadOnly(dialog_->textBeforeED);
91     bc().addReadOnly(dialog_->textAfterED);
92
93     bc().refresh();
94 }       
95
96 #if K
97 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
98 {
99     ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
100     
101     biblio::InfoMap const & theMap = controller().bibkeysInfo();
102     
103     if (ob == dialog_->browser_bib) {
104         fl_deselect_browser(dialog_->browser_cite);
105         
106         unsigned int const sel = fl_get_browser(dialog_->browser_bib);
107         if (sel < 1 || sel > bibkeys.size())
108             return ButtonPolicy::SMI_NOOP;
109         
110         // Put into browser_info the additional info associated with
111         // the selected browser_bib key
112         fl_clear_browser(dialog_->browser_info);
113         
114         string const tmp = formatted(biblio::getInfo(theMap,
115                                                      bibkeys[sel-1]),
116                                      dialog_->browser_info->w-10 );
117         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
118
119         // Highlight the selected browser_bib key in browser_cite if
120         // present
121         vector<string>::const_iterator cit =
122             find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
123         
124         if (cit != citekeys.end()) {
125             int const n = int(cit - citekeys.begin());
126             fl_select_browser_line(dialog_->browser_cite, n+1);
127             fl_set_browser_topline(dialog_->browser_cite, n+1);
128         }
129         
130         if (!controller().isReadonly()) {
131             if (cit != citekeys.end()) {
132                 setBibButtons(OFF);
133                 setCiteButtons(ON);
134             } else {
135                 setBibButtons(ON);
136                 setCiteButtons(OFF);
137             }
138         }
139         
140     } else if (ob == dialog_->browser_cite) {
141         unsigned int const sel = fl_get_browser(dialog_->browser_cite);
142         if (sel < 1 || sel > citekeys.size())
143             return ButtonPolicy::SMI_NOOP;
144         
145         if (!controller().isReadonly()) {
146             setBibButtons(OFF);
147             setCiteButtons(ON);
148         }
149         
150         // Highlight the selected browser_cite key in browser_bib
151         vector<string>::const_iterator cit =
152             find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
153         
154         if (cit != bibkeys.end()) {
155             int const n = int(cit - bibkeys.begin());
156             fl_select_browser_line(dialog_->browser_bib, n+1);
157             fl_set_browser_topline(dialog_->browser_bib, n+1);
158             
159             // Put into browser_info the additional info associated
160             // with the selected browser_cite key
161             fl_clear_browser(dialog_->browser_info);
162             string const tmp =
163                 formatted(biblio::getInfo(theMap,
164                                           bibkeys[sel-1]),
165                           dialog_->browser_info->w-10);
166             fl_add_browser_line(dialog_->browser_info, tmp.c_str());
167         }
168
169     } else if (ob == dialog_->button_add) {
170         unsigned int const sel = fl_get_browser(dialog_->browser_bib);
171         if (sel < 1 || sel > bibkeys.size())
172             return ButtonPolicy::SMI_NOOP;
173         
174         // Add the selected browser_bib key to browser_cite
175         fl_addto_browser(dialog_->browser_cite,
176                          bibkeys[sel-1].c_str());
177         citekeys.push_back(bibkeys[sel-1]);
178
179         int const n = int(citekeys.size());
180         fl_select_browser_line(dialog_->browser_cite, n);
181         
182         setBibButtons(OFF);
183         setCiteButtons(ON);
184         activate = ButtonPolicy::SMI_VALID;
185         
186     } else if (ob == dialog_->button_del) {
187         unsigned int const sel = fl_get_browser(dialog_->browser_cite);
188         if (sel < 1 || sel > citekeys.size())
189             return ButtonPolicy::SMI_NOOP;
190         
191         // Remove the selected key from browser_cite
192         fl_delete_browser_line(dialog_->browser_cite, sel) ;
193         citekeys.erase(citekeys.begin() + sel-1);
194         
195         setBibButtons(ON);
196         setCiteButtons(OFF);
197         activate = ButtonPolicy::SMI_VALID;
198         
199     } else if (ob == dialog_->button_up) {
200         unsigned int const sel = fl_get_browser(dialog_->browser_cite);
201         if (sel < 2 || sel > citekeys.size())
202             return ButtonPolicy::SMI_NOOP;
203         
204         // Move the selected key up one line
205         vector<string>::iterator it = citekeys.begin() + sel-1;
206         string const tmp = *it;
207         
208         fl_delete_browser_line(dialog_->browser_cite, sel);
209         citekeys.erase(it);
210         
211         fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
212         fl_select_browser_line(dialog_->browser_cite, sel-1);
213         citekeys.insert(it-1, tmp);
214         setCiteButtons(ON);
215         activate = ButtonPolicy::SMI_VALID;
216         
217     } else if (ob == dialog_->button_down) {
218         unsigned int const sel = fl_get_browser(dialog_->browser_cite);
219         if (sel < 1 || sel > citekeys.size()-1)
220             return ButtonPolicy::SMI_NOOP;
221         
222         // Move the selected key down one line
223         vector<string>::iterator it = citekeys.begin() + sel-1;
224         string const tmp = *it;
225         
226         fl_delete_browser_line(dialog_->browser_cite, sel);
227         citekeys.erase(it);
228         
229         fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
230         fl_select_browser_line(dialog_->browser_cite, sel+1);
231         citekeys.insert(it+1, tmp);
232         setCiteButtons(ON);
233         activate = ButtonPolicy::SMI_VALID;
234         
235     } else if (ob == dialog_->button_search_type) {
236         if (fl_get_button(dialog_->button_search_type))
237             fl_set_object_label(dialog_->button_search_type,
238                                 _("Regex"));
239         else
240             fl_set_object_label(dialog_->button_search_type,
241                                 _("Simple"));
242         return ButtonPolicy::SMI_NOOP;
243         
244     } else if (ob == dialog_->button_previous ||
245                ob == dialog_->button_next) {
246         
247         string const str = fl_get_input(dialog_->input_search);
248         
249         biblio::Direction const dir =
250             (ob == dialog_->button_previous) ?
251             biblio::BACKWARD : biblio::FORWARD;
252         
253         biblio::Search const type =
254             fl_get_button(dialog_->button_search_type) ?
255             biblio::REGEX : biblio::SIMPLE;
256         
257         vector<string>::const_iterator start = bibkeys.begin();
258         int const sel = fl_get_browser(dialog_->browser_bib);
259         if (sel >= 1 && sel <= int(bibkeys.size()))
260             start += sel-1;
261         
262         // Find the NEXT instance...
263         if (dir == biblio::FORWARD)
264             start += 1;
265         else
266             start -= 1;
267         
268         vector<string>::const_iterator const cit =
269             biblio::searchKeys(theMap, bibkeys, str,
270                                start, type, dir);
271         
272         if (cit == bibkeys.end())
273             return ButtonPolicy::SMI_NOOP;
274         
275         int const found = int(cit - bibkeys.begin()) + 1;
276         if (found == sel)
277             return ButtonPolicy::SMI_NOOP;
278         
279         // Update the display
280         int const top = max(found-5, 1);
281         fl_set_browser_topline(dialog_->browser_bib, top);
282         fl_select_browser_line(dialog_->browser_bib, found);
283         input(dialog_->browser_bib, 0);
284         
285     } else if (ob == dialog_->choice_style ||
286                ob == dialog_->input_before ||
287                ob == dialog_->input_after) {
288         activate = ButtonPolicy::SMI_VALID;
289     }
290     
291     return activate;
292 }
293 #endif
294
295
296 void FormCitation::update()
297 {
298     // Make the list of all available bibliography keys
299     bibkeys = biblio::getKeys(controller().bibkeysInfo());
300     updateBrowser(dialog_->bibLB, bibkeys);
301     
302     // Ditto for the keys cited in this inset
303     citekeys = getVectorFromString(controller().params().getContents());
304     updateBrowser(dialog_->citeLB, citekeys);
305
306     // No keys have been selected yet, so...
307     dialog_->infoML->clear();
308     setBibButtons(OFF);
309     setCiteButtons(OFF);
310
311     int noKeys = int(max(bibkeys.size(), citekeys.size()));
312
313     // Place bounds, so that 4 <= noKeys <= 10
314     noKeys = max(4, min(10, noKeys));
315
316     dialog_->textAfterED->setText( controller().params().getOptions().c_str());
317 }
318
319
320 void FormCitation::updateBrowser( QListBox* browser,
321                                   vector<string> const & keys) const
322 {
323     browser->clear();
324
325     for (vector<string>::const_iterator it = keys.begin();
326          it < keys.end(); ++it) {
327         string key = frontStrip(strip(*it));
328         browser->insertItem( key.c_str() );
329     }
330 }
331
332
333 void FormCitation::setBibButtons(State status) const
334 {
335     dialog_->addPB->setEnabled( (status == ON) );
336 }
337
338
339 void FormCitation::setCiteButtons(State status) const
340 {
341     int const sel     = dialog_->citeLB->currentItem();
342     int const maxline = dialog_->citeLB->count()-1;
343     bool const activate      = (status == ON);
344     bool const activate_up   = (activate && sel != 0);
345     bool const activate_down = (activate && sel != maxline);
346
347     dialog_->delPB->setEnabled( activate );
348     dialog_->upPB->setEnabled( activate_up );
349     dialog_->downPB->setEnabled( activate_down );
350 }
351
352