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"))
50 //FIXME: when/if we support the xr package for cross-reference
51 //between independant files. Those can be re-enabled.
52 refsL->setEnabled(false);
54 bufferCO->setEnabled(false);
57 // Enabling is set in updateRefs. Disable for now in case no
58 // call to updateContents follows (e.g. read-only documents).
59 sortCB->setEnabled(false);
60 caseSensitiveCB->setEnabled(false);
61 caseSensitiveCB->setChecked(false);
62 refsLW->setEnabled(false);
63 gotoPB->setEnabled(false);
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(dialog_rejected()));
71 connect(typeCO, SIGNAL(activated(int)),
72 this, SLOT(changed_adaptor()));
73 connect(referenceED, SIGNAL(textChanged(QString)),
74 this, SLOT(changed_adaptor()));
75 connect(nameED, SIGNAL(textChanged(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()),
84 this, SLOT(sortToggled()));
85 connect(caseSensitiveCB, SIGNAL(clicked()),
86 this, SLOT(caseSensitiveToggled()));
87 connect(gotoPB, SIGNAL(clicked()),
88 this, SLOT(gotoClicked()));
89 connect(updatePB, SIGNAL(clicked()),
90 this, SLOT(updateClicked()));
91 connect(bufferCO, SIGNAL(activated(int)),
92 this, SLOT(updateClicked()));
94 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
96 bc().setApply(applyPB);
97 bc().setCancel(closePB);
98 bc().addReadOnly(refsLW);
99 bc().addReadOnly(sortCB);
100 bc().addReadOnly(caseSensitiveCB);
101 bc().addReadOnly(nameED);
102 bc().addReadOnly(referenceED);
103 bc().addReadOnly(typeCO);
104 bc().addReadOnly(bufferCO);
106 restored_buffer_ = -1;
111 void GuiRef::changed_adaptor()
117 void GuiRef::gotoClicked()
123 void GuiRef::selectionChanged()
125 if (isBufferReadonly())
128 QList<QListWidgetItem *> selections = refsLW->selectedItems();
129 if (selections.isEmpty())
131 QListWidgetItem * sel = selections.first();
137 void GuiRef::refHighlighted(QListWidgetItem * sel)
139 if (isBufferReadonly())
142 /* int const cur_item = refsLW->currentRow();
143 bool const cur_item_selected = cur_item >= 0 ?
144 refsLB->isSelected(cur_item) : false;*/
145 bool const cur_item_selected = refsLW->isItemSelected(sel);
147 if (cur_item_selected)
148 referenceED->setText(sel->text());
152 gotoPB->setEnabled(true);
154 typeCO->setEnabled(true);
156 nameED->setEnabled(true);
160 void GuiRef::refSelected(QListWidgetItem * sel)
162 if (isBufferReadonly())
165 /* int const cur_item = refsLW->currentRow();
166 bool const cur_item_selected = cur_item >= 0 ?
167 refsLB->isSelected(cur_item) : false;*/
168 bool const cur_item_selected = refsLW->isItemSelected(sel);
170 if (cur_item_selected)
171 referenceED->setText(sel->text());
172 // <enter> or double click, inserts ref and closes dialog
177 void GuiRef::sortToggled()
179 caseSensitiveCB->setEnabled(sortCB->isChecked());
184 void GuiRef::caseSensitiveToggled()
190 void GuiRef::updateClicked()
196 void GuiRef::dialog_rejected()
199 // We have to do this manually, instead of calling slotClose(), because
200 // the dialog has already been made invisible before rejected() triggers.
201 Dialog::disconnect();
205 void GuiRef::reset_dialog()
212 void GuiRef::closeEvent(QCloseEvent * e)
220 void GuiRef::updateContents()
222 int orig_type = typeCO->currentIndex();
224 referenceED->setText(toqstr(params_["reference"]));
226 nameED->setText(toqstr(params_["name"]));
227 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
229 // restore type settings for new insets
230 if (params_["reference"].empty())
231 typeCO->setCurrentIndex(orig_type);
233 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
234 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
236 typeCO->setCurrentIndex(0);
238 // insert buffer list
240 FileNameList const & buffers = theBufferList().fileNames();
241 for (FileNameList::const_iterator it = buffers.begin();
242 it != buffers.end(); ++it) {
243 bufferCO->addItem(toqstr(makeDisplayPath(it->absFilename())));
246 int const thebuffer = theBufferList().bufferNum(buffer().fileName());
247 // restore the buffer combo setting for new insets
248 if (params_["reference"].empty() && restored_buffer_ != -1
249 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
250 bufferCO->setCurrentIndex(restored_buffer_);
252 int const num = theBufferList().bufferNum(buffer().fileName());
253 bufferCO->setCurrentIndex(num);
254 if (thebuffer != active_buffer_)
255 restored_buffer_ = num;
257 active_buffer_ = thebuffer;
260 bc().setValid(false);
264 void GuiRef::applyView()
266 last_reference_ = referenceED->text();
268 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
269 params_["reference"] = qstring_to_ucs4(last_reference_);
270 params_["name"] = qstring_to_ucs4(nameED->text());
272 restored_buffer_ = bufferCO->currentIndex();
276 bool GuiRef::nameAllowed()
278 KernelDocType const doc_type = docType();
279 return doc_type != LATEX && doc_type != LITERATE;
283 bool GuiRef::typeAllowed()
285 return docType() != DOCBOOK;
289 void GuiRef::setGoBack()
291 gotoPB->setText(qt_("&Go Back"));
292 gotoPB->setToolTip("");
293 gotoPB->setToolTip(qt_("Jump back"));
297 void GuiRef::setGotoRef()
299 gotoPB->setText(qt_("&Go to Label"));
300 gotoPB->setToolTip("");
301 gotoPB->setToolTip(qt_("Jump to label"));
305 void GuiRef::gotoRef()
307 string ref = fromqstr(referenceED->text());
321 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
323 return s1.toLower() < s2.toLower();
327 void GuiRef::redoRefs()
329 // Prevent these widgets from emitting any signals whilst
330 // we modify their state.
331 refsLW->blockSignals(true);
332 referenceED->blockSignals(true);
333 refsLW->setUpdatesEnabled(false);
337 // need this because Qt will send a highlight() here for
338 // the first item inserted
339 QString const oldSelection(referenceED->text());
341 QStringList refsStrings;
342 vector<docstring>::const_iterator iter;
343 for (iter = refs_.begin(); iter != refs_.end(); ++iter)
344 refsStrings.append(toqstr(*iter));
346 if (sortCB->isEnabled() && sortCB->isChecked()) {
347 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
348 qSort(refsStrings.begin(), refsStrings.end());
350 qSort(refsStrings.begin(), refsStrings.end(),
351 caseInsensitiveLessThan /*defined above*/);
354 refsLW->addItems(refsStrings);
356 referenceED->setText(oldSelection);
358 // restore the last selection or, for new insets, highlight
359 // the previous selection
360 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
361 bool const newInset = oldSelection.isEmpty();
362 QString textToFind = newInset ? last_reference_ : oldSelection;
363 last_reference_.clear();
364 for (int i = 0; i != refsLW->count(); ++i) {
365 QListWidgetItem * item = refsLW->item(i);
366 if (textToFind == item->text()) {
367 refsLW->setCurrentItem(item);
368 refsLW->setItemSelected(item, !newInset);
369 //Make sure selected item is visible
370 refsLW->scrollToItem(item);
371 last_reference_ = textToFind;
376 refsLW->setUpdatesEnabled(true);
379 // Re-activate the emission of signals by these widgets.
380 refsLW->blockSignals(false);
381 referenceED->blockSignals(false);
385 void GuiRef::updateRefs()
388 int const the_buffer = bufferCO->currentIndex();
389 if (the_buffer != -1) {
390 FileName const & name = theBufferList().fileNames()[the_buffer];
391 Buffer const * buf = theBufferList().getBuffer(name);
392 buf->getLabelList(refs_);
394 sortCB->setEnabled(!refs_.empty());
395 caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
396 refsLW->setEnabled(!refs_.empty());
397 // refsLW should only be the focus proxy when it is enabled
398 setFocusProxy(refs_.empty() ? 0 : refsLW);
399 gotoPB->setEnabled(!refs_.empty());
404 bool GuiRef::isValid()
406 return !referenceED->text().isEmpty();
410 void GuiRef::gotoRef(string const & ref)
412 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
413 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
417 void GuiRef::gotoBookmark()
419 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
423 bool GuiRef::initialiseParams(std::string const & data)
425 InsetCommand::string2params("ref", data, params_);
430 void GuiRef::dispatchParams()
432 std::string const lfun = InsetCommand::params2string("ref", params_);
433 dispatch(FuncRequest(getLfun(), lfun));
438 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
441 } // namespace frontend
444 #include "moc_GuiRef.cpp"