]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCitation.cpp
delete unneeded Menubar virtual interface.
[lyx.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 #include "support/docstring.h"
21
22 #include "debug.h"
23
24 #include <vector>
25 #include <string>
26
27 using std::vector;
28 using std::string;
29
30
31 template<typename String> static QStringList to_qstring_list(vector<String> const & v)
32 {
33         QStringList qlist;
34
35         for (size_t i = 0; i != v.size(); ++i) {
36                 if (v[i].empty())
37                         continue;
38                 qlist.append(lyx::toqstr(v[i]));
39         }
40         return qlist;
41 }
42
43
44 static vector<lyx::docstring> const to_docstring_vector(QStringList const & qlist)
45 {
46         vector<lyx::docstring> v;
47         for (int i = 0; i != qlist.size(); ++i) {
48                 if (qlist[i].isEmpty())
49                         continue;
50                 v.push_back(lyx::qstring_to_ucs4(qlist[i]));
51         }
52         return v;
53 }
54
55
56 namespace lyx {
57 namespace frontend {
58
59
60 QCitation::QCitation(Dialog & parent)
61         : ControlCitation(parent)
62 {
63 }
64
65
66 void QCitation::apply(int const choice, bool const full, bool const force,
67                       QString before, QString after)
68 {
69         if (cited_keys_.isEmpty())
70                 return;
71
72         vector<biblio::CiteStyle> const & styles =
73                 ControlCitation::getCiteStyles();
74
75         string const command =
76                 biblio::CitationStyle(styles[choice], full, force)
77                 .asLatexStr();
78
79         params().setCmdName(command);
80         params()["key"] = qstring_to_ucs4(cited_keys_.join(","));
81         params()["before"] = qstring_to_ucs4(before);
82         params()["after"] = qstring_to_ucs4(after);
83         dispatchParams();
84 }
85
86
87 void QCitation::clearSelection()
88 {
89         cited_keys_.clear();
90         selected_model_.setStringList(cited_keys_);
91 }
92
93
94 QString QCitation::textBefore()
95 {
96         return toqstr(params()["before"]);
97 }
98
99
100 QString QCitation::textAfter()
101 {
102         return toqstr(params()["after"]);
103 }
104
105
106 bool QCitation::initialiseParams(std::string const & data)
107 {
108         if (!ControlCitation::initialiseParams(data))
109                 return false;
110         init();
111         return true;
112 }
113
114
115 void QCitation::init()
116 {
117         // Make the list of all available bibliography keys
118         all_keys_ = to_qstring_list(availableKeys());
119         available_model_.setStringList(all_keys_);
120
121         // Ditto for the keys cited in this inset
122         QString str = toqstr(params()["key"]);
123         if (str.isEmpty())
124                 cited_keys_.clear();
125         else
126                 cited_keys_ = str.split(",");
127         selected_model_.setStringList(cited_keys_);
128 }
129
130
131 void QCitation::findKey(QString const & str, bool only_keys,
132         docstring field, docstring entryType,
133         bool case_sensitive, bool reg_exp, bool reset)
134 {
135         // Used for optimisation: store last searched string.
136         static QString last_searched_string;
137         // Used to disable the above optimisation.
138         static bool last_case_sensitive;
139         static bool last_reg_exp;
140         // Reset last_searched_string in case of changed option.
141         if (last_case_sensitive != case_sensitive
142                 || last_reg_exp != reg_exp) {
143                         LYXERR(Debug::GUI) << "QCitation::findKey: optimisation disabled!" << std::endl;
144                 last_searched_string.clear();
145         }
146         // save option for next search.
147         last_case_sensitive = case_sensitive;
148         last_reg_exp = reg_exp;
149
150         Qt::CaseSensitivity qtcase = case_sensitive?
151                         Qt::CaseSensitive: Qt::CaseInsensitive;
152         QStringList keys;
153         // If new string (str) contains the last searched one...
154         if (!reset &&
155                 !last_searched_string.isEmpty() &&
156                 str.size() > 1 &&
157                 str.contains(last_searched_string, qtcase))
158                 // ... then only search within already found list.
159                 keys = available_model_.stringList();
160         else
161                 // ... else search all keys.
162                 keys = all_keys_;
163         // save searched string for next search.
164         last_searched_string = str;
165
166         QStringList result;
167         
168         //First, filter by entryType, which will be faster than 
169         //what follows, so we may get to do that on less.
170         vector<docstring> keyVector = to_docstring_vector(keys);
171         filterByEntryType(keyVector, entryType);
172         
173         if (str.isEmpty())
174                 result = to_qstring_list(keyVector);
175         else
176                 result = to_qstring_list(searchKeys(keyVector, only_keys, 
177                         qstring_to_ucs4(str), field, case_sensitive, reg_exp));
178         
179         available_model_.setStringList(result);
180 }
181
182
183 QStringList QCitation::getFieldsAsQStringList() {
184         return to_qstring_list(availableFields());
185 }
186
187
188 QStringList QCitation::getEntriesAsQStringList() {
189         return to_qstring_list(availableEntries());
190 }
191
192
193 void QCitation::addKey(QModelIndex const & index)
194 {
195         cited_keys_.append(index.data().toString());
196         selected_model_.setStringList(cited_keys_);
197 }
198
199
200 void QCitation::deleteKey(QModelIndex const & index)
201 {
202         cited_keys_.removeAt(index.row());
203         selected_model_.setStringList(cited_keys_);
204 }
205
206
207 void QCitation::upKey(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 void QCitation::downKey(QModelIndex const & index)
216 {
217         int pos = index.row();
218         cited_keys_.swap(pos, pos + 1);
219         selected_model_.setStringList(cited_keys_);
220 }
221
222
223 QStringList QCitation::citationStyles(int sel)
224 {
225         docstring const key = qstring_to_ucs4(cited_keys_[sel]);
226         return to_qstring_list(getCiteStrings(key));
227 }
228
229
230 QString QCitation::getKeyInfo(QString const & sel)
231 {
232         return toqstr(getInfo(qstring_to_ucs4(sel)));
233 }
234
235
236 } // namespace frontend
237 } // namespace lyx