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()
121 void GuiRef::selectionChanged()
123 if (isBufferReadonly())
126 QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
127 if (selections.isEmpty())
129 QTreeWidgetItem * sel = selections.first();
135 void GuiRef::refHighlighted(QTreeWidgetItem * sel)
137 if (sel->childCount() > 0) {
138 sel->setExpanded(true);
142 /* int const cur_item = refsTW->currentRow();
143 bool const cur_item_selected = cur_item >= 0 ?
144 refsLB->isSelected(cur_item) : false;*/
145 bool const cur_item_selected = refsTW->isItemSelected(sel);
147 if (cur_item_selected)
148 referenceED->setText(sel->text(0));
152 gotoPB->setEnabled(true);
153 if (typeAllowed() && !isBufferReadonly())
154 typeCO->setEnabled(true);
155 nameED->setHidden(!nameAllowed());
156 nameL->setHidden(!nameAllowed());
160 void GuiRef::refSelected(QTreeWidgetItem * sel)
162 if (isBufferReadonly())
165 if (sel->childCount()) {
166 sel->setExpanded(false);
170 /* int const cur_item = refsTW->currentRow();
171 bool const cur_item_selected = cur_item >= 0 ?
172 refsLB->isSelected(cur_item) : false;*/
173 bool const cur_item_selected = refsTW->isItemSelected(sel);
175 if (cur_item_selected)
176 referenceED->setText(sel->text(0));
177 // <enter> or double click, inserts ref and closes dialog
182 void GuiRef::sortToggled()
184 caseSensitiveCB->setEnabled(sortCB->isChecked());
189 void GuiRef::caseSensitiveToggled()
195 void GuiRef::groupToggled()
201 void GuiRef::updateClicked()
207 void GuiRef::dialogRejected()
210 // We have to do this manually, instead of calling slotClose(), because
211 // the dialog has already been made invisible before rejected() triggers.
212 Dialog::disconnect();
216 void GuiRef::resetDialog()
223 void GuiRef::closeEvent(QCloseEvent * e)
231 void GuiRef::updateContents()
233 int orig_type = typeCO->currentIndex();
235 referenceED->clear();
238 referenceED->setText(toqstr(params_["reference"]));
239 nameED->setText(toqstr(params_["name"]));
240 nameED->setHidden(!nameAllowed());
241 nameL->setHidden(!nameAllowed());
243 // restore type settings for new insets
244 if (params_["reference"].empty())
245 typeCO->setCurrentIndex(orig_type);
247 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
248 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
250 typeCO->setCurrentIndex(0);
252 // insert buffer list
254 FileNameList const buffers(theBufferList().fileNames());
255 for (FileNameList::const_iterator it = buffers.begin();
256 it != buffers.end(); ++it) {
257 bufferCO->addItem(toqstr(makeDisplayPath(it->absFileName())));
260 int const thebuffer = theBufferList().bufferNum(buffer().fileName());
261 // restore the buffer combo setting for new insets
262 if (params_["reference"].empty() && restored_buffer_ != -1
263 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
264 bufferCO->setCurrentIndex(restored_buffer_);
266 int const num = theBufferList().bufferNum(buffer().fileName());
267 bufferCO->setCurrentIndex(num);
268 if (thebuffer != active_buffer_)
269 restored_buffer_ = num;
271 active_buffer_ = thebuffer;
274 bc().setValid(false);
278 void GuiRef::applyView()
280 last_reference_ = referenceED->text();
282 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
283 params_["reference"] = qstring_to_ucs4(last_reference_);
284 params_["name"] = qstring_to_ucs4(nameED->text());
286 restored_buffer_ = bufferCO->currentIndex();
290 bool GuiRef::nameAllowed()
292 KernelDocType const doc_type = docType();
293 return doc_type != LATEX && doc_type != LITERATE;
297 bool GuiRef::typeAllowed()
299 return docType() != DOCBOOK;
303 void GuiRef::setGoBack()
305 gotoPB->setText(qt_("&Go Back"));
306 gotoPB->setToolTip("");
307 gotoPB->setToolTip(qt_("Jump back"));
311 void GuiRef::setGotoRef()
313 gotoPB->setText(qt_("&Go to Label"));
314 gotoPB->setToolTip("");
315 gotoPB->setToolTip(qt_("Jump to label"));
319 void GuiRef::gotoRef()
321 string ref = fromqstr(referenceED->text());
335 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
337 return s1.toLower() < s2.toLower();
341 void GuiRef::redoRefs()
343 // Prevent these widgets from emitting any signals whilst
344 // we modify their state.
345 refsTW->blockSignals(true);
346 referenceED->blockSignals(true);
347 refsTW->setUpdatesEnabled(false);
351 // need this because Qt will send a highlight() here for
352 // the first item inserted
353 QString const oldSelection(referenceED->text());
355 QStringList refsStrings;
356 QStringList refsCategories;
357 vector<docstring>::const_iterator iter;
358 bool noprefix = false;
359 for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
360 QString const lab = toqstr(*iter);
361 refsStrings.append(lab);
362 if (groupCB->isChecked()) {
363 if (lab.contains(":")) {
364 QString const pref = lab.split(':')[0];
365 if (!refsCategories.contains(pref)) {
367 refsCategories.append(pref);
376 // sort categories case-intensively
377 qSort(refsCategories.begin(), refsCategories.end(),
378 caseInsensitiveLessThan /*defined above*/);
380 refsCategories.insert(0, qt_("<No prefix>"));
382 if (sortCB->isEnabled() && sortCB->isChecked()) {
383 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
384 qSort(refsStrings.begin(), refsStrings.end());
386 qSort(refsStrings.begin(), refsStrings.end(),
387 caseInsensitiveLessThan /*defined above*/);
390 if (groupCB->isChecked()) {
391 QList<QTreeWidgetItem *> refsCats;
392 for (int i = 0; i < refsCategories.size(); ++i) {
393 QString const cat = refsCategories.at(i);
394 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
395 item->setText(0, cat);
396 for (int i = 0; i < refsStrings.size(); ++i) {
397 QString const ref = refsStrings.at(i);
398 if ((ref.startsWith(cat + QString(":")))
399 || (cat == qt_("<No prefix>")
400 && (!ref.mid(1).contains(":") || ref.left(1).contains(":")))) {
401 QTreeWidgetItem * child =
402 new QTreeWidgetItem(item);
403 child->setText(0, ref);
404 item->addChild(child);
407 refsCats.append(item);
409 refsTW->addTopLevelItems(refsCats);
411 QList<QTreeWidgetItem *> refsItems;
412 for (int i = 0; i < refsStrings.size(); ++i) {
413 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
414 item->setText(0, refsStrings.at(i));
415 refsItems.append(item);
417 refsTW->addTopLevelItems(refsItems);
420 referenceED->setText(oldSelection);
422 // restore the last selection or, for new insets, highlight
423 // the previous selection
424 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
425 bool const newInset = oldSelection.isEmpty();
426 QString textToFind = newInset ? last_reference_ : oldSelection;
427 last_reference_.clear();
428 QTreeWidgetItemIterator it(refsTW);
430 if ((*it)->text(0) == textToFind) {
431 refsTW->setCurrentItem(*it);
432 refsTW->setItemSelected(*it, !newInset);
433 //Make sure selected item is visible
434 refsTW->scrollToItem(*it);
435 last_reference_ = textToFind;
441 refsTW->setUpdatesEnabled(true);
447 // Re-activate the emission of signals by these widgets.
448 refsTW->blockSignals(false);
449 referenceED->blockSignals(false);
453 void GuiRef::updateRefs()
456 int const the_buffer = bufferCO->currentIndex();
457 if (the_buffer != -1) {
458 FileNameList const names(theBufferList().fileNames());
459 FileName const & name = names[the_buffer];
460 Buffer const * buf = theBufferList().getBuffer(name);
461 buf->getLabelList(refs_);
463 sortCB->setEnabled(!refs_.empty());
464 caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
465 refsTW->setEnabled(!refs_.empty());
466 groupCB->setEnabled(!refs_.empty());
467 // refsTW should only be the focus proxy when it is enabled
468 setFocusProxy(refs_.empty() ? 0 : refsTW);
469 gotoPB->setEnabled(!refs_.empty());
474 bool GuiRef::isValid()
476 return !referenceED->text().isEmpty();
480 void GuiRef::gotoRef(string const & ref)
482 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
483 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
487 void GuiRef::gotoBookmark()
489 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
493 void GuiRef::filterLabels()
495 Qt::CaseSensitivity cs = csFindCB->isChecked() ?
496 Qt::CaseSensitive : Qt::CaseInsensitive;
497 QTreeWidgetItemIterator it(refsTW);
500 (*it)->childCount() == 0
501 && !(*it)->text(0).contains(findLE->text(), cs)
508 bool GuiRef::initialiseParams(std::string const & data)
510 InsetCommand::string2params(data, params_);
515 void GuiRef::dispatchParams()
517 std::string const lfun = InsetCommand::params2string(params_);
518 dispatch(FuncRequest(getLfun(), lfun));
523 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
526 } // namespace frontend
529 #include "moc_GuiRef.cpp"