]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QRef.C
some more random changes, added Timeout (make clean if LyX crashes !!)
[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         // FIXME: needs updating
171  
172         dialog_->referenceED->setText(params.getContents().c_str());
173         dialog_->nameED->setText(params.getOptions().c_str());
174
175         if (params.getCmdName()=="pageref") {
176                 type = PAGEREF;
177                 dialog_->typeCO->setCurrentItem(1);
178         } else if (params.getCmdName()=="vref") {
179                 type = VREF;
180                 dialog_->typeCO->setCurrentItem(2);
181         } else if (params.getCmdName()=="vpageref") {
182                 type = VPAGEREF;
183                 dialog_->typeCO->setCurrentItem(3);
184         } else if (params.getCmdName()=="prettyref") {
185                 type = PRETTYREF;
186                 dialog_->typeCO->setCurrentItem(4);
187         } else {
188                 type = REF;
189                 dialog_->typeCO->setCurrentItem(0);
190         }
191
192         dialog_->gotoPB->setText(_("&Goto reference"));
193
194         gotowhere = GOTOREF;
195
196         dialog_->sortCB->setChecked(sort);
197
198         do_ref_update();
199
200         dialog_->gotoPB->setEnabled(params.getContents()!="");
201         dialog_->okPB->setEnabled(params.getContents()!="");
202
203         dialog_->typeCO->setEnabled(!readonly);
204         dialog_->sortCB->setEnabled(!readonly);
205         dialog_->refsLB->setEnabled(!readonly);
206         dialog_->okPB->setEnabled(!readonly);
207         dialog_->updatePB->setEnabled(!readonly);
208         if (readonly)
209                 dialog_->cancelPB->setText(_("&Close"));
210         else
211                 dialog_->cancelPB->setText(_("&Cancel"));
212 }
213
214  
215 void QRef::apply()
216 {
217         if (readonly)
218                 return;
219
220         if (!lv_->view()->available())
221                 return;
222
223         switch (dialog_->typeCO->currentItem()) {
224                 case 0:
225                         params.setCmdName("ref");
226                         break;
227                 case 1:
228                         params.setCmdName("pageref");
229                         break;
230                 case 2:
231                         params.setCmdName("vref");
232                         break;
233                 case 3:
234                         params.setCmdName("vpageref");
235                         break;
236                 case 4:
237                         params.setCmdName("prettyref");
238                         break;
239                 default:
240                         lyxerr[Debug::GUI] << "Unknown Ref Type" << endl;
241         }
242
243         params.setContents(dialog_->referenceED->text().latin1());
244         params.setOptions(dialog_->nameED->text().latin1());
245
246         if (inset_ != 0) {
247                 if (params != inset_->params()) {
248                         inset_->setParams(params);
249                         lv_->view()->updateInset(inset_, true);
250                 }
251         } else
252                 lv_->getLyXFunc()->dispatch(LFUN_REF_INSERT, params.getAsString().c_str());
253 }
254
255  
256 void QRef::show()
257 {
258         if (!dialog_)
259                 dialog_ = new QRefDialog(this, 0, _("LyX: Cross Reference"), false);
260
261         if (!dialog_->isVisible()) {
262                 h_ = d_->hideBufferDependent.connect(slot(this, &QRef::hide));
263                 //u_ = d_->updateBufferDependent.connect(slot(this, &QRef::update));
264         }
265
266         dialog_->raise();
267         dialog_->setActiveWindow();
268
269         update();
270         dialog_->show();
271 }
272
273  
274 void QRef::close()
275 {
276         h_.disconnect();
277         u_.disconnect();
278         ih_.disconnect();
279         inset_ = 0;
280 }
281
282  
283 void QRef::hide()
284 {
285         dialog_->hide();
286         close();
287 }