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