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.
122 last_reference_.isEmpty() || last_reference_.isNull();
124 last_reference_ = referenceED->text();
127 last_reference_.clear();
131 void GuiRef::selectionChanged()
133 if (isBufferReadonly())
136 QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
137 if (selections.isEmpty())
139 QTreeWidgetItem * sel = selections.first();
145 void GuiRef::refHighlighted(QTreeWidgetItem * sel)
147 if (sel->childCount() > 0) {
148 sel->setExpanded(true);
152 /* int const cur_item = refsTW->currentRow();
153 bool const cur_item_selected = cur_item >= 0 ?
154 refsLB->isSelected(cur_item) : false;*/
155 bool const cur_item_selected = refsTW->isItemSelected(sel);
157 if (cur_item_selected)
158 referenceED->setText(sel->text(0));
162 gotoPB->setEnabled(true);
163 if (typeAllowed() && !isBufferReadonly())
164 typeCO->setEnabled(true);
165 nameED->setHidden(!nameAllowed());
166 nameL->setHidden(!nameAllowed());
170 void GuiRef::refSelected(QTreeWidgetItem * sel)
172 if (isBufferReadonly())
175 if (sel->childCount()) {
176 sel->setExpanded(false);
180 /* int const cur_item = refsTW->currentRow();
181 bool const cur_item_selected = cur_item >= 0 ?
182 refsLB->isSelected(cur_item) : false;*/
183 bool const cur_item_selected = refsTW->isItemSelected(sel);
185 if (cur_item_selected)
186 referenceED->setText(sel->text(0));
187 // <enter> or double click, inserts ref and closes dialog
192 void GuiRef::sortToggled()
194 caseSensitiveCB->setEnabled(sortCB->isChecked());
199 void GuiRef::caseSensitiveToggled()
205 void GuiRef::groupToggled()
211 void GuiRef::updateClicked()
217 void GuiRef::dialogRejected()
220 // We have to do this manually, instead of calling slotClose(), because
221 // the dialog has already been made invisible before rejected() triggers.
222 Dialog::disconnect();
226 void GuiRef::resetDialog()
233 void GuiRef::closeEvent(QCloseEvent * e)
241 void GuiRef::updateContents()
243 int orig_type = typeCO->currentIndex();
245 referenceED->clear();
248 referenceED->setText(toqstr(params_["reference"]));
249 nameED->setText(toqstr(params_["name"]));
250 nameED->setHidden(!nameAllowed());
251 nameL->setHidden(!nameAllowed());
253 // restore type settings for new insets
254 bool const new_inset = params_["reference"].empty();
256 typeCO->setCurrentIndex(orig_type);
258 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
259 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
261 typeCO->setCurrentIndex(0);
263 // insert buffer list
265 FileNameList const buffers(theBufferList().fileNames());
266 for (FileNameList::const_iterator it = buffers.begin();
267 it != buffers.end(); ++it) {
268 bufferCO->addItem(toqstr(makeDisplayPath(it->absFileName())));
271 int const thebuffer = theBufferList().bufferNum(buffer().fileName());
272 // restore the buffer combo setting for new insets
273 if (params_["reference"].empty() && restored_buffer_ != -1
274 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
275 bufferCO->setCurrentIndex(restored_buffer_);
277 int const num = theBufferList().bufferNum(buffer().fileName());
278 bufferCO->setCurrentIndex(num);
279 if (thebuffer != active_buffer_)
280 restored_buffer_ = num;
282 active_buffer_ = thebuffer;
285 // Activate OK/Apply buttons if the users inserts a new ref
286 // and we have a valid pre-setting.
287 bc().setValid(isValid() && new_inset);
291 void GuiRef::applyView()
293 last_reference_ = referenceED->text();
295 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
296 params_["reference"] = qstring_to_ucs4(last_reference_);
297 params_["name"] = qstring_to_ucs4(nameED->text());
299 restored_buffer_ = bufferCO->currentIndex();
303 bool GuiRef::nameAllowed()
305 KernelDocType const doc_type = docType();
306 return doc_type != LATEX && doc_type != LITERATE;
310 bool GuiRef::typeAllowed()
312 return docType() != DOCBOOK;
316 void GuiRef::setGoBack()
318 gotoPB->setText(qt_("&Go Back"));
319 gotoPB->setToolTip("");
320 gotoPB->setToolTip(qt_("Jump back"));
324 void GuiRef::setGotoRef()
326 gotoPB->setText(qt_("&Go to Label"));
327 gotoPB->setToolTip("");
328 gotoPB->setToolTip(qt_("Jump to label"));
332 void GuiRef::gotoRef()
334 string ref = fromqstr(referenceED->text());
348 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
350 return s1.toLower() < s2.toLower();
354 void GuiRef::redoRefs()
356 // Prevent these widgets from emitting any signals whilst
357 // we modify their state.
358 refsTW->blockSignals(true);
359 referenceED->blockSignals(true);
360 refsTW->setUpdatesEnabled(false);
364 // need this because Qt will send a highlight() here for
365 // the first item inserted
366 QString const oldSelection(referenceED->text());
368 QStringList refsStrings;
369 QStringList refsCategories;
370 vector<docstring>::const_iterator iter;
371 bool noprefix = false;
372 for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
373 QString const lab = toqstr(*iter);
374 refsStrings.append(lab);
375 if (groupCB->isChecked()) {
376 if (lab.contains(":")) {
377 QString const pref = lab.split(':')[0];
378 if (!refsCategories.contains(pref)) {
380 refsCategories.append(pref);
389 // sort categories case-intensively
390 qSort(refsCategories.begin(), refsCategories.end(),
391 caseInsensitiveLessThan /*defined above*/);
393 refsCategories.insert(0, qt_("<No prefix>"));
395 if (sortCB->isEnabled() && sortCB->isChecked()) {
396 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
397 qSort(refsStrings.begin(), refsStrings.end());
399 qSort(refsStrings.begin(), refsStrings.end(),
400 caseInsensitiveLessThan /*defined above*/);
403 if (groupCB->isChecked()) {
404 QList<QTreeWidgetItem *> refsCats;
405 for (int i = 0; i < refsCategories.size(); ++i) {
406 QString const cat = refsCategories.at(i);
407 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
408 item->setText(0, cat);
409 for (int i = 0; i < refsStrings.size(); ++i) {
410 QString const ref = refsStrings.at(i);
411 if ((ref.startsWith(cat + QString(":")))
412 || (cat == qt_("<No prefix>")
413 && (!ref.mid(1).contains(":") || ref.left(1).contains(":")))) {
414 QTreeWidgetItem * child =
415 new QTreeWidgetItem(item);
416 child->setText(0, ref);
417 item->addChild(child);
420 refsCats.append(item);
422 refsTW->addTopLevelItems(refsCats);
424 QList<QTreeWidgetItem *> refsItems;
425 for (int i = 0; i < refsStrings.size(); ++i) {
426 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
427 item->setText(0, refsStrings.at(i));
428 refsItems.append(item);
430 refsTW->addTopLevelItems(refsItems);
433 // restore the last selection or, for new insets, highlight
434 // the previous selection
435 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
436 bool const newInset = oldSelection.isEmpty();
437 QString textToFind = newInset ? last_reference_ : oldSelection;
438 referenceED->setText(textToFind);
439 last_reference_.clear();
440 QTreeWidgetItemIterator it(refsTW);
442 if ((*it)->text(0) == textToFind) {
443 refsTW->setCurrentItem(*it);
444 refsTW->setItemSelected(*it, true);
445 //Make sure selected item is visible
446 refsTW->scrollToItem(*it);
447 last_reference_ = textToFind;
453 refsTW->setUpdatesEnabled(true);
459 // Re-activate the emission of signals by these widgets.
460 refsTW->blockSignals(false);
461 referenceED->blockSignals(false);
465 void GuiRef::updateRefs()
468 int const the_buffer = bufferCO->currentIndex();
469 if (the_buffer != -1) {
470 FileNameList const names(theBufferList().fileNames());
471 FileName const & name = names[the_buffer];
472 Buffer const * buf = theBufferList().getBuffer(name);
473 buf->getLabelList(refs_);
475 sortCB->setEnabled(!refs_.empty());
476 caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
477 refsTW->setEnabled(!refs_.empty());
478 groupCB->setEnabled(!refs_.empty());
479 // refsTW should only be the focus proxy when it is enabled
480 setFocusProxy(refs_.empty() ? 0 : refsTW);
481 gotoPB->setEnabled(!refs_.empty());
486 bool GuiRef::isValid()
488 return !referenceED->text().isEmpty();
492 void GuiRef::gotoRef(string const & ref)
494 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
495 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
499 void GuiRef::gotoBookmark()
501 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
505 void GuiRef::filterLabels()
507 Qt::CaseSensitivity cs = csFindCB->isChecked() ?
508 Qt::CaseSensitive : Qt::CaseInsensitive;
509 QTreeWidgetItemIterator it(refsTW);
512 (*it)->childCount() == 0
513 && !(*it)->text(0).contains(findLE->text(), cs)
520 bool GuiRef::initialiseParams(std::string const & data)
522 InsetCommand::string2params(data, params_);
527 void GuiRef::dispatchParams()
529 std::string const lfun = InsetCommand::params2string(params_);
530 dispatch(FuncRequest(getLfun(), lfun));
535 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
538 } // namespace frontend
541 #include "moc_GuiRef.cpp"