]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QRef.C
5cdb5ca73b1bac2c92f06559a3851f55b322e103
[features.git] / src / frontends / qt4 / QRef.C
1 /**
2  * \file QRef.C
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 "QRefDialog.h"
16 #include "Qt2BC.h"
17 #include "qt_helpers.h"
18
19 #include "controllers/ButtonController.h"
20 #include "controllers/ControlRef.h"
21
22 #include "insets/insetref.h"
23
24 #include <QLineEdit>
25 #include <QCheckBox>
26 #include <QListWidget>
27 #include <QListWidgetItem>
28 #include <QPushButton>
29 #include <QToolTip>
30
31
32 using lyx::docstring;
33
34 using std::vector;
35 using std::string;
36
37 namespace lyx {
38 namespace frontend {
39
40 // full qualification because qt4 has also a ControlRef type
41 typedef QController<lyx::frontend::ControlRef, QView<QRefDialog> > ref_base_class;
42
43
44 QRef::QRef(Dialog & parent)
45         : ref_base_class(parent, _("Cross-reference")),
46         sort_(false), at_ref_(false)
47 {
48 }
49
50
51 void QRef::build_dialog()
52 {
53         dialog_.reset(new QRefDialog(this));
54
55         bcview().setOK(dialog_->okPB);
56         bcview().setApply(dialog_->applyPB);
57         bcview().setCancel(dialog_->closePB);
58         bcview().addReadOnly(dialog_->refsLW);
59         bcview().addReadOnly(dialog_->sortCB);
60         bcview().addReadOnly(dialog_->nameED);
61         bcview().addReadOnly(dialog_->referenceED);
62         bcview().addReadOnly(dialog_->typeCO);
63         bcview().addReadOnly(dialog_->bufferCO);
64
65         restored_buffer_ = -1;
66 }
67
68
69 void QRef::update_contents()
70 {
71         InsetCommandParams const & params = controller().params();
72
73         int orig_type = dialog_->typeCO->currentIndex();
74
75         dialog_->referenceED->setText(toqstr(params["reference"]));
76
77         dialog_->nameED->setText(toqstr(params["name"]));
78         dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
79
80         // restore type settings for new insets
81         if (params["reference"].empty())
82                 dialog_->typeCO->setCurrentIndex(orig_type);
83         else
84                 dialog_->typeCO->setCurrentIndex(InsetRef::getType(params.getCmdName()));
85         dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
86         if (!typeAllowed())
87                 dialog_->typeCO->setCurrentIndex(0);
88
89         dialog_->sortCB->setChecked(sort_);
90
91         // insert buffer list
92         dialog_->bufferCO->clear();
93         vector<string> const buffers = controller().getBufferList();
94         for (vector<string>::const_iterator it = buffers.begin();
95                 it != buffers.end(); ++it) {
96                 dialog_->bufferCO->addItem(toqstr(*it));
97         }
98         // restore the buffer combo setting for new insets
99         if (params["reference"].empty() && restored_buffer_ != -1
100         && restored_buffer_ < dialog_->bufferCO->count())
101                 dialog_->bufferCO->setCurrentIndex(restored_buffer_);
102         else
103                 dialog_->bufferCO->setCurrentIndex(controller().getBufferNum());
104
105         updateRefs();
106         bc().valid(false);
107 }
108
109
110 void QRef::apply()
111 {
112         InsetCommandParams & params = controller().params();
113         
114         last_reference_ = dialog_->referenceED->text();
115
116         params.setCmdName(InsetRef::getName(dialog_->typeCO->currentIndex()));
117         params["reference"] = qstring_to_ucs4(last_reference_);
118         params["name"] = qstring_to_ucs4(dialog_->nameED->text());
119
120         restored_buffer_ = dialog_->bufferCO->currentIndex();
121 }
122
123
124 bool QRef::nameAllowed()
125 {
126         Kernel::DocType const doc_type = kernel().docType();
127         return doc_type != Kernel::LATEX &&
128                 doc_type != Kernel::LITERATE;
129 }
130
131
132 bool QRef::typeAllowed()
133 {
134         Kernel::DocType const doc_type = kernel().docType();
135         return doc_type != Kernel::DOCBOOK;
136 }
137
138
139 void QRef::setGoBack()
140 {
141         dialog_->gotoPB->setText(qt_("&Go Back"));
142         dialog_->gotoPB->setToolTip("");
143         dialog_->gotoPB->setToolTip(qt_("Jump back"));
144 }
145
146
147 void QRef::setGotoRef()
148 {
149         dialog_->gotoPB->setText(qt_("&Go to Label"));
150         dialog_->gotoPB->setToolTip("");
151         dialog_->gotoPB->setToolTip(qt_("Jump to label"));
152 }
153
154
155 void QRef::gotoRef()
156 {
157         string ref(fromqstr(dialog_->referenceED->text()));
158
159         if (at_ref_) {
160                 // go back
161                 setGotoRef();
162                 controller().gotoBookmark();
163         } else {
164                 // go to the ref
165                 setGoBack();
166                 controller().gotoRef(ref);
167         }
168         at_ref_ = !at_ref_;
169 }
170
171
172 void QRef::redoRefs()
173 {
174         // Prevent these widgets from emitting any signals whilst
175         // we modify their state.
176         dialog_->refsLW->blockSignals(true);
177         dialog_->referenceED->blockSignals(true);
178         dialog_->refsLW->setUpdatesEnabled(false);
179         
180         dialog_->refsLW->clear();
181
182         // need this because Qt will send a highlight() here for
183         // the first item inserted
184         QString const oldSelection(dialog_->referenceED->text());
185
186         for (std::vector<docstring>::const_iterator iter = refs_.begin();
187                 iter != refs_.end(); ++iter) {
188                 dialog_->refsLW->addItem(toqstr(*iter));
189         }
190
191         if (sort_)
192                 dialog_->refsLW->sortItems();
193
194         dialog_->referenceED->setText(oldSelection);
195
196         // restore the last selection or, for new insets, highlight
197         // the previous selection
198         if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
199                 bool const newInset = oldSelection.isEmpty();
200                 QString textToFind = newInset ? last_reference_ : oldSelection;
201                 bool foundItem = false;
202                 for (int i = 0; !foundItem && i < dialog_->refsLW->count(); ++i) {
203                         QListWidgetItem * item = dialog_->refsLW->item(i);
204                         if (textToFind == item->text()) {
205                                 dialog_->refsLW->setCurrentItem(item);
206                                 dialog_->refsLW->setItemSelected(item, !newInset);
207                                 //Make sure selected item is visible
208                                 dialog_->refsLW->scrollToItem(item);
209                                 foundItem = true;
210                         }
211                 }
212                 if (foundItem)
213                         last_reference_ = textToFind;
214                 else last_reference_ = "";
215         }
216         dialog_->refsLW->setUpdatesEnabled(true);
217         dialog_->refsLW->update();
218
219         // Re-activate the emission of signals by these widgets.
220         dialog_->refsLW->blockSignals(false);
221         dialog_->referenceED->blockSignals(false);
222 }
223
224
225 void QRef::updateRefs()
226 {
227         refs_.clear();
228         string const name = controller().getBufferName(dialog_->bufferCO->currentIndex());
229         refs_ = controller().getLabelList(name);
230         dialog_->sortCB->setEnabled(!refs_.empty());
231         dialog_->refsLW->setEnabled(!refs_.empty());
232         dialog_->gotoPB->setEnabled(!refs_.empty());
233         redoRefs();
234 }
235
236 bool QRef::isValid()
237 {
238         return !dialog_->referenceED->text().isEmpty();
239 }
240
241 } // namespace frontend
242 } // namespace lyx