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/filetools.h" // MakeAbsPath, MakeDisplayPath
29 #include <QListWidget>
30 #include <QListWidgetItem>
31 #include <QPushButton>
33 #include <QCloseEvent>
36 using namespace lyx::support;
41 GuiRef::GuiRef(GuiView & lv)
42 : GuiCommand(lv, "ref", qt_("Cross-reference"))
49 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
50 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
51 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
52 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
53 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
55 connect(typeCO, SIGNAL(activated(int)),
56 this, SLOT(changed_adaptor()));
57 connect(referenceED, SIGNAL(textChanged(QString)),
58 this, SLOT(changed_adaptor()));
59 connect(nameED, SIGNAL(textChanged(QString)),
60 this, SLOT(changed_adaptor()));
61 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
62 this, SLOT(refHighlighted(QListWidgetItem *)));
63 connect(refsLW, SIGNAL(itemSelectionChanged()),
64 this, SLOT(selectionChanged()));
65 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
66 this, SLOT(refSelected(QListWidgetItem *)));
67 connect(sortCB, SIGNAL(clicked(bool)),
68 this, SLOT(sortToggled(bool)));
69 connect(gotoPB, SIGNAL(clicked()),
70 this, SLOT(gotoClicked()));
71 connect(updatePB, SIGNAL(clicked()),
72 this, SLOT(updateClicked()));
73 connect(bufferCO, SIGNAL(activated(int)),
74 this, SLOT(updateClicked()));
76 setFocusProxy(refsLW);
78 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
80 bc().setApply(applyPB);
81 bc().setCancel(closePB);
82 bc().addReadOnly(refsLW);
83 bc().addReadOnly(sortCB);
84 bc().addReadOnly(nameED);
85 bc().addReadOnly(referenceED);
86 bc().addReadOnly(typeCO);
87 bc().addReadOnly(bufferCO);
89 restored_buffer_ = -1;
93 void GuiRef::changed_adaptor()
99 void GuiRef::gotoClicked()
105 void GuiRef::selectionChanged()
107 if (isBufferReadonly())
110 QList<QListWidgetItem *> selections = refsLW->selectedItems();
111 if (selections.isEmpty())
113 QListWidgetItem * sel = selections.first();
119 void GuiRef::refHighlighted(QListWidgetItem * sel)
121 if (isBufferReadonly())
124 /* int const cur_item = refsLW->currentRow();
125 bool const cur_item_selected = cur_item >= 0 ?
126 refsLB->isSelected(cur_item) : false;*/
127 bool const cur_item_selected = refsLW->isItemSelected(sel);
129 if (cur_item_selected)
130 referenceED->setText(sel->text());
134 gotoPB->setEnabled(true);
136 typeCO->setEnabled(true);
138 nameED->setEnabled(true);
142 void GuiRef::refSelected(QListWidgetItem * sel)
144 if (isBufferReadonly())
147 /* int const cur_item = refsLW->currentRow();
148 bool const cur_item_selected = cur_item >= 0 ?
149 refsLB->isSelected(cur_item) : false;*/
150 bool const cur_item_selected = refsLW->isItemSelected(sel);
152 if (cur_item_selected)
153 referenceED->setText(sel->text());
154 // <enter> or double click, inserts ref and closes dialog
159 void GuiRef::sortToggled(bool on)
166 void GuiRef::updateClicked()
172 void GuiRef::reset_dialog()
179 void GuiRef::closeEvent(QCloseEvent * e)
183 GuiDialog::closeEvent(e);
187 void GuiRef::updateContents()
189 int orig_type = typeCO->currentIndex();
191 referenceED->setText(toqstr(params_["reference"]));
193 nameED->setText(toqstr(params_["name"]));
194 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
196 // restore type settings for new insets
197 if (params_["reference"].empty())
198 typeCO->setCurrentIndex(orig_type);
200 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
201 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
203 typeCO->setCurrentIndex(0);
205 sortCB->setChecked(sort_);
207 // insert buffer list
209 vector<string> buffers = theBufferList().getFileNames();
210 for (vector<string>::iterator it = buffers.begin();
211 it != buffers.end(); ++it) {
212 bufferCO->addItem(toqstr(lyx::to_utf8(makeDisplayPath(*it))));
215 // restore the buffer combo setting for new insets
216 if (params_["reference"].empty() && restored_buffer_ != -1
217 && restored_buffer_ < bufferCO->count())
218 bufferCO->setCurrentIndex(restored_buffer_);
220 int num = theBufferList().bufferNum(buffer().absFileName());
221 bufferCO->setCurrentIndex(num);
225 bc().setValid(false);
229 void GuiRef::applyView()
231 last_reference_ = referenceED->text();
233 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
234 params_["reference"] = qstring_to_ucs4(last_reference_);
235 params_["name"] = qstring_to_ucs4(nameED->text());
237 restored_buffer_ = bufferCO->currentIndex();
241 bool GuiRef::nameAllowed()
243 KernelDocType const doc_type = docType();
244 return doc_type != LATEX && doc_type != LITERATE;
248 bool GuiRef::typeAllowed()
250 return docType() != DOCBOOK;
254 void GuiRef::setGoBack()
256 gotoPB->setText(qt_("&Go Back"));
257 gotoPB->setToolTip("");
258 gotoPB->setToolTip(qt_("Jump back"));
262 void GuiRef::setGotoRef()
264 gotoPB->setText(qt_("&Go to Label"));
265 gotoPB->setToolTip("");
266 gotoPB->setToolTip(qt_("Jump to label"));
270 void GuiRef::gotoRef()
272 string ref = fromqstr(referenceED->text());
287 void GuiRef::redoRefs()
289 // Prevent these widgets from emitting any signals whilst
290 // we modify their state.
291 refsLW->blockSignals(true);
292 referenceED->blockSignals(true);
293 refsLW->setUpdatesEnabled(false);
297 // need this because Qt will send a highlight() here for
298 // the first item inserted
299 QString const oldSelection(referenceED->text());
301 for (vector<docstring>::const_iterator iter = refs_.begin();
302 iter != refs_.end(); ++iter) {
303 refsLW->addItem(toqstr(*iter));
309 referenceED->setText(oldSelection);
311 // restore the last selection or, for new insets, highlight
312 // the previous selection
313 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
314 bool const newInset = oldSelection.isEmpty();
315 QString textToFind = newInset ? last_reference_ : oldSelection;
316 last_reference_.clear();
317 for (int i = 0; i != refsLW->count(); ++i) {
318 QListWidgetItem * item = refsLW->item(i);
319 if (textToFind == item->text()) {
320 refsLW->setCurrentItem(item);
321 refsLW->setItemSelected(item, !newInset);
322 //Make sure selected item is visible
323 refsLW->scrollToItem(item);
324 last_reference_ = textToFind;
329 refsLW->setUpdatesEnabled(true);
332 // Re-activate the emission of signals by these widgets.
333 refsLW->blockSignals(false);
334 referenceED->blockSignals(false);
338 void GuiRef::updateRefs()
341 string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
342 Buffer const * buf = theBufferList().getBuffer(makeAbsPath(name).absFilename());
343 buf->getLabelList(refs_);
344 sortCB->setEnabled(!refs_.empty());
345 refsLW->setEnabled(!refs_.empty());
346 gotoPB->setEnabled(!refs_.empty());
351 bool GuiRef::isValid()
353 return !referenceED->text().isEmpty();
357 void GuiRef::gotoRef(string const & ref)
359 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
360 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
364 void GuiRef::gotoBookmark()
366 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
370 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
373 } // namespace frontend
376 #include "GuiRef_moc.cpp"