]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCitation.C
Whitespace cleanup.
[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 "QCitation.h"
15 #include "QCitationDialog.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 void toQStringList(QStringList & qlist, vector<string> const & v)
36 {
37         qlist.clear();
38         for (size_t i=0; i != v.size(); ++i) {
39                 if (v[i].empty())
40                         continue;
41                 qlist.append(toqstr(v[i]));
42         }
43 }
44
45 void toVector(vector<string> & v, const QStringList & qlist)
46 {
47         v.clear();
48
49         for (size_t i=0; i != qlist.size(); ++i)
50                 v.push_back(fromqstr(qlist[i]));
51 }
52
53 namespace lyx {
54 namespace frontend {
55
56 typedef QController<ControlCitation, QView<QCitationDialog> > base_class;
57
58
59 QCitation::QCitation(Dialog & parent)
60         : base_class(parent, _("Citation"))
61 {
62 }
63
64
65 void QCitation::apply()
66 {
67         InsetCommandParams & params = controller().params();
68         dialog_->apply(params);
69
70         params.setContents(fromqstr(selected_keys_.stringList().join("'")));
71 /*
72         if (dialog().controller().isBufferDependent()) {
73                 if (!dialog().kernel().isBufferAvailable() ||
74                     dialog().kernel().isBufferReadonly())
75                         return;
76         }
77
78         dialog().view().apply();
79         dialog().controller().dispatchParams();
80
81         if (dialog().controller().disconnectOnApply()) {
82                 dialog().kernel().disconnect(name());
83                 dialog().controller().initialiseParams(string());
84                 dialog().view().update();
85         }
86 */
87 //      dialog().ApplyButton();
88 //      dialog().apply();
89 }
90
91
92 void QCitation::build_dialog()
93 {
94         dialog_.reset(new QCitationDialog(this));
95 }
96
97
98 void QCitation::update_contents()
99 {
100         QStringList keys;
101
102         // Make the list of all available bibliography keys
103         toQStringList(keys,
104                 biblio::getKeys(controller().bibkeysInfo()));
105         available_keys_.setStringList(keys);
106
107         // Ditto for the keys cited in this inset
108         QString str = toqstr(controller().params().getContents());
109         if (!str.isEmpty()) {
110                 keys = str.split(",");
111                 selected_keys_.setStringList(keys);
112         }
113
114         dialog_->update(controller().params());
115
116         bc().valid(isValid());
117 }
118
119 void QCitation::hide()
120 {
121         QDialogView::hide();
122 }
123
124 bool QCitation::isValid()
125 {
126         return selected_keys_.rowCount() > 0;
127 }
128
129 QModelIndex QCitation::findKey(QString const & str, QModelIndex const & index) const
130 {
131         QStringList const avail = available_keys_.stringList();
132         int const pos = avail.indexOf(str, index.row());
133         if (pos == -1)
134                 return index;
135         return available_keys_.index(pos);
136 }
137
138 QModelIndex QCitation::findKey(QString const & str) const
139 {
140         cout << "Find text " << fromqstr(str) << endl;
141
142         QStringList const avail = available_keys_.stringList();
143         QRegExp reg_exp(str);
144
145         int const pos = avail.indexOf(reg_exp);
146         if (pos == -1)
147                 return QModelIndex();
148
149         cout << "found key " << fromqstr(avail[pos]) << " at pos " << pos << endl;
150         return available_keys_.index(pos);
151 }
152
153 void QCitation::addKeys(QModelIndexList const & indexes)
154 {
155         // = selectionModel->selectedIndexes();
156
157         QModelIndex index;
158
159         if (indexes.empty())
160                 return;
161
162         QStringList keys = selected_keys_.stringList();
163
164         foreach(index, indexes) {
165                 if (keys.indexOf(index.data().toString()) == -1)
166                         keys.append(index.data().toString());
167         }
168
169         selected_keys_.setStringList(keys);
170
171         changed();
172 }
173
174 void QCitation::deleteKeys(QModelIndexList const & indexes)
175 {
176         QModelIndex index;
177
178         if (indexes.empty())
179                 return;
180
181         QStringList keys = selected_keys_.stringList();
182
183         foreach(index, indexes) {
184                 int const pos = keys.indexOf(index.data().toString());
185                 if (pos != -1)
186                         keys.removeAt(pos);
187         }
188
189         selected_keys_.setStringList(keys);
190
191         changed();
192 }
193
194 void QCitation::upKey(QModelIndexList const & indexes)
195 {
196         if (indexes.empty() || indexes.size() > 1)
197                 return;
198
199         int pos = indexes[0].row();
200         if (pos < 1)
201                 return;
202
203         QStringList keys = selected_keys_.stringList();
204         keys.swap(pos, pos-1);
205         selected_keys_.setStringList(keys);
206
207         changed();
208 }
209
210 void QCitation::downKey(QModelIndexList const & indexes)
211 {
212         if (indexes.empty() || indexes.size() > 1)
213                 return;
214
215         int pos = indexes[0].row();
216         if (pos >= selected_keys_.rowCount() - 1)
217                 return;
218
219         QStringList keys = selected_keys_.stringList();
220         keys.swap(pos, pos+1);
221         selected_keys_.setStringList(keys);
222
223         changed();
224 }
225
226 } // namespace frontend
227 } // namespace lyx