]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitation.C
Minipage is no more (long live the box inset)
[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 "debug.h"
15 #include "ui/QCitationFindDialogBase.h"
16 #include "QCitationDialog.h"
17 #include "QCitation.h"
18
19 #include <qcheckbox.h>
20 #include <qcombobox.h>
21 #include <qlineedit.h>
22 #include <qlistbox.h>
23 #include <qmultilineedit.h>
24 #include <qpushbutton.h>
25 #include <qlabel.h>
26
27 #include "Qt2BC.h"
28 #include "ControlCitation.h"
29 #include "qt_helpers.h"
30 #include "support/lstrings.h"
31
32 using lyx::support::getStringFromVector;
33 using lyx::support::getVectorFromString;
34 using lyx::support::trim;
35
36 using std::find;
37 using std::string;
38 using std::vector;
39
40
41 typedef QController<ControlCitation, QView<QCitationDialog> > base_class;
42
43 QCitation::QCitation(Dialog & parent)
44         : base_class(parent, _("LyX: Citation Reference"))
45 {}
46
47
48 void QCitation::apply()
49 {
50         vector<biblio::CiteStyle> const & styles =
51                 ControlCitation::getCiteStyles();
52
53         int const choice = dialog_->citationStyleCO->currentItem();
54         bool const full  = dialog_->fulllistCB->isChecked();
55         bool const force = dialog_->forceuppercaseCB->isChecked();
56
57         string const command =
58                 biblio::getCiteCommand(styles[choice], full, force);
59
60         controller().params().setCmdName(command);
61         controller().params().setContents(getStringFromVector(citekeys));
62
63         string const after = fromqstr(dialog_->textAfterED->text());
64         controller().params().setOptions(after);
65 }
66
67
68 void QCitation::hide()
69 {
70         citekeys.clear();
71         bibkeys.clear();
72
73         QDialogView::hide();
74 }
75
76
77 void QCitation::build_dialog()
78 {
79         dialog_.reset(new QCitationDialog(this));
80
81         // Manage the ok, apply, restore and cancel/close buttons
82         bcview().setOK(dialog_->okPB);
83         bcview().setApply(dialog_->applyPB);
84         bcview().setCancel(dialog_->closePB);
85         bcview().setRestore(dialog_->restorePB);
86
87         bcview().addReadOnly(dialog_->addPB);
88         bcview().addReadOnly(dialog_->deletePB);
89         bcview().addReadOnly(dialog_->upPB);
90         bcview().addReadOnly(dialog_->downPB);
91         bcview().addReadOnly(dialog_->citationStyleCO);
92         bcview().addReadOnly(dialog_->forceuppercaseCB);
93         bcview().addReadOnly(dialog_->fulllistCB);
94         // add when enabled !
95         //bcview().addReadOnly(dialog_->textBeforeED);
96         bcview().addReadOnly(dialog_->textAfterED);
97 }
98
99
100 void QCitation::fillStyles()
101 {
102         if (citekeys.empty()) {
103                 dialog_->citationStyleCO->setEnabled(false);
104                 dialog_->citationStyleLA->setEnabled(false);
105                 return;
106         }
107
108         int const orig = dialog_->citationStyleCO->currentItem();
109
110         dialog_->citationStyleCO->clear();
111
112         int curr = dialog_->selectedLB->currentItem();
113         if (curr < 0)
114                 curr = 0;
115
116         string key = citekeys[curr];
117
118         vector<string> const & sty = controller().getCiteStrings(key);
119
120         bool const natbib = controller().usingNatbib();
121         dialog_->citationStyleCO->setEnabled(!sty.empty() && natbib);
122         dialog_->citationStyleLA->setEnabled(!sty.empty() && natbib);
123
124         for (vector<string>::const_iterator it = sty.begin();
125                 it != sty.end(); ++it) {
126                 dialog_->citationStyleCO->insertItem(toqstr(*it));
127         }
128
129         if (orig != -1 && orig < dialog_->citationStyleCO->count())
130                 dialog_->citationStyleCO->setCurrentItem(orig);
131 }
132
133
134 void QCitation::updateStyle()
135 {
136         bool const natbib = controller().usingNatbib();
137
138         dialog_->fulllistCB->setEnabled(natbib);
139         dialog_->forceuppercaseCB->setEnabled(natbib);
140
141         string const & command = controller().params().getCmdName();
142
143         // Find the style of the citekeys
144         vector<biblio::CiteStyle> const & styles =
145                 ControlCitation::getCiteStyles();
146         biblio::CitationStyle cs = biblio::getCitationStyle(command);
147
148         vector<biblio::CiteStyle>::const_iterator cit =
149                 find(styles.begin(), styles.end(), cs.style);
150
151         dialog_->citationStyleCO->setCurrentItem(0);
152         dialog_->fulllistCB->setChecked(false);
153         dialog_->forceuppercaseCB->setChecked(false);
154
155         if (cit != styles.end()) {
156                 int const i = int(cit - styles.begin());
157                 dialog_->citationStyleCO->setCurrentItem(i);
158                 dialog_->fulllistCB->setChecked(cs.full);
159                 dialog_->forceuppercaseCB->setChecked(cs.forceUCase);
160         }
161 }
162
163
164 void QCitation::update_contents()
165 {
166         // Make the list of all available bibliography keys
167         bibkeys = biblio::getKeys(controller().bibkeysInfo());
168         updateBrowser(dialog_->add_->availableLB, bibkeys);
169
170         // Ditto for the keys cited in this inset
171         citekeys = getVectorFromString(controller().params().getContents());
172         updateBrowser(dialog_->selectedLB, citekeys);
173
174         // No keys have been selected yet, so...
175         dialog_->infoML->clear();
176         dialog_->setButtons();
177
178         dialog_->textAfterED->setText(toqstr(controller().params().getOptions()));
179
180         fillStyles();
181         updateStyle();
182 }
183
184
185 void QCitation::updateBrowser(QListBox * browser,
186                               vector<string> const & keys) const
187 {
188         browser->clear();
189
190         for (vector<string>::const_iterator it = keys.begin();
191                 it < keys.end(); ++it) {
192                 string const key = trim(*it);
193                 // FIXME: why the .empty() test ?
194                 if (!key.empty())
195                         browser->insertItem(toqstr(key));
196         }
197 }