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 <QTreeWidget>
31 #include <QTreeWidgetItem>
32 #include <QPushButton>
34 #include <QCloseEvent>
35 #include <QHeaderView>
38 using namespace lyx::support;
43 GuiRef::GuiRef(GuiView & lv)
44 : GuiDialog(lv, "ref", qt_("Cross-reference")),
45 params_(insetCode("ref"))
51 refsTW->setColumnCount(1);
52 refsTW->header()->setVisible(false);
54 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
55 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
56 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
57 connect(closePB, SIGNAL(clicked()), this, SLOT(resetDialog()));
58 connect(this, SIGNAL(rejected()), this, SLOT(dialogRejected()));
60 connect(typeCO, SIGNAL(activated(int)),
61 this, SLOT(changed_adaptor()));
62 connect(referenceED, SIGNAL(textChanged(QString)),
63 this, SLOT(changed_adaptor()));
64 connect(findLE, SIGNAL(textEdited(QString)),
65 this, SLOT(filterLabels()));
66 connect(csFindCB, SIGNAL(clicked()),
67 this, SLOT(filterLabels()));
68 connect(nameED, SIGNAL(textChanged(QString)),
69 this, SLOT(changed_adaptor()));
70 connect(refsTW, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
71 this, SLOT(refHighlighted(QTreeWidgetItem *)));
72 connect(refsTW, SIGNAL(itemSelectionChanged()),
73 this, SLOT(selectionChanged()));
74 connect(refsTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
75 this, SLOT(refSelected(QTreeWidgetItem *)));
76 connect(sortCB, SIGNAL(clicked()),
77 this, SLOT(sortToggled()));
78 connect(caseSensitiveCB, SIGNAL(clicked()),
79 this, SLOT(caseSensitiveToggled()));
80 connect(groupCB, SIGNAL(clicked()),
81 this, SLOT(groupToggled()));
82 connect(gotoPB, SIGNAL(clicked()),
83 this, SLOT(gotoClicked()));
84 connect(updatePB, SIGNAL(clicked()),
85 this, SLOT(updateClicked()));
86 connect(bufferCO, SIGNAL(activated(int)),
87 this, SLOT(updateClicked()));
89 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
91 bc().setApply(applyPB);
92 bc().setCancel(closePB);
93 bc().addReadOnly(typeCO);
95 restored_buffer_ = -1;
100 void GuiRef::enableView(bool enable)
103 // In the opposite case, updateContents() will be called anyway.
105 GuiDialog::enableView(enable);
109 void GuiRef::changed_adaptor()
115 void GuiRef::gotoClicked()
117 // By setting last_reference_, we ensure that the reference
118 // to which we are going (or from which we are returning) is
119 // restored in the dialog. It's a bit of a hack, but it works,
120 // and no-one seems to have any better idea.
121 bool const toggled = last_reference_.empty();
123 last_reference_ = referenceED->text();
126 last_reference_.clear();
130 void GuiRef::selectionChanged()
132 if (isBufferReadonly())
135 QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
136 if (selections.isEmpty())
138 QTreeWidgetItem * sel = selections.first();
144 void GuiRef::refHighlighted(QTreeWidgetItem * sel)
146 if (sel->childCount() > 0) {
147 sel->setExpanded(true);
151 /* int const cur_item = refsTW->currentRow();
152 bool const cur_item_selected = cur_item >= 0 ?
153 refsLB->isSelected(cur_item) : false;*/
154 bool const cur_item_selected = refsTW->isItemSelected(sel);
156 if (cur_item_selected)
157 referenceED->setText(sel->text(0));
161 gotoPB->setEnabled(true);
162 if (typeAllowed() && !isBufferReadonly())
163 typeCO->setEnabled(true);
164 nameED->setHidden(!nameAllowed());
165 nameL->setHidden(!nameAllowed());
169 void GuiRef::refSelected(QTreeWidgetItem * sel)
171 if (isBufferReadonly())
174 if (sel->childCount()) {
175 sel->setExpanded(false);
179 /* int const cur_item = refsTW->currentRow();
180 bool const cur_item_selected = cur_item >= 0 ?
181 refsLB->isSelected(cur_item) : false;*/
182 bool const cur_item_selected = refsTW->isItemSelected(sel);
184 if (cur_item_selected)
185 referenceED->setText(sel->text(0));
186 // <enter> or double click, inserts ref and closes dialog
191 void GuiRef::sortToggled()
193 caseSensitiveCB->setEnabled(sortCB->isChecked());
198 void GuiRef::caseSensitiveToggled()
204 void GuiRef::groupToggled()
210 void GuiRef::updateClicked()
216 void GuiRef::dialogRejected()
219 // We have to do this manually, instead of calling slotClose(), because
220 // the dialog has already been made invisible before rejected() triggers.
221 Dialog::disconnect();
225 void GuiRef::resetDialog()
232 void GuiRef::closeEvent(QCloseEvent * e)
240 void GuiRef::updateContents()
242 int orig_type = typeCO->currentIndex();
244 referenceED->clear();
247 referenceED->setText(toqstr(params_["reference"]));
248 nameED->setText(toqstr(params_["name"]));
249 nameED->setHidden(!nameAllowed());
250 nameL->setHidden(!nameAllowed());
252 // restore type settings for new insets
253 if (params_["reference"].empty())
254 typeCO->setCurrentIndex(orig_type);
256 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
257 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
259 typeCO->setCurrentIndex(0);
261 // insert buffer list
263 FileNameList const buffers(theBufferList().fileNames());
264 for (FileNameList::const_iterator it = buffers.begin();
265 it != buffers.end(); ++it) {
266 bufferCO->addItem(toqstr(makeDisplayPath(it->absFileName())));
269 int const thebuffer = theBufferList().bufferNum(buffer().fileName());
270 // restore the buffer combo setting for new insets
271 if (params_["reference"].empty() && restored_buffer_ != -1
272 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
273 bufferCO->setCurrentIndex(restored_buffer_);
275 int const num = theBufferList().bufferNum(buffer().fileName());
276 bufferCO->setCurrentIndex(num);
277 if (thebuffer != active_buffer_)
278 restored_buffer_ = num;
280 active_buffer_ = thebuffer;
283 bc().setValid(false);
287 void GuiRef::applyView()
289 last_reference_ = referenceED->text();
291 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
292 params_["reference"] = qstring_to_ucs4(last_reference_);
293 params_["name"] = qstring_to_ucs4(nameED->text());
295 restored_buffer_ = bufferCO->currentIndex();
299 bool GuiRef::nameAllowed()
301 KernelDocType const doc_type = docType();
302 return doc_type != LATEX && doc_type != LITERATE;
306 bool GuiRef::typeAllowed()
308 return docType() != DOCBOOK;
312 void GuiRef::setGoBack()
314 gotoPB->setText(qt_("&Go Back"));
315 gotoPB->setToolTip("");
316 gotoPB->setToolTip(qt_("Jump back"));
320 void GuiRef::setGotoRef()
322 gotoPB->setText(qt_("&Go to Label"));
323 gotoPB->setToolTip("");
324 gotoPB->setToolTip(qt_("Jump to label"));
328 void GuiRef::gotoRef()
330 string ref = fromqstr(referenceED->text());
344 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
346 return s1.toLower() < s2.toLower();
350 void GuiRef::redoRefs()
352 // Prevent these widgets from emitting any signals whilst
353 // we modify their state.
354 refsTW->blockSignals(true);
355 referenceED->blockSignals(true);
356 refsTW->setUpdatesEnabled(false);
360 // need this because Qt will send a highlight() here for
361 // the first item inserted
362 QString const oldSelection(referenceED->text());
364 QStringList refsStrings;
365 QStringList refsCategories;
366 vector<docstring>::const_iterator iter;
367 bool noprefix = false;
368 for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
369 QString const lab = toqstr(*iter);
370 refsStrings.append(lab);
371 if (groupCB->isChecked()) {
372 if (lab.contains(":")) {
373 QString const pref = lab.split(':')[0];
374 if (!refsCategories.contains(pref)) {
376 refsCategories.append(pref);
385 // sort categories case-intensively
386 qSort(refsCategories.begin(), refsCategories.end(),
387 caseInsensitiveLessThan /*defined above*/);
389 refsCategories.insert(0, qt_("<No prefix>"));
391 if (sortCB->isEnabled() && sortCB->isChecked()) {
392 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
393 qSort(refsStrings.begin(), refsStrings.end());
395 qSort(refsStrings.begin(), refsStrings.end(),
396 caseInsensitiveLessThan /*defined above*/);
399 if (groupCB->isChecked()) {
400 QList<QTreeWidgetItem *> refsCats;
401 for (int i = 0; i < refsCategories.size(); ++i) {
402 QString const cat = refsCategories.at(i);
403 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
404 item->setText(0, cat);
405 for (int i = 0; i < refsStrings.size(); ++i) {
406 QString const ref = refsStrings.at(i);
407 if ((ref.startsWith(cat + QString(":")))
408 || (cat == qt_("<No prefix>")
409 && (!ref.mid(1).contains(":") || ref.left(1).contains(":")))) {
410 QTreeWidgetItem * child =
411 new QTreeWidgetItem(item);
412 child->setText(0, ref);
413 item->addChild(child);
416 refsCats.append(item);
418 refsTW->addTopLevelItems(refsCats);
420 QList<QTreeWidgetItem *> refsItems;
421 for (int i = 0; i < refsStrings.size(); ++i) {
422 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
423 item->setText(0, refsStrings.at(i));
424 refsItems.append(item);
426 refsTW->addTopLevelItems(refsItems);
429 // restore the last selection or, for new insets, highlight
430 // the previous selection
431 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
432 bool const newInset = oldSelection.isEmpty();
433 QString textToFind = newInset ? last_reference_ : oldSelection;
434 referenceED->setText(textToFind);
435 last_reference_.clear();
436 QTreeWidgetItemIterator it(refsTW);
438 if ((*it)->text(0) == textToFind) {
439 refsTW->setCurrentItem(*it);
440 refsTW->setItemSelected(*it, true);
441 //Make sure selected item is visible
442 refsTW->scrollToItem(*it);
443 last_reference_ = textToFind;
449 refsTW->setUpdatesEnabled(true);
455 // Re-activate the emission of signals by these widgets.
456 refsTW->blockSignals(false);
457 referenceED->blockSignals(false);
461 void GuiRef::updateRefs()
464 int const the_buffer = bufferCO->currentIndex();
465 if (the_buffer != -1) {
466 FileNameList const names(theBufferList().fileNames());
467 FileName const & name = names[the_buffer];
468 Buffer const * buf = theBufferList().getBuffer(name);
469 buf->getLabelList(refs_);
471 sortCB->setEnabled(!refs_.empty());
472 caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
473 refsTW->setEnabled(!refs_.empty());
474 groupCB->setEnabled(!refs_.empty());
475 // refsTW should only be the focus proxy when it is enabled
476 setFocusProxy(refs_.empty() ? 0 : refsTW);
477 gotoPB->setEnabled(!refs_.empty());
482 bool GuiRef::isValid()
484 return !referenceED->text().isEmpty();
488 void GuiRef::gotoRef(string const & ref)
490 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
491 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
495 void GuiRef::gotoBookmark()
497 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
501 void GuiRef::filterLabels()
503 Qt::CaseSensitivity cs = csFindCB->isChecked() ?
504 Qt::CaseSensitive : Qt::CaseInsensitive;
505 QTreeWidgetItemIterator it(refsTW);
508 (*it)->childCount() == 0
509 && !(*it)->text(0).contains(findLE->text(), cs)
516 bool GuiRef::initialiseParams(std::string const & data)
518 InsetCommand::string2params(data, params_);
523 void GuiRef::dispatchParams()
525 std::string const lfun = InsetCommand::params2string(params_);
526 dispatch(FuncRequest(getLfun(), lfun));
531 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
534 } // namespace frontend
537 #include "moc_GuiRef.cpp"