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>
37 using namespace lyx::support;
42 GuiRef::GuiRef(GuiView & lv)
43 : GuiDialog(lv, "ref", qt_("Cross-reference")),
44 params_(insetCode("ref"))
50 // Enabling is set in updateRefs. Disable for now in case no
51 // call to updateContents follows (e.g. read-only documents).
52 sortCB->setEnabled(false);
53 caseSensitiveCB->setEnabled(false);
54 caseSensitiveCB->setChecked(false);
55 refsTW->setEnabled(false);
56 gotoPB->setEnabled(false);
58 refsTW->setColumnCount(1);
59 refsTW->header()->setVisible(false);
61 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
62 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
63 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
64 connect(closePB, SIGNAL(clicked()), this, SLOT(resetDialog()));
65 connect(this, SIGNAL(rejected()), this, SLOT(dialogRejected()));
67 connect(typeCO, SIGNAL(activated(int)),
68 this, SLOT(changed_adaptor()));
69 connect(referenceED, SIGNAL(textChanged(QString)),
70 this, SLOT(changed_adaptor()));
71 connect(findLE, SIGNAL(returnPressed()),
72 this, SLOT(on_searchPB_clicked()));
73 connect(csFindCB, SIGNAL(clicked()),
74 this, SLOT(on_searchPB_clicked()));
75 connect(nameED, SIGNAL(textChanged(QString)),
76 this, SLOT(changed_adaptor()));
77 connect(refsTW, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
78 this, SLOT(refHighlighted(QTreeWidgetItem *)));
79 connect(refsTW, SIGNAL(itemSelectionChanged()),
80 this, SLOT(selectionChanged()));
81 connect(refsTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
82 this, SLOT(refSelected(QTreeWidgetItem *)));
83 connect(sortCB, SIGNAL(clicked()),
84 this, SLOT(sortToggled()));
85 connect(caseSensitiveCB, SIGNAL(clicked()),
86 this, SLOT(caseSensitiveToggled()));
87 connect(groupCB, SIGNAL(clicked()),
88 this, SLOT(groupToggled()));
89 connect(gotoPB, SIGNAL(clicked()),
90 this, SLOT(gotoClicked()));
91 connect(updatePB, SIGNAL(clicked()),
92 this, SLOT(updateClicked()));
93 connect(bufferCO, SIGNAL(activated(int)),
94 this, SLOT(updateClicked()));
96 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
98 bc().setApply(applyPB);
99 bc().setCancel(closePB);
100 bc().addReadOnly(refsTW);
101 bc().addReadOnly(sortCB);
102 bc().addReadOnly(caseSensitiveCB);
103 bc().addReadOnly(nameED);
104 bc().addReadOnly(referenceED);
105 bc().addReadOnly(typeCO);
106 bc().addReadOnly(bufferCO);
108 restored_buffer_ = -1;
113 void GuiRef::changed_adaptor()
119 void GuiRef::gotoClicked()
125 void GuiRef::selectionChanged()
127 if (isBufferReadonly())
130 QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
131 if (selections.isEmpty())
133 QTreeWidgetItem * sel = selections.first();
139 void GuiRef::refHighlighted(QTreeWidgetItem * sel)
141 if (isBufferReadonly())
144 if (sel->childCount() > 0) {
145 sel->setExpanded(true);
149 /* int const cur_item = refsTW->currentRow();
150 bool const cur_item_selected = cur_item >= 0 ?
151 refsLB->isSelected(cur_item) : false;*/
152 bool const cur_item_selected = refsTW->isItemSelected(sel);
154 if (cur_item_selected)
155 referenceED->setText(sel->text(0));
159 gotoPB->setEnabled(true);
161 typeCO->setEnabled(true);
162 nameED->setHidden(!nameAllowed());
163 nameL->setHidden(!nameAllowed());
167 void GuiRef::refSelected(QTreeWidgetItem * sel)
169 if (isBufferReadonly())
172 /* int const cur_item = refsTW->currentRow();
173 bool const cur_item_selected = cur_item >= 0 ?
174 refsLB->isSelected(cur_item) : false;*/
175 bool const cur_item_selected = refsTW->isItemSelected(sel);
177 if (cur_item_selected)
178 referenceED->setText(sel->text(0));
179 // <enter> or double click, inserts ref and closes dialog
184 void GuiRef::sortToggled()
186 caseSensitiveCB->setEnabled(sortCB->isChecked());
191 void GuiRef::caseSensitiveToggled()
197 void GuiRef::groupToggled()
203 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 refsCategories.append(qt_("No prefix"));
359 vector<docstring>::const_iterator iter;
360 for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
361 QString const lab = toqstr(*iter);
362 refsStrings.append(lab);
363 if (groupCB->isChecked() && lab.contains(":")) {
364 QString const pref = lab.split(':')[0];
365 if (!pref.isEmpty() && !refsCategories.contains(pref))
366 refsCategories.append(pref);
370 if (sortCB->isEnabled() && sortCB->isChecked()) {
371 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
372 qSort(refsStrings.begin(), refsStrings.end());
374 qSort(refsStrings.begin(), refsStrings.end(),
375 caseInsensitiveLessThan /*defined above*/);
378 if (groupCB->isChecked()) {
379 QList<QTreeWidgetItem *> refsCats;
380 for (int i = 0; i < refsCategories.size(); ++i) {
381 QString const cat = refsCategories.at(i);
382 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
383 item->setText(0, cat);
384 for (int i = 0; i < refsStrings.size(); ++i) {
385 QString const ref = refsStrings.at(i);
386 if ((ref.startsWith(cat + QString(":")))
387 || (cat == qt_("No prefix")
388 && !ref.contains(":"))) {
389 QTreeWidgetItem * child =
390 new QTreeWidgetItem(item);
391 child->setText(0, ref);
392 item->addChild(child);
395 refsCats.append(item);
397 refsTW->addTopLevelItems(refsCats);
399 QList<QTreeWidgetItem *> refsItems;
400 for (int i = 0; i < refsStrings.size(); ++i) {
401 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
402 item->setText(0, refsStrings.at(i));
403 refsItems.append(item);
405 refsTW->addTopLevelItems(refsItems);
408 referenceED->setText(oldSelection);
410 // restore the last selection or, for new insets, highlight
411 // the previous selection
412 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
413 bool const newInset = oldSelection.isEmpty();
414 QString textToFind = newInset ? last_reference_ : oldSelection;
415 last_reference_.clear();
416 QTreeWidgetItemIterator it(refsTW);
418 if ((*it)->text(0) == textToFind) {
419 refsTW->setCurrentItem(*it);
420 refsTW->setItemSelected(*it, !newInset);
421 //Make sure selected item is visible
422 refsTW->scrollToItem(*it);
423 last_reference_ = textToFind;
429 refsTW->setUpdatesEnabled(true);
432 // Re-activate the emission of signals by these widgets.
433 refsTW->blockSignals(false);
434 referenceED->blockSignals(false);
438 void GuiRef::updateRefs()
441 int const the_buffer = bufferCO->currentIndex();
442 if (the_buffer != -1) {
443 FileName const & name = theBufferList().fileNames()[the_buffer];
444 Buffer const * buf = theBufferList().getBuffer(name);
445 buf->getLabelList(refs_);
447 sortCB->setEnabled(!refs_.empty());
448 caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
449 refsTW->setEnabled(!refs_.empty());
450 // refsTW should only be the focus proxy when it is enabled
451 setFocusProxy(refs_.empty() ? 0 : refsTW);
452 gotoPB->setEnabled(!refs_.empty());
457 bool GuiRef::isValid()
459 return !referenceED->text().isEmpty();
463 void GuiRef::gotoRef(string const & ref)
465 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
466 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
470 void GuiRef::gotoBookmark()
472 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
476 void GuiRef::on_findLE_textChanged(const QString & text)
478 searchPB->setDisabled(text.isEmpty());
482 void GuiRef::on_searchPB_clicked()
484 QTreeWidgetItemIterator it(refsTW);
485 Qt::CaseSensitivity cs = csFindCB->isChecked() ?
486 Qt::CaseSensitive : Qt::CaseInsensitive;
489 (*it)->childCount() == 0
490 && !(*it)->text(0).contains(findLE->text(), cs)
497 bool GuiRef::initialiseParams(std::string const & data)
499 InsetCommand::string2params("ref", data, params_);
504 void GuiRef::dispatchParams()
506 std::string const lfun = InsetCommand::params2string("ref", params_);
507 dispatch(FuncRequest(getLfun(), lfun));
512 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
515 } // namespace frontend
518 #include "moc_GuiRef.cpp"