]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCitation.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[lyx.git] / src / frontends / qt4 / QCitation.C
1 /**
2  * \file QCitation.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Kalle Dalheimer
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ControlCitation.h"
15 #include "QCitation.h"
16 #include "Qt2BC.h"
17 #include "qt_helpers.h"
18
19 #include "bufferparams.h"
20
21 #include "controllers/ButtonController.h"
22 #include "controllers/ControlCitation.h"
23
24 #include "support/lstrings.h"
25
26 #include <vector>
27 #include <string>
28 #include <iostream>
29 using std::cout;
30 using std::endl;
31
32 using std::vector;
33 using std::string;
34
35
36 QStringList toQStringList(vector<string> const & v)
37 {
38         QStringList qlist;
39
40         for (size_t i=0; i != v.size(); ++i) {
41                 if (v[i].empty())
42                         continue;
43                 qlist.append(toqstr(v[i]));
44         }
45         return qlist;
46 }
47
48
49 void toVector(vector<string> & v, const QStringList & qlist)
50 {
51         v.clear();
52
53         for (size_t i=0; i != qlist.size(); ++i)
54                 v.push_back(fromqstr(qlist[i]));
55 }
56
57
58 namespace lyx {
59 namespace frontend {
60
61
62 QCitation::QCitation(Dialog & parent)
63         : ControlCitation(parent)
64 {
65 }
66
67
68 void QCitation::apply(int const choice, bool const full, bool const force,
69                                           QString before, QString after)
70 {
71 //      InsetCommandParams & params = params();
72
73         vector<biblio::CiteStyle> const & styles =
74                 ControlCitation::getCiteStyles();
75
76         string const command =
77                 biblio::CitationStyle(styles[choice], full, force)
78                 .asLatexStr();
79
80         params().setContents(fromqstr(selected_keys_.stringList().join(",")));
81         params().setSecOptions(fromqstr(before));
82         params().setOptions(fromqstr(after));
83         dispatchParams();
84
85 /*
86         if (dialog().controller().isBufferDependent()) {
87                 if (!dialog().kernel().isBufferAvailable() ||
88                     dialog().kernel().isBufferReadonly())
89                         return;
90         }
91         dialog().controller().dispatchParams();
92
93         if (dialog().controller().disconnectOnApply()) {
94                 dialog().kernel().disconnect(name());
95                 dialog().controller().initialiseParams(string());
96                 dialog().view().update();
97         }
98 */
99 }
100
101
102 QString QCitation::textBefore()
103 {
104         return toqstr(params().getSecOptions());
105 }
106
107
108 QString QCitation::textAfter()
109 {
110         return toqstr(params().getOptions());
111 }
112
113
114 void QCitation::updateModel()
115 {
116         // Make the list of all available bibliography keys
117         QStringList keys = toQStringList(biblio::getKeys(bibkeysInfo()));
118         available_keys_.setStringList(keys);
119
120         // Ditto for the keys cited in this inset
121         QString str = toqstr(params().getContents());
122         if (!str.isEmpty()) {
123                 keys = str.split(",");
124                 selected_keys_.setStringList(keys);
125         }
126 }
127
128
129 bool QCitation::isValid()
130 {
131         return selected_keys_.rowCount() > 0;
132 }
133
134
135 QModelIndex QCitation::findKey(QString const & str, QModelIndex const & index) const
136 {
137         QStringList const avail = available_keys_.stringList();
138         int const pos = avail.indexOf(str, index.row());
139         if (pos == -1)
140                 return index;
141         return available_keys_.index(pos);
142 }
143
144
145 QModelIndex QCitation::findKey(QString const & str) const
146 {
147         cout << "Find text " << fromqstr(str) << endl;
148
149         QStringList const avail = available_keys_.stringList();
150         QRegExp reg_exp(str);
151
152         int const pos = avail.indexOf(reg_exp);
153         if (pos == -1)
154                 return QModelIndex();
155
156         cout << "found key " << fromqstr(avail[pos]) << " at pos " << pos << endl;
157         return available_keys_.index(pos);
158 }
159
160
161 void QCitation::addKeys(QModelIndexList const & indexes)
162 {
163         QModelIndex index;
164
165         if (indexes.empty())
166                 return;
167
168         QStringList keys = selected_keys_.stringList();
169
170         foreach(index, indexes) {
171                 if (keys.indexOf(index.data().toString()) == -1)
172                         keys.append(index.data().toString());
173         }
174
175         selected_keys_.setStringList(keys);
176 }
177
178
179 void QCitation::deleteKeys(QModelIndexList const & indexes)
180 {
181         QModelIndex index;
182
183         if (indexes.empty())
184                 return;
185
186         QStringList keys = selected_keys_.stringList();
187
188         foreach(index, indexes) {
189                 int const pos = keys.indexOf(index.data().toString());
190                 if (pos != -1)
191                         keys.removeAt(pos);
192         }
193
194         selected_keys_.setStringList(keys);
195 }
196
197
198 void QCitation::upKey(QModelIndexList const & indexes)
199 {
200         if (indexes.empty() || indexes.size() > 1)
201                 return;
202
203         int pos = indexes[0].row();
204         if (pos < 1)
205                 return;
206
207         QStringList keys = selected_keys_.stringList();
208         keys.swap(pos, pos-1);
209         selected_keys_.setStringList(keys);
210 }
211
212
213 void QCitation::downKey(QModelIndexList const & indexes)
214 {
215         if (indexes.empty() || indexes.size() > 1)
216                 return;
217
218         int pos = indexes[0].row();
219         if (pos >= selected_keys_.rowCount() - 1)
220                 return;
221
222         QStringList keys = selected_keys_.stringList();
223         keys.swap(pos, pos+1);
224         selected_keys_.setStringList(keys);
225 }
226
227
228 QStringList QCitation::citationStyles(int sel)
229 {
230         string key = fromqstr(selected_keys_.stringList()[sel]);
231
232         return toQStringList(getCiteStrings(key));
233 }
234
235 } // namespace frontend
236 } // namespace lyx