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