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 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
51 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
52 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
53 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
54 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
56 connect(typeCO, SIGNAL(activated(int)),
57 this, SLOT(changed_adaptor()));
58 connect(referenceED, SIGNAL(textChanged(QString)),
59 this, SLOT(changed_adaptor()));
60 connect(nameED, SIGNAL(textChanged(QString)),
61 this, SLOT(changed_adaptor()));
62 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
63 this, SLOT(refHighlighted(QListWidgetItem *)));
64 connect(refsLW, SIGNAL(itemSelectionChanged()),
65 this, SLOT(selectionChanged()));
66 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
67 this, SLOT(refSelected(QListWidgetItem *)));
68 connect(sortCB, SIGNAL(clicked(bool)),
69 this, SLOT(sortToggled(bool)));
70 connect(gotoPB, SIGNAL(clicked()),
71 this, SLOT(gotoClicked()));
72 connect(updatePB, SIGNAL(clicked()),
73 this, SLOT(updateClicked()));
74 connect(bufferCO, SIGNAL(activated(int)),
75 this, SLOT(updateClicked()));
77 setFocusProxy(refsLW);
79 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
81 bc().setApply(applyPB);
82 bc().setCancel(closePB);
83 bc().addReadOnly(refsLW);
84 bc().addReadOnly(sortCB);
85 bc().addReadOnly(nameED);
86 bc().addReadOnly(referenceED);
87 bc().addReadOnly(typeCO);
88 bc().addReadOnly(bufferCO);
90 restored_buffer_ = -1;
94 void GuiRef::changed_adaptor()
100 void GuiRef::gotoClicked()
106 void GuiRef::selectionChanged()
108 if (isBufferReadonly())
111 QList<QListWidgetItem *> selections = refsLW->selectedItems();
112 if (selections.isEmpty())
114 QListWidgetItem * sel = selections.first();
120 void GuiRef::refHighlighted(QListWidgetItem * sel)
122 if (isBufferReadonly())
125 /* int const cur_item = refsLW->currentRow();
126 bool const cur_item_selected = cur_item >= 0 ?
127 refsLB->isSelected(cur_item) : false;*/
128 bool const cur_item_selected = refsLW->isItemSelected(sel);
130 if (cur_item_selected)
131 referenceED->setText(sel->text());
135 gotoPB->setEnabled(true);
137 typeCO->setEnabled(true);
139 nameED->setEnabled(true);
143 void GuiRef::refSelected(QListWidgetItem * sel)
145 if (isBufferReadonly())
148 /* int const cur_item = refsLW->currentRow();
149 bool const cur_item_selected = cur_item >= 0 ?
150 refsLB->isSelected(cur_item) : false;*/
151 bool const cur_item_selected = refsLW->isItemSelected(sel);
153 if (cur_item_selected)
154 referenceED->setText(sel->text());
155 // <enter> or double click, inserts ref and closes dialog
160 void GuiRef::sortToggled(bool on)
167 void GuiRef::updateClicked()
173 void GuiRef::reset_dialog()
180 void GuiRef::closeEvent(QCloseEvent * e)
188 void GuiRef::updateContents()
190 int orig_type = typeCO->currentIndex();
192 referenceED->setText(toqstr(params_["reference"]));
194 nameED->setText(toqstr(params_["name"]));
195 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
197 // restore type settings for new insets
198 if (params_["reference"].empty())
199 typeCO->setCurrentIndex(orig_type);
201 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
202 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
204 typeCO->setCurrentIndex(0);
206 sortCB->setChecked(sort_);
208 // insert buffer list
210 vector<string> buffers = theBufferList().getFileNames();
211 for (vector<string>::iterator it = buffers.begin();
212 it != buffers.end(); ++it) {
213 bufferCO->addItem(toqstr(makeDisplayPath(*it)));
216 // restore the buffer combo setting for new insets
217 if (params_["reference"].empty() && restored_buffer_ != -1
218 && restored_buffer_ < bufferCO->count())
219 bufferCO->setCurrentIndex(restored_buffer_);
221 int num = theBufferList().bufferNum(buffer().absFileName());
222 bufferCO->setCurrentIndex(num);
226 bc().setValid(false);
230 void GuiRef::applyView()
232 last_reference_ = referenceED->text();
234 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
235 params_["reference"] = qstring_to_ucs4(last_reference_);
236 params_["name"] = qstring_to_ucs4(nameED->text());
238 restored_buffer_ = bufferCO->currentIndex();
242 bool GuiRef::nameAllowed()
244 KernelDocType const doc_type = docType();
245 return doc_type != LATEX && doc_type != LITERATE;
249 bool GuiRef::typeAllowed()
251 return docType() != DOCBOOK;
255 void GuiRef::setGoBack()
257 gotoPB->setText(qt_("&Go Back"));
258 gotoPB->setToolTip("");
259 gotoPB->setToolTip(qt_("Jump back"));
263 void GuiRef::setGotoRef()
265 gotoPB->setText(qt_("&Go to Label"));
266 gotoPB->setToolTip("");
267 gotoPB->setToolTip(qt_("Jump to label"));
271 void GuiRef::gotoRef()
273 string ref = fromqstr(referenceED->text());
288 void GuiRef::redoRefs()
290 // Prevent these widgets from emitting any signals whilst
291 // we modify their state.
292 refsLW->blockSignals(true);
293 referenceED->blockSignals(true);
294 refsLW->setUpdatesEnabled(false);
298 // need this because Qt will send a highlight() here for
299 // the first item inserted
300 QString const oldSelection(referenceED->text());
302 for (vector<docstring>::const_iterator iter = refs_.begin();
303 iter != refs_.end(); ++iter) {
304 refsLW->addItem(toqstr(*iter));
310 referenceED->setText(oldSelection);
312 // restore the last selection or, for new insets, highlight
313 // the previous selection
314 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
315 bool const newInset = oldSelection.isEmpty();
316 QString textToFind = newInset ? last_reference_ : oldSelection;
317 last_reference_.clear();
318 for (int i = 0; i != refsLW->count(); ++i) {
319 QListWidgetItem * item = refsLW->item(i);
320 if (textToFind == item->text()) {
321 refsLW->setCurrentItem(item);
322 refsLW->setItemSelected(item, !newInset);
323 //Make sure selected item is visible
324 refsLW->scrollToItem(item);
325 last_reference_ = textToFind;
330 refsLW->setUpdatesEnabled(true);
333 // Re-activate the emission of signals by these widgets.
334 refsLW->blockSignals(false);
335 referenceED->blockSignals(false);
339 void GuiRef::updateRefs()
342 string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
343 Buffer const * buf = theBufferList().getBuffer(
344 support::makeAbsPath(name).absFilename());
345 buf->getLabelList(refs_);
346 sortCB->setEnabled(!refs_.empty());
347 refsLW->setEnabled(!refs_.empty());
348 gotoPB->setEnabled(!refs_.empty());
353 bool GuiRef::isValid()
355 return !referenceED->text().isEmpty();
359 void GuiRef::gotoRef(string const & ref)
361 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
362 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
366 void GuiRef::gotoBookmark()
368 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
372 bool GuiRef::initialiseParams(std::string const & data)
374 InsetCommand::string2params("ref", data, params_);
379 void GuiRef::dispatchParams()
381 std::string const lfun = InsetCommand::params2string("ref", params_);
382 dispatch(FuncRequest(getLfun(), lfun));
387 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
390 } // namespace frontend
393 #include "GuiRef_moc.cpp"