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