]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiRef.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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/filetools.h" // MakeAbsPath, MakeDisplayPath
26
27 #include <QLineEdit>
28 #include <QCheckBox>
29 #include <QListWidget>
30 #include <QListWidgetItem>
31 #include <QPushButton>
32 #include <QToolTip>
33 #include <QCloseEvent>
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39 namespace frontend {
40
41 GuiRef::GuiRef(GuiView & lv)
42         : GuiCommand(lv, "ref", qt_("Cross-reference"))
43 {
44         setupUi(this);
45
46         sort_ = false;
47         at_ref_ = false;
48
49         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
50         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
51         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
52         connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
53         connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
54
55         connect(typeCO, SIGNAL(activated(int)),
56                 this, SLOT(changed_adaptor()));
57         connect(referenceED, SIGNAL(textChanged(QString)),
58                 this, SLOT(changed_adaptor()));
59         connect(nameED, SIGNAL(textChanged(QString)),
60                 this, SLOT(changed_adaptor()));
61         connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
62                 this, SLOT(refHighlighted(QListWidgetItem *)));
63         connect(refsLW, SIGNAL(itemSelectionChanged()),
64                 this, SLOT(selectionChanged()));
65         connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
66                 this, SLOT(refSelected(QListWidgetItem *)));
67         connect(sortCB, SIGNAL(clicked(bool)),
68                 this, SLOT(sortToggled(bool)));
69         connect(gotoPB, SIGNAL(clicked()),
70                 this, SLOT(gotoClicked()));
71         connect(updatePB, SIGNAL(clicked()),
72                 this, SLOT(updateClicked()));
73         connect(bufferCO, SIGNAL(activated(int)),
74                 this, SLOT(updateClicked()));
75
76         setFocusProxy(refsLW);
77
78         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
79         bc().setOK(okPB);
80         bc().setApply(applyPB);
81         bc().setCancel(closePB);
82         bc().addReadOnly(refsLW);
83         bc().addReadOnly(sortCB);
84         bc().addReadOnly(nameED);
85         bc().addReadOnly(referenceED);
86         bc().addReadOnly(typeCO);
87         bc().addReadOnly(bufferCO);
88
89         restored_buffer_ = -1;
90 }
91
92
93 void GuiRef::changed_adaptor()
94 {
95         changed();
96 }
97
98
99 void GuiRef::gotoClicked()
100 {
101         gotoRef();
102 }
103
104
105 void GuiRef::selectionChanged()
106 {
107         if (isBufferReadonly())
108                 return;
109
110         QList<QListWidgetItem *> selections = refsLW->selectedItems();
111         if (selections.isEmpty())
112                 return;
113         QListWidgetItem * sel = selections.first();
114         refHighlighted(sel);
115         return;
116 }
117
118
119 void GuiRef::refHighlighted(QListWidgetItem * sel)
120 {
121         if (isBufferReadonly())
122                 return;
123
124 /*      int const cur_item = refsLW->currentRow();
125         bool const cur_item_selected = cur_item >= 0 ?
126                 refsLB->isSelected(cur_item) : false;*/
127         bool const cur_item_selected = refsLW->isItemSelected(sel);
128
129         if (cur_item_selected)
130                 referenceED->setText(sel->text());
131
132         if (at_ref_)
133                 gotoRef();
134         gotoPB->setEnabled(true);
135         if (typeAllowed())
136                 typeCO->setEnabled(true);
137         if (nameAllowed())
138                 nameED->setEnabled(true);
139 }
140
141
142 void GuiRef::refSelected(QListWidgetItem * sel)
143 {
144         if (isBufferReadonly())
145                 return;
146
147 /*      int const cur_item = refsLW->currentRow();
148         bool const cur_item_selected = cur_item >= 0 ?
149                 refsLB->isSelected(cur_item) : false;*/
150         bool const cur_item_selected = refsLW->isItemSelected(sel);
151
152         if (cur_item_selected)
153                 referenceED->setText(sel->text());
154         // <enter> or double click, inserts ref and closes dialog
155         slotOK();
156 }
157
158
159 void GuiRef::sortToggled(bool on)
160 {
161         sort_ = on;
162         redoRefs();
163 }
164
165
166 void GuiRef::updateClicked()
167 {
168         updateRefs();
169 }
170
171
172 void GuiRef::reset_dialog()
173 {
174         at_ref_ = false;
175         setGotoRef();
176 }
177
178
179 void GuiRef::closeEvent(QCloseEvent * e)
180 {
181         slotClose();
182         reset_dialog();
183         e->accept();
184 }
185
186
187 void GuiRef::updateContents()
188 {
189         int orig_type = typeCO->currentIndex();
190
191         referenceED->setText(toqstr(params_["reference"]));
192
193         nameED->setText(toqstr(params_["name"]));
194         nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
195
196         // restore type settings for new insets
197         if (params_["reference"].empty())
198                 typeCO->setCurrentIndex(orig_type);
199         else
200                 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
201         typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
202         if (!typeAllowed())
203                 typeCO->setCurrentIndex(0);
204
205         sortCB->setChecked(sort_);
206
207         // insert buffer list
208         bufferCO->clear();
209         vector<string> buffers = theBufferList().getFileNames();
210         for (vector<string>::iterator it = buffers.begin();
211              it != buffers.end(); ++it) {
212                 bufferCO->addItem(toqstr(lyx::to_utf8(makeDisplayPath(*it))));
213         }
214
215         // restore the buffer combo setting for new insets
216         if (params_["reference"].empty() && restored_buffer_ != -1
217             && restored_buffer_ < bufferCO->count()) 
218                 bufferCO->setCurrentIndex(restored_buffer_);
219         else {
220                 int num = theBufferList().bufferNum(buffer().absFileName());
221                 bufferCO->setCurrentIndex(num);
222         }
223
224         updateRefs();
225         bc().setValid(false);
226 }
227
228
229 void GuiRef::applyView()
230 {
231         last_reference_ = referenceED->text();
232
233         params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
234         params_["reference"] = qstring_to_ucs4(last_reference_);
235         params_["name"] = qstring_to_ucs4(nameED->text());
236
237         restored_buffer_ = bufferCO->currentIndex();
238 }
239
240
241 bool GuiRef::nameAllowed()
242 {
243         KernelDocType const doc_type = docType();
244         return doc_type != LATEX && doc_type != LITERATE;
245 }
246
247
248 bool GuiRef::typeAllowed()
249 {
250         return docType() != DOCBOOK;
251 }
252
253
254 void GuiRef::setGoBack()
255 {
256         gotoPB->setText(qt_("&Go Back"));
257         gotoPB->setToolTip("");
258         gotoPB->setToolTip(qt_("Jump back"));
259 }
260
261
262 void GuiRef::setGotoRef()
263 {
264         gotoPB->setText(qt_("&Go to Label"));
265         gotoPB->setToolTip("");
266         gotoPB->setToolTip(qt_("Jump to label"));
267 }
268
269
270 void GuiRef::gotoRef()
271 {
272         string ref = fromqstr(referenceED->text());
273
274         if (at_ref_) {
275                 // go back
276                 setGotoRef();
277                 gotoBookmark();
278         } else {
279                 // go to the ref
280                 setGoBack();
281                 gotoRef(ref);
282         }
283         at_ref_ = !at_ref_;
284 }
285
286
287 void GuiRef::redoRefs()
288 {
289         // Prevent these widgets from emitting any signals whilst
290         // we modify their state.
291         refsLW->blockSignals(true);
292         referenceED->blockSignals(true);
293         refsLW->setUpdatesEnabled(false);
294
295         refsLW->clear();
296
297         // need this because Qt will send a highlight() here for
298         // the first item inserted
299         QString const oldSelection(referenceED->text());
300
301         for (vector<docstring>::const_iterator iter = refs_.begin();
302                 iter != refs_.end(); ++iter) {
303                 refsLW->addItem(toqstr(*iter));
304         }
305
306         if (sort_)
307                 refsLW->sortItems();
308
309         referenceED->setText(oldSelection);
310
311         // restore the last selection or, for new insets, highlight
312         // the previous selection
313         if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
314                 bool const newInset = oldSelection.isEmpty();
315                 QString textToFind = newInset ? last_reference_ : oldSelection;
316                 last_reference_.clear();
317                 for (int i = 0; i != refsLW->count(); ++i) {
318                         QListWidgetItem * item = refsLW->item(i);
319                         if (textToFind == item->text()) {
320                                 refsLW->setCurrentItem(item);
321                                 refsLW->setItemSelected(item, !newInset);
322                                 //Make sure selected item is visible
323                                 refsLW->scrollToItem(item);
324                                 last_reference_ = textToFind;
325                                 break;
326                         }
327                 }
328         }
329         refsLW->setUpdatesEnabled(true);
330         refsLW->update();
331
332         // Re-activate the emission of signals by these widgets.
333         refsLW->blockSignals(false);
334         referenceED->blockSignals(false);
335 }
336
337
338 void GuiRef::updateRefs()
339 {
340         refs_.clear();
341         string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
342         Buffer const * buf = theBufferList().getBuffer(makeAbsPath(name).absFilename());
343         buf->getLabelList(refs_);
344         sortCB->setEnabled(!refs_.empty());
345         refsLW->setEnabled(!refs_.empty());
346         gotoPB->setEnabled(!refs_.empty());
347         redoRefs();
348 }
349
350
351 bool GuiRef::isValid()
352 {
353         return !referenceED->text().isEmpty();
354 }
355
356
357 void GuiRef::gotoRef(string const & ref)
358 {
359         dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
360         dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
361 }
362
363
364 void GuiRef::gotoBookmark()
365 {
366         dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
367 }
368
369
370 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
371
372
373 } // namespace frontend
374 } // namespace lyx
375
376 #include "GuiRef_moc.cpp"