]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiRef.cpp
Added "cancel" to the GUI handled list of LaTeX packages.
[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 #include <QHeaderView>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41 namespace frontend {
42
43 GuiRef::GuiRef(GuiView & lv)
44         : GuiDialog(lv, "ref", qt_("Cross-reference")),
45           params_(insetCode("ref"))
46 {
47         setupUi(this);
48
49         at_ref_ = false;
50
51         refsTW->setColumnCount(1);
52         refsTW->header()->setVisible(false);
53
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()));
59
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()));
88
89         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
90         bc().setOK(okPB);
91         bc().setApply(applyPB);
92         bc().setCancel(closePB);
93         bc().addReadOnly(typeCO);
94
95         restored_buffer_ = -1;
96         active_buffer_ = -1;
97 }
98
99
100 void GuiRef::enableView(bool enable)
101 {
102         if (!enable)
103                 // In the opposite case, updateContents() will be called anyway.
104                 updateContents();
105         GuiDialog::enableView(enable);
106 }
107
108
109 void GuiRef::changed_adaptor()
110 {
111         changed();
112 }
113
114
115 void GuiRef::gotoClicked()
116 {
117         gotoRef();
118 }
119
120
121 void GuiRef::selectionChanged()
122 {
123         if (isBufferReadonly())
124                 return;
125
126         QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
127         if (selections.isEmpty())
128                 return;
129         QTreeWidgetItem * sel = selections.first();
130         refHighlighted(sel);
131         return;
132 }
133
134
135 void GuiRef::refHighlighted(QTreeWidgetItem * sel)
136 {
137         if (sel->childCount() > 0) {
138                 sel->setExpanded(true);
139                 return;
140         }
141
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);
146
147         if (cur_item_selected)
148                 referenceED->setText(sel->text(0));
149
150         if (at_ref_)
151                 gotoRef();
152         gotoPB->setEnabled(true);
153         if (typeAllowed() && !isBufferReadonly())
154                 typeCO->setEnabled(true);
155         nameED->setHidden(!nameAllowed());
156         nameL->setHidden(!nameAllowed());
157 }
158
159
160 void GuiRef::refSelected(QTreeWidgetItem * sel)
161 {
162         if (isBufferReadonly())
163                 return;
164
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);
169
170         if (cur_item_selected)
171                 referenceED->setText(sel->text(0));
172         // <enter> or double click, inserts ref and closes dialog
173         slotOK();
174 }
175
176
177 void GuiRef::sortToggled()
178 {
179         caseSensitiveCB->setEnabled(sortCB->isChecked());
180         redoRefs();
181 }
182
183
184 void GuiRef::caseSensitiveToggled()
185 {
186         redoRefs();
187 }
188
189
190 void GuiRef::groupToggled()
191 {
192         redoRefs();
193 }
194
195
196 void GuiRef::updateClicked()
197 {
198         updateRefs();
199 }
200
201
202 void GuiRef::dialogRejected()
203 {
204         resetDialog();
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();
208 }
209
210
211 void GuiRef::resetDialog()
212 {
213         at_ref_ = false;
214         setGotoRef();
215 }
216
217
218 void GuiRef::closeEvent(QCloseEvent * e)
219 {
220         slotClose();
221         resetDialog();
222         e->accept();
223 }
224
225
226 void GuiRef::updateContents()
227 {
228         int orig_type = typeCO->currentIndex();
229
230         referenceED->clear();
231         nameED->clear();
232
233         referenceED->setText(toqstr(params_["reference"]));
234         nameED->setText(toqstr(params_["name"]));
235         nameED->setHidden(!nameAllowed());
236         nameL->setHidden(!nameAllowed());
237
238         // restore type settings for new insets
239         if (params_["reference"].empty())
240                 typeCO->setCurrentIndex(orig_type);
241         else
242                 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
243         typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
244         if (!typeAllowed())
245                 typeCO->setCurrentIndex(0);
246
247         // insert buffer list
248         bufferCO->clear();
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())));
253         }
254
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_);
260         else {
261                 int const num = theBufferList().bufferNum(buffer().fileName());
262                 bufferCO->setCurrentIndex(num);
263                 if (thebuffer != active_buffer_)
264                         restored_buffer_ = num;
265         }
266         active_buffer_ = thebuffer;
267
268         updateRefs();
269         bc().setValid(false);
270 }
271
272
273 void GuiRef::applyView()
274 {
275         last_reference_ = referenceED->text();
276
277         params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
278         params_["reference"] = qstring_to_ucs4(last_reference_);
279         params_["name"] = qstring_to_ucs4(nameED->text());
280
281         restored_buffer_ = bufferCO->currentIndex();
282 }
283
284
285 bool GuiRef::nameAllowed()
286 {
287         KernelDocType const doc_type = docType();
288         return doc_type != LATEX && doc_type != LITERATE;
289 }
290
291
292 bool GuiRef::typeAllowed()
293 {
294         return docType() != DOCBOOK;
295 }
296
297
298 void GuiRef::setGoBack()
299 {
300         gotoPB->setText(qt_("&Go Back"));
301         gotoPB->setToolTip("");
302         gotoPB->setToolTip(qt_("Jump back"));
303 }
304
305
306 void GuiRef::setGotoRef()
307 {
308         gotoPB->setText(qt_("&Go to Label"));
309         gotoPB->setToolTip("");
310         gotoPB->setToolTip(qt_("Jump to label"));
311 }
312
313
314 void GuiRef::gotoRef()
315 {
316         string ref = fromqstr(referenceED->text());
317
318         if (at_ref_) {
319                 // go back
320                 setGotoRef();
321                 gotoBookmark();
322         } else {
323                 // go to the ref
324                 setGoBack();
325                 gotoRef(ref);
326         }
327         at_ref_ = !at_ref_;
328 }
329
330 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
331 {
332         return s1.toLower() < s2.toLower();
333 }
334
335
336 void GuiRef::redoRefs()
337 {
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);
343
344         refsTW->clear();
345
346         // need this because Qt will send a highlight() here for
347         // the first item inserted
348         QString const oldSelection(referenceED->text());
349
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);
360                 }
361         }
362         // sort categories case-intensively
363         qSort(refsCategories.begin(), refsCategories.end(),
364               caseInsensitiveLessThan /*defined above*/);
365         refsCategories.insert(0, qt_("<No prefix>"));
366
367         if (sortCB->isEnabled() && sortCB->isChecked()) {
368                 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
369                         qSort(refsStrings.begin(), refsStrings.end());
370                 else
371                         qSort(refsStrings.begin(), refsStrings.end(),
372                               caseInsensitiveLessThan /*defined above*/);
373         }
374         
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);
390                                 }
391                         }
392                         refsCats.append(item);
393                 }
394                 refsTW->addTopLevelItems(refsCats);
395         } else {
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);
401                 }
402                 refsTW->addTopLevelItems(refsItems);
403         }
404
405         referenceED->setText(oldSelection);
406
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);
414                 while (*it) {
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;
421                                 break;
422                         }
423                         ++it;
424                 }
425         }
426         refsTW->setUpdatesEnabled(true);
427         refsTW->update();
428
429         // redo filter
430         filterLabels();
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::filterLabels()
477 {
478         Qt::CaseSensitivity cs = csFindCB->isChecked() ?
479                 Qt::CaseSensitive : Qt::CaseInsensitive;
480         QTreeWidgetItemIterator it(refsTW);
481         while (*it) {
482                 (*it)->setHidden(
483                         (*it)->childCount() == 0
484                         && !(*it)->text(0).contains(findLE->text(), cs)
485                 );
486                 ++it;
487         }
488 }
489
490
491 bool GuiRef::initialiseParams(std::string const & data)
492 {
493         InsetCommand::string2params(data, params_);
494         return true;
495 }
496
497
498 void GuiRef::dispatchParams()
499 {
500         std::string const lfun = InsetCommand::params2string(params_);
501         dispatch(FuncRequest(getLfun(), lfun));
502 }
503
504
505
506 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
507
508
509 } // namespace frontend
510 } // namespace lyx
511
512 #include "moc_GuiRef.cpp"