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/filetools.h" // makeAbsPath, makeDisplayPath
29 #include <QListWidget>
30 #include <QListWidgetItem>
31 #include <QPushButton>
33 #include <QCloseEvent>
36 using namespace lyx::support;
41 GuiRef::GuiRef(GuiView & lv)
42 : GuiDialog(lv, "ref", qt_("Cross-reference")),
43 params_(insetCode("ref"))
50 //FIXME: when/if we support the xr package for cross-reference
51 //between independant files. Those can be re-enabled.
52 refsL->setEnabled(false);
54 bufferCO->setEnabled(false);
57 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
58 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
59 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
60 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
61 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
63 connect(typeCO, SIGNAL(activated(int)),
64 this, SLOT(changed_adaptor()));
65 connect(referenceED, SIGNAL(textChanged(QString)),
66 this, SLOT(changed_adaptor()));
67 connect(nameED, SIGNAL(textChanged(QString)),
68 this, SLOT(changed_adaptor()));
69 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
70 this, SLOT(refHighlighted(QListWidgetItem *)));
71 connect(refsLW, SIGNAL(itemSelectionChanged()),
72 this, SLOT(selectionChanged()));
73 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
74 this, SLOT(refSelected(QListWidgetItem *)));
75 connect(sortCB, SIGNAL(clicked(bool)),
76 this, SLOT(sortToggled(bool)));
77 connect(gotoPB, SIGNAL(clicked()),
78 this, SLOT(gotoClicked()));
79 connect(updatePB, SIGNAL(clicked()),
80 this, SLOT(updateClicked()));
81 connect(bufferCO, SIGNAL(activated(int)),
82 this, SLOT(updateClicked()));
84 setFocusProxy(refsLW);
86 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
88 bc().setApply(applyPB);
89 bc().setCancel(closePB);
90 bc().addReadOnly(refsLW);
91 bc().addReadOnly(sortCB);
92 bc().addReadOnly(nameED);
93 bc().addReadOnly(referenceED);
94 bc().addReadOnly(typeCO);
95 bc().addReadOnly(bufferCO);
97 restored_buffer_ = -1;
101 void GuiRef::changed_adaptor()
107 void GuiRef::gotoClicked()
113 void GuiRef::selectionChanged()
115 if (isBufferReadonly())
118 QList<QListWidgetItem *> selections = refsLW->selectedItems();
119 if (selections.isEmpty())
121 QListWidgetItem * sel = selections.first();
127 void GuiRef::refHighlighted(QListWidgetItem * sel)
129 if (isBufferReadonly())
132 /* int const cur_item = refsLW->currentRow();
133 bool const cur_item_selected = cur_item >= 0 ?
134 refsLB->isSelected(cur_item) : false;*/
135 bool const cur_item_selected = refsLW->isItemSelected(sel);
137 if (cur_item_selected)
138 referenceED->setText(sel->text());
142 gotoPB->setEnabled(true);
144 typeCO->setEnabled(true);
146 nameED->setEnabled(true);
150 void GuiRef::refSelected(QListWidgetItem * sel)
152 if (isBufferReadonly())
155 /* int const cur_item = refsLW->currentRow();
156 bool const cur_item_selected = cur_item >= 0 ?
157 refsLB->isSelected(cur_item) : false;*/
158 bool const cur_item_selected = refsLW->isItemSelected(sel);
160 if (cur_item_selected)
161 referenceED->setText(sel->text());
162 // <enter> or double click, inserts ref and closes dialog
167 void GuiRef::sortToggled(bool on)
174 void GuiRef::updateClicked()
180 void GuiRef::reset_dialog()
187 void GuiRef::closeEvent(QCloseEvent * e)
195 void GuiRef::updateContents()
197 int orig_type = typeCO->currentIndex();
199 referenceED->setText(toqstr(params_["reference"]));
201 nameED->setText(toqstr(params_["name"]));
202 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
204 // restore type settings for new insets
205 if (params_["reference"].empty())
206 typeCO->setCurrentIndex(orig_type);
208 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
209 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
211 typeCO->setCurrentIndex(0);
213 sortCB->setChecked(sort_);
215 // insert buffer list
217 vector<string> buffers = theBufferList().getFileNames();
218 for (vector<string>::iterator it = buffers.begin();
219 it != buffers.end(); ++it) {
220 bufferCO->addItem(toqstr(makeDisplayPath(*it)));
223 // restore the buffer combo setting for new insets
224 if (params_["reference"].empty() && restored_buffer_ != -1
225 && restored_buffer_ < bufferCO->count())
226 bufferCO->setCurrentIndex(restored_buffer_);
228 int num = theBufferList().bufferNum(buffer().absFileName());
229 bufferCO->setCurrentIndex(num);
233 bc().setValid(false);
237 void GuiRef::applyView()
239 last_reference_ = referenceED->text();
241 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
242 params_["reference"] = qstring_to_ucs4(last_reference_);
243 params_["name"] = qstring_to_ucs4(nameED->text());
245 restored_buffer_ = bufferCO->currentIndex();
249 bool GuiRef::nameAllowed()
251 KernelDocType const doc_type = docType();
252 return doc_type != LATEX && doc_type != LITERATE;
256 bool GuiRef::typeAllowed()
258 return docType() != DOCBOOK;
262 void GuiRef::setGoBack()
264 gotoPB->setText(qt_("&Go Back"));
265 gotoPB->setToolTip("");
266 gotoPB->setToolTip(qt_("Jump back"));
270 void GuiRef::setGotoRef()
272 gotoPB->setText(qt_("&Go to Label"));
273 gotoPB->setToolTip("");
274 gotoPB->setToolTip(qt_("Jump to label"));
278 void GuiRef::gotoRef()
280 string ref = fromqstr(referenceED->text());
295 void GuiRef::redoRefs()
297 // Prevent these widgets from emitting any signals whilst
298 // we modify their state.
299 refsLW->blockSignals(true);
300 referenceED->blockSignals(true);
301 refsLW->setUpdatesEnabled(false);
305 // need this because Qt will send a highlight() here for
306 // the first item inserted
307 QString const oldSelection(referenceED->text());
309 for (vector<docstring>::const_iterator iter = refs_.begin();
310 iter != refs_.end(); ++iter) {
311 refsLW->addItem(toqstr(*iter));
317 referenceED->setText(oldSelection);
319 // restore the last selection or, for new insets, highlight
320 // the previous selection
321 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
322 bool const newInset = oldSelection.isEmpty();
323 QString textToFind = newInset ? last_reference_ : oldSelection;
324 last_reference_.clear();
325 for (int i = 0; i != refsLW->count(); ++i) {
326 QListWidgetItem * item = refsLW->item(i);
327 if (textToFind == item->text()) {
328 refsLW->setCurrentItem(item);
329 refsLW->setItemSelected(item, !newInset);
330 //Make sure selected item is visible
331 refsLW->scrollToItem(item);
332 last_reference_ = textToFind;
337 refsLW->setUpdatesEnabled(true);
340 // Re-activate the emission of signals by these widgets.
341 refsLW->blockSignals(false);
342 referenceED->blockSignals(false);
346 void GuiRef::updateRefs()
349 string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
350 Buffer const * buf = theBufferList().getBuffer(
351 support::makeAbsPath(name).absFilename());
352 buf->getLabelList(refs_);
353 sortCB->setEnabled(!refs_.empty());
354 refsLW->setEnabled(!refs_.empty());
355 gotoPB->setEnabled(!refs_.empty());
360 bool GuiRef::isValid()
362 return !referenceED->text().isEmpty();
366 void GuiRef::gotoRef(string const & ref)
368 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
369 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
373 void GuiRef::gotoBookmark()
375 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
379 bool GuiRef::initialiseParams(std::string const & data)
381 InsetCommand::string2params("ref", data, params_);
386 void GuiRef::dispatchParams()
388 std::string const lfun = InsetCommand::params2string("ref", params_);
389 dispatch(FuncRequest(getLfun(), lfun));
394 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
397 } // namespace frontend
400 #include "GuiRef_moc.cpp"