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 // Enabling is set in updateRefs. Disable for now in case no
59 // call to updateContents follows (e.g. read-only documents).
60 sortCB->setEnabled(false);
61 refsLW->setEnabled(false);
62 gotoPB->setEnabled(false);
64 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
65 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
66 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
67 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
68 connect(this, SIGNAL(rejected()), this, SLOT(dialog_rejected()));
70 connect(typeCO, SIGNAL(activated(int)),
71 this, SLOT(changed_adaptor()));
72 connect(referenceED, SIGNAL(textChanged(QString)),
73 this, SLOT(changed_adaptor()));
74 connect(nameED, SIGNAL(textChanged(QString)),
75 this, SLOT(changed_adaptor()));
76 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
77 this, SLOT(refHighlighted(QListWidgetItem *)));
78 connect(refsLW, SIGNAL(itemSelectionChanged()),
79 this, SLOT(selectionChanged()));
80 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
81 this, SLOT(refSelected(QListWidgetItem *)));
82 connect(sortCB, SIGNAL(clicked(bool)),
83 this, SLOT(sortToggled(bool)));
84 connect(gotoPB, SIGNAL(clicked()),
85 this, SLOT(gotoClicked()));
86 connect(updatePB, SIGNAL(clicked()),
87 this, SLOT(updateClicked()));
88 connect(bufferCO, SIGNAL(activated(int)),
89 this, SLOT(updateClicked()));
91 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
93 bc().setApply(applyPB);
94 bc().setCancel(closePB);
95 bc().addReadOnly(refsLW);
96 bc().addReadOnly(sortCB);
97 bc().addReadOnly(nameED);
98 bc().addReadOnly(referenceED);
99 bc().addReadOnly(typeCO);
100 bc().addReadOnly(bufferCO);
102 restored_buffer_ = -1;
107 void GuiRef::changed_adaptor()
113 void GuiRef::gotoClicked()
119 void GuiRef::selectionChanged()
121 if (isBufferReadonly())
124 QList<QListWidgetItem *> selections = refsLW->selectedItems();
125 if (selections.isEmpty())
127 QListWidgetItem * sel = selections.first();
133 void GuiRef::refHighlighted(QListWidgetItem * sel)
135 if (isBufferReadonly())
138 /* int const cur_item = refsLW->currentRow();
139 bool const cur_item_selected = cur_item >= 0 ?
140 refsLB->isSelected(cur_item) : false;*/
141 bool const cur_item_selected = refsLW->isItemSelected(sel);
143 if (cur_item_selected)
144 referenceED->setText(sel->text());
148 gotoPB->setEnabled(true);
150 typeCO->setEnabled(true);
152 nameED->setEnabled(true);
156 void GuiRef::refSelected(QListWidgetItem * sel)
158 if (isBufferReadonly())
161 /* int const cur_item = refsLW->currentRow();
162 bool const cur_item_selected = cur_item >= 0 ?
163 refsLB->isSelected(cur_item) : false;*/
164 bool const cur_item_selected = refsLW->isItemSelected(sel);
166 if (cur_item_selected)
167 referenceED->setText(sel->text());
168 // <enter> or double click, inserts ref and closes dialog
173 void GuiRef::sortToggled(bool on)
180 void GuiRef::updateClicked()
186 void GuiRef::dialog_rejected()
189 // We have to do this manually, instead of calling slotClose(), because
190 // the dialog has already been made invisible before rejected() triggers.
191 Dialog::disconnect();
195 void GuiRef::reset_dialog()
202 void GuiRef::closeEvent(QCloseEvent * e)
210 void GuiRef::updateContents()
212 int orig_type = typeCO->currentIndex();
214 referenceED->setText(toqstr(params_["reference"]));
216 nameED->setText(toqstr(params_["name"]));
217 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
219 // restore type settings for new insets
220 if (params_["reference"].empty())
221 typeCO->setCurrentIndex(orig_type);
223 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
224 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
226 typeCO->setCurrentIndex(0);
228 sortCB->setChecked(sort_);
230 // insert buffer list
232 FileNameList const & buffers = theBufferList().fileNames();
233 for (FileNameList::const_iterator it = buffers.begin();
234 it != buffers.end(); ++it) {
235 bufferCO->addItem(toqstr(makeDisplayPath(it->absFilename())));
238 int const thebuffer = theBufferList().bufferNum(buffer().fileName());
239 // restore the buffer combo setting for new insets
240 if (params_["reference"].empty() && restored_buffer_ != -1
241 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
242 bufferCO->setCurrentIndex(restored_buffer_);
244 int const num = theBufferList().bufferNum(buffer().fileName());
245 bufferCO->setCurrentIndex(num);
246 if (thebuffer != active_buffer_)
247 restored_buffer_ = num;
249 active_buffer_ = thebuffer;
252 bc().setValid(false);
256 void GuiRef::applyView()
258 last_reference_ = referenceED->text();
260 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
261 params_["reference"] = qstring_to_ucs4(last_reference_);
262 params_["name"] = qstring_to_ucs4(nameED->text());
264 restored_buffer_ = bufferCO->currentIndex();
268 bool GuiRef::nameAllowed()
270 KernelDocType const doc_type = docType();
271 return doc_type != LATEX && doc_type != LITERATE;
275 bool GuiRef::typeAllowed()
277 return docType() != DOCBOOK;
281 void GuiRef::setGoBack()
283 gotoPB->setText(qt_("&Go Back"));
284 gotoPB->setToolTip("");
285 gotoPB->setToolTip(qt_("Jump back"));
289 void GuiRef::setGotoRef()
291 gotoPB->setText(qt_("&Go to Label"));
292 gotoPB->setToolTip("");
293 gotoPB->setToolTip(qt_("Jump to label"));
297 void GuiRef::gotoRef()
299 string ref = fromqstr(referenceED->text());
314 void GuiRef::redoRefs()
316 // Prevent these widgets from emitting any signals whilst
317 // we modify their state.
318 refsLW->blockSignals(true);
319 referenceED->blockSignals(true);
320 refsLW->setUpdatesEnabled(false);
324 // need this because Qt will send a highlight() here for
325 // the first item inserted
326 QString const oldSelection(referenceED->text());
328 for (vector<docstring>::const_iterator iter = refs_.begin();
329 iter != refs_.end(); ++iter) {
330 refsLW->addItem(toqstr(*iter));
336 referenceED->setText(oldSelection);
338 // restore the last selection or, for new insets, highlight
339 // the previous selection
340 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
341 bool const newInset = oldSelection.isEmpty();
342 QString textToFind = newInset ? last_reference_ : oldSelection;
343 last_reference_.clear();
344 for (int i = 0; i != refsLW->count(); ++i) {
345 QListWidgetItem * item = refsLW->item(i);
346 if (textToFind == item->text()) {
347 refsLW->setCurrentItem(item);
348 refsLW->setItemSelected(item, !newInset);
349 //Make sure selected item is visible
350 refsLW->scrollToItem(item);
351 last_reference_ = textToFind;
356 refsLW->setUpdatesEnabled(true);
359 // Re-activate the emission of signals by these widgets.
360 refsLW->blockSignals(false);
361 referenceED->blockSignals(false);
365 void GuiRef::updateRefs()
368 int const the_buffer = bufferCO->currentIndex();
369 if (the_buffer != -1) {
370 FileName const & name = theBufferList().fileNames()[the_buffer];
371 Buffer const * buf = theBufferList().getBuffer(name);
372 buf->getLabelList(refs_);
374 sortCB->setEnabled(!refs_.empty());
375 refsLW->setEnabled(!refs_.empty());
376 // refsLW should only be the focus proxy when it is enabled
377 setFocusProxy(refs_.empty() ? 0 : refsLW);
378 gotoPB->setEnabled(!refs_.empty());
383 bool GuiRef::isValid()
385 return !referenceED->text().isEmpty();
389 void GuiRef::gotoRef(string const & ref)
391 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
392 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
396 void GuiRef::gotoBookmark()
398 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
402 bool GuiRef::initialiseParams(std::string const & data)
404 InsetCommand::string2params("ref", data, params_);
409 void GuiRef::dispatchParams()
411 std::string const lfun = InsetCommand::params2string("ref", params_);
412 dispatch(FuncRequest(getLfun(), lfun));
417 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
420 } // namespace frontend
423 #include "moc_GuiRef.cpp"