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/filetools.h" // MakeAbsPath, MakeDisplayPath
26 #include <boost/filesystem/operations.hpp>
30 #include <QListWidget>
31 #include <QListWidgetItem>
32 #include <QPushButton>
34 #include <QCloseEvent>
45 using support::makeAbsPath;
46 using support::makeDisplayPath;
48 //FIXME It should be possible to eliminate lfun_name_
49 //now and recover that information from params().insetType().
50 //But let's not do that quite yet.
51 /// Flags what action is taken by Kernel::dispatch()
52 static std::string const lfun_name_ = "ref";
54 GuiRef::GuiRef(LyXView & lv)
55 : GuiDialog(lv, "ref"), params_("ref")
58 setViewTitle(_("Cross-reference"));
63 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
64 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
65 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
66 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
67 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
69 connect(typeCO, SIGNAL(activated(int)),
70 this, SLOT(changed_adaptor()));
71 connect(referenceED, SIGNAL(textChanged(QString)),
72 this, SLOT(changed_adaptor()));
73 connect(nameED, SIGNAL(textChanged(QString)),
74 this, SLOT(changed_adaptor()));
75 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
76 this, SLOT(refHighlighted(QListWidgetItem *)));
77 connect(refsLW, SIGNAL(itemSelectionChanged()),
78 this, SLOT(selectionChanged()));
79 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
80 this, SLOT(refSelected(QListWidgetItem *)));
81 connect(sortCB, SIGNAL(clicked(bool)),
82 this, SLOT(sortToggled(bool)));
83 connect(gotoPB, SIGNAL(clicked()),
84 this, SLOT(gotoClicked()));
85 connect(updatePB, SIGNAL(clicked()),
86 this, SLOT(updateClicked()));
87 connect(bufferCO, SIGNAL(activated(int)),
88 this, SLOT(updateClicked()));
90 setFocusProxy(refsLW);
92 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
94 bc().setApply(applyPB);
95 bc().setCancel(closePB);
96 bc().addReadOnly(refsLW);
97 bc().addReadOnly(sortCB);
98 bc().addReadOnly(nameED);
99 bc().addReadOnly(referenceED);
100 bc().addReadOnly(typeCO);
101 bc().addReadOnly(bufferCO);
103 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 (controller().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::reset_dialog()
193 void GuiRef::closeEvent(QCloseEvent * e)
197 GuiDialog::closeEvent(e);
201 void GuiRef::updateContents()
203 int orig_type = typeCO->currentIndex();
205 referenceED->setText(toqstr(params_["reference"]));
207 nameED->setText(toqstr(params_["name"]));
208 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
210 // restore type settings for new insets
211 if (params_["reference"].empty())
212 typeCO->setCurrentIndex(orig_type);
214 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
215 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
217 typeCO->setCurrentIndex(0);
219 sortCB->setChecked(sort_);
221 // insert buffer list
223 vector<string> buffers = theBufferList().getFileNames();
224 for (vector<string>::iterator it = buffers.begin();
225 it != buffers.end(); ++it) {
226 bufferCO->addItem(toqstr(lyx::to_utf8(makeDisplayPath(*it))));
229 // restore the buffer combo setting for new insets
230 if (params_["reference"].empty() && restored_buffer_ != -1
231 && restored_buffer_ < bufferCO->count())
232 bufferCO->setCurrentIndex(restored_buffer_);
234 bufferCO->setCurrentIndex(bufferNum());
237 bc().setValid(false);
241 void GuiRef::applyView()
243 last_reference_ = referenceED->text();
245 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
246 params_["reference"] = qstring_to_ucs4(last_reference_);
247 params_["name"] = qstring_to_ucs4(nameED->text());
249 restored_buffer_ = bufferCO->currentIndex();
253 bool GuiRef::nameAllowed()
255 KernelDocType const doc_type = docType();
256 return doc_type != LATEX && doc_type != LITERATE;
260 bool GuiRef::typeAllowed()
262 return docType() != DOCBOOK;
266 void GuiRef::setGoBack()
268 gotoPB->setText(qt_("&Go Back"));
269 gotoPB->setToolTip("");
270 gotoPB->setToolTip(qt_("Jump back"));
274 void GuiRef::setGotoRef()
276 gotoPB->setText(qt_("&Go to Label"));
277 gotoPB->setToolTip("");
278 gotoPB->setToolTip(qt_("Jump to label"));
282 void GuiRef::gotoRef()
284 string ref = fromqstr(referenceED->text());
299 void GuiRef::redoRefs()
301 // Prevent these widgets from emitting any signals whilst
302 // we modify their state.
303 refsLW->blockSignals(true);
304 referenceED->blockSignals(true);
305 refsLW->setUpdatesEnabled(false);
309 // need this because Qt will send a highlight() here for
310 // the first item inserted
311 QString const oldSelection(referenceED->text());
313 for (std::vector<docstring>::const_iterator iter = refs_.begin();
314 iter != refs_.end(); ++iter) {
315 refsLW->addItem(toqstr(*iter));
321 referenceED->setText(oldSelection);
323 // restore the last selection or, for new insets, highlight
324 // the previous selection
325 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
326 bool const newInset = oldSelection.isEmpty();
327 QString textToFind = newInset ? last_reference_ : oldSelection;
328 last_reference_.clear();
329 for (int i = 0; i != refsLW->count(); ++i) {
330 QListWidgetItem * item = refsLW->item(i);
331 if (textToFind == item->text()) {
332 refsLW->setCurrentItem(item);
333 refsLW->setItemSelected(item, !newInset);
334 //Make sure selected item is visible
335 refsLW->scrollToItem(item);
336 last_reference_ = textToFind;
341 refsLW->setUpdatesEnabled(true);
344 // Re-activate the emission of signals by these widgets.
345 refsLW->blockSignals(false);
346 referenceED->blockSignals(false);
350 void GuiRef::updateRefs()
353 string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
354 Buffer const * buf = theBufferList().getBuffer(makeAbsPath(name).absFilename());
355 buf->getLabelList(refs_);
356 sortCB->setEnabled(!refs_.empty());
357 refsLW->setEnabled(!refs_.empty());
358 gotoPB->setEnabled(!refs_.empty());
363 bool GuiRef::isValid()
365 return !referenceED->text().isEmpty();
369 void GuiRef::gotoRef(string const & ref)
371 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
372 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
376 void GuiRef::gotoBookmark()
378 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
382 int GuiRef::bufferNum() const
384 vector<string> buffers = theBufferList().getFileNames();
385 string const name = buffer().fileName();
386 vector<string>::const_iterator cit =
387 std::find(buffers.begin(), buffers.end(), name);
388 if (cit == buffers.end())
390 return int(cit - buffers.begin());
394 bool GuiRef::initialiseParams(string const & data)
396 // The name passed with LFUN_INSET_APPLY is also the name
397 // used to identify the mailer.
398 InsetCommandMailer::string2params(lfun_name_, data, params_);
403 void GuiRef::clearParams()
409 void GuiRef::dispatchParams()
411 string const lfun = InsetCommandMailer::params2string(lfun_name_, params_);
412 dispatch(FuncRequest(getLfun(), lfun));
416 Dialog * createGuiRef(LyXView & lv) { return new GuiRef(lv); }
419 } // namespace frontend
422 #include "GuiRef_moc.cpp"