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