]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCitationDialog.C
Added initial qt4 work by Abdelrazak Younes
[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 "ui/QCitationFindUi.h"
17 #include "QCitation.h"
18 #include "qt_helpers.h"
19
20 #include "controllers/ControlCitation.h"
21 #include "controllers/ButtonController.h"
22
23 #include <qcheckbox.h>
24 #include <qlineedit.h>
25 #include <q3listbox.h>
26 #include <q3multilineedit.h>
27 #include <qpushbutton.h>
28
29
30 using std::vector;
31 using std::string;
32
33 namespace lyx {
34 namespace frontend {
35
36 QCitationDialog::QCitationDialog(QCitation * form)
37         : form_(form)
38 {
39         setupUi(this);
40         connect(restorePB, SIGNAL(clicked()),
41                 form, SLOT(slotRestore()));
42         connect(okPB, SIGNAL(clicked()),
43                 form, SLOT(slotOK()));
44         connect(applyPB, SIGNAL(clicked()),
45                 form, SLOT(slotApply()));
46         connect(closePB, SIGNAL(clicked()),
47                 form, SLOT(slotClose()));
48
49     connect( citationStyleCO, SIGNAL( activated(int) ), this, SLOT( changed_adaptor() ) );
50     connect( fulllistCB, SIGNAL( clicked() ), this, SLOT( changed_adaptor() ) );
51     connect( forceuppercaseCB, SIGNAL( clicked() ), this, SLOT( changed_adaptor() ) );
52     connect( textBeforeED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed_adaptor() ) );
53     connect( textAfterED, SIGNAL( textChanged(const QString&) ), this, SLOT( changed_adaptor() ) );
54     connect( upPB, SIGNAL( clicked() ), this, SLOT( up() ) );
55     connect( downPB, SIGNAL( clicked() ), this, SLOT( down() ) );
56     connect( selectedLB, SIGNAL( currentChanged(QListBoxItem*) ), this, SLOT( selectedChanged() ) );
57     connect( addPB, SIGNAL( clicked() ), this, SLOT( add() ) );
58     connect( deletePB, SIGNAL( clicked() ), this, SLOT( del() ) );
59
60         add_ = new QDialog(this, "", true);
61         ui_.setupUi(add_);
62
63     connect( ui_.addPB, SIGNAL( clicked() ), this, SLOT( accept() ) );
64     connect( ui_.closePB, SIGNAL( clicked() ), this, SLOT( reject() ) );
65
66         connect(ui_.previousPB, SIGNAL(clicked()), this, SLOT(previous()));
67         connect(ui_.nextPB, SIGNAL(clicked()), this, SLOT(next()));
68         connect(ui_.availableLB, SIGNAL(currentChanged(Q3ListBoxItem *)), this, SLOT(availableChanged()));
69         connect(ui_.availableLB, SIGNAL(selected(Q3ListBoxItem *)), this, SLOT(addCitation()));
70         connect(ui_.availableLB, SIGNAL(selected(Q3ListBoxItem *)), add_, SLOT(accept()));
71         connect(ui_.addPB, SIGNAL(clicked()), this, SLOT(addCitation()));
72         connect(selectedLB, SIGNAL(returnPressed(Q3ListBoxItem *)), form, SLOT(slotOK()));
73 }
74
75
76 QCitationDialog::~QCitationDialog()
77 {
78 }
79
80
81 void QCitationDialog::setButtons()
82 {
83         if (form_->readOnly())
84                 return;
85
86         int const sel_nr = selectedLB->currentItem();
87         int const avail_nr = ui_.availableLB->currentItem();
88
89         ui_.addPB->setEnabled(avail_nr >= 0);
90         deletePB->setEnabled(sel_nr >= 0);
91         upPB->setEnabled(sel_nr > 0);
92         downPB->setEnabled(sel_nr >= 0 && sel_nr < int(selectedLB->count() - 1));
93 }
94
95
96 void QCitationDialog::openFind()
97 {
98         if (form_->readOnly())
99                 return;
100
101         if (isVisible() && selectedLB->count() == 0 
102             && ui_.availableLB->count() != 0){
103                 // open the find dialog
104                 add();
105                 // and let the user press ok after a selection
106                 if (selectedLB->count() != 0)
107                         form_->bc().valid();
108         }
109 }
110
111
112 void QCitationDialog::selectedChanged()
113 {
114         form_->fillStyles();
115         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
116         infoML->clear();
117
118         int const sel = selectedLB->currentItem();
119         if (sel < 0) {
120                 setButtons();
121                 return;
122         }
123
124         if (!theMap.empty())
125                 infoML->setText(
126                         toqstr(biblio::getInfo(theMap, form_->citekeys[sel])));
127         setButtons();
128 }
129
130
131 void QCitationDialog::previous()
132 {
133         find(biblio::BACKWARD);
134 }
135
136
137 void QCitationDialog::next()
138 {
139         find(biblio::FORWARD);
140 }
141
142
143 void QCitationDialog::availableChanged()
144 {
145         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
146         ui_.infoML->clear();
147
148         int const sel = ui_.availableLB->currentItem();
149         if (sel < 0) {
150                 setButtons();
151                 return;
152         }
153
154         if (!theMap.empty())
155                 ui_.infoML->setText(
156                         toqstr(biblio::getInfo(theMap, form_->bibkeys[sel])));
157         setButtons();
158 }
159
160
161 void QCitationDialog::addCitation()
162 {
163         int const sel = ui_.availableLB->currentItem();
164
165         if (sel < 0)
166                 return;
167
168         // Add the selected browser_bib keys to browser_cite
169         // multiple selections are possible
170         for (unsigned int i = 0; i != ui_.availableLB->count(); i++) {
171                 if (ui_.availableLB->isSelected(i)) {
172                         // do not allow duplicates
173                         if ((selectedLB->findItem(ui_.availableLB->text(i))) == 0) {
174                                 selectedLB->insertItem(toqstr(form_->bibkeys[i]));
175                                 form_->citekeys.push_back(form_->bibkeys[i]);
176                         }
177                 }
178         }
179
180         int const n = int(form_->citekeys.size());
181         selectedLB->setSelected(n - 1, true);
182
183         form_->changed();
184         form_->fillStyles();
185         setButtons();
186 }
187
188
189 void QCitationDialog::del()
190 {
191         int const sel = selectedLB->currentItem();
192
193         // Remove the selected key from browser_cite
194         selectedLB->removeItem(sel);
195         form_->citekeys.erase(form_->citekeys.begin() + sel);
196
197         form_->changed();
198         form_->fillStyles();
199         setButtons();
200 }
201
202
203 void QCitationDialog::up()
204 {
205         int const sel = selectedLB->currentItem();
206
207         // Move the selected key up one line
208         vector<string>::iterator it = form_->citekeys.begin() + sel;
209         string const tmp = *it;
210
211         selectedLB->removeItem(sel);
212         form_->citekeys.erase(it);
213
214         selectedLB->insertItem(toqstr(tmp), sel - 1);
215         selectedLB->setSelected(sel - 1, true);
216         form_->citekeys.insert(it - 1, tmp);
217
218         form_->changed();
219         form_->fillStyles();
220         setButtons();
221 }
222
223
224 void QCitationDialog::down()
225 {
226         int const sel = selectedLB->currentItem();
227
228         // Move the selected key down one line
229         vector<string>::iterator it = form_->citekeys.begin() + sel;
230         string const tmp = *it;
231
232         selectedLB->removeItem(sel);
233         form_->citekeys.erase(it);
234
235         selectedLB->insertItem(toqstr(tmp), sel + 1);
236         selectedLB->setSelected(sel + 1, true);
237         form_->citekeys.insert(it + 1, tmp);
238
239         form_->changed();
240         form_->fillStyles();
241         setButtons();
242 }
243
244
245 void QCitationDialog::add()
246 {
247         add_->exec();
248 }
249
250
251 void QCitationDialog::changed_adaptor()
252 {
253         form_->changed();
254 }
255
256
257 void QCitationDialog::find(biblio::Direction dir)
258 {
259         biblio::InfoMap const & theMap = form_->controller().bibkeysInfo();
260
261         biblio::Search const type = ui_.searchTypeCB->isChecked()
262                 ? biblio::REGEX : biblio::SIMPLE;
263
264         vector<string>::const_iterator start = form_->bibkeys.begin();
265         int const sel = ui_.availableLB->currentItem();
266         if (sel >= 0 && sel <= int(form_->bibkeys.size()-1))
267                 start += sel;
268
269         // Find the NEXT instance...
270         if (dir == biblio::FORWARD)
271                 start += 1;
272
273         bool const casesens = ui_.searchCaseCB->isChecked();
274         string const str = fromqstr(ui_.searchED->text());
275
276         vector<string>::const_iterator cit =
277                 biblio::searchKeys(theMap, form_->bibkeys, str,
278                                    start, type, dir, casesens);
279
280         // not found. let's loop round
281         if (cit == form_->bibkeys.end()) {
282                 if (dir == biblio::FORWARD) {
283                         start = form_->bibkeys.begin();
284                 }
285                 else start = form_->bibkeys.end() - 1;
286
287                 cit = biblio::searchKeys(theMap, form_->bibkeys, str,
288                                          start, type, dir, casesens);
289
290                 if (cit == form_->bibkeys.end())
291                         return;
292         }
293
294         int const found = int(cit - form_->bibkeys.begin());
295         if (found == sel) {
296                 return;
297         }
298
299         // Update the display
300         // note that we have multi selection mode!
301         ui_.availableLB->setSelected(sel, false);
302         ui_.availableLB->setSelected(found, true);
303         ui_.availableLB->setCurrentItem(found);
304         ui_.availableLB->ensureCurrentVisible();
305 }
306
307 } // namespace frontend
308 } // namespace lyx