]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitation.C
some tabular fixes for the problems reported by Helge
[lyx.git] / src / frontends / qt2 / QCitation.C
1 /**
2  * \file QCitation.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Kalle Dalheimer
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QCitation.h"
15 #include "QCitationDialog.h"
16 #include "ui/QCitationFindDialogBase.h"
17 #include "Qt2BC.h"
18 #include "qt_helpers.h"
19
20 #include "bufferparams.h"
21
22 #include "controllers/ButtonController.h"
23 #include "controllers/ControlCitation.h"
24
25 #include "support/lstrings.h"
26
27 #include <qcheckbox.h>
28 #include <qcombobox.h>
29 #include <qlineedit.h>
30 #include <qlistbox.h>
31 #include <qmultilineedit.h>
32 #include <qpushbutton.h>
33 #include <qlabel.h>
34
35 using std::find;
36 using std::string;
37 using std::vector;
38
39 namespace lyx {
40
41 using support::getStringFromVector;
42 using support::getVectorFromString;
43 using support::trim;
44
45 namespace frontend {
46
47 typedef QController<ControlCitation, QView<QCitationDialog> > base_class;
48
49 QCitation::QCitation(Dialog & parent)
50         : base_class(parent, _("LyX: Citation Reference"))
51 {}
52
53
54 void QCitation::apply()
55 {
56         vector<biblio::CiteStyle> const & styles =
57                 ControlCitation::getCiteStyles();
58
59         int const choice = dialog_->citationStyleCO->currentItem();
60         bool const full  = dialog_->fulllistCB->isChecked();
61         bool const force = dialog_->forceuppercaseCB->isChecked();
62
63         string const command =
64                 biblio::CitationStyle(styles[choice], full, force)
65                 .asLatexStr();
66
67         controller().params().setCmdName(command);
68         controller().params().setContents(getStringFromVector(citekeys));
69
70         string const before = fromqstr(dialog_->textBeforeED->text());
71         controller().params().setSecOptions(before);
72
73         string const after = fromqstr(dialog_->textAfterED->text());
74         controller().params().setOptions(after);
75
76         style_ = choice;
77         open_find_ = false;
78 }
79
80
81 void QCitation::hide()
82 {
83         citekeys.clear();
84         bibkeys.clear();
85         open_find_ = true;
86
87         QDialogView::hide();
88 }
89
90
91 void QCitation::build_dialog()
92 {
93         dialog_.reset(new QCitationDialog(this));
94
95         // Manage the ok, apply, restore and cancel/close buttons
96         bcview().setOK(dialog_->okPB);
97         bcview().setApply(dialog_->applyPB);
98         bcview().setCancel(dialog_->closePB);
99         bcview().setRestore(dialog_->restorePB);
100
101         bcview().addReadOnly(dialog_->addPB);
102         bcview().addReadOnly(dialog_->deletePB);
103         bcview().addReadOnly(dialog_->upPB);
104         bcview().addReadOnly(dialog_->downPB);
105         bcview().addReadOnly(dialog_->citationStyleCO);
106         bcview().addReadOnly(dialog_->forceuppercaseCB);
107         bcview().addReadOnly(dialog_->fulllistCB);
108         bcview().addReadOnly(dialog_->textBeforeED);
109         bcview().addReadOnly(dialog_->textAfterED);
110
111         open_find_ = true;
112 }
113
114
115 void QCitation::fillStyles()
116 {
117         if (citekeys.empty()) {
118                 dialog_->citationStyleCO->setEnabled(false);
119                 dialog_->citationStyleLA->setEnabled(false);
120                 return;
121         }
122
123         int const orig = dialog_->citationStyleCO->currentItem();
124
125         dialog_->citationStyleCO->clear();
126
127         int curr = dialog_->selectedLB->currentItem();
128         if (curr < 0)
129                 curr = 0;
130
131         string key = citekeys[curr];
132
133         vector<string> const & sty = controller().getCiteStrings(key);
134
135         biblio::CiteEngine const engine = controller().getEngine();
136         bool const basic_engine = engine == biblio::ENGINE_BASIC;
137
138         dialog_->citationStyleCO->setEnabled(!sty.empty() && !basic_engine);
139         dialog_->citationStyleLA->setEnabled(!sty.empty() && !basic_engine);
140
141         for (vector<string>::const_iterator it = sty.begin();
142                 it != sty.end(); ++it) {
143                 dialog_->citationStyleCO->insertItem(toqstr(*it));
144         }
145
146         if (orig != -1 && orig < dialog_->citationStyleCO->count())
147                 dialog_->citationStyleCO->setCurrentItem(orig);
148 }
149
150
151 void QCitation::updateStyle()
152 {
153         biblio::CiteEngine const engine = controller().getEngine();
154         bool const natbib_engine =
155                 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
156                 engine == biblio::ENGINE_NATBIB_NUMERICAL;
157         bool const basic_engine = engine == biblio::ENGINE_BASIC;
158
159         dialog_->fulllistCB->setEnabled(natbib_engine);
160         dialog_->forceuppercaseCB->setEnabled(natbib_engine);
161         dialog_->textBeforeED->setEnabled(!basic_engine);
162
163         string const & command = controller().params().getCmdName();
164
165         // Find the style of the citekeys
166         vector<biblio::CiteStyle> const & styles =
167                 ControlCitation::getCiteStyles();
168         biblio::CitationStyle const cs(command);
169
170         vector<biblio::CiteStyle>::const_iterator cit =
171                 find(styles.begin(), styles.end(), cs.style);
172
173         // restore the latest natbib style
174         if (style_ >= 0 && style_ < dialog_->citationStyleCO->count())
175                 dialog_->citationStyleCO->setCurrentItem(style_);
176         else
177                 dialog_->citationStyleCO->setCurrentItem(0);
178         dialog_->fulllistCB->setChecked(false);
179         dialog_->forceuppercaseCB->setChecked(false);
180
181         if (cit != styles.end()) {
182                 int const i = int(cit - styles.begin());
183                 dialog_->citationStyleCO->setCurrentItem(i);
184                 dialog_->fulllistCB->setChecked(cs.full);
185                 dialog_->forceuppercaseCB->setChecked(cs.forceUCase);
186         }
187 }
188
189
190 void QCitation::update_contents()
191 {
192         // Make the list of all available bibliography keys
193         bibkeys = biblio::getKeys(controller().bibkeysInfo());
194         updateBrowser(dialog_->add_->availableLB, bibkeys);
195
196         // Ditto for the keys cited in this inset
197         citekeys = getVectorFromString(controller().params().getContents());
198         updateBrowser(dialog_->selectedLB, citekeys);
199
200         // No keys have been selected yet, so...
201         dialog_->infoML->clear();
202         dialog_->setButtons();
203
204         dialog_->textBeforeED->setText(
205                 toqstr(controller().params().getSecOptions()));
206         dialog_->textAfterED->setText(
207                 toqstr(controller().params().getOptions()));
208
209         fillStyles();
210         updateStyle();
211
212         // open the find dialog if nothing has been selected (yet)
213         // the bool prevents that this is also done after "apply"
214         if (open_find_)
215                 dialog_->openFind();
216
217         bc().valid(isValid());
218 }
219
220
221 void QCitation::updateBrowser(QListBox * browser,
222                               vector<string> const & keys) const
223 {
224         browser->clear();
225
226         for (vector<string>::const_iterator it = keys.begin();
227                 it < keys.end(); ++it) {
228                 string const key = trim(*it);
229                 // FIXME: why the .empty() test ?
230                 if (!key.empty())
231                         browser->insertItem(toqstr(key));
232         }
233 }
234
235
236 bool QCitation::isValid()
237 {
238         return dialog_->selectedLB->count() > 0;
239 }
240
241
242 } // namespace frontend
243 } // namespace lyx