3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
17 #include "BufferList.h"
18 #include "FuncRequest.h"
20 #include "qt_helpers.h"
22 #include "insets/InsetRef.h"
24 #include "support/FileName.h"
25 #include "support/FileNameList.h"
26 #include "support/filetools.h" // makeAbsPath, makeDisplayPath
30 #include <QListWidget>
31 #include <QListWidgetItem>
32 #include <QPushButton>
34 #include <QCloseEvent>
37 using namespace lyx::support;
42 GuiRef::GuiRef(GuiView & lv)
43 : GuiDialog(lv, "ref", qt_("Cross-reference")),
44 params_(insetCode("ref"))
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);
55 bufferCO->setEnabled(false);
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()));
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()));
85 setFocusProxy(refsLW);
87 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
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);
98 restored_buffer_ = -1;
103 void GuiRef::changed_adaptor()
109 void GuiRef::gotoClicked()
115 void GuiRef::selectionChanged()
117 if (isBufferReadonly())
120 QList<QListWidgetItem *> selections = refsLW->selectedItems();
121 if (selections.isEmpty())
123 QListWidgetItem * sel = selections.first();
129 void GuiRef::refHighlighted(QListWidgetItem * sel)
131 if (isBufferReadonly())
134 /* int const cur_item = refsLW->currentRow();
135 bool const cur_item_selected = cur_item >= 0 ?
136 refsLB->isSelected(cur_item) : false;*/
137 bool const cur_item_selected = refsLW->isItemSelected(sel);
139 if (cur_item_selected)
140 referenceED->setText(sel->text());
144 gotoPB->setEnabled(true);
146 typeCO->setEnabled(true);
148 nameED->setEnabled(true);
152 void GuiRef::refSelected(QListWidgetItem * sel)
154 if (isBufferReadonly())
157 /* int const cur_item = refsLW->currentRow();
158 bool const cur_item_selected = cur_item >= 0 ?
159 refsLB->isSelected(cur_item) : false;*/
160 bool const cur_item_selected = refsLW->isItemSelected(sel);
162 if (cur_item_selected)
163 referenceED->setText(sel->text());
164 // <enter> or double click, inserts ref and closes dialog
169 void GuiRef::sortToggled(bool on)
176 void GuiRef::updateClicked()
182 void GuiRef::dialog_rejected()
185 // We have to do this manually, instead of calling slotClose(), because
186 // the dialog has already been made invisible before rejected() triggers.
187 Dialog::disconnect();
191 void GuiRef::reset_dialog()
198 void GuiRef::closeEvent(QCloseEvent * e)
206 void GuiRef::updateContents()
208 int orig_type = typeCO->currentIndex();
210 referenceED->setText(toqstr(params_["reference"]));
212 nameED->setText(toqstr(params_["name"]));
213 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
215 // restore type settings for new insets
216 if (params_["reference"].empty())
217 typeCO->setCurrentIndex(orig_type);
219 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
220 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
222 typeCO->setCurrentIndex(0);
224 sortCB->setChecked(sort_);
226 // insert buffer list
228 FileNameList const & buffers = theBufferList().fileNames();
229 for (FileNameList::const_iterator it = buffers.begin();
230 it != buffers.end(); ++it) {
231 bufferCO->addItem(toqstr(makeDisplayPath(it->absFilename())));
234 int const thebuffer = theBufferList().bufferNum(buffer().fileName());
235 // restore the buffer combo setting for new insets
236 if (params_["reference"].empty() && restored_buffer_ != -1
237 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
238 bufferCO->setCurrentIndex(restored_buffer_);
240 int const num = theBufferList().bufferNum(buffer().fileName());
241 bufferCO->setCurrentIndex(num);
242 if (thebuffer != active_buffer_)
243 restored_buffer_ = num;
245 active_buffer_ = thebuffer;
248 bc().setValid(false);
252 void GuiRef::applyView()
254 last_reference_ = referenceED->text();
256 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
257 params_["reference"] = qstring_to_ucs4(last_reference_);
258 params_["name"] = qstring_to_ucs4(nameED->text());
260 restored_buffer_ = bufferCO->currentIndex();
264 bool GuiRef::nameAllowed()
266 KernelDocType const doc_type = docType();
267 return doc_type != LATEX && doc_type != LITERATE;
271 bool GuiRef::typeAllowed()
273 return docType() != DOCBOOK;
277 void GuiRef::setGoBack()
279 gotoPB->setText(qt_("&Go Back"));
280 gotoPB->setToolTip("");
281 gotoPB->setToolTip(qt_("Jump back"));
285 void GuiRef::setGotoRef()
287 gotoPB->setText(qt_("&Go to Label"));
288 gotoPB->setToolTip("");
289 gotoPB->setToolTip(qt_("Jump to label"));
293 void GuiRef::gotoRef()
295 string ref = fromqstr(referenceED->text());
310 void GuiRef::redoRefs()
312 // Prevent these widgets from emitting any signals whilst
313 // we modify their state.
314 refsLW->blockSignals(true);
315 referenceED->blockSignals(true);
316 refsLW->setUpdatesEnabled(false);
320 // need this because Qt will send a highlight() here for
321 // the first item inserted
322 QString const oldSelection(referenceED->text());
324 for (vector<docstring>::const_iterator iter = refs_.begin();
325 iter != refs_.end(); ++iter) {
326 refsLW->addItem(toqstr(*iter));
332 referenceED->setText(oldSelection);
334 // restore the last selection or, for new insets, highlight
335 // the previous selection
336 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
337 bool const newInset = oldSelection.isEmpty();
338 QString textToFind = newInset ? last_reference_ : oldSelection;
339 last_reference_.clear();
340 for (int i = 0; i != refsLW->count(); ++i) {
341 QListWidgetItem * item = refsLW->item(i);
342 if (textToFind == item->text()) {
343 refsLW->setCurrentItem(item);
344 refsLW->setItemSelected(item, !newInset);
345 //Make sure selected item is visible
346 refsLW->scrollToItem(item);
347 last_reference_ = textToFind;
352 refsLW->setUpdatesEnabled(true);
355 // Re-activate the emission of signals by these widgets.
356 refsLW->blockSignals(false);
357 referenceED->blockSignals(false);
361 void GuiRef::updateRefs()
364 FileName const & name = theBufferList().fileNames()[bufferCO->currentIndex()];
365 Buffer const * buf = theBufferList().getBuffer(name);
366 buf->getLabelList(refs_);
367 sortCB->setEnabled(!refs_.empty());
368 refsLW->setEnabled(!refs_.empty());
369 // refsLW should only be the focus proxy when it is enabled
370 setFocusProxy(refs_.empty() ? 0 : refsLW);
371 gotoPB->setEnabled(!refs_.empty());
376 bool GuiRef::isValid()
378 return !referenceED->text().isEmpty();
382 void GuiRef::gotoRef(string const & ref)
384 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
385 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
389 void GuiRef::gotoBookmark()
391 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
395 bool GuiRef::initialiseParams(std::string const & data)
397 InsetCommand::string2params("ref", data, params_);
402 void GuiRef::dispatchParams()
404 std::string const lfun = InsetCommand::params2string("ref", params_);
405 dispatch(FuncRequest(getLfun(), lfun));
410 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
413 } // namespace frontend
416 #include "moc_GuiRef.cpp"