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"
32 template<typename String> static QStringList to_qstring_list(vector<String> const & v)
36 for (size_t i=0; i != v.size(); ++i) {
39 qlist.append(lyx::toqstr(v[i]));
45 vector<string> const to_string_vector(QStringList const & qlist)
48 for (int i = 0; i != qlist.size(); ++i) {
49 if (qlist[i].isEmpty())
51 v.push_back(lyx::fromqstr(qlist[i]));
63 QCitation::QCitation(Dialog & parent)
64 : ControlCitation(parent)
69 void QCitation::apply(int const choice, bool const full, bool const force,
70 QString before, QString after)
72 if (cited_keys_.isEmpty())
75 vector<biblio::CiteStyle> const & styles =
76 ControlCitation::getCiteStyles();
78 string const command =
79 biblio::CitationStyle(styles[choice], full, force)
82 params().setCmdName(command);
83 params()["key"] = qstring_to_ucs4(cited_keys_.join(","));
84 params()["before"] = qstring_to_ucs4(before);
85 params()["after"] = qstring_to_ucs4(after);
90 void QCitation::clearSelection()
93 selected_model_.setStringList(cited_keys_);
97 QString QCitation::textBefore()
99 return toqstr(params()["before"]);
103 QString QCitation::textAfter()
105 return toqstr(params()["after"]);
109 bool QCitation::initialiseParams(std::string const & data)
111 if (!ControlCitation::initialiseParams(data))
118 void QCitation::init()
120 // Make the list of all available bibliography keys
121 all_keys_ = to_qstring_list(availableKeys());
122 available_model_.setStringList(all_keys_);
124 // Ditto for the keys cited in this inset
125 QString str = toqstr(params()["key"]);
129 cited_keys_ = str.split(",");
131 selected_model_.setStringList(cited_keys_);
135 void QCitation::findKey(QString const & str, bool only_keys,
136 bool case_sensitive, bool reg_exp)
139 available_model_.setStringList(all_keys_);
143 // Used for optimisation: store last searched string.
144 static QString last_searched_string;
145 // Used to disable the above optimisation.
146 static bool last_case_sensitive;
147 static bool last_reg_exp;
148 // Reset last_searched_string in case of changed option.
149 if (last_case_sensitive != case_sensitive
150 || last_reg_exp != reg_exp) {
151 lyxerr[Debug::GUI] << "QCitation::findKey: optimisation disabled!" << std::endl;
152 last_searched_string.clear();
154 // save option for next search.
155 last_case_sensitive = case_sensitive;
156 last_reg_exp = reg_exp;
158 Qt::CaseSensitivity qtcase = case_sensitive?
159 Qt::CaseSensitive: Qt::CaseInsensitive;
161 // If new string (str) contains the last searched one...
162 if (!last_searched_string.isEmpty() && str.size() > 1
163 && str.contains(last_searched_string, qtcase))
164 // ... then only search within already found list.
165 keys = available_model_.stringList();
167 // ... else search all keys.
169 // save searched string for next search.
170 last_searched_string = str;
174 // Search only within the matching keys:
175 result = keys.filter(str, qtcase);
177 // Search within matching keys and associated info.
178 result = to_qstring_list(searchKeys(to_string_vector(keys),
179 qstring_to_ucs4(str), case_sensitive, reg_exp));
181 available_model_.setStringList(result);
185 void QCitation::addKey(QModelIndex const & index)
187 cited_keys_.append(index.data().toString());
188 selected_model_.setStringList(cited_keys_);
192 void QCitation::deleteKey(QModelIndex const & index)
194 cited_keys_.removeAt(index.row());
195 selected_model_.setStringList(cited_keys_);
199 void QCitation::upKey(QModelIndex const & index)
201 int pos = index.row();
202 cited_keys_.swap(pos, pos - 1);
203 selected_model_.setStringList(cited_keys_);
207 void QCitation::downKey(QModelIndex const & index)
209 int pos = index.row();
210 cited_keys_.swap(pos, pos + 1);
211 selected_model_.setStringList(cited_keys_);
215 QStringList QCitation::citationStyles(int sel)
217 string const key = fromqstr(cited_keys_[sel]);
218 return to_qstring_list(getCiteStrings(key));
222 QString QCitation::getKeyInfo(QString const & sel)
224 return toqstr(getInfo(fromqstr(sel)));
228 } // namespace frontend