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