]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QRef.C
The big renaming. Yowser.
[lyx.git] / src / frontends / qt2 / QRef.C
1 /**
2  * \file QRef.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon
7  */
8
9 #include <config.h>
10
11 #include "QRefDialog.h"
12
13 #include "QtLyXView.h"
14 #include "BufferView.h"
15 #include "Dialogs.h"
16 #include "QRef.h"
17 #include "gettext.h"
18 #include "buffer.h"
19 #include "lyxfunc.h"
20 #include "debug.h"
21
22 #include <qpushbutton.h>
23 #include <qlineedit.h>
24 #include <qlistbox.h>
25 #include <qcombobox.h>
26 #include <qcheckbox.h>
27
28 using std::endl;
29
30 QRef::QRef(LyXView *v, Dialogs *d)
31         : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
32         sort(0), gotowhere(GOTOREF), type(REF), refs(0)
33 {
34         // let the dialog be shown
35         // This is a permanent connection so we won't bother
36         // storing a copy because we won't be disconnecting.
37         d->showRef.connect(slot(this, &QRef::showRef));
38         d->createRef.connect(slot(this, &QRef::createRef));
39 }
40
41
42 QRef::~QRef()
43 {
44         delete dialog_;
45 }
46
47
48 void QRef::showRef(InsetCommand * const inset)
49 {
50         // FIXME: when could inset be 0 here ?
51         if (inset==0)
52                 return;
53
54         inset_ = inset;
55         readonly = lv_->buffer()->isReadonly();
56         //ih_ = inset_->hide.connect(slot(this,&QRef::hide));
57         params = inset->params();
58         
59         show();
60 }
61
62
63 void QRef::createRef(string const & arg)
64 {
65         if (inset_)
66                 close();
67
68         readonly = lv_->buffer()->isReadonly();
69         params.setFromString(arg);
70         show();
71 }
72
73
74 void QRef::select(const char *text)
75 {
76         highlight(text);
77         goto_ref();
78 }
79
80  
81 void QRef::highlight(const char *text)
82 {
83         if (gotowhere==GOTOBACK)
84                 goto_ref();
85
86         dialog_->gotoPB->setEnabled(true);
87         if (!readonly) {
88                 dialog_->typeCO->setEnabled(true);
89                 dialog_->referenceED->setText(text);
90                 dialog_->okPB->setEnabled(true);
91         }
92 }
93
94  
95 void QRef::set_sort(bool on)
96 {
97         if (on!=sort) {
98                 sort=on;
99                 dialog_->refsLB->clear();
100                 updateRefs();
101         }
102 }
103
104  
105 void QRef::goto_ref()
106 {
107         switch (gotowhere) {
108                 case GOTOREF:
109                         lv_->getLyXFunc()->dispatch(LFUN_REF_GOTO, dialog_->referenceED->text().latin1());
110                         gotowhere=GOTOBACK;
111                         dialog_->gotoPB->setText(_("&Go back"));
112                         break;
113                 case GOTOBACK:
114                         //FIXME lv_->getLyXFunc()->dispatch(LFUN_REF_BACK);
115                         gotowhere=GOTOREF;
116                         dialog_->gotoPB->setText(_("&Goto reference"));
117                         break;
118                 }
119 }
120
121  
122 void QRef::updateRefs()
123 {
124         // list will be re-done, should go back if necessary
125         if (gotowhere==GOTOBACK) {
126                 //FIXME lv_->getLyXFunc()->dispatch(LFUN_REF_BACK);
127                 gotowhere = GOTOREF;
128                 dialog_->gotoPB->setText(_("&Goto reference"));
129         }
130
131         dialog_->refsLB->setAutoUpdate(false);
132
133         // need this because Qt will send a highlight() here for
134         // the first item inserted
135         string tmp(dialog_->referenceED->text());
136
137         for (std::vector< string >::const_iterator iter = refs.begin();
138                 iter != refs.end(); ++iter) {
139                 if (sort)
140                         dialog_->refsLB->inSort(iter->c_str());
141                 else
142                         dialog_->refsLB->insertItem(iter->c_str());
143         }
144
145         dialog_->referenceED->setText(tmp.c_str());
146
147         for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
148                 if (!strcmp(dialog_->referenceED->text(),dialog_->refsLB->text(i)))
149                         dialog_->refsLB->setCurrentItem(i);
150         }
151
152         dialog_->refsLB->setAutoUpdate(true);
153         dialog_->refsLB->update();
154 }
155
156  
157 void QRef::do_ref_update()
158 {
159         refs.clear();
160         dialog_->refsLB->clear();
161         refs = lv_->buffer()->getLabelList();
162         if (!refs.empty())
163                 dialog_->sortCB->setEnabled(true);
164         updateRefs();
165 }
166  
167
168 void QRef::update()
169 {
170         dialog_->referenceED->setText(params.getContents().c_str());
171         dialog_->nameED->setText(params.getOptions().c_str());
172
173         if (params.getCmdName()=="pageref") {
174                 type = PAGEREF;
175                 dialog_->typeCO->setCurrentItem(1);
176         } else if (params.getCmdName()=="vref") {
177                 type = VREF;
178                 dialog_->typeCO->setCurrentItem(2);
179         } else if (params.getCmdName()=="vpageref") {
180                 type = VPAGEREF;
181                 dialog_->typeCO->setCurrentItem(3);
182         } else if (params.getCmdName()=="prettyref") {
183                 type = PRETTYREF;
184                 dialog_->typeCO->setCurrentItem(4);
185         } else {
186                 type = REF;
187                 dialog_->typeCO->setCurrentItem(0);
188         }
189
190         dialog_->gotoPB->setText(_("&Goto reference"));
191
192         gotowhere = GOTOREF;
193
194         dialog_->sortCB->setChecked(sort);
195
196         do_ref_update();
197
198         dialog_->gotoPB->setEnabled(params.getContents()!="");
199         dialog_->okPB->setEnabled(params.getContents()!="");
200
201         dialog_->typeCO->setEnabled(!readonly);
202         dialog_->sortCB->setEnabled(!readonly);
203         dialog_->refsLB->setEnabled(!readonly);
204         dialog_->okPB->setEnabled(!readonly);
205         dialog_->updatePB->setEnabled(!readonly);
206         if (readonly)
207                 dialog_->cancelPB->setText(_("&Close"));
208         else
209                 dialog_->cancelPB->setText(_("&Cancel"));
210 }
211
212  
213 void QRef::apply()
214 {
215         if (readonly)
216                 return;
217
218         if (!lv_->view()->available())
219                 return;
220
221         switch (dialog_->typeCO->currentItem()) {
222                 case 0:
223                         params.setCmdName("ref");
224                         break;
225                 case 1:
226                         params.setCmdName("pageref");
227                         break;
228                 case 2:
229                         params.setCmdName("vref");
230                         break;
231                 case 3:
232                         params.setCmdName("vpageref");
233                         break;
234                 case 4:
235                         params.setCmdName("prettyref");
236                         break;
237                 default:
238                         lyxerr[Debug::GUI] << "Unknown Ref Type" << endl;
239         }
240
241         params.setContents(dialog_->referenceED->text().latin1());
242         params.setOptions(dialog_->nameED->text().latin1());
243
244         if (inset_ != 0) {
245                 if (params != inset_->params()) {
246                         inset_->setParams(params);
247                         lv_->view()->updateInset(inset_, true);
248                 }
249         } else
250                 lv_->getLyXFunc()->dispatch(LFUN_REF_INSERT, params.getAsString().c_str());
251 }
252
253  
254 void QRef::show()
255 {
256         if (!dialog_)
257                 dialog_ = new QRefDialog(this, 0, _("LyX: Cross Reference"), false);
258
259         if (!dialog_->isVisible()) {
260                 h_ = d_->hideBufferDependent.connect(slot(this, &QRef::hide));
261                 //u_ = d_->updateBufferDependent.connect(slot(this, &QRef::update));
262         }
263
264         dialog_->raise();
265         dialog_->setActiveWindow();
266
267         update();
268         dialog_->show();
269 }
270
271  
272 void QRef::close()
273 {
274         h_.disconnect();
275         u_.disconnect();
276         ih_.disconnect();
277         inset_ = 0;
278 }
279
280  
281 void QRef::hide()
282 {
283         dialog_->hide();
284         close();
285 }