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
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "QCitation.h"
17 #include "qt_helpers.h"
19 #include "support/lstrings.h"
20 #include "support/docstring.h"
31 template<typename String> static QStringList to_qstring_list(vector<String> const & v)
35 for (size_t i = 0; i != v.size(); ++i) {
38 qlist.append(lyx::toqstr(v[i]));
44 static vector<lyx::docstring> const to_docstring_vector(QStringList const & qlist)
46 vector<lyx::docstring> v;
47 for (int i = 0; i != qlist.size(); ++i) {
48 if (qlist[i].isEmpty())
50 v.push_back(lyx::qstring_to_ucs4(qlist[i]));
60 QCitation::QCitation(Dialog & parent)
61 : ControlCitation(parent)
66 void QCitation::apply(int const choice, bool const full, bool const force,
67 QString before, QString after)
69 if (cited_keys_.isEmpty())
72 vector<biblio::CiteStyle> const & styles =
73 ControlCitation::getCiteStyles();
75 string const command =
76 biblio::CitationStyle(styles[choice], full, force)
79 params().setCmdName(command);
80 params()["key"] = qstring_to_ucs4(cited_keys_.join(","));
81 params()["before"] = qstring_to_ucs4(before);
82 params()["after"] = qstring_to_ucs4(after);
87 void QCitation::clearSelection()
90 selected_model_.setStringList(cited_keys_);
94 QString QCitation::textBefore()
96 return toqstr(params()["before"]);
100 QString QCitation::textAfter()
102 return toqstr(params()["after"]);
106 bool QCitation::initialiseParams(std::string const & data)
108 if (!ControlCitation::initialiseParams(data))
115 void QCitation::init()
117 // Make the list of all available bibliography keys
118 all_keys_ = to_qstring_list(availableKeys());
119 available_model_.setStringList(all_keys_);
121 // Ditto for the keys cited in this inset
122 QString str = toqstr(params()["key"]);
126 cited_keys_ = str.split(",");
127 selected_model_.setStringList(cited_keys_);
131 void QCitation::findKey(QString const & str, bool only_keys,
132 docstring field, docstring entryType,
133 bool case_sensitive, bool reg_exp, bool reset)
135 // Used for optimisation: store last searched string.
136 static QString last_searched_string;
137 // Used to disable the above optimisation.
138 static bool last_case_sensitive;
139 static bool last_reg_exp;
140 // Reset last_searched_string in case of changed option.
141 if (last_case_sensitive != case_sensitive
142 || last_reg_exp != reg_exp) {
143 LYXERR(Debug::GUI) << "QCitation::findKey: optimisation disabled!" << std::endl;
144 last_searched_string.clear();
146 // save option for next search.
147 last_case_sensitive = case_sensitive;
148 last_reg_exp = reg_exp;
150 Qt::CaseSensitivity qtcase = case_sensitive?
151 Qt::CaseSensitive: Qt::CaseInsensitive;
153 // If new string (str) contains the last searched one...
155 !last_searched_string.isEmpty() &&
157 str.contains(last_searched_string, qtcase))
158 // ... then only search within already found list.
159 keys = available_model_.stringList();
161 // ... else search all keys.
163 // save searched string for next search.
164 last_searched_string = str;
168 //First, filter by entryType, which will be faster than
169 //what follows, so we may get to do that on less.
170 vector<docstring> keyVector = to_docstring_vector(keys);
171 filterByEntryType(keyVector, entryType);
174 result = to_qstring_list(keyVector);
176 result = to_qstring_list(searchKeys(keyVector, only_keys,
177 qstring_to_ucs4(str), field, case_sensitive, reg_exp));
179 available_model_.setStringList(result);
183 QStringList QCitation::getFieldsAsQStringList() {
184 return to_qstring_list(availableFields());
188 QStringList QCitation::getEntriesAsQStringList() {
189 return to_qstring_list(availableEntries());
193 void QCitation::addKey(QModelIndex const & index)
195 cited_keys_.append(index.data().toString());
196 selected_model_.setStringList(cited_keys_);
200 void QCitation::deleteKey(QModelIndex const & index)
202 cited_keys_.removeAt(index.row());
203 selected_model_.setStringList(cited_keys_);
207 void QCitation::upKey(QModelIndex const & index)
209 int pos = index.row();
210 cited_keys_.swap(pos, pos - 1);
211 selected_model_.setStringList(cited_keys_);
215 void QCitation::downKey(QModelIndex const & index)
217 int pos = index.row();
218 cited_keys_.swap(pos, pos + 1);
219 selected_model_.setStringList(cited_keys_);
223 QStringList QCitation::citationStyles(int sel)
225 docstring const key = qstring_to_ucs4(cited_keys_[sel]);
226 return to_qstring_list(getCiteStrings(key));
230 QString QCitation::getKeyInfo(QString const & sel)
232 return toqstr(getInfo(qstring_to_ucs4(sel)));
236 } // namespace frontend