2 * \file GuiCitation.cpp
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
9 * \author Richard Heck (adapted to QSelectionManager)
11 * Full author contact details are available in file CREDITS.
16 #include "GuiCitation.h"
18 #include "qt_helpers.h"
20 #include "support/lstrings.h"
21 #include "support/docstring.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 static vector<lyx::docstring> const to_docstring_vector(QStringList const & qlist)
47 vector<lyx::docstring> v;
48 for (int i = 0; i != qlist.size(); ++i) {
49 if (qlist[i].isEmpty())
51 v.push_back(lyx::qstring_to_ucs4(qlist[i]));
61 GuiCitation::GuiCitation(Dialog & parent)
62 : ControlCitation(parent)
67 void GuiCitation::apply(int const choice, bool const full, bool const force,
68 QString before, QString after)
70 if (cited_keys_.isEmpty())
73 vector<biblio::CiteStyle> const & styles =
74 ControlCitation::getCiteStyles();
76 string const command =
77 biblio::CitationStyle(styles[choice], full, force)
80 params().setCmdName(command);
81 params()["key"] = qstring_to_ucs4(cited_keys_.join(","));
82 params()["before"] = qstring_to_ucs4(before);
83 params()["after"] = qstring_to_ucs4(after);
88 void GuiCitation::clearSelection()
91 selected_model_.setStringList(cited_keys_);
95 QString GuiCitation::textBefore()
97 return toqstr(params()["before"]);
101 QString GuiCitation::textAfter()
103 return toqstr(params()["after"]);
107 bool GuiCitation::initialiseParams(std::string const & data)
109 if (!ControlCitation::initialiseParams(data))
116 void GuiCitation::init()
118 // Make the list of all available bibliography keys
119 all_keys_ = to_qstring_list(availableKeys());
120 available_model_.setStringList(all_keys_);
122 // Ditto for the keys cited in this inset
123 QString str = toqstr(params()["key"]);
127 cited_keys_ = str.split(",");
128 selected_model_.setStringList(cited_keys_);
132 void GuiCitation::findKey(QString const & str, bool only_keys,
133 docstring field, docstring entryType,
134 bool case_sensitive, bool reg_exp, bool reset)
136 // Used for optimisation: store last searched string.
137 static QString last_searched_string;
138 // Used to disable the above optimisation.
139 static bool last_case_sensitive;
140 static bool last_reg_exp;
141 // Reset last_searched_string in case of changed option.
142 if (last_case_sensitive != case_sensitive
143 || last_reg_exp != reg_exp) {
144 LYXERR(Debug::GUI) << "GuiCitation::findKey: optimisation disabled!" << std::endl;
145 last_searched_string.clear();
147 // save option for next search.
148 last_case_sensitive = case_sensitive;
149 last_reg_exp = reg_exp;
151 Qt::CaseSensitivity qtcase = case_sensitive?
152 Qt::CaseSensitive: Qt::CaseInsensitive;
154 // If new string (str) contains the last searched one...
156 !last_searched_string.isEmpty() &&
158 str.contains(last_searched_string, qtcase))
159 // ... then only search within already found list.
160 keys = available_model_.stringList();
162 // ... else search all keys.
164 // save searched string for next search.
165 last_searched_string = str;
169 //First, filter by entryType, which will be faster than
170 //what follows, so we may get to do that on less.
171 vector<docstring> keyVector = to_docstring_vector(keys);
172 filterByEntryType(keyVector, entryType);
175 result = to_qstring_list(keyVector);
177 result = to_qstring_list(searchKeys(keyVector, only_keys,
178 qstring_to_ucs4(str), field, case_sensitive, reg_exp));
180 available_model_.setStringList(result);
184 QStringList GuiCitation::getFieldsAsQStringList() {
185 return to_qstring_list(availableFields());
189 QStringList GuiCitation::getEntriesAsQStringList() {
190 return to_qstring_list(availableEntries());
194 QStringList GuiCitation::citationStyles(int sel)
196 docstring const key = qstring_to_ucs4(cited_keys_[sel]);
197 return to_qstring_list(getCiteStrings(key));
201 QString GuiCitation::getKeyInfo(QString const & sel)
203 return toqstr(getInfo(qstring_to_ucs4(sel)));
206 void GuiCitation::setCitedKeys()
208 cited_keys_ = selected_model_.stringList();
212 } // namespace frontend