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