]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QRef.C
Joao latest bits
[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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "debug.h"
14 #include "ControlRef.h"
15 #include "insets/insetref.h"
16
17 #include "QRef.h"
18 #include "QRefDialog.h"
19 #include "Qt2BC.h"
20
21 #include <qlineedit.h>
22 #include <qcheckbox.h>
23 #include <qlistbox.h>
24 #include <qcombobox.h>
25 #include <qpushbutton.h>
26 #include <qtooltip.h>
27
28 #include "qt_helpers.h"
29
30 using std::vector;
31 using std::string;
32
33
34 typedef QController<ControlRef, QView<QRefDialog> > base_class;
35
36
37 QRef::QRef(Dialog & parent)
38         : base_class(parent, _("LyX: Cross-reference")),
39         sort_(false), at_ref_(false)
40 {
41 }
42
43
44 void QRef::build_dialog()
45 {
46         dialog_.reset(new QRefDialog(this));
47
48         bcview().setOK(dialog_->okPB);
49         bcview().setApply(dialog_->applyPB);
50         bcview().setCancel(dialog_->closePB);
51         bcview().addReadOnly(dialog_->refsLB);
52         bcview().addReadOnly(dialog_->sortCB);
53         bcview().addReadOnly(dialog_->nameED);
54         bcview().addReadOnly(dialog_->referenceED);
55         bcview().addReadOnly(dialog_->typeCO);
56         bcview().addReadOnly(dialog_->bufferCO);
57 }
58
59
60 void QRef::update_contents()
61 {
62         InsetCommandParams const & params = controller().params();
63
64         dialog_->referenceED->setText(toqstr(params.getContents()));
65
66         dialog_->nameED->setText(toqstr(params.getOptions()));
67         dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
68
69         dialog_->typeCO->setCurrentItem(InsetRef::getType(params.getCmdName()));
70         dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
71         if (!typeAllowed())
72                 dialog_->typeCO->setCurrentItem(0);
73
74         dialog_->sortCB->setChecked(sort_);
75
76         // insert buffer list
77         dialog_->bufferCO->clear();
78         vector<string> const buffers = controller().getBufferList();
79         for (vector<string>::const_iterator it = buffers.begin();
80                 it != buffers.end(); ++it) {
81                 dialog_->bufferCO->insertItem(toqstr(*it));
82         }
83         dialog_->bufferCO->setCurrentItem(controller().getBufferNum());
84
85         updateRefs();
86 }
87
88
89 void QRef::apply()
90 {
91         InsetCommandParams & params = controller().params();
92
93         params.setCmdName(InsetRef::getName(dialog_->typeCO->currentItem()));
94         params.setContents(fromqstr(dialog_->referenceED->text()));
95         params.setOptions(fromqstr(dialog_->nameED->text()));
96 }
97
98
99 bool QRef::nameAllowed()
100 {
101         Kernel::DocTypes doc_type = kernel().docType();
102         return doc_type != Kernel::LATEX &&
103                 doc_type != Kernel::LITERATE;
104 }
105
106
107 bool QRef::typeAllowed()
108 {
109         Kernel::DocTypes doc_type = kernel().docType();
110         return doc_type != Kernel::LINUXDOC &&
111                 doc_type != Kernel::DOCBOOK;
112 }
113
114
115 void QRef::setGoBack()
116 {
117         dialog_->gotoPB->setText(qt_("&Go Back"));
118         QToolTip::remove(dialog_->gotoPB);
119         QToolTip::add(dialog_->gotoPB, qt_("Jump back"));
120 }
121
122
123 void QRef::setGotoRef()
124 {
125         dialog_->gotoPB->setText(qt_("&Go to Reference"));
126         QToolTip::remove(dialog_->gotoPB);
127         QToolTip::add(dialog_->gotoPB, qt_("Jump to reference"));
128 }
129
130
131 void QRef::gotoRef()
132 {
133         string ref(fromqstr(dialog_->referenceED->text()));
134
135         if (at_ref_) {
136                 // go back
137                 setGotoRef();
138                 controller().gotoBookmark();
139         } else {
140                 // go to the ref
141                 setGoBack();
142                 controller().gotoRef(ref);
143         }
144         at_ref_ = !at_ref_;
145 }
146
147
148 void QRef::redoRefs()
149 {
150         dialog_->refsLB->setAutoUpdate(false);
151         dialog_->refsLB->clear();
152
153         // need this because Qt will send a highlight() here for
154         // the first item inserted
155         QString const tmp(dialog_->referenceED->text());
156
157         for (std::vector<string>::const_iterator iter = refs_.begin();
158                 iter != refs_.end(); ++iter) {
159                 dialog_->refsLB->insertItem(toqstr(*iter));
160         }
161
162         if (sort_)
163                 dialog_->refsLB->sort();
164
165         dialog_->referenceED->setText(tmp);
166
167         for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
168                 if (tmp == dialog_->refsLB->text(i))
169                         dialog_->refsLB->setCurrentItem(i);
170         }
171
172         dialog_->refsLB->setAutoUpdate(true);
173         dialog_->refsLB->update();
174 }
175
176
177 void QRef::updateRefs()
178 {
179         refs_.clear();
180         if (at_ref_)
181                 gotoRef();
182         string const name = controller().getBufferName(dialog_->bufferCO->currentItem());
183         refs_ = controller().getLabelList(name);
184         dialog_->sortCB->setEnabled(!refs_.empty());
185         dialog_->refsLB->setEnabled(!refs_.empty());
186         redoRefs();
187 }