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