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