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 "QCitation.h"
15 #include "QCitationDialog.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"
35 void toQStringList(QStringList & qlist, vector<string> const & v)
38 for (size_t i=0; i != v.size(); ++i) {
41 qlist.append(toqstr(v[i]));
45 void toVector(vector<string> & v, const QStringList & qlist)
49 for (size_t i=0; i != qlist.size(); ++i)
50 v.push_back(fromqstr(qlist[i]));
56 typedef QController<ControlCitation, QView<QCitationDialog> > base_class;
59 QCitation::QCitation(Dialog & parent)
60 : base_class(parent, _("Citation"))
65 void QCitation::apply()
67 InsetCommandParams & params = controller().params();
68 dialog_->apply(params);
70 params.setContents(fromqstr(selected_keys_.stringList().join("'")));
72 if (dialog().controller().isBufferDependent()) {
73 if (!dialog().kernel().isBufferAvailable() ||
74 dialog().kernel().isBufferReadonly())
78 dialog().view().apply();
79 dialog().controller().dispatchParams();
81 if (dialog().controller().disconnectOnApply()) {
82 dialog().kernel().disconnect(name());
83 dialog().controller().initialiseParams(string());
84 dialog().view().update();
87 // dialog().ApplyButton();
92 void QCitation::build_dialog()
94 dialog_.reset(new QCitationDialog(this));
98 void QCitation::update_contents()
102 // Make the list of all available bibliography keys
104 biblio::getKeys(controller().bibkeysInfo()));
105 available_keys_.setStringList(keys);
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);
114 dialog_->update(controller().params());
116 bc().valid(isValid());
119 void QCitation::hide()
124 bool QCitation::isValid()
126 return selected_keys_.rowCount() > 0;
129 QModelIndex QCitation::findKey(QString const & str, QModelIndex const & index) const
131 QStringList const avail = available_keys_.stringList();
132 int const pos = avail.indexOf(str, index.row());
135 return available_keys_.index(pos);
138 QModelIndex QCitation::findKey(QString const & str) const
140 cout << "Find text " << fromqstr(str) << endl;
142 QStringList const avail = available_keys_.stringList();
143 QRegExp reg_exp(str);
145 int const pos = avail.indexOf(reg_exp);
147 return QModelIndex();
149 cout << "found key " << fromqstr(avail[pos]) << " at pos " << pos << endl;
150 return available_keys_.index(pos);
153 void QCitation::addKeys(QModelIndexList const & indexes)
155 // = selectionModel->selectedIndexes();
162 QStringList keys = selected_keys_.stringList();
164 foreach(index, indexes) {
165 if (keys.indexOf(index.data().toString()) == -1)
166 keys.append(index.data().toString());
169 selected_keys_.setStringList(keys);
174 void QCitation::deleteKeys(QModelIndexList const & indexes)
181 QStringList keys = selected_keys_.stringList();
183 foreach(index, indexes) {
184 int const pos = keys.indexOf(index.data().toString());
189 selected_keys_.setStringList(keys);
194 void QCitation::upKey(QModelIndexList const & indexes)
196 if (indexes.empty() || indexes.size() > 1)
199 int pos = indexes[0].row();
203 QStringList keys = selected_keys_.stringList();
204 keys.swap(pos, pos-1);
205 selected_keys_.setStringList(keys);
210 void QCitation::downKey(QModelIndexList const & indexes)
212 if (indexes.empty() || indexes.size() > 1)
215 int pos = indexes[0].row();
216 if (pos >= selected_keys_.rowCount() - 1)
219 QStringList keys = selected_keys_.stringList();
220 keys.swap(pos, pos+1);
221 selected_keys_.setStringList(keys);
226 } // namespace frontend