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