]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCitationDialog.C
partial fonts fix. Like Juergen said we really need our own dialog.
[lyx.git] / src / frontends / qt2 / 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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <algorithm>
18
19 #include "gettext.h"
20 #include "controllers/ControlCitation.h"
21 #include "LyXView.h"
22 #include "buffer.h"
23
24 #include <qcheckbox.h>
25 #include <qcombobox.h>
26 #include <qlineedit.h>
27 #include <qlistbox.h>
28 #include <qmultilineedit.h>
29 #include <qpushbutton.h>
30
31 #include "QCitationDialog.h"
32 #include "QCitation.h"
33 #include "support/lstrings.h"
34
35 using std::vector;
36 using std::find;
37 using std::max;
38
39
40 QCitationDialog::QCitationDialog(QCitation * form)
41         : QCitationDialogBase(0, 0, false, 0),
42         form_(form)
43 {
44         connect(restorePB, SIGNAL(clicked()),
45                 form, SLOT(slotRestore()));
46         connect(okPB, SIGNAL(clicked()),
47                 form, SLOT(slotOK()));
48         connect(applyPB, SIGNAL(clicked()),
49                 form, SLOT(slotApply()));
50         connect(closePB, SIGNAL(clicked()),
51                 form, SLOT(slotClose()));
52         connect(searchED, SIGNAL(returnPressed()),
53                 this, SLOT(slotNextClicked()));
54
55         textBeforeED->setText(_("Not yet supported"));
56         textBeforeED->setReadOnly(true);
57         textBeforeED->setFocusPolicy(QWidget::NoFocus);
58         citationStyleCO->setEnabled(false);
59         citationStyleCO->setFocusPolicy(QWidget::NoFocus);
60 }
61
62
63 QCitationDialog::~QCitationDialog()
64 {
65 }
66
67
68 void QCitationDialog::slotBibSelected(int sel)
69 {
70         slotBibHighlighted(sel);
71
72         if (form_->readOnly())
73                 return;
74
75         slotAddClicked();
76 }
77
78
79 void QCitationDialog::slotBibHighlighted(int sel)
80 {
81         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
82
83         citeLB->clearSelection();
84
85         // FIXME: why would this happen ?
86         if (sel < 0 || sel >= (int)form_->bibkeys.size()) {
87                 return;
88         }
89
90         // Put into browser_info the additional info associated with
91         // the selected browser_bib key
92         infoML->clear();
93
94         infoML->setText(biblio::getInfo(theMap, form_->bibkeys[sel]).c_str());
95
96         // Highlight the selected browser_bib key in browser_cite if
97         // present
98         vector<string>::const_iterator cit =
99                 std::find(form_->citekeys.begin(), form_->citekeys.end(),
100                           form_->bibkeys[sel]);
101
102         if (cit != form_->citekeys.end()) {
103                 int const n = int(cit - form_->citekeys.begin());
104                 citeLB->setSelected(n, true);
105                 citeLB->setTopItem(n);
106         }
107
108         if (!form_->readOnly()) {
109                 if (cit != form_->citekeys.end()) {
110                         form_->setBibButtons(QCitation::OFF);
111                         form_->setCiteButtons(QCitation::ON);
112                 } else {
113                         form_->setBibButtons(QCitation::ON);
114                         form_->setCiteButtons(QCitation::OFF);
115                 }
116         }
117 }
118
119
120 void QCitationDialog::slotCiteHighlighted(int sel)
121 {
122         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
123
124         // FIXME: why would this happen ?
125         if (sel < 0 || sel >= (int)form_->citekeys.size()) {
126                 return;
127         }
128
129         if (!form_->readOnly()) {
130                 form_->setBibButtons(QCitation::OFF);
131                 form_->setCiteButtons(QCitation::ON);
132         }
133
134         // Highlight the selected browser_cite key in browser_bib
135         vector<string>::const_iterator cit =
136                 std::find(form_->bibkeys.begin(),
137                 form_->bibkeys.end(), form_->citekeys[sel]);
138
139         if (cit != form_->bibkeys.end()) {
140                 int const n = int(cit - form_->bibkeys.begin());
141                 bibLB->setSelected(n, true);
142                 bibLB->setTopItem(n);
143
144                 // Put into browser_info the additional info associated
145                 // with the selected browser_cite key
146                 infoML->clear();
147                 infoML->setText(biblio::getInfo(theMap, form_->citekeys[sel]).c_str());
148         }
149 }
150
151
152 void QCitationDialog::slotAddClicked()
153 {
154         int const sel = bibLB->currentItem();
155
156         // FIXME: why ?
157         if (sel < 0 || sel >= (int)form_->bibkeys.size()) {
158                 return;
159         }
160
161         // Add the selected browser_bib key to browser_cite
162         citeLB->insertItem(form_->bibkeys[sel].c_str());
163         form_->citekeys.push_back(form_->bibkeys[sel]);
164
165         int const n = int(form_->citekeys.size());
166         citeLB->setSelected(n - 1, true);
167
168         slotBibHighlighted(sel);
169         form_->setBibButtons(QCitation::OFF);
170         form_->setCiteButtons(QCitation::ON);
171         form_->changed();
172 }
173
174
175 void QCitationDialog::slotDelClicked()
176 {
177         int const sel = citeLB->currentItem();
178
179         // FIXME: why ?
180         if (sel < 0 || sel >= (int)form_->citekeys.size()) {
181                 return;
182         }
183
184         // Remove the selected key from browser_cite
185         citeLB->removeItem(sel);
186         form_->citekeys.erase(form_->citekeys.begin() + sel);
187
188         form_->setBibButtons(QCitation::ON);
189         form_->setCiteButtons(QCitation::OFF);
190         form_->changed();
191 }
192
193
194 void QCitationDialog::slotUpClicked()
195 {
196         int const sel = citeLB->currentItem();
197
198         // FIXME: why ?
199         if (sel < 1 || sel >= (int)form_->citekeys.size()) {
200                 return;
201         }
202
203         // Move the selected key up one line
204         vector<string>::iterator it = form_->citekeys.begin() + sel;
205         string const tmp = *it;
206
207         citeLB->removeItem(sel);
208         form_->citekeys.erase(it);
209
210         citeLB->insertItem(tmp.c_str(), sel - 1);
211         citeLB->setSelected(sel - 1, true);
212         form_->citekeys.insert(it - 1, tmp);
213         form_->setCiteButtons(QCitation::ON);
214         form_->changed();
215 }
216
217
218 void QCitationDialog::slotDownClicked()
219 {
220         int const sel = citeLB->currentItem();
221
222         // FIXME: ?
223         if (sel < 0 || sel >= (int)form_->citekeys.size() - 1) {
224                 return;
225         }
226
227         // Move the selected key down one line
228         vector<string>::iterator it = form_->citekeys.begin() + sel;
229         string const tmp = *it;
230
231         citeLB->removeItem(sel);
232         form_->citekeys.erase(it);
233
234         citeLB->insertItem(tmp.c_str(), sel + 1);
235         citeLB->setSelected(sel + 1, true);
236         form_->citekeys.insert(it + 1, tmp);
237         form_->setCiteButtons(QCitation::ON);
238         form_->changed();
239 }
240
241
242 void QCitationDialog::slotPreviousClicked()
243 {
244         doFind(biblio::BACKWARD);
245 }
246
247
248 void QCitationDialog::slotNextClicked()
249 {
250         doFind(biblio::FORWARD);
251 }
252
253
254 void QCitationDialog::changed_adaptor()
255 {
256         form_->changed();
257 }
258
259
260 void QCitationDialog::doFind(biblio::Direction dir)
261 {
262         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
263         string const str = searchED->text().latin1();
264
265         biblio::Search const type =
266                 searchTypeCB->isChecked() ?
267                 biblio::REGEX : biblio::SIMPLE;
268
269         vector<string>::const_iterator start = form_->bibkeys.begin();
270         int const sel = bibLB->currentItem();
271         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
272                 start += sel;
273
274         // Find the NEXT instance...
275         if (dir == biblio::FORWARD)
276                 start += 1;
277         else
278                 start -= 1;
279
280         bool const caseSensitive = searchCaseCB->isChecked();
281
282         vector<string>::const_iterator cit =
283                 biblio::searchKeys(theMap, form_->bibkeys, str,
284                                    start, type, dir, caseSensitive);
285
286         // not found. let's loop round
287         if (cit == form_->bibkeys.end()) {
288                 if (dir == biblio::FORWARD) {
289                         start = form_->bibkeys.begin();
290                 }
291                 else start = form_->bibkeys.end() - 1;
292
293                 cit = biblio::searchKeys(theMap, form_->bibkeys, str,
294                                          start, type, dir, caseSensitive);
295
296                 if (cit == form_->bibkeys.end())
297                         return;
298         }
299
300         int const found = int(cit - form_->bibkeys.begin());
301         if (found == sel) {
302                 return;
303         }
304
305         // Update the display
306         int const top = max(found - 5, 1);
307         bibLB->setTopItem(top);
308         bibLB->setSelected(found, true);
309         slotBibHighlighted(0);
310 }