3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Kalle Dalheimer
9 * Full author contact details are available in file CREDITS.
14 #include "ControlCitation.h"
15 #include "QCitation.h"
17 #include "qt_helpers.h"
19 #include "bufferparams.h"
21 #include "controllers/ButtonController.h"
22 #include "controllers/ControlCitation.h"
24 #include "support/lstrings.h"
36 QStringList toQStringList(vector<string> const & v)
40 for (size_t i=0; i != v.size(); ++i) {
43 qlist.append(toqstr(v[i]));
49 void toVector(vector<string> & v, const QStringList & qlist)
53 for (size_t i=0; i != qlist.size(); ++i)
54 v.push_back(fromqstr(qlist[i]));
62 QCitation::QCitation(Dialog & parent)
63 : ControlCitation(parent)
68 void QCitation::apply(int const choice, bool const full, bool const force,
69 QString before, QString after)
71 // InsetCommandParams & params = params();
73 vector<biblio::CiteStyle> const & styles =
74 ControlCitation::getCiteStyles();
76 string const command =
77 biblio::CitationStyle(styles[choice], full, force)
80 params().setContents(fromqstr(selected_keys_.stringList().join(",")));
81 params().setSecOptions(fromqstr(before));
82 params().setOptions(fromqstr(after));
86 if (dialog().controller().isBufferDependent()) {
87 if (!dialog().kernel().isBufferAvailable() ||
88 dialog().kernel().isBufferReadonly())
91 dialog().controller().dispatchParams();
93 if (dialog().controller().disconnectOnApply()) {
94 dialog().kernel().disconnect(name());
95 dialog().controller().initialiseParams(string());
96 dialog().view().update();
102 QString QCitation::textBefore()
104 return toqstr(params().getSecOptions());
108 QString QCitation::textAfter()
110 return toqstr(params().getOptions());
114 void QCitation::updateModel()
116 // Make the list of all available bibliography keys
117 QStringList keys = toQStringList(biblio::getKeys(bibkeysInfo()));
118 available_keys_.setStringList(keys);
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);
129 bool QCitation::isValid()
131 return selected_keys_.rowCount() > 0;
135 QModelIndex QCitation::findKey(QString const & str, QModelIndex const & index) const
137 QStringList const avail = available_keys_.stringList();
138 int const pos = avail.indexOf(str, index.row());
141 return available_keys_.index(pos);
145 QModelIndex QCitation::findKey(QString const & str) const
147 cout << "Find text " << fromqstr(str) << endl;
149 QStringList const avail = available_keys_.stringList();
150 QRegExp reg_exp(str);
152 int const pos = avail.indexOf(reg_exp);
154 return QModelIndex();
156 cout << "found key " << fromqstr(avail[pos]) << " at pos " << pos << endl;
157 return available_keys_.index(pos);
161 void QCitation::addKeys(QModelIndexList const & indexes)
168 QStringList keys = selected_keys_.stringList();
170 foreach(index, indexes) {
171 if (keys.indexOf(index.data().toString()) == -1)
172 keys.append(index.data().toString());
175 selected_keys_.setStringList(keys);
179 void QCitation::deleteKeys(QModelIndexList const & indexes)
186 QStringList keys = selected_keys_.stringList();
188 foreach(index, indexes) {
189 int const pos = keys.indexOf(index.data().toString());
194 selected_keys_.setStringList(keys);
198 void QCitation::upKey(QModelIndexList const & indexes)
200 if (indexes.empty() || indexes.size() > 1)
203 int pos = indexes[0].row();
207 QStringList keys = selected_keys_.stringList();
208 keys.swap(pos, pos-1);
209 selected_keys_.setStringList(keys);
213 void QCitation::downKey(QModelIndexList const & indexes)
215 if (indexes.empty() || indexes.size() > 1)
218 int pos = indexes[0].row();
219 if (pos >= selected_keys_.rowCount() - 1)
222 QStringList keys = selected_keys_.stringList();
223 keys.swap(pos, pos+1);
224 selected_keys_.setStringList(keys);
228 QStringList QCitation::citationStyles(int sel)
230 string key = fromqstr(selected_keys_.stringList()[sel]);
232 return toQStringList(getCiteStrings(key));
235 } // namespace frontend