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"), Controller(*static_cast<Dialog*>(this)),
59 setController(this, false);
60 setViewTitle(_("Cross-reference"));
65 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
66 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
67 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
68 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
69 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
71 connect(typeCO, SIGNAL(activated(int)),
72 this, SLOT(changed_adaptor()));
73 connect(referenceED, SIGNAL(textChanged(const QString &)),
74 this, SLOT(changed_adaptor()));
75 connect(nameED, SIGNAL(textChanged(const QString &)),
76 this, SLOT(changed_adaptor()));
77 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
78 this, SLOT(refHighlighted(QListWidgetItem *)));
79 connect(refsLW, SIGNAL(itemSelectionChanged()),
80 this, SLOT(selectionChanged()));
81 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
82 this, SLOT(refSelected(QListWidgetItem *)));
83 connect(sortCB, SIGNAL(clicked(bool)),
84 this, SLOT(sortToggled(bool)));
85 connect(gotoPB, SIGNAL(clicked()),
86 this, SLOT(gotoClicked()));
87 connect(updatePB, SIGNAL(clicked()),
88 this, SLOT(updateClicked()));
89 connect(bufferCO, SIGNAL(activated(int)),
90 this, SLOT(updateClicked()));
92 setFocusProxy(refsLW);
94 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
96 bc().setApply(applyPB);
97 bc().setCancel(closePB);
98 bc().addReadOnly(refsLW);
99 bc().addReadOnly(sortCB);
100 bc().addReadOnly(nameED);
101 bc().addReadOnly(referenceED);
102 bc().addReadOnly(typeCO);
103 bc().addReadOnly(bufferCO);
105 restored_buffer_ = -1;
109 void GuiRef::changed_adaptor()
115 void GuiRef::gotoClicked()
120 void GuiRef::selectionChanged()
122 if (isBufferReadonly())
125 QList<QListWidgetItem *> selections = refsLW->selectedItems();
126 if (selections.isEmpty())
128 QListWidgetItem * sel = selections.first();
134 void GuiRef::refHighlighted(QListWidgetItem * sel)
136 if (controller().isBufferReadonly())
139 /* int const cur_item = refsLW->currentRow();
140 bool const cur_item_selected = cur_item >= 0 ?
141 refsLB->isSelected(cur_item) : false;*/
142 bool const cur_item_selected = refsLW->isItemSelected(sel);
144 if (cur_item_selected)
145 referenceED->setText(sel->text());
149 gotoPB->setEnabled(true);
151 typeCO->setEnabled(true);
153 nameED->setEnabled(true);
157 void GuiRef::refSelected(QListWidgetItem * sel)
159 if (isBufferReadonly())
162 /* int const cur_item = refsLW->currentRow();
163 bool const cur_item_selected = cur_item >= 0 ?
164 refsLB->isSelected(cur_item) : false;*/
165 bool const cur_item_selected = refsLW->isItemSelected(sel);
167 if (cur_item_selected)
168 referenceED->setText(sel->text());
169 // <enter> or double click, inserts ref and closes dialog
174 void GuiRef::sortToggled(bool on)
181 void GuiRef::updateClicked()
187 void GuiRef::reset_dialog()
194 void GuiRef::closeEvent(QCloseEvent * e)
198 GuiDialog::closeEvent(e);
202 void GuiRef::updateContents()
204 int orig_type = typeCO->currentIndex();
206 referenceED->setText(toqstr(params_["reference"]));
208 nameED->setText(toqstr(params_["name"]));
209 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
211 // restore type settings for new insets
212 if (params_["reference"].empty())
213 typeCO->setCurrentIndex(orig_type);
215 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
216 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
218 typeCO->setCurrentIndex(0);
220 sortCB->setChecked(sort_);
222 // insert buffer list
224 vector<string> buffers = theBufferList().getFileNames();
225 for (vector<string>::iterator it = buffers.begin();
226 it != buffers.end(); ++it) {
227 bufferCO->addItem(toqstr(lyx::to_utf8(makeDisplayPath(*it))));
230 // restore the buffer combo setting for new insets
231 if (params_["reference"].empty() && restored_buffer_ != -1
232 && restored_buffer_ < bufferCO->count())
233 bufferCO->setCurrentIndex(restored_buffer_);
235 bufferCO->setCurrentIndex(bufferNum());
238 bc().setValid(false);
242 void GuiRef::applyView()
244 last_reference_ = referenceED->text();
246 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
247 params_["reference"] = qstring_to_ucs4(last_reference_);
248 params_["name"] = qstring_to_ucs4(nameED->text());
250 restored_buffer_ = bufferCO->currentIndex();
254 bool GuiRef::nameAllowed()
256 KernelDocType const doc_type = docType();
257 return doc_type != LATEX && doc_type != LITERATE;
261 bool GuiRef::typeAllowed()
263 return docType() != DOCBOOK;
267 void GuiRef::setGoBack()
269 gotoPB->setText(qt_("&Go Back"));
270 gotoPB->setToolTip("");
271 gotoPB->setToolTip(qt_("Jump back"));
275 void GuiRef::setGotoRef()
277 gotoPB->setText(qt_("&Go to Label"));
278 gotoPB->setToolTip("");
279 gotoPB->setToolTip(qt_("Jump to label"));
283 void GuiRef::gotoRef()
285 string ref = fromqstr(referenceED->text());
300 void GuiRef::redoRefs()
302 // Prevent these widgets from emitting any signals whilst
303 // we modify their state.
304 refsLW->blockSignals(true);
305 referenceED->blockSignals(true);
306 refsLW->setUpdatesEnabled(false);
310 // need this because Qt will send a highlight() here for
311 // the first item inserted
312 QString const oldSelection(referenceED->text());
314 for (std::vector<docstring>::const_iterator iter = refs_.begin();
315 iter != refs_.end(); ++iter) {
316 refsLW->addItem(toqstr(*iter));
322 referenceED->setText(oldSelection);
324 // restore the last selection or, for new insets, highlight
325 // the previous selection
326 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
327 bool const newInset = oldSelection.isEmpty();
328 QString textToFind = newInset ? last_reference_ : oldSelection;
329 bool foundItem = false;
330 for (int i = 0; !foundItem && i < refsLW->count(); ++i) {
331 QListWidgetItem * item = refsLW->item(i);
332 if (textToFind == item->text()) {
333 refsLW->setCurrentItem(item);
334 refsLW->setItemSelected(item, !newInset);
335 //Make sure selected item is visible
336 refsLW->scrollToItem(item);
341 last_reference_ = textToFind;
342 else last_reference_ = "";
344 refsLW->setUpdatesEnabled(true);
347 // Re-activate the emission of signals by these widgets.
348 refsLW->blockSignals(false);
349 referenceED->blockSignals(false);
353 void GuiRef::updateRefs()
356 string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
357 Buffer const * buf = theBufferList().getBuffer(makeAbsPath(name).absFilename());
358 buf->getLabelList(refs_);
359 sortCB->setEnabled(!refs_.empty());
360 refsLW->setEnabled(!refs_.empty());
361 gotoPB->setEnabled(!refs_.empty());
366 bool GuiRef::isValid()
368 return !referenceED->text().isEmpty();
372 void GuiRef::gotoRef(string const & ref)
374 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
375 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
379 void GuiRef::gotoBookmark()
381 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
385 int GuiRef::bufferNum() const
387 vector<string> buffers = theBufferList().getFileNames();
388 string const name = buffer().fileName();
389 vector<string>::const_iterator cit =
390 std::find(buffers.begin(), buffers.end(), name);
391 if (cit == buffers.end())
393 return int(cit - buffers.begin());
397 bool GuiRef::initialiseParams(string const & data)
399 // The name passed with LFUN_INSET_APPLY is also the name
400 // used to identify the mailer.
401 InsetCommandMailer::string2params(lfun_name_, data, params_);
406 void GuiRef::clearParams()
412 void GuiRef::dispatchParams()
414 string const lfun = InsetCommandMailer::params2string(lfun_name_, params_);
415 dispatch(FuncRequest(getLfun(), lfun));
419 Dialog * createGuiRef(LyXView & lv) { return new GuiRef(lv); }
422 } // namespace frontend
425 #include "GuiRef_moc.cpp"