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