]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCitationDialog.C
This commit saves the need to check for lyx::use_gui in a number of places.
[lyx.git] / src / frontends / qt4 / QCitationDialog.C
1 /**
2  * \file QCitationDialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Kalle Dalheimer
7  * \author John Levon
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "QCitationDialog.h"
16 #include "QCitation.h"
17 #include "qt_helpers.h"
18
19 #include "bufferparams.h"
20 #include "gettext.h"
21
22 #include "controllers/ControlCitation.h"
23
24 #include "support/lstrings.h"
25
26 #include <vector>
27 #include <string>
28
29 using std::vector;
30 using std::string;
31
32 namespace lyx {
33
34 using support::getStringFromVector;
35 using support::getVectorFromString;
36
37 namespace frontend {
38
39
40 QCitationDialog::QCitationDialog(Dialog & dialog, QCitation * form)
41         : Dialog::View(dialog, _("Citation")), form_(form)
42 {
43         setupUi(this);
44
45         setWindowTitle(toqstr("LyX: " + getTitle()));
46
47         selectedLV->setModel(form_->selected());
48         availableLV->setModel(form_->available());
49
50     connect(citationStyleCO, SIGNAL(activated(int)),
51                 this, SLOT(changed()));
52     connect(fulllistCB, SIGNAL(clicked()),
53                 this, SLOT(changed()));
54     connect(forceuppercaseCB, SIGNAL(clicked()),
55                 this, SLOT(changed()));
56     connect(textBeforeED, SIGNAL(textChanged(const QString&)),
57                 this, SLOT(changed()));
58     connect(textAfterED, SIGNAL(textChanged(const QString&)),
59                 this, SLOT(changed()));
60 }
61
62
63 QCitationDialog::~QCitationDialog()
64 {
65 }
66
67
68 void QCitationDialog::apply()
69 {
70         int  const choice = std::max(0, citationStyleCO->currentIndex());
71         style_ = choice;
72         bool const full  = fulllistCB->isChecked();
73         bool const force = forceuppercaseCB->isChecked();
74
75         QString const before = textBeforeED->text();
76         QString const after = textAfterED->text();
77
78         form_->apply(choice, full, force, before, after);
79 }
80
81
82 void QCitationDialog::hide()
83 {
84         accept();
85 }
86
87
88 void QCitationDialog::show()
89 {
90         QDialog::show();
91 }
92
93
94 bool QCitationDialog::isVisible() const
95 {
96         return QDialog::isVisible();
97 }
98  
99
100 void QCitationDialog::on_okPB_clicked()
101 {
102         apply();
103         accept();
104 }
105
106
107 void QCitationDialog::on_cancelPB_clicked()
108 {
109         accept();
110 }
111
112
113 void QCitationDialog::on_applyPB_clicked()
114 {
115         apply();
116 }
117
118
119 void QCitationDialog::on_restorePB_clicked()
120 {
121         update();
122 }
123
124
125 void QCitationDialog::update()
126 {
127         form_->updateModel();
128
129         QModelIndex idxa = availableLV->currentIndex();
130         if (!idxa.isValid())
131                 availableLV->setCurrentIndex(availableLV->model()->index(0,0));
132
133         QModelIndex idx = selectedLV->currentIndex();
134         if (form_->isValid() && !idx.isValid()) {
135                 selectedLV->setCurrentIndex(selectedLV->model()->index(0,0));
136                 updateInfo(selectedLV->currentIndex());
137         } else
138                 updateInfo(availableLV->currentIndex());
139
140         setButtons();
141
142         textBeforeED->setText(form_->textBefore());
143         textAfterED->setText(form_->textAfter());
144
145         fillStyles();
146         updateStyle();
147 }
148
149
150 void QCitationDialog::updateStyle()
151 {
152         biblio::CiteEngine const engine = form_->getEngine();
153         bool const natbib_engine =
154                 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
155                 engine == biblio::ENGINE_NATBIB_NUMERICAL;
156         bool const basic_engine = engine == biblio::ENGINE_BASIC;
157
158         fulllistCB->setEnabled(natbib_engine);
159         forceuppercaseCB->setEnabled(natbib_engine);
160         textBeforeED->setEnabled(!basic_engine);
161
162         string const & command = form_->params().getCmdName();
163
164         // Find the style of the citekeys
165         vector<biblio::CiteStyle> const & styles =
166                 ControlCitation::getCiteStyles();
167         biblio::CitationStyle const cs(command);
168
169         vector<biblio::CiteStyle>::const_iterator cit =
170                 std::find(styles.begin(), styles.end(), cs.style);
171
172         // restore the latest natbib style
173         if (style_ >= 0 && style_ < citationStyleCO->count())
174                 citationStyleCO->setCurrentIndex(style_);
175         else
176                 citationStyleCO->setCurrentIndex(0);
177
178         fulllistCB->setChecked(false);
179         forceuppercaseCB->setChecked(false);
180
181         if (cit != styles.end()) {
182                 int const i = int(cit - styles.begin());
183                 citationStyleCO->setCurrentIndex(i);
184                 fulllistCB->setChecked(cs.full);
185                 forceuppercaseCB->setChecked(cs.forceUCase);
186         }
187 }
188
189
190 void QCitationDialog::fillStyles()
191 {
192         int const orig = citationStyleCO->currentIndex();
193
194         citationStyleCO->clear();
195
196         QStringList selected_keys = form_->selected()->stringList();
197         if (selected_keys.empty()) {
198                 citationStyleCO->setEnabled(false);
199                 citationStyleLA->setEnabled(false);
200                 return;
201         }
202
203         if (selectedLV->selectionModel()->selectedIndexes().empty())
204                 return;
205         
206         int curr = selectedLV->selectionModel()->selectedIndexes()[0].row();//selectedLV->currentItem();
207
208         QStringList sty = form_->citationStyles(curr);
209
210         bool const basic_engine = 
211                 (form_->getEngine() == biblio::ENGINE_BASIC);
212
213         citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine);
214         citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine);
215
216         citationStyleCO->insertItems(0, sty);
217
218         if (orig != -1 && orig < citationStyleCO->count())
219                 citationStyleCO->setCurrentIndex(orig);
220 }
221
222
223 bool QCitationDialog::isSelected(const QModelIndex & idx)
224 {
225         QString const str = idx.data().toString();
226         return !form_->selected()->stringList().filter(str).isEmpty();
227 }
228
229
230 void QCitationDialog::setButtons()
231 {
232         int const arows = availableLV->model()->rowCount();
233         addPB->setEnabled(arows>0 && !isSelected(availableLV->currentIndex()));
234
235         int const srows = selectedLV->model()->rowCount();
236         int const sel_nr = selectedLV->currentIndex().row();
237         deletePB->setEnabled(sel_nr >= 0);
238         upPB->setEnabled(sel_nr > 0);
239         downPB->setEnabled(sel_nr >= 0 && sel_nr < srows - 1);
240         applyPB->setEnabled(srows>0);
241         okPB->setEnabled(srows>0);
242 }
243
244
245 void QCitationDialog::updateInfo(const QModelIndex & idx)
246 {
247         if (idx.isValid()) {
248                 QString const keytxt = form_->getKeyInfo(idx.data().toString());
249                 infoML->document()->setPlainText(keytxt);
250         } else
251                 infoML->document()->clear();
252 }
253
254
255 void QCitationDialog::on_selectedLV_clicked(const QModelIndex & idx)
256 {
257         updateInfo(idx);
258         changed();
259 }
260
261 void QCitationDialog::on_availableLV_clicked(const QModelIndex & idx)
262 {
263         updateInfo(idx);
264         setButtons();
265 }
266
267
268 void QCitationDialog::on_availableLV_activated(const QModelIndex & idx)
269 {
270         if (isSelected(idx))
271                 return;
272
273         on_addPB_clicked();             
274 }
275
276
277 void QCitationDialog::on_addPB_clicked()
278 {
279         QModelIndex idx = selectedLV->currentIndex();
280         form_->addKey(availableLV->currentIndex());
281         if (idx.isValid())
282                 selectedLV->setCurrentIndex(idx);
283         changed();
284 }
285
286
287 void QCitationDialog::on_deletePB_clicked()
288 {
289         QModelIndex idx = selectedLV->currentIndex();
290         int nrows = selectedLV->model()->rowCount();
291         
292         form_->deleteKey(idx);
293
294         if (idx.row() == nrows - 1)     
295                 idx = idx.sibling(idx.row() - 1, idx.column());
296
297         if (nrows>1)
298                 selectedLV->setCurrentIndex(idx);
299
300         updateInfo(selectedLV->currentIndex());
301         changed();
302 }
303
304
305 void QCitationDialog::on_upPB_clicked()
306 {
307         QModelIndex idx = selectedLV->currentIndex();
308         form_->upKey(idx);
309         selectedLV->setCurrentIndex(idx.sibling(idx.row() - 1, idx.column()));
310         changed();
311 }
312
313
314 void QCitationDialog::on_downPB_clicked()
315 {
316         QModelIndex idx = selectedLV->currentIndex();
317         form_->downKey(idx);
318         selectedLV->setCurrentIndex(idx.sibling(idx.row() + 1, idx.column()));
319         changed();
320 }
321
322
323 void QCitationDialog::on_findLE_textChanged(const QString & text)
324 {
325         form_->findKey(text);
326         availableLV->setModel(form_->found());
327         changed();
328 }
329
330
331 void QCitationDialog::changed()
332 {
333         fillStyles();
334         setButtons();
335 }
336
337
338 } // namespace frontend
339 } // namespace lyx
340
341 #include "QCitationDialog_moc.cpp"