]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QRef.C
Remove quite a few compiler warnings:
[lyx.git] / src / frontends / qt4 / 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  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QRef.h"
15 #include "QRefDialog.h"
16 #include "Qt2BC.h"
17 #include "qt_helpers.h"
18
19 #include "controllers/ButtonController.h"
20 #include "controllers/ControlRef.h"
21
22 #include "insets/insetref.h"
23
24 #include <QLineEdit>
25 #include <QCheckBox>
26 #include <QListWidget>
27 #include <QListWidgetItem>
28 #include <QPushButton>
29 #include <QToolTip>
30
31
32 using std::vector;
33 using std::string;
34
35 namespace lyx {
36 namespace frontend {
37
38 // full qualification because qt4 has also a ControlRef type
39 typedef QController<lyx::frontend::ControlRef, QView<QRefDialog> > base_class;
40
41
42 QRef::QRef(Dialog & parent)
43         : base_class(parent, _("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_->refsLW);
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         restored_buffer_ = -1;
64 }
65
66
67 void QRef::update_contents()
68 {
69         InsetCommandParams const & params = controller().params();
70
71         int orig_type = dialog_->typeCO->currentItem();
72
73         dialog_->referenceED->setText(toqstr(params.getContents()));
74
75         dialog_->nameED->setText(toqstr(params.getOptions()));
76         dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
77
78         // restore type settings for new insets
79         if (params.getContents().empty())
80                 dialog_->typeCO->setCurrentItem(orig_type);
81         else
82                 dialog_->typeCO->setCurrentItem(InsetRef::getType(params.getCmdName()));
83         dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
84         if (!typeAllowed())
85                 dialog_->typeCO->setCurrentItem(0);
86
87         dialog_->sortCB->setChecked(sort_);
88
89         // insert buffer list
90         dialog_->bufferCO->clear();
91         vector<string> const buffers = controller().getBufferList();
92         for (vector<string>::const_iterator it = buffers.begin();
93                 it != buffers.end(); ++it) {
94                 dialog_->bufferCO->insertItem(toqstr(*it));
95         }
96         // restore the buffer combo setting for new insets
97         if (params.getContents().empty() && restored_buffer_ != -1
98         && restored_buffer_ < dialog_->bufferCO->count())
99                 dialog_->bufferCO->setCurrentItem(restored_buffer_);
100         else
101                 dialog_->bufferCO->setCurrentItem(controller().getBufferNum());
102
103         updateRefs();
104         bc().valid(false);
105 }
106
107
108 void QRef::apply()
109 {
110         InsetCommandParams & params = controller().params();
111
112         params.setCmdName(InsetRef::getName(dialog_->typeCO->currentItem()));
113         params.setContents(fromqstr(dialog_->referenceED->text()));
114         params.setOptions(fromqstr(dialog_->nameED->text()));
115
116         restored_buffer_ = dialog_->bufferCO->currentItem();
117 }
118
119
120 bool QRef::nameAllowed()
121 {
122         Kernel::DocType const doc_type = kernel().docType();
123         return doc_type != Kernel::LATEX &&
124                 doc_type != Kernel::LITERATE;
125 }
126
127
128 bool QRef::typeAllowed()
129 {
130         Kernel::DocType const doc_type = kernel().docType();
131         return doc_type != Kernel::LINUXDOC &&
132                 doc_type != Kernel::DOCBOOK;
133 }
134
135
136 void QRef::setGoBack()
137 {
138         dialog_->gotoPB->setText(qt_("&Go Back"));
139         QToolTip::remove(dialog_->gotoPB);
140         QToolTip::add(dialog_->gotoPB, qt_("Jump back"));
141 }
142
143
144 void QRef::setGotoRef()
145 {
146         dialog_->gotoPB->setText(qt_("&Go to Label"));
147         QToolTip::remove(dialog_->gotoPB);
148         QToolTip::add(dialog_->gotoPB, qt_("Jump to label"));
149 }
150
151
152 void QRef::gotoRef()
153 {
154         string ref(fromqstr(dialog_->referenceED->text()));
155
156         if (at_ref_) {
157                 // go back
158                 setGotoRef();
159                 controller().gotoBookmark();
160         } else {
161                 // go to the ref
162                 setGoBack();
163                 controller().gotoRef(ref);
164         }
165         at_ref_ = !at_ref_;
166 }
167
168
169 void QRef::redoRefs()
170 {
171         // Prevent these widgets from emitting any signals whilst
172         // we modify their state.
173         dialog_->refsLW->blockSignals(true);
174         dialog_->referenceED->blockSignals(true);
175
176         int lastref = dialog_->refsLW->currentRow();
177
178         dialog_->refsLW->setUpdatesEnabled(false);
179         dialog_->refsLW->clear();
180
181         // need this because Qt will send a highlight() here for
182         // the first item inserted
183         QString const tmp(dialog_->referenceED->text());
184
185         for (std::vector<string>::const_iterator iter = refs_.begin();
186                 iter != refs_.end(); ++iter) {
187                 dialog_->refsLW->addItem(toqstr(*iter));
188         }
189
190         if (sort_)
191                 dialog_->refsLW->sortItems();
192
193         dialog_->referenceED->setText(tmp);
194
195         // restore the last selection for new insets
196         if (tmp.isEmpty() && lastref != -1
197                 && lastref < int(dialog_->refsLW->count())) {
198                 dialog_->refsLW->setCurrentRow(lastref);
199                 dialog_->refsLW->clearSelection();
200         } else
201                 for (int i = 0; i < dialog_->refsLW->count(); ++i) {
202                         QListWidgetItem * item = dialog_->refsLW->item(i);
203                         if (tmp == item->text()) {
204                                 dialog_->refsLW->setItemSelected(item, true);
205                         }
206                 }
207
208         dialog_->refsLW->setUpdatesEnabled(true);
209         dialog_->refsLW->update();
210
211         // Re-activate the emission of signals by these widgets.
212         dialog_->refsLW->blockSignals(false);
213         dialog_->referenceED->blockSignals(false);
214 }
215
216
217 void QRef::updateRefs()
218 {
219         refs_.clear();
220         if (at_ref_)
221                 gotoRef();
222         string const name = controller().getBufferName(dialog_->bufferCO->currentItem());
223         refs_ = controller().getLabelList(name);
224         dialog_->sortCB->setEnabled(!refs_.empty());
225         dialog_->refsLW->setEnabled(!refs_.empty());
226         dialog_->gotoPB->setEnabled(!refs_.empty());
227         redoRefs();
228 }
229
230 bool QRef::isValid()
231 {
232         return !dialog_->referenceED->text().isEmpty();
233 }
234
235 } // namespace frontend
236 } // namespace lyx