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