]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QRef.C
Added initial qt4 work by Abdelrazak Younes
[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.h>
25 #include <qcheckbox.h>
26 #include <q3listbox.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, _("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         bc().valid(false);
103 }
104
105
106 void QRef::apply()
107 {
108         InsetCommandParams & params = controller().params();
109
110         params.setCmdName(InsetRef::getName(dialog_->typeCO->currentItem()));
111         params.setContents(fromqstr(dialog_->referenceED->text()));
112         params.setOptions(fromqstr(dialog_->nameED->text()));
113
114         restored_buffer_ = dialog_->bufferCO->currentItem();
115 }
116
117
118 bool QRef::nameAllowed()
119 {
120         Kernel::DocType const doc_type = kernel().docType();
121         return doc_type != Kernel::LATEX &&
122                 doc_type != Kernel::LITERATE;
123 }
124
125
126 bool QRef::typeAllowed()
127 {
128         Kernel::DocType const doc_type = kernel().docType();
129         return doc_type != Kernel::LINUXDOC &&
130                 doc_type != Kernel::DOCBOOK;
131 }
132
133
134 void QRef::setGoBack()
135 {
136         dialog_->gotoPB->setText(qt_("&Go Back"));
137         QToolTip::remove(dialog_->gotoPB);
138         QToolTip::add(dialog_->gotoPB, qt_("Jump back"));
139 }
140
141
142 void QRef::setGotoRef()
143 {
144         dialog_->gotoPB->setText(qt_("&Go to Label"));
145         QToolTip::remove(dialog_->gotoPB);
146         QToolTip::add(dialog_->gotoPB, qt_("Jump to label"));
147 }
148
149
150 void QRef::gotoRef()
151 {
152         string ref(fromqstr(dialog_->referenceED->text()));
153
154         if (at_ref_) {
155                 // go back
156                 setGotoRef();
157                 controller().gotoBookmark();
158         } else {
159                 // go to the ref
160                 setGoBack();
161                 controller().gotoRef(ref);
162         }
163         at_ref_ = !at_ref_;
164 }
165
166
167 void QRef::redoRefs()
168 {
169         // Prevent these widgets from emitting any signals whilst
170         // we modify their state.
171         dialog_->refsLB->blockSignals(true);
172         dialog_->referenceED->blockSignals(true);
173
174         int lastref = dialog_->refsLB->currentItem();
175
176         dialog_->refsLB->setAutoUpdate(false);
177         dialog_->refsLB->clear();
178
179         // need this because Qt will send a highlight() here for
180         // the first item inserted
181         QString const tmp(dialog_->referenceED->text());
182
183         for (std::vector<string>::const_iterator iter = refs_.begin();
184                 iter != refs_.end(); ++iter) {
185                 dialog_->refsLB->insertItem(toqstr(*iter));
186         }
187
188         if (sort_)
189                 dialog_->refsLB->sort();
190
191         dialog_->referenceED->setText(tmp);
192
193         // restore the last selection for new insets
194         if (tmp.isEmpty() && lastref != -1
195                 && lastref < int(dialog_->refsLB->count())) {
196                 dialog_->refsLB->setCurrentItem(lastref);
197                 dialog_->refsLB->clearSelection();
198         } else
199                 for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
200                         if (tmp == dialog_->refsLB->text(i))
201                                 dialog_->refsLB->setSelected(i, true);
202                 }
203
204         dialog_->refsLB->setAutoUpdate(true);
205         dialog_->refsLB->update();
206
207         // Re-activate the emission of signals by these widgets.
208         dialog_->refsLB->blockSignals(false);
209         dialog_->referenceED->blockSignals(false);
210 }
211
212
213 void QRef::updateRefs()
214 {
215         refs_.clear();
216         if (at_ref_)
217                 gotoRef();
218         string const name = controller().getBufferName(dialog_->bufferCO->currentItem());
219         refs_ = controller().getLabelList(name);
220         dialog_->sortCB->setEnabled(!refs_.empty());
221         dialog_->refsLB->setEnabled(!refs_.empty());
222         dialog_->gotoPB->setEnabled(!refs_.empty());
223         redoRefs();
224 }
225
226 bool QRef::isValid()
227 {
228         return !dialog_->referenceED->text().isEmpty();
229 }
230
231 } // namespace frontend
232 } // namespace lyx