]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiRef.cpp
d344ab64faaa77255f82ef56983b4a238cb9e2c5
[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         refsLW->setEnabled(false);
61         gotoPB->setEnabled(false);
62
63         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
64         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
65         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
66         connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
67         connect(this, SIGNAL(rejected()), this, SLOT(dialog_rejected()));
68
69         connect(typeCO, SIGNAL(activated(int)),
70                 this, SLOT(changed_adaptor()));
71         connect(referenceED, SIGNAL(textChanged(QString)),
72                 this, SLOT(changed_adaptor()));
73         connect(nameED, SIGNAL(textChanged(QString)),
74                 this, SLOT(changed_adaptor()));
75         connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
76                 this, SLOT(refHighlighted(QListWidgetItem *)));
77         connect(refsLW, SIGNAL(itemSelectionChanged()),
78                 this, SLOT(selectionChanged()));
79         connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
80                 this, SLOT(refSelected(QListWidgetItem *)));
81         connect(sortCB, SIGNAL(clicked()),
82                 this, SLOT(sortToggled()));
83         connect(gotoPB, SIGNAL(clicked()),
84                 this, SLOT(gotoClicked()));
85         connect(updatePB, SIGNAL(clicked()),
86                 this, SLOT(updateClicked()));
87         connect(bufferCO, SIGNAL(activated(int)),
88                 this, SLOT(updateClicked()));
89
90         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
91         bc().setOK(okPB);
92         bc().setApply(applyPB);
93         bc().setCancel(closePB);
94         bc().addReadOnly(refsLW);
95         bc().addReadOnly(sortCB);
96         bc().addReadOnly(nameED);
97         bc().addReadOnly(referenceED);
98         bc().addReadOnly(typeCO);
99         bc().addReadOnly(bufferCO);
100
101         restored_buffer_ = -1;
102         active_buffer_ = -1;
103 }
104
105
106 void GuiRef::changed_adaptor()
107 {
108         changed();
109 }
110
111
112 void GuiRef::gotoClicked()
113 {
114         gotoRef();
115 }
116
117
118 void GuiRef::selectionChanged()
119 {
120         if (isBufferReadonly())
121                 return;
122
123         QList<QListWidgetItem *> selections = refsLW->selectedItems();
124         if (selections.isEmpty())
125                 return;
126         QListWidgetItem * sel = selections.first();
127         refHighlighted(sel);
128         return;
129 }
130
131
132 void GuiRef::refHighlighted(QListWidgetItem * sel)
133 {
134         if (isBufferReadonly())
135                 return;
136
137 /*      int const cur_item = refsLW->currentRow();
138         bool const cur_item_selected = cur_item >= 0 ?
139                 refsLB->isSelected(cur_item) : false;*/
140         bool const cur_item_selected = refsLW->isItemSelected(sel);
141
142         if (cur_item_selected)
143                 referenceED->setText(sel->text());
144
145         if (at_ref_)
146                 gotoRef();
147         gotoPB->setEnabled(true);
148         if (typeAllowed())
149                 typeCO->setEnabled(true);
150         if (nameAllowed())
151                 nameED->setEnabled(true);
152 }
153
154
155 void GuiRef::refSelected(QListWidgetItem * sel)
156 {
157         if (isBufferReadonly())
158                 return;
159
160 /*      int const cur_item = refsLW->currentRow();
161         bool const cur_item_selected = cur_item >= 0 ?
162                 refsLB->isSelected(cur_item) : false;*/
163         bool const cur_item_selected = refsLW->isItemSelected(sel);
164
165         if (cur_item_selected)
166                 referenceED->setText(sel->text());
167         // <enter> or double click, inserts ref and closes dialog
168         slotOK();
169 }
170
171
172 void GuiRef::sortToggled()
173 {
174         redoRefs();
175 }
176
177
178 void GuiRef::updateClicked()
179 {
180         updateRefs();
181 }
182
183
184 void GuiRef::dialog_rejected()
185 {
186         reset_dialog();
187         // We have to do this manually, instead of calling slotClose(), because
188         // the dialog has already been made invisible before rejected() triggers.
189         Dialog::disconnect();
190 }
191
192
193 void GuiRef::reset_dialog()
194 {
195         at_ref_ = false;
196         setGotoRef();
197 }
198
199
200 void GuiRef::closeEvent(QCloseEvent * e)
201 {
202         slotClose();
203         reset_dialog();
204         e->accept();
205 }
206
207
208 void GuiRef::updateContents()
209 {
210         int orig_type = typeCO->currentIndex();
211
212         referenceED->setText(toqstr(params_["reference"]));
213
214         nameED->setText(toqstr(params_["name"]));
215         nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
216
217         // restore type settings for new insets
218         if (params_["reference"].empty())
219                 typeCO->setCurrentIndex(orig_type);
220         else
221                 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
222         typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
223         if (!typeAllowed())
224                 typeCO->setCurrentIndex(0);
225
226         // insert buffer list
227         bufferCO->clear();
228         FileNameList const & buffers = theBufferList().fileNames();
229         for (FileNameList::const_iterator it = buffers.begin();
230              it != buffers.end(); ++it) {
231                 bufferCO->addItem(toqstr(makeDisplayPath(it->absFilename())));
232         }
233
234         int const thebuffer = theBufferList().bufferNum(buffer().fileName());
235         // restore the buffer combo setting for new insets
236         if (params_["reference"].empty() && restored_buffer_ != -1
237             && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
238                 bufferCO->setCurrentIndex(restored_buffer_);
239         else {
240                 int const num = theBufferList().bufferNum(buffer().fileName());
241                 bufferCO->setCurrentIndex(num);
242                 if (thebuffer != active_buffer_)
243                         restored_buffer_ = num;
244         }
245         active_buffer_ = thebuffer;
246
247         updateRefs();
248         bc().setValid(false);
249 }
250
251
252 void GuiRef::applyView()
253 {
254         last_reference_ = referenceED->text();
255
256         params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
257         params_["reference"] = qstring_to_ucs4(last_reference_);
258         params_["name"] = qstring_to_ucs4(nameED->text());
259
260         restored_buffer_ = bufferCO->currentIndex();
261 }
262
263
264 bool GuiRef::nameAllowed()
265 {
266         KernelDocType const doc_type = docType();
267         return doc_type != LATEX && doc_type != LITERATE;
268 }
269
270
271 bool GuiRef::typeAllowed()
272 {
273         return docType() != DOCBOOK;
274 }
275
276
277 void GuiRef::setGoBack()
278 {
279         gotoPB->setText(qt_("&Go Back"));
280         gotoPB->setToolTip("");
281         gotoPB->setToolTip(qt_("Jump back"));
282 }
283
284
285 void GuiRef::setGotoRef()
286 {
287         gotoPB->setText(qt_("&Go to Label"));
288         gotoPB->setToolTip("");
289         gotoPB->setToolTip(qt_("Jump to label"));
290 }
291
292
293 void GuiRef::gotoRef()
294 {
295         string ref = fromqstr(referenceED->text());
296
297         if (at_ref_) {
298                 // go back
299                 setGotoRef();
300                 gotoBookmark();
301         } else {
302                 // go to the ref
303                 setGoBack();
304                 gotoRef(ref);
305         }
306         at_ref_ = !at_ref_;
307 }
308
309
310 void GuiRef::redoRefs()
311 {
312         // Prevent these widgets from emitting any signals whilst
313         // we modify their state.
314         refsLW->blockSignals(true);
315         referenceED->blockSignals(true);
316         refsLW->setUpdatesEnabled(false);
317
318         refsLW->clear();
319
320         // need this because Qt will send a highlight() here for
321         // the first item inserted
322         QString const oldSelection(referenceED->text());
323
324         for (vector<docstring>::const_iterator iter = refs_.begin();
325                 iter != refs_.end(); ++iter) {
326                 refsLW->addItem(toqstr(*iter));
327         }
328
329         if (sortCB->isEnabled() && sortCB->isChecked())
330                 refsLW->sortItems();
331
332         referenceED->setText(oldSelection);
333
334         // restore the last selection or, for new insets, highlight
335         // the previous selection
336         if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
337                 bool const newInset = oldSelection.isEmpty();
338                 QString textToFind = newInset ? last_reference_ : oldSelection;
339                 last_reference_.clear();
340                 for (int i = 0; i != refsLW->count(); ++i) {
341                         QListWidgetItem * item = refsLW->item(i);
342                         if (textToFind == item->text()) {
343                                 refsLW->setCurrentItem(item);
344                                 refsLW->setItemSelected(item, !newInset);
345                                 //Make sure selected item is visible
346                                 refsLW->scrollToItem(item);
347                                 last_reference_ = textToFind;
348                                 break;
349                         }
350                 }
351         }
352         refsLW->setUpdatesEnabled(true);
353         refsLW->update();
354
355         // Re-activate the emission of signals by these widgets.
356         refsLW->blockSignals(false);
357         referenceED->blockSignals(false);
358 }
359
360
361 void GuiRef::updateRefs()
362 {
363         refs_.clear();
364         int const the_buffer = bufferCO->currentIndex();
365         if (the_buffer != -1) {
366                 FileName const & name = theBufferList().fileNames()[the_buffer];
367                 Buffer const * buf = theBufferList().getBuffer(name);
368                 buf->getLabelList(refs_);
369         }       
370         sortCB->setEnabled(!refs_.empty());
371         refsLW->setEnabled(!refs_.empty());
372         // refsLW should only be the focus proxy when it is enabled
373         setFocusProxy(refs_.empty() ? 0 : refsLW);
374         gotoPB->setEnabled(!refs_.empty());
375         redoRefs();
376 }
377
378
379 bool GuiRef::isValid()
380 {
381         return !referenceED->text().isEmpty();
382 }
383
384
385 void GuiRef::gotoRef(string const & ref)
386 {
387         dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
388         dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
389 }
390
391
392 void GuiRef::gotoBookmark()
393 {
394         dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
395 }
396
397
398 bool GuiRef::initialiseParams(std::string const & data)
399 {
400         InsetCommand::string2params("ref", data, params_);
401         return true;
402 }
403
404
405 void GuiRef::dispatchParams()
406 {
407         std::string const lfun = InsetCommand::params2string("ref", params_);
408         dispatch(FuncRequest(getLfun(), lfun));
409 }
410
411
412
413 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
414
415
416 } // namespace frontend
417 } // namespace lyx
418
419 #include "moc_GuiRef.cpp"