]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCitation.C
512d00b296d74f124d008a086176cdf0eaa148eb
[lyx.git] / src / frontends / qt4 / QCitation.C
1 /**
2  * \file QCitation.C
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  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "QCitation.h"
16
17 #include "qt_helpers.h"
18
19 #include "support/lstrings.h"
20
21 #include "debug.h"
22
23 #include <vector>
24 #include <string>
25
26 using std::vector;
27 using std::string;
28
29
30 namespace {
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 vector<string> const to_string_vector(QStringList const & qlist)
46 {
47         vector<string> v;
48         for (int i = 0; i != qlist.size(); ++i) {
49                 if (qlist[i].isEmpty())
50                         continue;
51                 v.push_back(lyx::fromqstr(qlist[i]));
52         }
53         return v;
54 }
55
56 }
57
58
59 namespace lyx {
60 namespace frontend {
61
62
63 QCitation::QCitation(Dialog & parent)
64         : ControlCitation(parent)
65 {
66 }
67
68
69 void QCitation::apply(int const choice, bool const full, bool const force,
70                       QString before, QString after)
71 {
72         if (cited_keys_.isEmpty())
73                 return;
74
75         vector<biblio::CiteStyle> const & styles =
76                 ControlCitation::getCiteStyles();
77
78         string const command =
79                 biblio::CitationStyle(styles[choice], full, force)
80                 .asLatexStr();
81
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);
86         dispatchParams();
87 }
88
89
90 void QCitation::clearSelection()
91 {
92         cited_keys_.clear();
93         selected_model_.setStringList(cited_keys_);
94 }
95
96         
97 QString QCitation::textBefore()
98 {
99         return toqstr(params()["before"]);
100 }
101
102
103 QString QCitation::textAfter()
104 {
105         return toqstr(params()["after"]);
106 }
107
108
109 bool QCitation::initialiseParams(std::string const & data)
110 {
111         if (!ControlCitation::initialiseParams(data))
112                 return false;
113         init();
114         return true;
115 }
116
117
118 void QCitation::init()
119 {
120         // Make the list of all available bibliography keys
121         all_keys_ = to_qstring_list(availableKeys());
122         available_model_.setStringList(all_keys_);
123
124         // Ditto for the keys cited in this inset
125         QString str = toqstr(params()["key"]);
126         if (str.isEmpty())
127                 cited_keys_.clear();
128         else
129                 cited_keys_ = str.split(",");
130
131         selected_model_.setStringList(cited_keys_);
132 }
133
134
135 void QCitation::findKey(QString const & str, bool only_keys,
136                 bool case_sensitive, bool reg_exp)
137 {
138         if (str.isEmpty()) {
139                 available_model_.setStringList(all_keys_);
140                 return;
141         }
142
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();
153         }
154         // save option for next search.
155         last_case_sensitive = case_sensitive;
156         last_reg_exp = reg_exp;
157
158         Qt::CaseSensitivity qtcase = case_sensitive?
159                         Qt::CaseSensitive: Qt::CaseInsensitive;
160         QStringList keys;
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();
166         else
167                 // ... else search all keys.
168                 keys = all_keys_;
169         // save searched string for next search.
170         last_searched_string = str;
171
172         QStringList result;
173         if (only_keys)
174                 // Search only within the matching keys:
175                 result = keys.filter(str, qtcase);
176         else
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));
180
181         available_model_.setStringList(result);
182 }
183
184
185 void QCitation::addKey(QModelIndex const & index)
186 {
187         cited_keys_.append(index.data().toString());
188         selected_model_.setStringList(cited_keys_);
189 }
190
191
192 void QCitation::deleteKey(QModelIndex const & index)
193 {
194         cited_keys_.removeAt(index.row());
195         selected_model_.setStringList(cited_keys_);
196 }
197
198
199 void QCitation::upKey(QModelIndex const & index)
200 {
201         int pos = index.row();
202         cited_keys_.swap(pos, pos - 1);
203         selected_model_.setStringList(cited_keys_);
204 }
205
206
207 void QCitation::downKey(QModelIndex const & index)
208 {
209         int pos = index.row();
210         cited_keys_.swap(pos, pos + 1);
211         selected_model_.setStringList(cited_keys_);
212 }
213
214
215 QStringList QCitation::citationStyles(int sel)
216 {
217         string const key = fromqstr(cited_keys_[sel]);
218         return to_qstring_list(getCiteStrings(key));
219 }
220
221
222 QString QCitation::getKeyInfo(QString const & sel)
223 {
224         return toqstr(getInfo(fromqstr(sel)));
225 }
226
227
228 } // namespace frontend
229 } // namespace lyx