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(reset_dialog()));
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;
102 void GuiRef::changed_adaptor()
108 void GuiRef::gotoClicked()
114 void GuiRef::selectionChanged()
116 if (isBufferReadonly())
119 QList<QListWidgetItem *> selections = refsLW->selectedItems();
120 if (selections.isEmpty())
122 QListWidgetItem * sel = selections.first();
128 void GuiRef::refHighlighted(QListWidgetItem * sel)
130 if (isBufferReadonly())
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);
138 if (cur_item_selected)
139 referenceED->setText(sel->text());
143 gotoPB->setEnabled(true);
145 typeCO->setEnabled(true);
147 nameED->setEnabled(true);
151 void GuiRef::refSelected(QListWidgetItem * sel)
153 if (isBufferReadonly())
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);
161 if (cur_item_selected)
162 referenceED->setText(sel->text());
163 // <enter> or double click, inserts ref and closes dialog
168 void GuiRef::sortToggled(bool on)
175 void GuiRef::updateClicked()
181 void GuiRef::reset_dialog()
188 void GuiRef::closeEvent(QCloseEvent * e)
196 void GuiRef::updateContents()
198 int orig_type = typeCO->currentIndex();
200 referenceED->setText(toqstr(params_["reference"]));
202 nameED->setText(toqstr(params_["name"]));
203 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
205 // restore type settings for new insets
206 if (params_["reference"].empty())
207 typeCO->setCurrentIndex(orig_type);
209 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
210 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
212 typeCO->setCurrentIndex(0);
214 sortCB->setChecked(sort_);
216 // insert buffer list
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())));
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_);
229 int num = theBufferList().bufferNum(buffer().fileName());
230 bufferCO->setCurrentIndex(num);
234 bc().setValid(false);
238 void GuiRef::applyView()
240 last_reference_ = referenceED->text();
242 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
243 params_["reference"] = qstring_to_ucs4(last_reference_);
244 params_["name"] = qstring_to_ucs4(nameED->text());
246 restored_buffer_ = bufferCO->currentIndex();
250 bool GuiRef::nameAllowed()
252 KernelDocType const doc_type = docType();
253 return doc_type != LATEX && doc_type != LITERATE;
257 bool GuiRef::typeAllowed()
259 return docType() != DOCBOOK;
263 void GuiRef::setGoBack()
265 gotoPB->setText(qt_("&Go Back"));
266 gotoPB->setToolTip("");
267 gotoPB->setToolTip(qt_("Jump back"));
271 void GuiRef::setGotoRef()
273 gotoPB->setText(qt_("&Go to Label"));
274 gotoPB->setToolTip("");
275 gotoPB->setToolTip(qt_("Jump to label"));
279 void GuiRef::gotoRef()
281 string ref = fromqstr(referenceED->text());
296 void GuiRef::redoRefs()
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);
306 // need this because Qt will send a highlight() here for
307 // the first item inserted
308 QString const oldSelection(referenceED->text());
310 for (vector<docstring>::const_iterator iter = refs_.begin();
311 iter != refs_.end(); ++iter) {
312 refsLW->addItem(toqstr(*iter));
318 referenceED->setText(oldSelection);
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;
338 refsLW->setUpdatesEnabled(true);
341 // Re-activate the emission of signals by these widgets.
342 refsLW->blockSignals(false);
343 referenceED->blockSignals(false);
347 void GuiRef::updateRefs()
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());
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"