]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiRef.cpp
Fix the tab ordering of PanelStack and PrefsUi.
[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         // Enabling is set in updateRefs. Disable for now in case no
52         // call to updateContents follows (e.g. read-only documents).
53         sortCB->setEnabled(false);
54         caseSensitiveCB->setEnabled(false);
55         caseSensitiveCB->setChecked(false);
56         refsTW->setEnabled(false);
57         gotoPB->setEnabled(false);
58
59         refsTW->setColumnCount(1);
60         refsTW->header()->setVisible(false);
61
62         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
63         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
64         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
65         connect(closePB, SIGNAL(clicked()), this, SLOT(resetDialog()));
66         connect(this, SIGNAL(rejected()), this, SLOT(dialogRejected()));
67
68         connect(typeCO, SIGNAL(activated(int)),
69                 this, SLOT(changed_adaptor()));
70         connect(referenceED, SIGNAL(textChanged(QString)),
71                 this, SLOT(changed_adaptor()));
72         connect(findLE, SIGNAL(textEdited(QString)), 
73                 this, SLOT(filterLabels()));
74         connect(csFindCB, SIGNAL(clicked()), 
75                 this, SLOT(filterLabels()));
76         connect(nameED, SIGNAL(textChanged(QString)),
77                 this, SLOT(changed_adaptor()));
78         connect(refsTW, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
79                 this, SLOT(refHighlighted(QTreeWidgetItem *)));
80         connect(refsTW, SIGNAL(itemSelectionChanged()),
81                 this, SLOT(selectionChanged()));
82         connect(refsTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
83                 this, SLOT(refSelected(QTreeWidgetItem *)));
84         connect(sortCB, SIGNAL(clicked()),
85                 this, SLOT(sortToggled()));
86         connect(caseSensitiveCB, SIGNAL(clicked()),
87                 this, SLOT(caseSensitiveToggled()));
88         connect(groupCB, SIGNAL(clicked()),
89                 this, SLOT(groupToggled()));
90         connect(gotoPB, SIGNAL(clicked()),
91                 this, SLOT(gotoClicked()));
92         connect(updatePB, SIGNAL(clicked()),
93                 this, SLOT(updateClicked()));
94         connect(bufferCO, SIGNAL(activated(int)),
95                 this, SLOT(updateClicked()));
96
97         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
98         bc().setOK(okPB);
99         bc().setApply(applyPB);
100         bc().setCancel(closePB);
101         bc().addReadOnly(refsTW);
102         bc().addReadOnly(sortCB);
103         bc().addReadOnly(caseSensitiveCB);
104         bc().addReadOnly(nameED);
105         bc().addReadOnly(referenceED);
106         bc().addReadOnly(typeCO);
107         bc().addReadOnly(bufferCO);
108
109         restored_buffer_ = -1;
110         active_buffer_ = -1;
111 }
112
113
114 void GuiRef::changed_adaptor()
115 {
116         changed();
117 }
118
119
120 void GuiRef::gotoClicked()
121 {
122         gotoRef();
123 }
124
125
126 void GuiRef::selectionChanged()
127 {
128         if (isBufferReadonly())
129                 return;
130
131         QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
132         if (selections.isEmpty())
133                 return;
134         QTreeWidgetItem * sel = selections.first();
135         refHighlighted(sel);
136         return;
137 }
138
139
140 void GuiRef::refHighlighted(QTreeWidgetItem * sel)
141 {
142         if (isBufferReadonly())
143                 return;
144
145         if (sel->childCount() > 0) {
146                 sel->setExpanded(true);
147                 return;
148         }
149
150 /*      int const cur_item = refsTW->currentRow();
151         bool const cur_item_selected = cur_item >= 0 ?
152                 refsLB->isSelected(cur_item) : false;*/
153         bool const cur_item_selected = refsTW->isItemSelected(sel);
154
155         if (cur_item_selected)
156                 referenceED->setText(sel->text(0));
157
158         if (at_ref_)
159                 gotoRef();
160         gotoPB->setEnabled(true);
161         if (typeAllowed())
162                 typeCO->setEnabled(true);
163         nameED->setHidden(!nameAllowed());
164         nameL->setHidden(!nameAllowed());
165 }
166
167
168 void GuiRef::refSelected(QTreeWidgetItem * sel)
169 {
170         if (isBufferReadonly())
171                 return;
172
173 /*      int const cur_item = refsTW->currentRow();
174         bool const cur_item_selected = cur_item >= 0 ?
175                 refsLB->isSelected(cur_item) : false;*/
176         bool const cur_item_selected = refsTW->isItemSelected(sel);
177
178         if (cur_item_selected)
179                 referenceED->setText(sel->text(0));
180         // <enter> or double click, inserts ref and closes dialog
181         slotOK();
182 }
183
184
185 void GuiRef::sortToggled()
186 {
187         caseSensitiveCB->setEnabled(sortCB->isChecked());
188         redoRefs();
189 }
190
191
192 void GuiRef::caseSensitiveToggled()
193 {
194         redoRefs();
195 }
196
197
198 void GuiRef::groupToggled()
199 {
200         redoRefs();
201 }
202
203
204 void GuiRef::updateClicked()
205 {
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         vector<docstring>::const_iterator iter;
359         for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
360                 QString const lab = toqstr(*iter);
361                 refsStrings.append(lab);
362                 if (groupCB->isChecked() && lab.contains(":")) {
363                         QString const pref = lab.split(':')[0];
364                         if (!pref.isEmpty() && !refsCategories.contains(pref))
365                                   refsCategories.append(pref);
366                 }
367         }
368         // sort categories case-intensively
369         qSort(refsCategories.begin(), refsCategories.end(),
370               caseInsensitiveLessThan /*defined above*/);
371         refsCategories.insert(0, qt_("<No prefix>"));
372
373         if (sortCB->isEnabled() && sortCB->isChecked()) {
374                 if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
375                         qSort(refsStrings.begin(), refsStrings.end());
376                 else
377                         qSort(refsStrings.begin(), refsStrings.end(),
378                               caseInsensitiveLessThan /*defined above*/);
379         }
380         
381         if (groupCB->isChecked()) {
382                 QList<QTreeWidgetItem *> refsCats;
383                 for (int i = 0; i < refsCategories.size(); ++i) {
384                         QString const cat = refsCategories.at(i);
385                         QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
386                         item->setText(0, cat);
387                         for (int i = 0; i < refsStrings.size(); ++i) {
388                                 QString const ref = refsStrings.at(i);
389                                 if ((ref.startsWith(cat + QString(":")))
390                                     || (cat == qt_("<No prefix>")
391                                         && !ref.contains(":"))) {
392                                         QTreeWidgetItem * child =
393                                                 new QTreeWidgetItem(item);
394                                         child->setText(0, ref);
395                                         item->addChild(child);
396                                 }
397                         }
398                         refsCats.append(item);
399                 }
400                 refsTW->addTopLevelItems(refsCats);
401         } else {
402                 QList<QTreeWidgetItem *> refsItems;
403                 for (int i = 0; i < refsStrings.size(); ++i) {
404                         QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
405                         item->setText(0, refsStrings.at(i));
406                         refsItems.append(item);
407                 }
408                 refsTW->addTopLevelItems(refsItems);
409         }
410
411         referenceED->setText(oldSelection);
412
413         // restore the last selection or, for new insets, highlight
414         // the previous selection
415         if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
416                 bool const newInset = oldSelection.isEmpty();
417                 QString textToFind = newInset ? last_reference_ : oldSelection;
418                 last_reference_.clear();
419                 QTreeWidgetItemIterator it(refsTW);
420                 while (*it) {
421                         if ((*it)->text(0) == textToFind) {
422                                 refsTW->setCurrentItem(*it);
423                                 refsTW->setItemSelected(*it, !newInset);
424                                 //Make sure selected item is visible
425                                 refsTW->scrollToItem(*it);
426                                 last_reference_ = textToFind;
427                                 break;
428                         }
429                         ++it;
430                 }
431         }
432         refsTW->setUpdatesEnabled(true);
433         refsTW->update();
434
435         // redo filter
436         filterLabels();
437
438         // Re-activate the emission of signals by these widgets.
439         refsTW->blockSignals(false);
440         referenceED->blockSignals(false);
441 }
442
443
444 void GuiRef::updateRefs()
445 {
446         refs_.clear();
447         int const the_buffer = bufferCO->currentIndex();
448         if (the_buffer != -1) {
449                 FileName const & name = theBufferList().fileNames()[the_buffer];
450                 Buffer const * buf = theBufferList().getBuffer(name);
451                 buf->getLabelList(refs_);
452         }       
453         sortCB->setEnabled(!refs_.empty());
454         caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
455         refsTW->setEnabled(!refs_.empty());
456         // refsTW should only be the focus proxy when it is enabled
457         setFocusProxy(refs_.empty() ? 0 : refsTW);
458         gotoPB->setEnabled(!refs_.empty());
459         redoRefs();
460 }
461
462
463 bool GuiRef::isValid()
464 {
465         return !referenceED->text().isEmpty();
466 }
467
468
469 void GuiRef::gotoRef(string const & ref)
470 {
471         dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
472         dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
473 }
474
475
476 void GuiRef::gotoBookmark()
477 {
478         dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
479 }
480
481
482 void GuiRef::filterLabels()
483 {
484         Qt::CaseSensitivity cs = csFindCB->isChecked() ?
485                 Qt::CaseSensitive : Qt::CaseInsensitive;
486         QTreeWidgetItemIterator it(refsTW);
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(data, params_);
500         return true;
501 }
502
503
504 void GuiRef::dispatchParams()
505 {
506         std::string const lfun = InsetCommand::params2string(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"