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