]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiRef.cpp
Fix bug http://www.lyx.org/trac/ticket/5812
[lyx.git] / src / frontends / qt4 / GuiRef.cpp
1 /**
2  * \file GuiRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiRef.h"
15
16 #include "Buffer.h"
17 #include "BufferList.h"
18 #include "FuncRequest.h"
19
20 #include "qt_helpers.h"
21
22 #include "insets/InsetRef.h"
23
24 #include "support/FileName.h"
25 #include "support/FileNameList.h"
26 #include "support/filetools.h" // makeAbsPath, makeDisplayPath
27
28 #include <QLineEdit>
29 #include <QCheckBox>
30 #include <QListWidget>
31 #include <QListWidgetItem>
32 #include <QPushButton>
33 #include <QToolTip>
34 #include <QCloseEvent>
35
36 using namespace std;
37 using namespace lyx::support;
38
39 namespace lyx {
40 namespace frontend {
41
42 GuiRef::GuiRef(GuiView & lv)
43         : GuiDialog(lv, "ref", qt_("Cross-reference")),
44           params_(insetCode("ref"))
45 {
46         setupUi(this);
47
48         at_ref_ = false;
49
50         //FIXME: when/if we support the xr package for cross-reference
51         //between independant files. Those can be re-enabled.
52         refsL->setEnabled(false);
53         refsL->hide();
54         bufferCO->setEnabled(false);
55         bufferCO->hide();
56
57         // Enabling is set in updateRefs. Disable for now in case no
58         // call to updateContents follows (e.g. read-only documents).
59         sortCB->setEnabled(false);
60         caseSensitiveCB->setEnabled(false);
61         caseSensitiveCB->setChecked(false);
62         refsLW->setEnabled(false);
63         gotoPB->setEnabled(false);
64
65         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
66         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
67         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
68         connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
69         connect(this, SIGNAL(rejected()), this, SLOT(dialog_rejected()));
70
71         connect(typeCO, SIGNAL(activated(int)),
72                 this, SLOT(changed_adaptor()));
73         connect(referenceED, SIGNAL(textChanged(QString)),
74                 this, SLOT(changed_adaptor()));
75         connect(nameED, SIGNAL(textChanged(QString)),
76                 this, SLOT(changed_adaptor()));
77         connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
78                 this, SLOT(refHighlighted(QListWidgetItem *)));
79         connect(refsLW, SIGNAL(itemSelectionChanged()),
80                 this, SLOT(selectionChanged()));
81         connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
82                 this, SLOT(refSelected(QListWidgetItem *)));
83         connect(sortCB, SIGNAL(clicked()),
84                 this, SLOT(sortToggled()));
85         connect(caseSensitiveCB, SIGNAL(clicked()),
86                 this, SLOT(caseSensitiveToggled()));
87         connect(gotoPB, SIGNAL(clicked()),
88                 this, SLOT(gotoClicked()));
89         connect(updatePB, SIGNAL(clicked()),
90                 this, SLOT(updateClicked()));
91         connect(bufferCO, SIGNAL(activated(int)),
92                 this, SLOT(updateClicked()));
93
94         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
95         bc().setOK(okPB);
96         bc().setApply(applyPB);
97         bc().setCancel(closePB);
98         bc().addReadOnly(refsLW);
99         bc().addReadOnly(sortCB);
100         bc().addReadOnly(caseSensitiveCB);
101         bc().addReadOnly(nameED);
102         bc().addReadOnly(referenceED);
103         bc().addReadOnly(typeCO);
104         bc().addReadOnly(bufferCO);
105
106         restored_buffer_ = -1;
107         active_buffer_ = -1;
108 }
109
110
111 void GuiRef::changed_adaptor()
112 {
113         changed();
114 }
115
116
117 void GuiRef::gotoClicked()
118 {
119         gotoRef();
120 }
121
122
123 void GuiRef::selectionChanged()
124 {
125         if (isBufferReadonly())
126                 return;
127
128         QList<QListWidgetItem *> selections = refsLW->selectedItems();
129         if (selections.isEmpty())
130                 return;
131         QListWidgetItem * sel = selections.first();
132         refHighlighted(sel);
133         return;
134 }
135
136
137 void GuiRef::refHighlighted(QListWidgetItem * sel)
138 {
139         if (isBufferReadonly())
140                 return;
141
142 /*      int const cur_item = refsLW->currentRow();
143         bool const cur_item_selected = cur_item >= 0 ?
144                 refsLB->isSelected(cur_item) : false;*/
145         bool const cur_item_selected = refsLW->isItemSelected(sel);
146
147         if (cur_item_selected)
148                 referenceED->setText(sel->text());
149
150         if (at_ref_)
151                 gotoRef();
152         gotoPB->setEnabled(true);
153         if (typeAllowed())
154                 typeCO->setEnabled(true);
155         if (nameAllowed())
156                 nameED->setEnabled(true);
157 }
158
159
160 void GuiRef::refSelected(QListWidgetItem * sel)
161 {
162         if (isBufferReadonly())
163                 return;
164
165 /*      int const cur_item = refsLW->currentRow();
166         bool const cur_item_selected = cur_item >= 0 ?
167                 refsLB->isSelected(cur_item) : false;*/
168         bool const cur_item_selected = refsLW->isItemSelected(sel);
169
170         if (cur_item_selected)
171                 referenceED->setText(sel->text());
172         // <enter> or double click, inserts ref and closes dialog
173         slotOK();
174 }
175
176
177 void GuiRef::sortToggled()
178 {
179         caseSensitiveCB->setEnabled(sortCB->isChecked());
180         redoRefs();
181 }
182
183
184 void GuiRef::caseSensitiveToggled()
185 {
186         redoRefs();
187 }
188
189
190 void GuiRef::updateClicked()
191 {
192         updateRefs();
193 }
194
195
196 void GuiRef::dialog_rejected()
197 {
198         reset_dialog();
199         // We have to do this manually, instead of calling slotClose(), because
200         // the dialog has already been made invisible before rejected() triggers.
201         Dialog::disconnect();
202 }
203
204
205 void GuiRef::reset_dialog()
206 {
207         at_ref_ = false;
208         setGotoRef();
209 }
210
211
212 void GuiRef::closeEvent(QCloseEvent * e)
213 {
214         slotClose();
215         reset_dialog();
216         e->accept();
217 }
218
219
220 void GuiRef::updateContents()
221 {
222         int orig_type = typeCO->currentIndex();
223
224         referenceED->setText(toqstr(params_["reference"]));
225
226         nameED->setText(toqstr(params_["name"]));
227         nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
228
229         // restore type settings for new insets
230         if (params_["reference"].empty())
231                 typeCO->setCurrentIndex(orig_type);
232         else
233                 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
234         typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
235         if (!typeAllowed())
236                 typeCO->setCurrentIndex(0);
237
238         // insert buffer list
239         bufferCO->clear();
240         FileNameList const & buffers = theBufferList().fileNames();
241         for (FileNameList::const_iterator it = buffers.begin();
242              it != buffers.end(); ++it) {
243                 bufferCO->addItem(toqstr(makeDisplayPath(it->absFilename())));
244         }
245
246         int const thebuffer = theBufferList().bufferNum(buffer().fileName());
247         // restore the buffer combo setting for new insets
248         if (params_["reference"].empty() && restored_buffer_ != -1
249             && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
250                 bufferCO->setCurrentIndex(restored_buffer_);
251         else {
252                 int const num = theBufferList().bufferNum(buffer().fileName());
253                 bufferCO->setCurrentIndex(num);
254                 if (thebuffer != active_buffer_)
255                         restored_buffer_ = num;
256         }
257         active_buffer_ = thebuffer;
258
259         updateRefs();
260         bc().setValid(false);
261 }
262
263
264 void GuiRef::applyView()
265 {
266         last_reference_ = referenceED->text();
267
268         params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
269         params_["reference"] = qstring_to_ucs4(last_reference_);
270         params_["name"] = qstring_to_ucs4(nameED->text());
271
272         restored_buffer_ = bufferCO->currentIndex();
273 }
274
275
276 bool GuiRef::nameAllowed()
277 {
278         KernelDocType const doc_type = docType();
279         return doc_type != LATEX && doc_type != LITERATE;
280 }
281
282
283 bool GuiRef::typeAllowed()
284 {
285         return docType() != DOCBOOK;
286 }
287
288
289 void GuiRef::setGoBack()
290 {
291         gotoPB->setText(qt_("&Go Back"));
292         gotoPB->setToolTip("");
293         gotoPB->setToolTip(qt_("Jump back"));
294 }
295
296
297 void GuiRef::setGotoRef()
298 {
299         gotoPB->setText(qt_("&Go to Label"));
300         gotoPB->setToolTip("");
301         gotoPB->setToolTip(qt_("Jump to label"));
302 }
303
304
305 void GuiRef::gotoRef()
306 {
307         string ref = fromqstr(referenceED->text());
308
309         if (at_ref_) {
310                 // go back
311                 setGotoRef();
312                 gotoBookmark();
313         } else {
314                 // go to the ref
315                 setGoBack();
316                 gotoRef(ref);
317         }
318         at_ref_ = !at_ref_;
319 }
320
321 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
322 {
323         return s1.toLower() < s2.toLower();
324 }
325
326
327 void GuiRef::redoRefs()
328 {
329         // Prevent these widgets from emitting any signals whilst
330         // we modify their state.
331         refsLW->blockSignals(true);
332         referenceED->blockSignals(true);
333         refsLW->setUpdatesEnabled(false);
334
335         refsLW->clear();
336
337         // need this because Qt will send a highlight() here for
338         // the first item inserted
339         QString const oldSelection(referenceED->text());
340
341         QStringList refsStrings;
342         vector<docstring>::const_iterator iter;
343         for (iter = refs_.begin(); iter != refs_.end(); ++iter)
344                 refsStrings.append(toqstr(*iter));
345
346         if (sortCB->isEnabled() && sortCB->isChecked()) {
347                 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
348                         qSort(refsStrings.begin(), refsStrings.end());
349                 else
350                         qSort(refsStrings.begin(), refsStrings.end(),
351                               caseInsensitiveLessThan /*defined above*/);
352         }
353
354         refsLW->addItems(refsStrings);
355
356         referenceED->setText(oldSelection);
357
358         // restore the last selection or, for new insets, highlight
359         // the previous selection
360         if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
361                 bool const newInset = oldSelection.isEmpty();
362                 QString textToFind = newInset ? last_reference_ : oldSelection;
363                 last_reference_.clear();
364                 for (int i = 0; i != refsLW->count(); ++i) {
365                         QListWidgetItem * item = refsLW->item(i);
366                         if (textToFind == item->text()) {
367                                 refsLW->setCurrentItem(item);
368                                 refsLW->setItemSelected(item, !newInset);
369                                 //Make sure selected item is visible
370                                 refsLW->scrollToItem(item);
371                                 last_reference_ = textToFind;
372                                 break;
373                         }
374                 }
375         }
376         refsLW->setUpdatesEnabled(true);
377         refsLW->update();
378
379         // Re-activate the emission of signals by these widgets.
380         refsLW->blockSignals(false);
381         referenceED->blockSignals(false);
382 }
383
384
385 void GuiRef::updateRefs()
386 {
387         refs_.clear();
388         int const the_buffer = bufferCO->currentIndex();
389         if (the_buffer != -1) {
390                 FileName const & name = theBufferList().fileNames()[the_buffer];
391                 Buffer const * buf = theBufferList().getBuffer(name);
392                 buf->getLabelList(refs_);
393         }       
394         sortCB->setEnabled(!refs_.empty());
395         caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
396         refsLW->setEnabled(!refs_.empty());
397         // refsLW should only be the focus proxy when it is enabled
398         setFocusProxy(refs_.empty() ? 0 : refsLW);
399         gotoPB->setEnabled(!refs_.empty());
400         redoRefs();
401 }
402
403
404 bool GuiRef::isValid()
405 {
406         return !referenceED->text().isEmpty();
407 }
408
409
410 void GuiRef::gotoRef(string const & ref)
411 {
412         dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
413         dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
414 }
415
416
417 void GuiRef::gotoBookmark()
418 {
419         dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
420 }
421
422
423 bool GuiRef::initialiseParams(std::string const & data)
424 {
425         InsetCommand::string2params("ref", data, params_);
426         return true;
427 }
428
429
430 void GuiRef::dispatchParams()
431 {
432         std::string const lfun = InsetCommand::params2string("ref", params_);
433         dispatch(FuncRequest(getLfun(), lfun));
434 }
435
436
437
438 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
439
440
441 } // namespace frontend
442 } // namespace lyx
443
444 #include "moc_GuiRef.cpp"