]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiCitation.cpp
move our stuff off the Q* namespace
[features.git] / src / frontends / qt4 / GuiCitation.cpp
1 /**
2  * \file GuiCitation.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Kalle Dalheimer
8  * \author Abdelrazak Younes
9  * \author Richard Heck (adapted to QSelectionManager)
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiCitation.h"
17
18 #include "qt_helpers.h"
19
20 #include "support/lstrings.h"
21 #include "support/docstring.h"
22
23 #include "debug.h"
24
25 #include <vector>
26 #include <string>
27
28 using std::vector;
29 using std::string;
30
31
32 template<typename String> static QStringList to_qstring_list(vector<String> const & v)
33 {
34         QStringList qlist;
35
36         for (size_t i = 0; i != v.size(); ++i) {
37                 if (v[i].empty())
38                         continue;
39                 qlist.append(lyx::toqstr(v[i]));
40         }
41         return qlist;
42 }
43
44
45 static vector<lyx::docstring> const to_docstring_vector(QStringList const & qlist)
46 {
47         vector<lyx::docstring> v;
48         for (int i = 0; i != qlist.size(); ++i) {
49                 if (qlist[i].isEmpty())
50                         continue;
51                 v.push_back(lyx::qstring_to_ucs4(qlist[i]));
52         }
53         return v;
54 }
55
56
57 namespace lyx {
58 namespace frontend {
59
60
61 GuiCitation::GuiCitation(Dialog & parent)
62         : ControlCitation(parent)
63 {
64 }
65
66
67 void GuiCitation::apply(int const choice, bool const full, bool const force,
68                       QString before, QString after)
69 {
70         if (cited_keys_.isEmpty())
71                 return;
72
73         vector<biblio::CiteStyle> const & styles =
74                 ControlCitation::getCiteStyles();
75
76         string const command =
77                 biblio::CitationStyle(styles[choice], full, force)
78                 .asLatexStr();
79
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);
84         dispatchParams();
85 }
86
87
88 void GuiCitation::clearSelection()
89 {
90         cited_keys_.clear();
91         selected_model_.setStringList(cited_keys_);
92 }
93
94
95 QString GuiCitation::textBefore()
96 {
97         return toqstr(params()["before"]);
98 }
99
100
101 QString GuiCitation::textAfter()
102 {
103         return toqstr(params()["after"]);
104 }
105
106
107 bool GuiCitation::initialiseParams(std::string const & data)
108 {
109         if (!ControlCitation::initialiseParams(data))
110                 return false;
111         init();
112         return true;
113 }
114
115
116 void GuiCitation::init()
117 {
118         // Make the list of all available bibliography keys
119         all_keys_ = to_qstring_list(availableKeys());
120         available_model_.setStringList(all_keys_);
121
122         // Ditto for the keys cited in this inset
123         QString str = toqstr(params()["key"]);
124         if (str.isEmpty())
125                 cited_keys_.clear();
126         else
127                 cited_keys_ = str.split(",");
128         selected_model_.setStringList(cited_keys_);
129 }
130
131
132 void GuiCitation::findKey(QString const & str, bool only_keys,
133         docstring field, docstring entryType,
134         bool case_sensitive, bool reg_exp, bool reset)
135 {
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();
146         }
147         // save option for next search.
148         last_case_sensitive = case_sensitive;
149         last_reg_exp = reg_exp;
150
151         Qt::CaseSensitivity qtcase = case_sensitive?
152                         Qt::CaseSensitive: Qt::CaseInsensitive;
153         QStringList keys;
154         // If new string (str) contains the last searched one...
155         if (!reset &&
156                 !last_searched_string.isEmpty() &&
157                 str.size() > 1 &&
158                 str.contains(last_searched_string, qtcase))
159                 // ... then only search within already found list.
160                 keys = available_model_.stringList();
161         else
162                 // ... else search all keys.
163                 keys = all_keys_;
164         // save searched string for next search.
165         last_searched_string = str;
166
167         QStringList result;
168         
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);
173         
174         if (str.isEmpty())
175                 result = to_qstring_list(keyVector);
176         else
177                 result = to_qstring_list(searchKeys(keyVector, only_keys, 
178                         qstring_to_ucs4(str), field, case_sensitive, reg_exp));
179         
180         available_model_.setStringList(result);
181 }
182
183
184 QStringList GuiCitation::getFieldsAsQStringList() {
185         return to_qstring_list(availableFields());
186 }
187
188
189 QStringList GuiCitation::getEntriesAsQStringList() {
190         return to_qstring_list(availableEntries());
191 }
192
193
194 QStringList GuiCitation::citationStyles(int sel)
195 {
196         docstring const key = qstring_to_ucs4(cited_keys_[sel]);
197         return to_qstring_list(getCiteStrings(key));
198 }
199
200
201 QString GuiCitation::getKeyInfo(QString const & sel)
202 {
203         return toqstr(getInfo(qstring_to_ucs4(sel)));
204 }
205
206 void GuiCitation::setCitedKeys() 
207 {
208         cited_keys_ = selected_model_.stringList();
209 }
210
211
212 } // namespace frontend
213 } // namespace lyx