]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiRef.cpp
Introducing TextClassPtr.h to minimize header dependencies.
[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/filetools.h" // MakeAbsPath, MakeDisplayPath
25
26 #include <boost/filesystem/operations.hpp>
27
28 #include <QLineEdit>
29 #include <QCheckBox>
30 #include <QListWidget>
31 #include <QListWidgetItem>
32 #include <QPushButton>
33 #include <QToolTip>
34 #include <QCloseEvent>
35
36 using std::vector;
37 using std::string;
38
39 namespace lyx {
40 namespace frontend {
41
42 using support::makeAbsPath;
43 using support::makeDisplayPath;
44
45 GuiRef::GuiRef(LyXView & lv)
46         : GuiCommand(lv, "ref")
47 {
48         setupUi(this);
49         setViewTitle(_("Cross-reference"));
50
51         sort_ = false;
52         at_ref_ = 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(reset_dialog()));
58         connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
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(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()));
80
81         setFocusProxy(refsLW);
82
83         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
84         bc().setOK(okPB);
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);
93
94         restored_buffer_ = -1;
95 }
96
97
98 void GuiRef::changed_adaptor()
99 {
100         changed();
101 }
102
103
104 void GuiRef::gotoClicked()
105 {
106         gotoRef();
107 }
108
109
110 void GuiRef::selectionChanged()
111 {
112         if (isBufferReadonly())
113                 return;
114
115         QList<QListWidgetItem *> selections = refsLW->selectedItems();
116         if (selections.isEmpty())
117                 return;
118         QListWidgetItem * sel = selections.first();
119         refHighlighted(sel);
120         return;
121 }
122
123
124 void GuiRef::refHighlighted(QListWidgetItem * sel)
125 {
126         if (isBufferReadonly())
127                 return;
128
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);
133
134         if (cur_item_selected)
135                 referenceED->setText(sel->text());
136
137         if (at_ref_)
138                 gotoRef();
139         gotoPB->setEnabled(true);
140         if (typeAllowed())
141                 typeCO->setEnabled(true);
142         if (nameAllowed())
143                 nameED->setEnabled(true);
144 }
145
146
147 void GuiRef::refSelected(QListWidgetItem * sel)
148 {
149         if (isBufferReadonly())
150                 return;
151
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);
156
157         if (cur_item_selected)
158                 referenceED->setText(sel->text());
159         // <enter> or double click, inserts ref and closes dialog
160         slotOK();
161 }
162
163
164 void GuiRef::sortToggled(bool on)
165 {
166         sort_ = on;
167         redoRefs();
168 }
169
170
171 void GuiRef::updateClicked()
172 {
173         updateRefs();
174 }
175
176
177 void GuiRef::reset_dialog()
178 {
179         at_ref_ = false;
180         setGotoRef();
181 }
182
183
184 void GuiRef::closeEvent(QCloseEvent * e)
185 {
186         slotClose();
187         reset_dialog();
188         GuiDialog::closeEvent(e);
189 }
190
191
192 void GuiRef::updateContents()
193 {
194         int orig_type = typeCO->currentIndex();
195
196         referenceED->setText(toqstr(params_["reference"]));
197
198         nameED->setText(toqstr(params_["name"]));
199         nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
200
201         // restore type settings for new insets
202         if (params_["reference"].empty())
203                 typeCO->setCurrentIndex(orig_type);
204         else
205                 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
206         typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
207         if (!typeAllowed())
208                 typeCO->setCurrentIndex(0);
209
210         sortCB->setChecked(sort_);
211
212         // insert buffer list
213         bufferCO->clear();
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))));
218         }
219
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_);
224         else {
225                 int num = theBufferList().bufferNum(buffer().absFileName());
226                 bufferCO->setCurrentIndex(num);
227         }
228
229         updateRefs();
230         bc().setValid(false);
231 }
232
233
234 void GuiRef::applyView()
235 {
236         last_reference_ = referenceED->text();
237
238         params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
239         params_["reference"] = qstring_to_ucs4(last_reference_);
240         params_["name"] = qstring_to_ucs4(nameED->text());
241
242         restored_buffer_ = bufferCO->currentIndex();
243 }
244
245
246 bool GuiRef::nameAllowed()
247 {
248         KernelDocType const doc_type = docType();
249         return doc_type != LATEX && doc_type != LITERATE;
250 }
251
252
253 bool GuiRef::typeAllowed()
254 {
255         return docType() != DOCBOOK;
256 }
257
258
259 void GuiRef::setGoBack()
260 {
261         gotoPB->setText(qt_("&Go Back"));
262         gotoPB->setToolTip("");
263         gotoPB->setToolTip(qt_("Jump back"));
264 }
265
266
267 void GuiRef::setGotoRef()
268 {
269         gotoPB->setText(qt_("&Go to Label"));
270         gotoPB->setToolTip("");
271         gotoPB->setToolTip(qt_("Jump to label"));
272 }
273
274
275 void GuiRef::gotoRef()
276 {
277         string ref = fromqstr(referenceED->text());
278
279         if (at_ref_) {
280                 // go back
281                 setGotoRef();
282                 gotoBookmark();
283         } else {
284                 // go to the ref
285                 setGoBack();
286                 gotoRef(ref);
287         }
288         at_ref_ = !at_ref_;
289 }
290
291
292 void GuiRef::redoRefs()
293 {
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);
299
300         refsLW->clear();
301
302         // need this because Qt will send a highlight() here for
303         // the first item inserted
304         QString const oldSelection(referenceED->text());
305
306         for (std::vector<docstring>::const_iterator iter = refs_.begin();
307                 iter != refs_.end(); ++iter) {
308                 refsLW->addItem(toqstr(*iter));
309         }
310
311         if (sort_)
312                 refsLW->sortItems();
313
314         referenceED->setText(oldSelection);
315
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;
330                                 break;
331                         }
332                 }
333         }
334         refsLW->setUpdatesEnabled(true);
335         refsLW->update();
336
337         // Re-activate the emission of signals by these widgets.
338         refsLW->blockSignals(false);
339         referenceED->blockSignals(false);
340 }
341
342
343 void GuiRef::updateRefs()
344 {
345         refs_.clear();
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());
352         redoRefs();
353 }
354
355
356 bool GuiRef::isValid()
357 {
358         return !referenceED->text().isEmpty();
359 }
360
361
362 void GuiRef::gotoRef(string const & ref)
363 {
364         dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
365         dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
366 }
367
368
369 void GuiRef::gotoBookmark()
370 {
371         dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
372 }
373
374
375 Dialog * createGuiRef(LyXView & lv) { return new GuiRef(lv); }
376
377
378 } // namespace frontend
379 } // namespace lyx
380
381 #include "GuiRef_moc.cpp"