]> git.lyx.org Git - features.git/blob - src/frontends/qt2/QCitation.C
83e86325111df8de4e389b251a9c9b94db5cee29
[features.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         style_ = choice;
67         open_find_ = false;
68 }
69
70
71 void QCitation::hide()
72 {
73         citekeys.clear();
74         bibkeys.clear();
75         open_find_ = true;
76
77         QDialogView::hide();
78 }
79
80
81 void QCitation::build_dialog()
82 {
83         dialog_.reset(new QCitationDialog(this));
84
85         // Manage the ok, apply, restore and cancel/close buttons
86         bcview().setOK(dialog_->okPB);
87         bcview().setApply(dialog_->applyPB);
88         bcview().setCancel(dialog_->closePB);
89         bcview().setRestore(dialog_->restorePB);
90
91         bcview().addReadOnly(dialog_->addPB);
92         bcview().addReadOnly(dialog_->deletePB);
93         bcview().addReadOnly(dialog_->upPB);
94         bcview().addReadOnly(dialog_->downPB);
95         bcview().addReadOnly(dialog_->citationStyleCO);
96         bcview().addReadOnly(dialog_->forceuppercaseCB);
97         bcview().addReadOnly(dialog_->fulllistCB);
98         // add when enabled !
99         //bcview().addReadOnly(dialog_->textBeforeED);
100         bcview().addReadOnly(dialog_->textAfterED);
101         
102         open_find_ = true;
103 }
104
105
106 void QCitation::fillStyles()
107 {
108         if (citekeys.empty()) {
109                 dialog_->citationStyleCO->setEnabled(false);
110                 dialog_->citationStyleLA->setEnabled(false);
111                 return;
112         }
113
114         int const orig = dialog_->citationStyleCO->currentItem();
115
116         dialog_->citationStyleCO->clear();
117
118         int curr = dialog_->selectedLB->currentItem();
119         if (curr < 0)
120                 curr = 0;
121
122         string key = citekeys[curr];
123
124         vector<string> const & sty = controller().getCiteStrings(key);
125
126         bool const natbib = controller().usingNatbib();
127         dialog_->citationStyleCO->setEnabled(!sty.empty() && natbib);
128         dialog_->citationStyleLA->setEnabled(!sty.empty() && natbib);
129
130         for (vector<string>::const_iterator it = sty.begin();
131                 it != sty.end(); ++it) {
132                 dialog_->citationStyleCO->insertItem(toqstr(*it));
133         }
134
135         if (orig != -1 && orig < dialog_->citationStyleCO->count())
136                 dialog_->citationStyleCO->setCurrentItem(orig);
137 }
138
139
140 void QCitation::updateStyle()
141 {
142         bool const natbib = controller().usingNatbib();
143
144         dialog_->fulllistCB->setEnabled(natbib);
145         dialog_->forceuppercaseCB->setEnabled(natbib);
146
147         string const & command = controller().params().getCmdName();
148
149         // Find the style of the citekeys
150         vector<biblio::CiteStyle> const & styles =
151                 ControlCitation::getCiteStyles();
152         biblio::CitationStyle cs = biblio::getCitationStyle(command);
153
154         vector<biblio::CiteStyle>::const_iterator cit =
155                 find(styles.begin(), styles.end(), cs.style);
156         
157         // restore the latest natbib style 
158         if (style_ >= 0 && style_ < dialog_->citationStyleCO->count())
159                 dialog_->citationStyleCO->setCurrentItem(style_);
160         else
161                 dialog_->citationStyleCO->setCurrentItem(0);
162         dialog_->fulllistCB->setChecked(false);
163         dialog_->forceuppercaseCB->setChecked(false);
164
165         if (cit != styles.end()) {
166                 int const i = int(cit - styles.begin());
167                 dialog_->citationStyleCO->setCurrentItem(i);
168                 dialog_->fulllistCB->setChecked(cs.full);
169                 dialog_->forceuppercaseCB->setChecked(cs.forceUCase);
170         }
171 }
172
173
174 void QCitation::update_contents()
175 {
176         // Make the list of all available bibliography keys
177         bibkeys = biblio::getKeys(controller().bibkeysInfo());
178         updateBrowser(dialog_->add_->availableLB, bibkeys);
179
180         // Ditto for the keys cited in this inset
181         citekeys = getVectorFromString(controller().params().getContents());
182         updateBrowser(dialog_->selectedLB, citekeys);
183
184         // No keys have been selected yet, so...
185         dialog_->infoML->clear();
186         dialog_->setButtons();
187
188         dialog_->textAfterED->setText(toqstr(controller().params().getOptions()));
189
190         fillStyles();
191         updateStyle();
192         
193         // open the find dialog if nothing has been selected (yet)
194         // the bool prevents that this is also done after "apply"
195         if (open_find_)
196                 dialog_->openFind();
197 }
198
199
200 void QCitation::updateBrowser(QListBox * browser,
201                               vector<string> const & keys) const
202 {
203         browser->clear();
204
205         for (vector<string>::const_iterator it = keys.begin();
206                 it < keys.end(); ++it) {
207                 string const key = trim(*it);
208                 // FIXME: why the .empty() test ?
209                 if (!key.empty())
210                         browser->insertItem(toqstr(key));
211         }
212 }