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