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"
30 template<typename String> static QStringList to_qstring_list(vector<String> const & v)
34 for (size_t i = 0; i != v.size(); ++i) {
37 qlist.append(lyx::toqstr(v[i]));
43 static vector<string> const to_string_vector(QStringList const & qlist)
46 for (int i = 0; i != qlist.size(); ++i) {
47 if (qlist[i].isEmpty())
49 v.push_back(lyx::fromqstr(qlist[i]));
59 QCitation::QCitation(Dialog & parent)
60 : ControlCitation(parent)
65 void QCitation::apply(int const choice, bool const full, bool const force,
66 QString before, QString after)
68 if (cited_keys_.isEmpty())
71 vector<biblio::CiteStyle> const & styles =
72 ControlCitation::getCiteStyles();
74 string const command =
75 biblio::CitationStyle(styles[choice], full, force)
78 params().setCmdName(command);
79 params()["key"] = qstring_to_ucs4(cited_keys_.join(","));
80 params()["before"] = qstring_to_ucs4(before);
81 params()["after"] = qstring_to_ucs4(after);
86 void QCitation::clearSelection()
89 selected_model_.setStringList(cited_keys_);
93 QString QCitation::textBefore()
95 return toqstr(params()["before"]);
99 QString QCitation::textAfter()
101 return toqstr(params()["after"]);
105 bool QCitation::initialiseParams(std::string const & data)
107 if (!ControlCitation::initialiseParams(data))
114 void QCitation::init()
116 // Make the list of all available bibliography keys
117 all_keys_ = to_qstring_list(availableKeys());
118 available_model_.setStringList(all_keys_);
120 // Ditto for the keys cited in this inset
121 QString str = toqstr(params()["key"]);
125 cited_keys_ = str.split(",");
127 selected_model_.setStringList(cited_keys_);
131 void QCitation::findKey(QString const & str, bool only_keys,
132 bool case_sensitive, bool reg_exp)
135 available_model_.setStringList(all_keys_);
139 // Used for optimisation: store last searched string.
140 static QString last_searched_string;
141 // Used to disable the above optimisation.
142 static bool last_case_sensitive;
143 static bool last_reg_exp;
144 // Reset last_searched_string in case of changed option.
145 if (last_case_sensitive != case_sensitive
146 || last_reg_exp != reg_exp) {
147 LYXERR(Debug::GUI) << "QCitation::findKey: optimisation disabled!" << std::endl;
148 last_searched_string.clear();
150 // save option for next search.
151 last_case_sensitive = case_sensitive;
152 last_reg_exp = reg_exp;
154 Qt::CaseSensitivity qtcase = case_sensitive?
155 Qt::CaseSensitive: Qt::CaseInsensitive;
157 // If new string (str) contains the last searched one...
158 if (!last_searched_string.isEmpty() && str.size() > 1
159 && str.contains(last_searched_string, qtcase))
160 // ... then only search within already found list.
161 keys = available_model_.stringList();
163 // ... else search all keys.
165 // save searched string for next search.
166 last_searched_string = str;
170 // Search only within the matching keys:
171 result = keys.filter(str, qtcase);
173 // Search within matching keys and associated info.
174 result = to_qstring_list(searchKeys(to_string_vector(keys),
175 qstring_to_ucs4(str), case_sensitive, reg_exp));
177 available_model_.setStringList(result);
181 void QCitation::addKey(QModelIndex const & index)
183 cited_keys_.append(index.data().toString());
184 selected_model_.setStringList(cited_keys_);
188 void QCitation::deleteKey(QModelIndex const & index)
190 cited_keys_.removeAt(index.row());
191 selected_model_.setStringList(cited_keys_);
195 void QCitation::upKey(QModelIndex const & index)
197 int pos = index.row();
198 cited_keys_.swap(pos, pos - 1);
199 selected_model_.setStringList(cited_keys_);
203 void QCitation::downKey(QModelIndex const & index)
205 int pos = index.row();
206 cited_keys_.swap(pos, pos + 1);
207 selected_model_.setStringList(cited_keys_);
211 QStringList QCitation::citationStyles(int sel)
213 string const key = fromqstr(cited_keys_[sel]);
214 return to_qstring_list(getCiteStrings(key));
218 QString QCitation::getKeyInfo(QString const & sel)
220 return toqstr(getInfo(fromqstr(sel)));
224 } // namespace frontend