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