]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiRef.cpp
4da6c346548e208d7c195fbece6065eb26c2e9d2
[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 #include <algorithm>
37
38 using std::find;
39 using std::vector;
40 using std::string;
41
42 namespace lyx {
43 namespace frontend {
44
45 using support::makeAbsPath;
46 using support::makeDisplayPath;
47
48 //FIXME It should be possible to eliminate lfun_name_
49 //now and recover that information from params().insetType().
50 //But let's not do that quite yet.
51 /// Flags what action is taken by Kernel::dispatch()
52 static std::string const lfun_name_ = "ref";
53
54 GuiRef::GuiRef(LyXView & lv)
55         : GuiDialog(lv, "ref"), params_("ref")
56 {
57         setupUi(this);
58         setViewTitle(_("Cross-reference"));
59
60         sort_ = false;
61         at_ref_ = 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(reset_dialog()));
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(bool)),
82                 this, SLOT(sortToggled(bool)));
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         setFocusProxy(refsLW);
91
92         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
93         bc().setOK(okPB);
94         bc().setApply(applyPB);
95         bc().setCancel(closePB);
96         bc().addReadOnly(refsLW);
97         bc().addReadOnly(sortCB);
98         bc().addReadOnly(nameED);
99         bc().addReadOnly(referenceED);
100         bc().addReadOnly(typeCO);
101         bc().addReadOnly(bufferCO);
102
103         restored_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::reset_dialog()
187 {
188         at_ref_ = false;
189         setGotoRef();
190 }
191
192
193 void GuiRef::closeEvent(QCloseEvent * e)
194 {
195         slotClose();
196         reset_dialog();
197         GuiDialog::closeEvent(e);
198 }
199
200
201 void GuiRef::updateContents()
202 {
203         int orig_type = typeCO->currentIndex();
204
205         referenceED->setText(toqstr(params_["reference"]));
206
207         nameED->setText(toqstr(params_["name"]));
208         nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
209
210         // restore type settings for new insets
211         if (params_["reference"].empty())
212                 typeCO->setCurrentIndex(orig_type);
213         else
214                 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
215         typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
216         if (!typeAllowed())
217                 typeCO->setCurrentIndex(0);
218
219         sortCB->setChecked(sort_);
220
221         // insert buffer list
222         bufferCO->clear();
223         vector<string> buffers = theBufferList().getFileNames();
224         for (vector<string>::iterator it = buffers.begin();
225              it != buffers.end(); ++it) {
226                 bufferCO->addItem(toqstr(lyx::to_utf8(makeDisplayPath(*it))));
227         }
228
229         // restore the buffer combo setting for new insets
230         if (params_["reference"].empty() && restored_buffer_ != -1
231         && restored_buffer_ < bufferCO->count())
232                 bufferCO->setCurrentIndex(restored_buffer_);
233         else
234                 bufferCO->setCurrentIndex(bufferNum());
235
236         updateRefs();
237         bc().setValid(false);
238 }
239
240
241 void GuiRef::applyView()
242 {
243         last_reference_ = referenceED->text();
244
245         params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
246         params_["reference"] = qstring_to_ucs4(last_reference_);
247         params_["name"] = qstring_to_ucs4(nameED->text());
248
249         restored_buffer_ = bufferCO->currentIndex();
250 }
251
252
253 bool GuiRef::nameAllowed()
254 {
255         KernelDocType const doc_type = docType();
256         return doc_type != LATEX && doc_type != LITERATE;
257 }
258
259
260 bool GuiRef::typeAllowed()
261 {
262         return docType() != DOCBOOK;
263 }
264
265
266 void GuiRef::setGoBack()
267 {
268         gotoPB->setText(qt_("&Go Back"));
269         gotoPB->setToolTip("");
270         gotoPB->setToolTip(qt_("Jump back"));
271 }
272
273
274 void GuiRef::setGotoRef()
275 {
276         gotoPB->setText(qt_("&Go to Label"));
277         gotoPB->setToolTip("");
278         gotoPB->setToolTip(qt_("Jump to label"));
279 }
280
281
282 void GuiRef::gotoRef()
283 {
284         string ref = fromqstr(referenceED->text());
285
286         if (at_ref_) {
287                 // go back
288                 setGotoRef();
289                 gotoBookmark();
290         } else {
291                 // go to the ref
292                 setGoBack();
293                 gotoRef(ref);
294         }
295         at_ref_ = !at_ref_;
296 }
297
298
299 void GuiRef::redoRefs()
300 {
301         // Prevent these widgets from emitting any signals whilst
302         // we modify their state.
303         refsLW->blockSignals(true);
304         referenceED->blockSignals(true);
305         refsLW->setUpdatesEnabled(false);
306
307         refsLW->clear();
308
309         // need this because Qt will send a highlight() here for
310         // the first item inserted
311         QString const oldSelection(referenceED->text());
312
313         for (std::vector<docstring>::const_iterator iter = refs_.begin();
314                 iter != refs_.end(); ++iter) {
315                 refsLW->addItem(toqstr(*iter));
316         }
317
318         if (sort_)
319                 refsLW->sortItems();
320
321         referenceED->setText(oldSelection);
322
323         // restore the last selection or, for new insets, highlight
324         // the previous selection
325         if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
326                 bool const newInset = oldSelection.isEmpty();
327                 QString textToFind = newInset ? last_reference_ : oldSelection;
328                 last_reference_.clear();
329                 for (int i = 0; i != refsLW->count(); ++i) {
330                         QListWidgetItem * item = refsLW->item(i);
331                         if (textToFind == item->text()) {
332                                 refsLW->setCurrentItem(item);
333                                 refsLW->setItemSelected(item, !newInset);
334                                 //Make sure selected item is visible
335                                 refsLW->scrollToItem(item);
336                                 last_reference_ = textToFind;
337                                 break;
338                         }
339                 }
340         }
341         refsLW->setUpdatesEnabled(true);
342         refsLW->update();
343
344         // Re-activate the emission of signals by these widgets.
345         refsLW->blockSignals(false);
346         referenceED->blockSignals(false);
347 }
348
349
350 void GuiRef::updateRefs()
351 {
352         refs_.clear();
353         string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
354         Buffer const * buf = theBufferList().getBuffer(makeAbsPath(name).absFilename());
355         buf->getLabelList(refs_);
356         sortCB->setEnabled(!refs_.empty());
357         refsLW->setEnabled(!refs_.empty());
358         gotoPB->setEnabled(!refs_.empty());
359         redoRefs();
360 }
361
362
363 bool GuiRef::isValid()
364 {
365         return !referenceED->text().isEmpty();
366 }
367
368
369 void GuiRef::gotoRef(string const & ref)
370 {
371         dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
372         dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
373 }
374
375
376 void GuiRef::gotoBookmark()
377 {
378         dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
379 }
380
381
382 int GuiRef::bufferNum() const
383 {
384         vector<string> buffers = theBufferList().getFileNames();
385         string const name = buffer().fileName();
386         vector<string>::const_iterator cit =
387                 std::find(buffers.begin(), buffers.end(), name);
388         if (cit == buffers.end())
389                 return 0;
390         return int(cit - buffers.begin());
391 }
392
393
394 bool GuiRef::initialiseParams(string const & data)
395 {
396         // The name passed with LFUN_INSET_APPLY is also the name
397         // used to identify the mailer.
398         InsetCommandMailer::string2params(lfun_name_, data, params_);
399         return true;
400 }
401
402
403 void GuiRef::clearParams()
404 {
405         params_.clear();
406 }
407
408
409 void GuiRef::dispatchParams()
410 {
411         string const lfun = InsetCommandMailer::params2string(lfun_name_, params_);
412         dispatch(FuncRequest(getLfun(), lfun));
413 }
414
415
416 Dialog * createGuiRef(LyXView & lv) { return new GuiRef(lv); }
417
418
419 } // namespace frontend
420 } // namespace lyx
421
422 #include "GuiRef_moc.cpp"