]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QCitation.cpp
Whitespace cleanup
[features.git] / src / frontends / qt4 / QCitation.cpp
1 /*
2  * \file QCitation.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  *
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 template<typename String> static QStringList to_qstring_list(vector<String> const & v)
31 {
32         QStringList qlist;
33
34         for (size_t i = 0; i != v.size(); ++i) {
35                 if (v[i].empty())
36                         continue;
37                 qlist.append(lyx::toqstr(v[i]));
38         }
39         return qlist;
40 }
41
42
43 static vector<string> const to_string_vector(QStringList const & qlist)
44 {
45         vector<string> v;
46         for (int i = 0; i != qlist.size(); ++i) {
47                 if (qlist[i].isEmpty())
48                         continue;
49                 v.push_back(lyx::fromqstr(qlist[i]));
50         }
51         return v;
52 }
53
54
55 namespace lyx {
56 namespace frontend {
57
58
59 QCitation::QCitation(Dialog & parent)
60         : ControlCitation(parent)
61 {
62 }
63
64
65 void QCitation::apply(int const choice, bool const full, bool const force,
66                       QString before, QString after)
67 {
68         if (cited_keys_.isEmpty())
69                 return;
70
71         vector<biblio::CiteStyle> const & styles =
72                 ControlCitation::getCiteStyles();
73
74         string const command =
75                 biblio::CitationStyle(styles[choice], full, force)
76                 .asLatexStr();
77
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);
82         dispatchParams();
83 }
84
85
86 void QCitation::clearSelection()
87 {
88         cited_keys_.clear();
89         selected_model_.setStringList(cited_keys_);
90 }
91
92
93 QString QCitation::textBefore()
94 {
95         return toqstr(params()["before"]);
96 }
97
98
99 QString QCitation::textAfter()
100 {
101         return toqstr(params()["after"]);
102 }
103
104
105 bool QCitation::initialiseParams(std::string const & data)
106 {
107         if (!ControlCitation::initialiseParams(data))
108                 return false;
109         init();
110         return true;
111 }
112
113
114 void QCitation::init()
115 {
116         // Make the list of all available bibliography keys
117         all_keys_ = to_qstring_list(availableKeys());
118         available_model_.setStringList(all_keys_);
119
120         // Ditto for the keys cited in this inset
121         QString str = toqstr(params()["key"]);
122         if (str.isEmpty())
123                 cited_keys_.clear();
124         else
125                 cited_keys_ = str.split(",");
126
127         selected_model_.setStringList(cited_keys_);
128 }
129
130
131 void QCitation::findKey(QString const & str, bool only_keys,
132                 bool case_sensitive, bool reg_exp)
133 {
134         if (str.isEmpty()) {
135                 available_model_.setStringList(all_keys_);
136                 return;
137         }
138
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();
149         }
150         // save option for next search.
151         last_case_sensitive = case_sensitive;
152         last_reg_exp = reg_exp;
153
154         Qt::CaseSensitivity qtcase = case_sensitive?
155                         Qt::CaseSensitive: Qt::CaseInsensitive;
156         QStringList keys;
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();
162         else
163                 // ... else search all keys.
164                 keys = all_keys_;
165         // save searched string for next search.
166         last_searched_string = str;
167
168         QStringList result;
169         if (only_keys)
170                 // Search only within the matching keys:
171                 result = keys.filter(str, qtcase);
172         else
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));
176
177         available_model_.setStringList(result);
178 }
179
180
181 void QCitation::addKey(QModelIndex const & index)
182 {
183         cited_keys_.append(index.data().toString());
184         selected_model_.setStringList(cited_keys_);
185 }
186
187
188 void QCitation::deleteKey(QModelIndex const & index)
189 {
190         cited_keys_.removeAt(index.row());
191         selected_model_.setStringList(cited_keys_);
192 }
193
194
195 void QCitation::upKey(QModelIndex const & index)
196 {
197         int pos = index.row();
198         cited_keys_.swap(pos, pos - 1);
199         selected_model_.setStringList(cited_keys_);
200 }
201
202
203 void QCitation::downKey(QModelIndex const & index)
204 {
205         int pos = index.row();
206         cited_keys_.swap(pos, pos + 1);
207         selected_model_.setStringList(cited_keys_);
208 }
209
210
211 QStringList QCitation::citationStyles(int sel)
212 {
213         string const key = fromqstr(cited_keys_[sel]);
214         return to_qstring_list(getCiteStrings(key));
215 }
216
217
218 QString QCitation::getKeyInfo(QString const & sel)
219 {
220         return toqstr(getInfo(fromqstr(sel)));
221 }
222
223
224 } // namespace frontend
225 } // namespace lyx