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/filetools.h" // MakeAbsPath, MakeDisplayPath
26 #include <boost/filesystem/operations.hpp>
30 #include <QListWidget>
31 #include <QListWidgetItem>
32 #include <QPushButton>
34 #include <QCloseEvent>
42 using support::makeAbsPath;
43 using support::makeDisplayPath;
45 GuiRef::GuiRef(GuiView & lv)
46 : GuiCommand(lv, "ref")
49 setViewTitle(_("Cross-reference"));
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(reset_dialog()));
58 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
60 connect(typeCO, SIGNAL(activated(int)),
61 this, SLOT(changed_adaptor()));
62 connect(referenceED, SIGNAL(textChanged(QString)),
63 this, SLOT(changed_adaptor()));
64 connect(nameED, SIGNAL(textChanged(QString)),
65 this, SLOT(changed_adaptor()));
66 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
67 this, SLOT(refHighlighted(QListWidgetItem *)));
68 connect(refsLW, SIGNAL(itemSelectionChanged()),
69 this, SLOT(selectionChanged()));
70 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
71 this, SLOT(refSelected(QListWidgetItem *)));
72 connect(sortCB, SIGNAL(clicked(bool)),
73 this, SLOT(sortToggled(bool)));
74 connect(gotoPB, SIGNAL(clicked()),
75 this, SLOT(gotoClicked()));
76 connect(updatePB, SIGNAL(clicked()),
77 this, SLOT(updateClicked()));
78 connect(bufferCO, SIGNAL(activated(int)),
79 this, SLOT(updateClicked()));
81 setFocusProxy(refsLW);
83 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
85 bc().setApply(applyPB);
86 bc().setCancel(closePB);
87 bc().addReadOnly(refsLW);
88 bc().addReadOnly(sortCB);
89 bc().addReadOnly(nameED);
90 bc().addReadOnly(referenceED);
91 bc().addReadOnly(typeCO);
92 bc().addReadOnly(bufferCO);
94 restored_buffer_ = -1;
98 void GuiRef::changed_adaptor()
104 void GuiRef::gotoClicked()
110 void GuiRef::selectionChanged()
112 if (isBufferReadonly())
115 QList<QListWidgetItem *> selections = refsLW->selectedItems();
116 if (selections.isEmpty())
118 QListWidgetItem * sel = selections.first();
124 void GuiRef::refHighlighted(QListWidgetItem * sel)
126 if (isBufferReadonly())
129 /* int const cur_item = refsLW->currentRow();
130 bool const cur_item_selected = cur_item >= 0 ?
131 refsLB->isSelected(cur_item) : false;*/
132 bool const cur_item_selected = refsLW->isItemSelected(sel);
134 if (cur_item_selected)
135 referenceED->setText(sel->text());
139 gotoPB->setEnabled(true);
141 typeCO->setEnabled(true);
143 nameED->setEnabled(true);
147 void GuiRef::refSelected(QListWidgetItem * sel)
149 if (isBufferReadonly())
152 /* int const cur_item = refsLW->currentRow();
153 bool const cur_item_selected = cur_item >= 0 ?
154 refsLB->isSelected(cur_item) : false;*/
155 bool const cur_item_selected = refsLW->isItemSelected(sel);
157 if (cur_item_selected)
158 referenceED->setText(sel->text());
159 // <enter> or double click, inserts ref and closes dialog
164 void GuiRef::sortToggled(bool on)
171 void GuiRef::updateClicked()
177 void GuiRef::reset_dialog()
184 void GuiRef::closeEvent(QCloseEvent * e)
188 GuiDialog::closeEvent(e);
192 void GuiRef::updateContents()
194 int orig_type = typeCO->currentIndex();
196 referenceED->setText(toqstr(params_["reference"]));
198 nameED->setText(toqstr(params_["name"]));
199 nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
201 // restore type settings for new insets
202 if (params_["reference"].empty())
203 typeCO->setCurrentIndex(orig_type);
205 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
206 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
208 typeCO->setCurrentIndex(0);
210 sortCB->setChecked(sort_);
212 // insert buffer list
214 vector<string> buffers = theBufferList().getFileNames();
215 for (vector<string>::iterator it = buffers.begin();
216 it != buffers.end(); ++it) {
217 bufferCO->addItem(toqstr(lyx::to_utf8(makeDisplayPath(*it))));
220 // restore the buffer combo setting for new insets
221 if (params_["reference"].empty() && restored_buffer_ != -1
222 && restored_buffer_ < bufferCO->count())
223 bufferCO->setCurrentIndex(restored_buffer_);
225 int num = theBufferList().bufferNum(buffer().absFileName());
226 bufferCO->setCurrentIndex(num);
230 bc().setValid(false);
234 void GuiRef::applyView()
236 last_reference_ = referenceED->text();
238 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
239 params_["reference"] = qstring_to_ucs4(last_reference_);
240 params_["name"] = qstring_to_ucs4(nameED->text());
242 restored_buffer_ = bufferCO->currentIndex();
246 bool GuiRef::nameAllowed()
248 KernelDocType const doc_type = docType();
249 return doc_type != LATEX && doc_type != LITERATE;
253 bool GuiRef::typeAllowed()
255 return docType() != DOCBOOK;
259 void GuiRef::setGoBack()
261 gotoPB->setText(qt_("&Go Back"));
262 gotoPB->setToolTip("");
263 gotoPB->setToolTip(qt_("Jump back"));
267 void GuiRef::setGotoRef()
269 gotoPB->setText(qt_("&Go to Label"));
270 gotoPB->setToolTip("");
271 gotoPB->setToolTip(qt_("Jump to label"));
275 void GuiRef::gotoRef()
277 string ref = fromqstr(referenceED->text());
292 void GuiRef::redoRefs()
294 // Prevent these widgets from emitting any signals whilst
295 // we modify their state.
296 refsLW->blockSignals(true);
297 referenceED->blockSignals(true);
298 refsLW->setUpdatesEnabled(false);
302 // need this because Qt will send a highlight() here for
303 // the first item inserted
304 QString const oldSelection(referenceED->text());
306 for (std::vector<docstring>::const_iterator iter = refs_.begin();
307 iter != refs_.end(); ++iter) {
308 refsLW->addItem(toqstr(*iter));
314 referenceED->setText(oldSelection);
316 // restore the last selection or, for new insets, highlight
317 // the previous selection
318 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
319 bool const newInset = oldSelection.isEmpty();
320 QString textToFind = newInset ? last_reference_ : oldSelection;
321 last_reference_.clear();
322 for (int i = 0; i != refsLW->count(); ++i) {
323 QListWidgetItem * item = refsLW->item(i);
324 if (textToFind == item->text()) {
325 refsLW->setCurrentItem(item);
326 refsLW->setItemSelected(item, !newInset);
327 //Make sure selected item is visible
328 refsLW->scrollToItem(item);
329 last_reference_ = textToFind;
334 refsLW->setUpdatesEnabled(true);
337 // Re-activate the emission of signals by these widgets.
338 refsLW->blockSignals(false);
339 referenceED->blockSignals(false);
343 void GuiRef::updateRefs()
346 string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
347 Buffer const * buf = theBufferList().getBuffer(makeAbsPath(name).absFilename());
348 buf->getLabelList(refs_);
349 sortCB->setEnabled(!refs_.empty());
350 refsLW->setEnabled(!refs_.empty());
351 gotoPB->setEnabled(!refs_.empty());
356 bool GuiRef::isValid()
358 return !referenceED->text().isEmpty();
362 void GuiRef::gotoRef(string const & ref)
364 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
365 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
369 void GuiRef::gotoBookmark()
371 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
375 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
378 } // namespace frontend
381 #include "GuiRef_moc.cpp"