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 bool noprefix = false;
354 for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
355 QString const lab = toqstr(*iter);
356 refsStrings.append(lab);
357 if (groupCB->isChecked()) {
358 if (lab.contains(":")) {
359 QString const pref = lab.split(':')[0];
360 if (!refsCategories.contains(pref)) {
362 refsCategories.append(pref);
371 // sort categories case-intensively
372 qSort(refsCategories.begin(), refsCategories.end(),
373 caseInsensitiveLessThan /*defined above*/);
375 refsCategories.insert(0, qt_("<No prefix>"));
377 if (sortCB->isEnabled() && sortCB->isChecked()) {
378 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
379 qSort(refsStrings.begin(), refsStrings.end());
381 qSort(refsStrings.begin(), refsStrings.end(),
382 caseInsensitiveLessThan /*defined above*/);
385 if (groupCB->isChecked()) {
386 QList<QTreeWidgetItem *> refsCats;
387 for (int i = 0; i < refsCategories.size(); ++i) {
388 QString const cat = refsCategories.at(i);
389 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
390 item->setText(0, cat);
391 for (int i = 0; i < refsStrings.size(); ++i) {
392 QString const ref = refsStrings.at(i);
393 if ((ref.startsWith(cat + QString(":")))
394 || (cat == qt_("<No prefix>")
395 && (!ref.mid(1).contains(":") || ref.left(1).contains(":")))) {
396 QTreeWidgetItem * child =
397 new QTreeWidgetItem(item);
398 child->setText(0, ref);
399 item->addChild(child);
402 refsCats.append(item);
404 refsTW->addTopLevelItems(refsCats);
406 QList<QTreeWidgetItem *> refsItems;
407 for (int i = 0; i < refsStrings.size(); ++i) {
408 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
409 item->setText(0, refsStrings.at(i));
410 refsItems.append(item);
412 refsTW->addTopLevelItems(refsItems);
415 referenceED->setText(oldSelection);
417 // restore the last selection or, for new insets, highlight
418 // the previous selection
419 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
420 bool const newInset = oldSelection.isEmpty();
421 QString textToFind = newInset ? last_reference_ : oldSelection;
422 last_reference_.clear();
423 QTreeWidgetItemIterator it(refsTW);
425 if ((*it)->text(0) == textToFind) {
426 refsTW->setCurrentItem(*it);
427 refsTW->setItemSelected(*it, !newInset);
428 //Make sure selected item is visible
429 refsTW->scrollToItem(*it);
430 last_reference_ = textToFind;
436 refsTW->setUpdatesEnabled(true);
442 // Re-activate the emission of signals by these widgets.
443 refsTW->blockSignals(false);
444 referenceED->blockSignals(false);
448 void GuiRef::updateRefs()
451 int const the_buffer = bufferCO->currentIndex();
452 if (the_buffer != -1) {
453 FileName const & name = theBufferList().fileNames()[the_buffer];
454 Buffer const * buf = theBufferList().getBuffer(name);
455 buf->getLabelList(refs_);
457 sortCB->setEnabled(!refs_.empty());
458 caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
459 refsTW->setEnabled(!refs_.empty());
460 // refsTW should only be the focus proxy when it is enabled
461 setFocusProxy(refs_.empty() ? 0 : refsTW);
462 gotoPB->setEnabled(!refs_.empty());
467 bool GuiRef::isValid()
469 return !referenceED->text().isEmpty();
473 void GuiRef::gotoRef(string const & ref)
475 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
476 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
480 void GuiRef::gotoBookmark()
482 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
486 void GuiRef::filterLabels()
488 Qt::CaseSensitivity cs = csFindCB->isChecked() ?
489 Qt::CaseSensitive : Qt::CaseInsensitive;
490 QTreeWidgetItemIterator it(refsTW);
493 (*it)->childCount() == 0
494 && !(*it)->text(0).contains(findLE->text(), cs)
501 bool GuiRef::initialiseParams(std::string const & data)
503 InsetCommand::string2params(data, params_);
508 void GuiRef::dispatchParams()
510 std::string const lfun = InsetCommand::params2string(params_);
511 dispatch(FuncRequest(getLfun(), lfun));
516 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
519 } // namespace frontend
522 #include "moc_GuiRef.cpp"