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 // Enabling is set in updateRefs. Disable for now in case no
52 // call to updateContents follows (e.g. read-only documents).
53 sortCB->setEnabled(false);
54 caseSensitiveCB->setEnabled(false);
55 caseSensitiveCB->setChecked(false);
56 refsTW->setEnabled(false);
57 gotoPB->setEnabled(false);
59 refsTW->setColumnCount(1);
60 refsTW->header()->setVisible(false);
62 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
63 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
64 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
65 connect(closePB, SIGNAL(clicked()), this, SLOT(resetDialog()));
66 connect(this, SIGNAL(rejected()), this, SLOT(dialogRejected()));
68 connect(typeCO, SIGNAL(activated(int)),
69 this, SLOT(changed_adaptor()));
70 connect(referenceED, SIGNAL(textChanged(QString)),
71 this, SLOT(changed_adaptor()));
72 connect(findLE, SIGNAL(textEdited(QString)),
73 this, SLOT(filterLabels()));
74 connect(csFindCB, SIGNAL(clicked()),
75 this, SLOT(filterLabels()));
76 connect(nameED, SIGNAL(textChanged(QString)),
77 this, SLOT(changed_adaptor()));
78 connect(refsTW, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
79 this, SLOT(refHighlighted(QTreeWidgetItem *)));
80 connect(refsTW, SIGNAL(itemSelectionChanged()),
81 this, SLOT(selectionChanged()));
82 connect(refsTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
83 this, SLOT(refSelected(QTreeWidgetItem *)));
84 connect(sortCB, SIGNAL(clicked()),
85 this, SLOT(sortToggled()));
86 connect(caseSensitiveCB, SIGNAL(clicked()),
87 this, SLOT(caseSensitiveToggled()));
88 connect(groupCB, SIGNAL(clicked()),
89 this, SLOT(groupToggled()));
90 connect(gotoPB, SIGNAL(clicked()),
91 this, SLOT(gotoClicked()));
92 connect(updatePB, SIGNAL(clicked()),
93 this, SLOT(updateClicked()));
94 connect(bufferCO, SIGNAL(activated(int)),
95 this, SLOT(updateClicked()));
97 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
99 bc().setApply(applyPB);
100 bc().setCancel(closePB);
101 bc().addReadOnly(refsTW);
102 bc().addReadOnly(sortCB);
103 bc().addReadOnly(caseSensitiveCB);
104 bc().addReadOnly(nameED);
105 bc().addReadOnly(referenceED);
106 bc().addReadOnly(typeCO);
107 bc().addReadOnly(bufferCO);
109 restored_buffer_ = -1;
114 void GuiRef::changed_adaptor()
120 void GuiRef::gotoClicked()
126 void GuiRef::selectionChanged()
128 if (isBufferReadonly())
131 QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
132 if (selections.isEmpty())
134 QTreeWidgetItem * sel = selections.first();
140 void GuiRef::refHighlighted(QTreeWidgetItem * sel)
142 if (isBufferReadonly())
145 if (sel->childCount() > 0) {
146 sel->setExpanded(true);
150 /* int const cur_item = refsTW->currentRow();
151 bool const cur_item_selected = cur_item >= 0 ?
152 refsLB->isSelected(cur_item) : false;*/
153 bool const cur_item_selected = refsTW->isItemSelected(sel);
155 if (cur_item_selected)
156 referenceED->setText(sel->text(0));
160 gotoPB->setEnabled(true);
162 typeCO->setEnabled(true);
163 nameED->setHidden(!nameAllowed());
164 nameL->setHidden(!nameAllowed());
168 void GuiRef::refSelected(QTreeWidgetItem * sel)
170 if (isBufferReadonly())
173 /* int const cur_item = refsTW->currentRow();
174 bool const cur_item_selected = cur_item >= 0 ?
175 refsLB->isSelected(cur_item) : false;*/
176 bool const cur_item_selected = refsTW->isItemSelected(sel);
178 if (cur_item_selected)
179 referenceED->setText(sel->text(0));
180 // <enter> or double click, inserts ref and closes dialog
185 void GuiRef::sortToggled()
187 caseSensitiveCB->setEnabled(sortCB->isChecked());
192 void GuiRef::caseSensitiveToggled()
198 void GuiRef::groupToggled()
204 void GuiRef::updateClicked()
210 void GuiRef::dialogRejected()
213 // We have to do this manually, instead of calling slotClose(), because
214 // the dialog has already been made invisible before rejected() triggers.
215 Dialog::disconnect();
219 void GuiRef::resetDialog()
226 void GuiRef::closeEvent(QCloseEvent * e)
234 void GuiRef::updateContents()
236 int orig_type = typeCO->currentIndex();
238 referenceED->setText(toqstr(params_["reference"]));
240 nameED->setText(toqstr(params_["name"]));
241 nameED->setHidden(!nameAllowed() && !isBufferReadonly());
242 nameL->setHidden(!nameAllowed() && !isBufferReadonly());
244 // restore type settings for new insets
245 if (params_["reference"].empty())
246 typeCO->setCurrentIndex(orig_type);
248 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
249 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
251 typeCO->setCurrentIndex(0);
253 // insert buffer list
255 FileNameList const & buffers = theBufferList().fileNames();
256 for (FileNameList::const_iterator it = buffers.begin();
257 it != buffers.end(); ++it) {
258 bufferCO->addItem(toqstr(makeDisplayPath(it->absFileName())));
261 int const thebuffer = theBufferList().bufferNum(buffer().fileName());
262 // restore the buffer combo setting for new insets
263 if (params_["reference"].empty() && restored_buffer_ != -1
264 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
265 bufferCO->setCurrentIndex(restored_buffer_);
267 int const num = theBufferList().bufferNum(buffer().fileName());
268 bufferCO->setCurrentIndex(num);
269 if (thebuffer != active_buffer_)
270 restored_buffer_ = num;
272 active_buffer_ = thebuffer;
275 bc().setValid(false);
279 void GuiRef::applyView()
281 last_reference_ = referenceED->text();
283 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
284 params_["reference"] = qstring_to_ucs4(last_reference_);
285 params_["name"] = qstring_to_ucs4(nameED->text());
287 restored_buffer_ = bufferCO->currentIndex();
291 bool GuiRef::nameAllowed()
293 KernelDocType const doc_type = docType();
294 return doc_type != LATEX && doc_type != LITERATE;
298 bool GuiRef::typeAllowed()
300 return docType() != DOCBOOK;
304 void GuiRef::setGoBack()
306 gotoPB->setText(qt_("&Go Back"));
307 gotoPB->setToolTip("");
308 gotoPB->setToolTip(qt_("Jump back"));
312 void GuiRef::setGotoRef()
314 gotoPB->setText(qt_("&Go to Label"));
315 gotoPB->setToolTip("");
316 gotoPB->setToolTip(qt_("Jump to label"));
320 void GuiRef::gotoRef()
322 string ref = fromqstr(referenceED->text());
336 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
338 return s1.toLower() < s2.toLower();
342 void GuiRef::redoRefs()
344 // Prevent these widgets from emitting any signals whilst
345 // we modify their state.
346 refsTW->blockSignals(true);
347 referenceED->blockSignals(true);
348 refsTW->setUpdatesEnabled(false);
352 // need this because Qt will send a highlight() here for
353 // the first item inserted
354 QString const oldSelection(referenceED->text());
356 QStringList refsStrings;
357 QStringList refsCategories;
358 vector<docstring>::const_iterator iter;
359 for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
360 QString const lab = toqstr(*iter);
361 refsStrings.append(lab);
362 if (groupCB->isChecked() && lab.contains(":")) {
363 QString const pref = lab.split(':')[0];
364 if (!pref.isEmpty() && !refsCategories.contains(pref))
365 refsCategories.append(pref);
368 // sort categories case-intensively
369 qSort(refsCategories.begin(), refsCategories.end(),
370 caseInsensitiveLessThan /*defined above*/);
371 refsCategories.insert(0, qt_("<No prefix>"));
373 if (sortCB->isEnabled() && sortCB->isChecked()) {
374 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
375 qSort(refsStrings.begin(), refsStrings.end());
377 qSort(refsStrings.begin(), refsStrings.end(),
378 caseInsensitiveLessThan /*defined above*/);
381 if (groupCB->isChecked()) {
382 QList<QTreeWidgetItem *> refsCats;
383 for (int i = 0; i < refsCategories.size(); ++i) {
384 QString const cat = refsCategories.at(i);
385 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
386 item->setText(0, cat);
387 for (int i = 0; i < refsStrings.size(); ++i) {
388 QString const ref = refsStrings.at(i);
389 if ((ref.startsWith(cat + QString(":")))
390 || (cat == qt_("<No prefix>")
391 && !ref.contains(":"))) {
392 QTreeWidgetItem * child =
393 new QTreeWidgetItem(item);
394 child->setText(0, ref);
395 item->addChild(child);
398 refsCats.append(item);
400 refsTW->addTopLevelItems(refsCats);
402 QList<QTreeWidgetItem *> refsItems;
403 for (int i = 0; i < refsStrings.size(); ++i) {
404 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
405 item->setText(0, refsStrings.at(i));
406 refsItems.append(item);
408 refsTW->addTopLevelItems(refsItems);
411 referenceED->setText(oldSelection);
413 // restore the last selection or, for new insets, highlight
414 // the previous selection
415 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
416 bool const newInset = oldSelection.isEmpty();
417 QString textToFind = newInset ? last_reference_ : oldSelection;
418 last_reference_.clear();
419 QTreeWidgetItemIterator it(refsTW);
421 if ((*it)->text(0) == textToFind) {
422 refsTW->setCurrentItem(*it);
423 refsTW->setItemSelected(*it, !newInset);
424 //Make sure selected item is visible
425 refsTW->scrollToItem(*it);
426 last_reference_ = textToFind;
432 refsTW->setUpdatesEnabled(true);
438 // Re-activate the emission of signals by these widgets.
439 refsTW->blockSignals(false);
440 referenceED->blockSignals(false);
444 void GuiRef::updateRefs()
447 int const the_buffer = bufferCO->currentIndex();
448 if (the_buffer != -1) {
449 FileName const & name = theBufferList().fileNames()[the_buffer];
450 Buffer const * buf = theBufferList().getBuffer(name);
451 buf->getLabelList(refs_);
453 sortCB->setEnabled(!refs_.empty());
454 caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
455 refsTW->setEnabled(!refs_.empty());
456 // refsTW should only be the focus proxy when it is enabled
457 setFocusProxy(refs_.empty() ? 0 : refsTW);
458 gotoPB->setEnabled(!refs_.empty());
463 bool GuiRef::isValid()
465 return !referenceED->text().isEmpty();
469 void GuiRef::gotoRef(string const & ref)
471 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
472 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
476 void GuiRef::gotoBookmark()
478 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
482 void GuiRef::filterLabels()
484 Qt::CaseSensitivity cs = csFindCB->isChecked() ?
485 Qt::CaseSensitive : Qt::CaseInsensitive;
486 QTreeWidgetItemIterator it(refsTW);
489 (*it)->childCount() == 0
490 && !(*it)->text(0).contains(findLE->text(), cs)
497 bool GuiRef::initialiseParams(std::string const & data)
499 InsetCommand::string2params(data, params_);
504 void GuiRef::dispatchParams()
506 std::string const lfun = InsetCommand::params2string(params_);
507 dispatch(FuncRequest(getLfun(), lfun));
512 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
515 } // namespace frontend
518 #include "moc_GuiRef.cpp"