]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/FormCitation.C
Fix for qt2 citation dialog crash when selecting first key
[lyx.git] / src / frontends / qt2 / FormCitation.C
1 /* This file is part of
2  * ======================================================
3  *
4  *                 LyX, The Document Processor
5  *
6  *                 Copyright 2000 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \author Angus Leeming <a.leeming@ic.ac.uk>
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "FormCitationDialogImpl.h"
20 #include "FormCitation.h"
21
22 #include <qcheckbox.h>
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 "helper_funcs.h"
35
36
37 using std::find;
38 using std::max;
39 using std::min;
40 using std::pair;
41 using std::sort;
42 using std::vector;
43
44 typedef Qt2CB<ControlCitation, Qt2DB<FormCitationDialogImpl> > base_class;
45
46 FormCitation::FormCitation(ControlCitation & c)
47         : base_class(c, _("Citation"))
48 {}
49
50
51 void FormCitation::apply()
52 {
53         controller().params().setCmdName("cite");
54         controller().params().setContents(getStringFromVector(citekeys));
55         
56         string const after  = dialog_->textAfterED->text().latin1();
57         controller().params().setOptions(after);
58 }
59
60
61 void FormCitation::hide()
62 {
63         citekeys.clear();
64         bibkeys.clear();
65         
66         Qt2Base::hide();
67 }
68
69
70 void FormCitation::build()
71 {
72         // PENDING(kalle) Parent?
73         dialog_.reset( new FormCitationDialogImpl( this ));
74
75         dialog_->searchTypeCB->setChecked( false );
76         dialog_->searchCaseCB->setChecked( false );
77         
78         // Manage the ok, apply, restore and cancel/close buttons
79         bc().setOK(dialog_->okPB);
80         bc().setApply(dialog_->applyPB);
81         bc().setCancel(dialog_->cancelPB);
82         bc().setRestore(dialog_->restorePB);
83
84         bc().addReadOnly(dialog_->addPB);
85         bc().addReadOnly(dialog_->delPB);
86         bc().addReadOnly(dialog_->upPB);
87         bc().addReadOnly(dialog_->downPB);
88         bc().addReadOnly(dialog_->citationStyleCO);
89         bc().addReadOnly(dialog_->textBeforeED);
90         bc().addReadOnly(dialog_->textAfterED);
91 }       
92
93
94 void FormCitation::update()
95 {
96         // Make the list of all available bibliography keys
97         bibkeys = biblio::getKeys(controller().bibkeysInfo());
98         updateBrowser(dialog_->bibLB, bibkeys);
99         
100         // Ditto for the keys cited in this inset
101         citekeys = getVectorFromString(controller().params().getContents());
102         updateBrowser(dialog_->citeLB, citekeys);
103
104         // No keys have been selected yet, so...
105         dialog_->infoML->clear();
106         setBibButtons(OFF);
107         setCiteButtons(OFF);
108
109         dialog_->textAfterED->setText( controller().params().getOptions().c_str());
110 }
111
112
113 void FormCitation::updateBrowser( QListBox* browser,
114                                   vector<string> const & keys) const
115 {
116         browser->clear();
117
118         for (vector<string>::const_iterator it = keys.begin();
119                 it < keys.end(); ++it) {
120                 string key = frontStrip(strip(*it));
121                 if( !key.empty() )
122                         browser->insertItem( key.c_str() );
123         }
124 }
125
126
127 void FormCitation::setBibButtons(State status) const
128 {
129         dialog_->addPB->setEnabled( (status == ON) );
130 }
131
132
133 void FormCitation::setCiteButtons(State status) const
134 {
135         int const sel = dialog_->citeLB->currentItem();
136         int const maxline = dialog_->citeLB->count()-1;
137         bool const activate = (status == ON);
138         bool const activate_up = (activate && sel != 0);
139         bool const activate_down = (activate && sel != maxline);
140
141         dialog_->delPB->setEnabled( activate );
142         dialog_->upPB->setEnabled( activate_up );
143         dialog_->downPB->setEnabled( activate_down );
144 }