]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiRef.cpp
93f8193fc2d8546e9607e1a7aad98ec33c26bb2c
[lyx.git] / src / frontends / qt4 / GuiRef.cpp
1 /**
2  * \file GuiRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiRef.h"
15
16 #include "Buffer.h"
17 #include "BufferList.h"
18 #include "FuncRequest.h"
19
20 #include "qt_helpers.h"
21
22 #include "insets/InsetRef.h"
23
24 #include "support/FileName.h"
25 #include "support/FileNameList.h"
26 #include "support/filetools.h" // makeAbsPath, makeDisplayPath
27
28 #include <QLineEdit>
29 #include <QCheckBox>
30 #include <QTreeWidget>
31 #include <QTreeWidgetItem>
32 #include <QPushButton>
33 #include <QToolTip>
34 #include <QCloseEvent>
35
36 using namespace std;
37 using namespace lyx::support;
38
39 namespace lyx {
40 namespace frontend {
41
42 GuiRef::GuiRef(GuiView & lv)
43         : GuiDialog(lv, "ref", qt_("Cross-reference")),
44           params_(insetCode("ref"))
45 {
46         setupUi(this);
47
48         at_ref_ = false;
49
50         // Enabling is set in updateRefs. Disable for now in case no
51         // call to updateContents follows (e.g. read-only documents).
52         sortCB->setEnabled(false);
53         caseSensitiveCB->setEnabled(false);
54         caseSensitiveCB->setChecked(false);
55         refsTW->setEnabled(false);
56         gotoPB->setEnabled(false);
57
58         refsTW->setColumnCount(1);
59         refsTW->header()->setVisible(false);
60
61         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
62         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
63         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
64         connect(closePB, SIGNAL(clicked()), this, SLOT(resetDialog()));
65         connect(this, SIGNAL(rejected()), this, SLOT(dialogRejected()));
66
67         connect(typeCO, SIGNAL(activated(int)),
68                 this, SLOT(changed_adaptor()));
69         connect(referenceED, SIGNAL(textChanged(QString)),
70                 this, SLOT(changed_adaptor()));
71         connect(findLE, SIGNAL(returnPressed()), 
72                 this, SLOT(on_searchPB_clicked()));
73         connect(csFindCB, SIGNAL(clicked()), 
74                 this, SLOT(on_searchPB_clicked()));
75         connect(nameED, SIGNAL(textChanged(QString)),
76                 this, SLOT(changed_adaptor()));
77         connect(refsTW, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
78                 this, SLOT(refHighlighted(QTreeWidgetItem *)));
79         connect(refsTW, SIGNAL(itemSelectionChanged()),
80                 this, SLOT(selectionChanged()));
81         connect(refsTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
82                 this, SLOT(refSelected(QTreeWidgetItem *)));
83         connect(sortCB, SIGNAL(clicked()),
84                 this, SLOT(sortToggled()));
85         connect(caseSensitiveCB, SIGNAL(clicked()),
86                 this, SLOT(caseSensitiveToggled()));
87         connect(groupCB, SIGNAL(clicked()),
88                 this, SLOT(groupToggled()));
89         connect(gotoPB, SIGNAL(clicked()),
90                 this, SLOT(gotoClicked()));
91         connect(updatePB, SIGNAL(clicked()),
92                 this, SLOT(updateClicked()));
93         connect(bufferCO, SIGNAL(activated(int)),
94                 this, SLOT(updateClicked()));
95
96         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
97         bc().setOK(okPB);
98         bc().setApply(applyPB);
99         bc().setCancel(closePB);
100         bc().addReadOnly(refsTW);
101         bc().addReadOnly(sortCB);
102         bc().addReadOnly(caseSensitiveCB);
103         bc().addReadOnly(nameED);
104         bc().addReadOnly(referenceED);
105         bc().addReadOnly(typeCO);
106         bc().addReadOnly(bufferCO);
107
108         restored_buffer_ = -1;
109         active_buffer_ = -1;
110 }
111
112
113 void GuiRef::changed_adaptor()
114 {
115         changed();
116 }
117
118
119 void GuiRef::gotoClicked()
120 {
121         gotoRef();
122 }
123
124
125 void GuiRef::selectionChanged()
126 {
127         if (isBufferReadonly())
128                 return;
129
130         QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
131         if (selections.isEmpty())
132                 return;
133         QTreeWidgetItem * sel = selections.first();
134         refHighlighted(sel);
135         return;
136 }
137
138
139 void GuiRef::refHighlighted(QTreeWidgetItem * sel)
140 {
141         if (isBufferReadonly())
142                 return;
143
144         if (sel->childCount() > 0) {
145                 sel->setExpanded(true);
146                 return;
147         }
148
149 /*      int const cur_item = refsTW->currentRow();
150         bool const cur_item_selected = cur_item >= 0 ?
151                 refsLB->isSelected(cur_item) : false;*/
152         bool const cur_item_selected = refsTW->isItemSelected(sel);
153
154         if (cur_item_selected)
155                 referenceED->setText(sel->text(0));
156
157         if (at_ref_)
158                 gotoRef();
159         gotoPB->setEnabled(true);
160         if (typeAllowed())
161                 typeCO->setEnabled(true);
162         nameED->setHidden(!nameAllowed());
163         nameL->setHidden(!nameAllowed());
164 }
165
166
167 void GuiRef::refSelected(QTreeWidgetItem * sel)
168 {
169         if (isBufferReadonly())
170                 return;
171
172 /*      int const cur_item = refsTW->currentRow();
173         bool const cur_item_selected = cur_item >= 0 ?
174                 refsLB->isSelected(cur_item) : false;*/
175         bool const cur_item_selected = refsTW->isItemSelected(sel);
176
177         if (cur_item_selected)
178                 referenceED->setText(sel->text(0));
179         // <enter> or double click, inserts ref and closes dialog
180         slotOK();
181 }
182
183
184 void GuiRef::sortToggled()
185 {
186         caseSensitiveCB->setEnabled(sortCB->isChecked());
187         redoRefs();
188 }
189
190
191 void GuiRef::caseSensitiveToggled()
192 {
193         redoRefs();
194 }
195
196
197 void GuiRef::groupToggled()
198 {
199         redoRefs();
200 }
201
202
203 void GuiRef::updateClicked()
204 {
205         findLE->clear();
206         updateRefs();
207 }
208
209
210 void GuiRef::dialogRejected()
211 {
212         resetDialog();
213         // We have to do this manually, instead of calling slotClose(), because
214         // the dialog has already been made invisible before rejected() triggers.
215         Dialog::disconnect();
216 }
217
218
219 void GuiRef::resetDialog()
220 {
221         at_ref_ = false;
222         setGotoRef();
223 }
224
225
226 void GuiRef::closeEvent(QCloseEvent * e)
227 {
228         slotClose();
229         resetDialog();
230         e->accept();
231 }
232
233
234 void GuiRef::updateContents()
235 {
236         int orig_type = typeCO->currentIndex();
237
238         referenceED->setText(toqstr(params_["reference"]));
239
240         nameED->setText(toqstr(params_["name"]));
241         nameED->setHidden(!nameAllowed() && !isBufferReadonly());
242         nameL->setHidden(!nameAllowed() && !isBufferReadonly());
243
244         // restore type settings for new insets
245         if (params_["reference"].empty())
246                 typeCO->setCurrentIndex(orig_type);
247         else
248                 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
249         typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
250         if (!typeAllowed())
251                 typeCO->setCurrentIndex(0);
252
253         // insert buffer list
254         bufferCO->clear();
255         FileNameList const & buffers = theBufferList().fileNames();
256         for (FileNameList::const_iterator it = buffers.begin();
257              it != buffers.end(); ++it) {
258                 bufferCO->addItem(toqstr(makeDisplayPath(it->absFilename())));
259         }
260
261         int const thebuffer = theBufferList().bufferNum(buffer().fileName());
262         // restore the buffer combo setting for new insets
263         if (params_["reference"].empty() && restored_buffer_ != -1
264             && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
265                 bufferCO->setCurrentIndex(restored_buffer_);
266         else {
267                 int const num = theBufferList().bufferNum(buffer().fileName());
268                 bufferCO->setCurrentIndex(num);
269                 if (thebuffer != active_buffer_)
270                         restored_buffer_ = num;
271         }
272         active_buffer_ = thebuffer;
273
274         updateRefs();
275         bc().setValid(false);
276 }
277
278
279 void GuiRef::applyView()
280 {
281         last_reference_ = referenceED->text();
282
283         params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
284         params_["reference"] = qstring_to_ucs4(last_reference_);
285         params_["name"] = qstring_to_ucs4(nameED->text());
286
287         restored_buffer_ = bufferCO->currentIndex();
288 }
289
290
291 bool GuiRef::nameAllowed()
292 {
293         KernelDocType const doc_type = docType();
294         return doc_type != LATEX && doc_type != LITERATE;
295 }
296
297
298 bool GuiRef::typeAllowed()
299 {
300         return docType() != DOCBOOK;
301 }
302
303
304 void GuiRef::setGoBack()
305 {
306         gotoPB->setText(qt_("&Go Back"));
307         gotoPB->setToolTip("");
308         gotoPB->setToolTip(qt_("Jump back"));
309 }
310
311
312 void GuiRef::setGotoRef()
313 {
314         gotoPB->setText(qt_("&Go to Label"));
315         gotoPB->setToolTip("");
316         gotoPB->setToolTip(qt_("Jump to label"));
317 }
318
319
320 void GuiRef::gotoRef()
321 {
322         string ref = fromqstr(referenceED->text());
323
324         if (at_ref_) {
325                 // go back
326                 setGotoRef();
327                 gotoBookmark();
328         } else {
329                 // go to the ref
330                 setGoBack();
331                 gotoRef(ref);
332         }
333         at_ref_ = !at_ref_;
334 }
335
336 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
337 {
338         return s1.toLower() < s2.toLower();
339 }
340
341
342 void GuiRef::redoRefs()
343 {
344         // Prevent these widgets from emitting any signals whilst
345         // we modify their state.
346         refsTW->blockSignals(true);
347         referenceED->blockSignals(true);
348         refsTW->setUpdatesEnabled(false);
349
350         refsTW->clear();
351
352         // need this because Qt will send a highlight() here for
353         // the first item inserted
354         QString const oldSelection(referenceED->text());
355
356         QStringList refsStrings;
357         QStringList refsCategories;
358         refsCategories.append(qt_("No prefix"));
359         vector<docstring>::const_iterator iter;
360         for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
361                 QString const lab = toqstr(*iter);
362                 refsStrings.append(lab);
363                 if (groupCB->isChecked() && lab.contains(":")) {
364                         QString const pref = lab.split(':')[0];
365                         if (!pref.isEmpty() && !refsCategories.contains(pref))
366                                   refsCategories.append(pref);
367                 }
368         }
369
370         if (sortCB->isEnabled() && sortCB->isChecked()) {
371                 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
372                         qSort(refsStrings.begin(), refsStrings.end());
373                 else
374                         qSort(refsStrings.begin(), refsStrings.end(),
375                               caseInsensitiveLessThan /*defined above*/);
376         }
377         
378         if (groupCB->isChecked()) {
379                 QList<QTreeWidgetItem *> refsCats;
380                 for (int i = 0; i < refsCategories.size(); ++i) {
381                         QString const cat = refsCategories.at(i);
382                         QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
383                         item->setText(0, cat);
384                         for (int i = 0; i < refsStrings.size(); ++i) {
385                                 QString const ref = refsStrings.at(i);
386                                 if ((ref.startsWith(cat + QString(":")))
387                                     || (cat == qt_("No prefix")
388                                         && !ref.contains(":"))) {
389                                         QTreeWidgetItem * child =
390                                                 new QTreeWidgetItem(item);
391                                         child->setText(0, ref);
392                                         item->addChild(child);
393                                 }
394                         }
395                         refsCats.append(item);
396                 }
397                 refsTW->addTopLevelItems(refsCats);
398         } else {
399                 QList<QTreeWidgetItem *> refsItems;
400                 for (int i = 0; i < refsStrings.size(); ++i) {
401                         QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
402                         item->setText(0, refsStrings.at(i));
403                         refsItems.append(item);
404                 }
405                 refsTW->addTopLevelItems(refsItems);
406         }
407
408         referenceED->setText(oldSelection);
409
410         // restore the last selection or, for new insets, highlight
411         // the previous selection
412         if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
413                 bool const newInset = oldSelection.isEmpty();
414                 QString textToFind = newInset ? last_reference_ : oldSelection;
415                 last_reference_.clear();
416                 QTreeWidgetItemIterator it(refsTW);
417                 while (*it) {
418                         if ((*it)->text(0) == textToFind) {
419                                 refsTW->setCurrentItem(*it);
420                                 refsTW->setItemSelected(*it, !newInset);
421                                 //Make sure selected item is visible
422                                 refsTW->scrollToItem(*it);
423                                 last_reference_ = textToFind;
424                                 break;
425                         }
426                         ++it;
427                 }
428         }
429         refsTW->setUpdatesEnabled(true);
430         refsTW->update();
431
432         // Re-activate the emission of signals by these widgets.
433         refsTW->blockSignals(false);
434         referenceED->blockSignals(false);
435 }
436
437
438 void GuiRef::updateRefs()
439 {
440         refs_.clear();
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_);
446         }       
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());
453         redoRefs();
454 }
455
456
457 bool GuiRef::isValid()
458 {
459         return !referenceED->text().isEmpty();
460 }
461
462
463 void GuiRef::gotoRef(string const & ref)
464 {
465         dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
466         dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
467 }
468
469
470 void GuiRef::gotoBookmark()
471 {
472         dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
473 }
474
475
476 void GuiRef::on_findLE_textChanged(const QString & text)
477 {
478         searchPB->setDisabled(text.isEmpty());
479 }
480
481
482 void GuiRef::on_searchPB_clicked()
483 {
484         QTreeWidgetItemIterator it(refsTW);
485         Qt::CaseSensitivity cs = csFindCB->isChecked() ?
486                 Qt::CaseSensitive : Qt::CaseInsensitive;
487         while (*it) {
488                 (*it)->setHidden(
489                         (*it)->childCount() == 0
490                         && !(*it)->text(0).contains(findLE->text(), cs)
491                 );
492                 ++it;
493         }
494 }
495
496
497 bool GuiRef::initialiseParams(std::string const & data)
498 {
499         InsetCommand::string2params("ref", data, params_);
500         return true;
501 }
502
503
504 void GuiRef::dispatchParams()
505 {
506         std::string const lfun = InsetCommand::params2string("ref", params_);
507         dispatch(FuncRequest(getLfun(), lfun));
508 }
509
510
511
512 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
513
514
515 } // namespace frontend
516 } // namespace lyx
517
518 #include "moc_GuiRef.cpp"