]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiRef.cpp
2f59e6913d40a724f7279d42f8dbf39ebb96353d
[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(reset_dialog()));
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::reset_dialog()
182 {
183         at_ref_ = false;
184         setGotoRef();
185 }
186
187
188 void GuiRef::closeEvent(QCloseEvent * e)
189 {
190         slotClose();
191         reset_dialog();
192         e->accept();
193 }
194
195
196 void GuiRef::updateContents()
197 {
198         int orig_type = typeCO->currentIndex();
199
200         referenceED->setText(toqstr(params_["reference"]));
201
202         nameED->setText(toqstr(params_["name"]));
203         nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
204
205         // restore type settings for new insets
206         if (params_["reference"].empty())
207                 typeCO->setCurrentIndex(orig_type);
208         else
209                 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
210         typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
211         if (!typeAllowed())
212                 typeCO->setCurrentIndex(0);
213
214         sortCB->setChecked(sort_);
215
216         // insert buffer list
217         bufferCO->clear();
218         FileNameList const & buffers = theBufferList().fileNames();
219         for (FileNameList::const_iterator it = buffers.begin();
220              it != buffers.end(); ++it) {
221                 bufferCO->addItem(toqstr(makeDisplayPath(it->absFilename())));
222         }
223
224         // restore the buffer combo setting for new insets
225         if (params_["reference"].empty() && restored_buffer_ != -1
226             && restored_buffer_ < bufferCO->count()) 
227                 bufferCO->setCurrentIndex(restored_buffer_);
228         else {
229                 int num = theBufferList().bufferNum(buffer().fileName());
230                 bufferCO->setCurrentIndex(num);
231         }
232
233         updateRefs();
234         bc().setValid(false);
235 }
236
237
238 void GuiRef::applyView()
239 {
240         last_reference_ = referenceED->text();
241
242         params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
243         params_["reference"] = qstring_to_ucs4(last_reference_);
244         params_["name"] = qstring_to_ucs4(nameED->text());
245
246         restored_buffer_ = bufferCO->currentIndex();
247 }
248
249
250 bool GuiRef::nameAllowed()
251 {
252         KernelDocType const doc_type = docType();
253         return doc_type != LATEX && doc_type != LITERATE;
254 }
255
256
257 bool GuiRef::typeAllowed()
258 {
259         return docType() != DOCBOOK;
260 }
261
262
263 void GuiRef::setGoBack()
264 {
265         gotoPB->setText(qt_("&Go Back"));
266         gotoPB->setToolTip("");
267         gotoPB->setToolTip(qt_("Jump back"));
268 }
269
270
271 void GuiRef::setGotoRef()
272 {
273         gotoPB->setText(qt_("&Go to Label"));
274         gotoPB->setToolTip("");
275         gotoPB->setToolTip(qt_("Jump to label"));
276 }
277
278
279 void GuiRef::gotoRef()
280 {
281         string ref = fromqstr(referenceED->text());
282
283         if (at_ref_) {
284                 // go back
285                 setGotoRef();
286                 gotoBookmark();
287         } else {
288                 // go to the ref
289                 setGoBack();
290                 gotoRef(ref);
291         }
292         at_ref_ = !at_ref_;
293 }
294
295
296 void GuiRef::redoRefs()
297 {
298         // Prevent these widgets from emitting any signals whilst
299         // we modify their state.
300         refsLW->blockSignals(true);
301         referenceED->blockSignals(true);
302         refsLW->setUpdatesEnabled(false);
303
304         refsLW->clear();
305
306         // need this because Qt will send a highlight() here for
307         // the first item inserted
308         QString const oldSelection(referenceED->text());
309
310         for (vector<docstring>::const_iterator iter = refs_.begin();
311                 iter != refs_.end(); ++iter) {
312                 refsLW->addItem(toqstr(*iter));
313         }
314
315         if (sort_)
316                 refsLW->sortItems();
317
318         referenceED->setText(oldSelection);
319
320         // restore the last selection or, for new insets, highlight
321         // the previous selection
322         if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
323                 bool const newInset = oldSelection.isEmpty();
324                 QString textToFind = newInset ? last_reference_ : oldSelection;
325                 last_reference_.clear();
326                 for (int i = 0; i != refsLW->count(); ++i) {
327                         QListWidgetItem * item = refsLW->item(i);
328                         if (textToFind == item->text()) {
329                                 refsLW->setCurrentItem(item);
330                                 refsLW->setItemSelected(item, !newInset);
331                                 //Make sure selected item is visible
332                                 refsLW->scrollToItem(item);
333                                 last_reference_ = textToFind;
334                                 break;
335                         }
336                 }
337         }
338         refsLW->setUpdatesEnabled(true);
339         refsLW->update();
340
341         // Re-activate the emission of signals by these widgets.
342         refsLW->blockSignals(false);
343         referenceED->blockSignals(false);
344 }
345
346
347 void GuiRef::updateRefs()
348 {
349         refs_.clear();
350         FileName const & name = theBufferList().fileNames()[bufferCO->currentIndex()];
351         Buffer const * buf = theBufferList().getBuffer(name);
352         buf->getLabelList(refs_);
353         sortCB->setEnabled(!refs_.empty());
354         refsLW->setEnabled(!refs_.empty());
355         gotoPB->setEnabled(!refs_.empty());
356         redoRefs();
357 }
358
359
360 bool GuiRef::isValid()
361 {
362         return !referenceED->text().isEmpty();
363 }
364
365
366 void GuiRef::gotoRef(string const & ref)
367 {
368         dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
369         dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
370 }
371
372
373 void GuiRef::gotoBookmark()
374 {
375         dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
376 }
377
378
379 bool GuiRef::initialiseParams(std::string const & data)
380 {
381         InsetCommand::string2params("ref", data, params_);
382         return true;
383 }
384
385
386 void GuiRef::dispatchParams()
387 {
388         std::string const lfun = InsetCommand::params2string("ref", params_);
389         dispatch(FuncRequest(getLfun(), lfun));
390 }
391
392
393
394 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
395
396
397 } // namespace frontend
398 } // namespace lyx
399
400 #include "GuiRef_moc.cpp"