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 /* int const cur_item = refsTW->currentRow();
166 bool const cur_item_selected = cur_item >= 0 ?
167 refsLB->isSelected(cur_item) : false;*/
168 bool const cur_item_selected = refsTW->isItemSelected(sel);
170 if (cur_item_selected)
171 referenceED->setText(sel->text(0));
172 // <enter> or double click, inserts ref and closes dialog
177 void GuiRef::sortToggled()
179 caseSensitiveCB->setEnabled(sortCB->isChecked());
184 void GuiRef::caseSensitiveToggled()
190 void GuiRef::groupToggled()
196 void GuiRef::updateClicked()
202 void GuiRef::dialogRejected()
205 // We have to do this manually, instead of calling slotClose(), because
206 // the dialog has already been made invisible before rejected() triggers.
207 Dialog::disconnect();
211 void GuiRef::resetDialog()
218 void GuiRef::closeEvent(QCloseEvent * e)
226 void GuiRef::updateContents()
228 int orig_type = typeCO->currentIndex();
230 referenceED->clear();
233 referenceED->setText(toqstr(params_["reference"]));
234 nameED->setText(toqstr(params_["name"]));
235 nameED->setHidden(!nameAllowed());
236 nameL->setHidden(!nameAllowed());
238 // restore type settings for new insets
239 if (params_["reference"].empty())
240 typeCO->setCurrentIndex(orig_type);
242 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
243 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
245 typeCO->setCurrentIndex(0);
247 // insert buffer list
249 FileNameList const & buffers = theBufferList().fileNames();
250 for (FileNameList::const_iterator it = buffers.begin();
251 it != buffers.end(); ++it) {
252 bufferCO->addItem(toqstr(makeDisplayPath(it->absFileName())));
255 int const thebuffer = theBufferList().bufferNum(buffer().fileName());
256 // restore the buffer combo setting for new insets
257 if (params_["reference"].empty() && restored_buffer_ != -1
258 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
259 bufferCO->setCurrentIndex(restored_buffer_);
261 int const num = theBufferList().bufferNum(buffer().fileName());
262 bufferCO->setCurrentIndex(num);
263 if (thebuffer != active_buffer_)
264 restored_buffer_ = num;
266 active_buffer_ = thebuffer;
269 bc().setValid(false);
273 void GuiRef::applyView()
275 last_reference_ = referenceED->text();
277 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
278 params_["reference"] = qstring_to_ucs4(last_reference_);
279 params_["name"] = qstring_to_ucs4(nameED->text());
281 restored_buffer_ = bufferCO->currentIndex();
285 bool GuiRef::nameAllowed()
287 KernelDocType const doc_type = docType();
288 return doc_type != LATEX && doc_type != LITERATE;
292 bool GuiRef::typeAllowed()
294 return docType() != DOCBOOK;
298 void GuiRef::setGoBack()
300 gotoPB->setText(qt_("&Go Back"));
301 gotoPB->setToolTip("");
302 gotoPB->setToolTip(qt_("Jump back"));
306 void GuiRef::setGotoRef()
308 gotoPB->setText(qt_("&Go to Label"));
309 gotoPB->setToolTip("");
310 gotoPB->setToolTip(qt_("Jump to label"));
314 void GuiRef::gotoRef()
316 string ref = fromqstr(referenceED->text());
330 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
332 return s1.toLower() < s2.toLower();
336 void GuiRef::redoRefs()
338 // Prevent these widgets from emitting any signals whilst
339 // we modify their state.
340 refsTW->blockSignals(true);
341 referenceED->blockSignals(true);
342 refsTW->setUpdatesEnabled(false);
346 // need this because Qt will send a highlight() here for
347 // the first item inserted
348 QString const oldSelection(referenceED->text());
350 QStringList refsStrings;
351 QStringList refsCategories;
352 vector<docstring>::const_iterator iter;
353 for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
354 QString const lab = toqstr(*iter);
355 refsStrings.append(lab);
356 if (groupCB->isChecked() && lab.contains(":")) {
357 QString const pref = lab.split(':')[0];
358 if (!pref.isEmpty() && !refsCategories.contains(pref))
359 refsCategories.append(pref);
362 // sort categories case-intensively
363 qSort(refsCategories.begin(), refsCategories.end(),
364 caseInsensitiveLessThan /*defined above*/);
365 refsCategories.insert(0, qt_("<No prefix>"));
367 if (sortCB->isEnabled() && sortCB->isChecked()) {
368 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
369 qSort(refsStrings.begin(), refsStrings.end());
371 qSort(refsStrings.begin(), refsStrings.end(),
372 caseInsensitiveLessThan /*defined above*/);
375 if (groupCB->isChecked()) {
376 QList<QTreeWidgetItem *> refsCats;
377 for (int i = 0; i < refsCategories.size(); ++i) {
378 QString const cat = refsCategories.at(i);
379 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
380 item->setText(0, cat);
381 for (int i = 0; i < refsStrings.size(); ++i) {
382 QString const ref = refsStrings.at(i);
383 if ((ref.startsWith(cat + QString(":")))
384 || (cat == qt_("<No prefix>")
385 && !ref.contains(":"))) {
386 QTreeWidgetItem * child =
387 new QTreeWidgetItem(item);
388 child->setText(0, ref);
389 item->addChild(child);
392 refsCats.append(item);
394 refsTW->addTopLevelItems(refsCats);
396 QList<QTreeWidgetItem *> refsItems;
397 for (int i = 0; i < refsStrings.size(); ++i) {
398 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
399 item->setText(0, refsStrings.at(i));
400 refsItems.append(item);
402 refsTW->addTopLevelItems(refsItems);
405 referenceED->setText(oldSelection);
407 // restore the last selection or, for new insets, highlight
408 // the previous selection
409 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
410 bool const newInset = oldSelection.isEmpty();
411 QString textToFind = newInset ? last_reference_ : oldSelection;
412 last_reference_.clear();
413 QTreeWidgetItemIterator it(refsTW);
415 if ((*it)->text(0) == textToFind) {
416 refsTW->setCurrentItem(*it);
417 refsTW->setItemSelected(*it, !newInset);
418 //Make sure selected item is visible
419 refsTW->scrollToItem(*it);
420 last_reference_ = textToFind;
426 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::filterLabels()
478 Qt::CaseSensitivity cs = csFindCB->isChecked() ?
479 Qt::CaseSensitive : Qt::CaseInsensitive;
480 QTreeWidgetItemIterator it(refsTW);
483 (*it)->childCount() == 0
484 && !(*it)->text(0).contains(findLE->text(), cs)
491 bool GuiRef::initialiseParams(std::string const & data)
493 InsetCommand::string2params(data, params_);
498 void GuiRef::dispatchParams()
500 std::string const lfun = InsetCommand::params2string(params_);
501 dispatch(FuncRequest(getLfun(), lfun));
506 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
509 } // namespace frontend
512 #include "moc_GuiRef.cpp"