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>
42 using support::makeAbsPath;
43 using support::makeDisplayPath;
45 //FIXME It should be possible to eliminate lfun_name_
46 //now and recover that information from params().insetType().
47 //But let's not do that quite yet.
48 /// Flags what action is taken by Kernel::dispatch()
49 static std::string const lfun_name_ = "ref";
51 GuiRef::GuiRef(LyXView & lv)
52 : GuiDialog(lv, "ref"), params_(REF_CODE)
55 setViewTitle(_("Cross-reference"));
60 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
61 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
62 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
63 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
64 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
66 connect(typeCO, SIGNAL(activated(int)),
67 this, SLOT(changed_adaptor()));
68 connect(referenceED, SIGNAL(textChanged(QString)),
69 this, SLOT(changed_adaptor()));
70 connect(nameED, SIGNAL(textChanged(QString)),
71 this, SLOT(changed_adaptor()));
72 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
73 this, SLOT(refHighlighted(QListWidgetItem *)));
74 connect(refsLW, SIGNAL(itemSelectionChanged()),
75 this, SLOT(selectionChanged()));
76 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
77 this, SLOT(refSelected(QListWidgetItem *)));
78 connect(sortCB, SIGNAL(clicked(bool)),
79 this, SLOT(sortToggled(bool)));
80 connect(gotoPB, SIGNAL(clicked()),
81 this, SLOT(gotoClicked()));
82 connect(updatePB, SIGNAL(clicked()),
83 this, SLOT(updateClicked()));
84 connect(bufferCO, SIGNAL(activated(int)),
85 this, SLOT(updateClicked()));
87 setFocusProxy(refsLW);
89 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
91 bc().setApply(applyPB);
92 bc().setCancel(closePB);
93 bc().addReadOnly(refsLW);
94 bc().addReadOnly(sortCB);
95 bc().addReadOnly(nameED);
96 bc().addReadOnly(referenceED);
97 bc().addReadOnly(typeCO);
98 bc().addReadOnly(bufferCO);
100 restored_buffer_ = -1;
104 void GuiRef::changed_adaptor()
110 void GuiRef::gotoClicked()
116 void GuiRef::selectionChanged()
118 if (isBufferReadonly())
121 QList<QListWidgetItem *> selections = refsLW->selectedItems();
122 if (selections.isEmpty())
124 QListWidgetItem * sel = selections.first();
130 void GuiRef::refHighlighted(QListWidgetItem * sel)
132 if (isBufferReadonly())
135 /* int const cur_item = refsLW->currentRow();
136 bool const cur_item_selected = cur_item >= 0 ?
137 refsLB->isSelected(cur_item) : false;*/
138 bool const cur_item_selected = refsLW->isItemSelected(sel);
140 if (cur_item_selected)
141 referenceED->setText(sel->text());
145 gotoPB->setEnabled(true);
147 typeCO->setEnabled(true);
149 nameED->setEnabled(true);
153 void GuiRef::refSelected(QListWidgetItem * sel)
155 if (isBufferReadonly())
158 /* int const cur_item = refsLW->currentRow();
159 bool const cur_item_selected = cur_item >= 0 ?
160 refsLB->isSelected(cur_item) : false;*/
161 bool const cur_item_selected = refsLW->isItemSelected(sel);
163 if (cur_item_selected)
164 referenceED->setText(sel->text());
165 // <enter> or double click, inserts ref and closes dialog
170 void GuiRef::sortToggled(bool on)
177 void GuiRef::updateClicked()
183 void GuiRef::reset_dialog()
190 void GuiRef::closeEvent(QCloseEvent * e)
194 GuiDialog::closeEvent(e);
198 void GuiRef::updateContents()
200 int orig_type = typeCO->currentIndex();
202 referenceED->setText(toqstr(params_["reference"]));
204 nameED->setText(toqstr(params_["name"]));
205 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
207 // restore type settings for new insets
208 if (params_["reference"].empty())
209 typeCO->setCurrentIndex(orig_type);
211 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
212 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
214 typeCO->setCurrentIndex(0);
216 sortCB->setChecked(sort_);
218 // insert buffer list
220 vector<string> buffers = theBufferList().getFileNames();
221 for (vector<string>::iterator it = buffers.begin();
222 it != buffers.end(); ++it) {
223 bufferCO->addItem(toqstr(lyx::to_utf8(makeDisplayPath(*it))));
226 // restore the buffer combo setting for new insets
227 if (params_["reference"].empty() && restored_buffer_ != -1
228 && restored_buffer_ < bufferCO->count())
229 bufferCO->setCurrentIndex(restored_buffer_);
231 int num = theBufferList().bufferNum(buffer().absFileName());
232 bufferCO->setCurrentIndex(num);
236 bc().setValid(false);
240 void GuiRef::applyView()
242 last_reference_ = referenceED->text();
244 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
245 params_["reference"] = qstring_to_ucs4(last_reference_);
246 params_["name"] = qstring_to_ucs4(nameED->text());
248 restored_buffer_ = bufferCO->currentIndex();
252 bool GuiRef::nameAllowed()
254 KernelDocType const doc_type = docType();
255 return doc_type != LATEX && doc_type != LITERATE;
259 bool GuiRef::typeAllowed()
261 return docType() != DOCBOOK;
265 void GuiRef::setGoBack()
267 gotoPB->setText(qt_("&Go Back"));
268 gotoPB->setToolTip("");
269 gotoPB->setToolTip(qt_("Jump back"));
273 void GuiRef::setGotoRef()
275 gotoPB->setText(qt_("&Go to Label"));
276 gotoPB->setToolTip("");
277 gotoPB->setToolTip(qt_("Jump to label"));
281 void GuiRef::gotoRef()
283 string ref = fromqstr(referenceED->text());
298 void GuiRef::redoRefs()
300 // Prevent these widgets from emitting any signals whilst
301 // we modify their state.
302 refsLW->blockSignals(true);
303 referenceED->blockSignals(true);
304 refsLW->setUpdatesEnabled(false);
308 // need this because Qt will send a highlight() here for
309 // the first item inserted
310 QString const oldSelection(referenceED->text());
312 for (std::vector<docstring>::const_iterator iter = refs_.begin();
313 iter != refs_.end(); ++iter) {
314 refsLW->addItem(toqstr(*iter));
320 referenceED->setText(oldSelection);
322 // restore the last selection or, for new insets, highlight
323 // the previous selection
324 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
325 bool const newInset = oldSelection.isEmpty();
326 QString textToFind = newInset ? last_reference_ : oldSelection;
327 last_reference_.clear();
328 for (int i = 0; i != refsLW->count(); ++i) {
329 QListWidgetItem * item = refsLW->item(i);
330 if (textToFind == item->text()) {
331 refsLW->setCurrentItem(item);
332 refsLW->setItemSelected(item, !newInset);
333 //Make sure selected item is visible
334 refsLW->scrollToItem(item);
335 last_reference_ = textToFind;
340 refsLW->setUpdatesEnabled(true);
343 // Re-activate the emission of signals by these widgets.
344 refsLW->blockSignals(false);
345 referenceED->blockSignals(false);
349 void GuiRef::updateRefs()
352 string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
353 Buffer const * buf = theBufferList().getBuffer(makeAbsPath(name).absFilename());
354 buf->getLabelList(refs_);
355 sortCB->setEnabled(!refs_.empty());
356 refsLW->setEnabled(!refs_.empty());
357 gotoPB->setEnabled(!refs_.empty());
362 bool GuiRef::isValid()
364 return !referenceED->text().isEmpty();
368 void GuiRef::gotoRef(string const & ref)
370 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
371 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
375 void GuiRef::gotoBookmark()
377 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
381 int GuiRef::bufferNum() const
386 bool GuiRef::initialiseParams(string const & data)
388 // The name passed with LFUN_INSET_APPLY is also the name
389 // used to identify the mailer.
390 InsetCommandMailer::string2params(lfun_name_, data, params_);
395 void GuiRef::clearParams()
401 void GuiRef::dispatchParams()
403 string const lfun = InsetCommandMailer::params2string(lfun_name_, params_);
404 dispatch(FuncRequest(getLfun(), lfun));
408 Dialog * createGuiRef(LyXView & lv) { return new GuiRef(lv); }
411 } // namespace frontend
414 #include "GuiRef_moc.cpp"