]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QRef.C
* src/frontends/qt4/GuiSelection.C
[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 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> > base_class;
42
43
44 QRef::QRef(Dialog & parent)
45         : 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         params.setCmdName(InsetRef::getName(dialog_->typeCO->currentIndex()));
115         params["reference"] = qstring_to_ucs4(dialog_->referenceED->text());
116         params["name"] = qstring_to_ucs4(dialog_->nameED->text());
117
118         restored_buffer_ = dialog_->bufferCO->currentIndex();
119 }
120
121
122 bool QRef::nameAllowed()
123 {
124         Kernel::DocType const doc_type = kernel().docType();
125         return doc_type != Kernel::LATEX &&
126                 doc_type != Kernel::LITERATE;
127 }
128
129
130 bool QRef::typeAllowed()
131 {
132         Kernel::DocType const doc_type = kernel().docType();
133         return doc_type != Kernel::DOCBOOK;
134 }
135
136
137 void QRef::setGoBack()
138 {
139         dialog_->gotoPB->setText(qt_("&Go Back"));
140         dialog_->gotoPB->setToolTip("");
141         dialog_->gotoPB->setToolTip(qt_("Jump back"));
142 }
143
144
145 void QRef::setGotoRef()
146 {
147         dialog_->gotoPB->setText(qt_("&Go to Label"));
148         dialog_->gotoPB->setToolTip("");
149         dialog_->gotoPB->setToolTip(qt_("Jump to label"));
150 }
151
152
153 void QRef::gotoRef()
154 {
155         string ref(fromqstr(dialog_->referenceED->text()));
156
157         if (at_ref_) {
158                 // go back
159                 setGotoRef();
160                 controller().gotoBookmark();
161         } else {
162                 // go to the ref
163                 setGoBack();
164                 controller().gotoRef(ref);
165         }
166         at_ref_ = !at_ref_;
167 }
168
169
170 void QRef::redoRefs()
171 {
172         // Prevent these widgets from emitting any signals whilst
173         // we modify their state.
174         dialog_->refsLW->blockSignals(true);
175         dialog_->referenceED->blockSignals(true);
176
177         int lastref = dialog_->refsLW->currentRow();
178
179         dialog_->refsLW->setUpdatesEnabled(false);
180         dialog_->refsLW->clear();
181
182         // need this because Qt will send a highlight() here for
183         // the first item inserted
184         QString const tmp(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(tmp);
195
196         // restore the last selection for new insets
197         if (tmp.isEmpty() && lastref != -1
198                 && lastref < int(dialog_->refsLW->count())) {
199                 dialog_->refsLW->setCurrentRow(lastref);
200                 dialog_->refsLW->clearSelection();
201         } else
202                 for (int i = 0; i < dialog_->refsLW->count(); ++i) {
203                         QListWidgetItem * item = dialog_->refsLW->item(i);
204                         if (tmp == item->text()) {
205                                 dialog_->refsLW->setItemSelected(item, true);
206                         }
207                 }
208
209         dialog_->refsLW->setUpdatesEnabled(true);
210         dialog_->refsLW->update();
211
212         // Re-activate the emission of signals by these widgets.
213         dialog_->refsLW->blockSignals(false);
214         dialog_->referenceED->blockSignals(false);
215 }
216
217
218 void QRef::updateRefs()
219 {
220         refs_.clear();
221         if (at_ref_)
222                 gotoRef();
223         string const name = controller().getBufferName(dialog_->bufferCO->currentIndex());
224         refs_ = controller().getLabelList(name);
225         dialog_->sortCB->setEnabled(!refs_.empty());
226         dialog_->refsLW->setEnabled(!refs_.empty());
227         dialog_->gotoPB->setEnabled(!refs_.empty());
228         redoRefs();
229 }
230
231 bool QRef::isValid()
232 {
233         return !dialog_->referenceED->text().isEmpty();
234 }
235
236 } // namespace frontend
237 } // namespace lyx