]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QCitation.C
Convert most of the bibtex machinery to docstring.
[features.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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ControlCitation.h"
15 #include "QCitation.h"
16 #include "Qt2BC.h"
17 #include "qt_helpers.h"
18
19 #include "bufferparams.h"
20
21 #include "controllers/ButtonController.h"
22 #include "controllers/ControlCitation.h"
23
24 #include "support/lstrings.h"
25
26 #include <vector>
27 #include <string>
28
29 using std::vector;
30 using std::string;
31
32
33 namespace {
34
35 template<typename String> static QStringList toQStringList(vector<String> const & v)
36 {
37         QStringList qlist;
38
39         for (size_t i=0; i != v.size(); ++i) {
40                 if (v[i].empty())
41                         continue;
42                 qlist.append(lyx::toqstr(v[i]));
43         }
44         return qlist;
45 }
46
47 }
48
49
50 namespace lyx {
51 namespace frontend {
52
53
54 QCitation::QCitation(Dialog & parent)
55         : ControlCitation(parent)
56 {
57 }
58
59
60 void QCitation::apply(int const choice, bool const full, bool const force,
61                       QString before, QString after)
62 {
63         if (selected_keys_.rowCount() == 0)
64                 return;
65
66         vector<biblio::CiteStyle> const & styles =
67                 ControlCitation::getCiteStyles();
68
69         string const command =
70                 biblio::CitationStyle(styles[choice], full, force)
71                 .asLatexStr();
72
73         params().setCmdName(command);
74         params()["key"] = qstring_to_ucs4(selected_keys_.stringList().join(","));
75         params()["before"] = qstring_to_ucs4(before);
76         params()["after"] = qstring_to_ucs4(after);
77         dispatchParams();
78 }
79
80
81 void QCitation::clearSelection()
82 {
83         selected_keys_.setStringList(QStringList());
84 }
85
86         
87 QString QCitation::textBefore()
88 {
89         return toqstr(params()["before"]);
90 }
91
92
93 QString QCitation::textAfter()
94 {
95         return toqstr(params()["after"]);
96 }
97
98
99 void QCitation::updateModel()
100 {
101         // Make the list of all available bibliography keys
102         QStringList keys = toQStringList(biblio::getKeys(bibkeysInfo()));
103         available_keys_.setStringList(keys);
104
105         // Ditto for the keys cited in this inset
106         QString str = toqstr(params()["key"]);
107         if (!str.isEmpty()) {
108                 keys = str.split(",");
109                 selected_keys_.setStringList(keys);
110         }
111 }
112
113
114 void QCitation::findKey(QString const & str)
115 {
116         QStringList sl = available_keys_.stringList().filter(str, Qt::CaseInsensitive);
117         found_keys_.setStringList(sl);
118 }
119
120
121 void QCitation::addKey(QModelIndex const & index)
122 {
123         QStringList keys = selected_keys_.stringList();
124         keys.append(index.data().toString());
125         selected_keys_.setStringList(keys);
126 }
127
128
129 void QCitation::deleteKey(QModelIndex const & index)
130 {
131         QStringList keys = selected_keys_.stringList();
132         keys.removeAt(index.row());
133         selected_keys_.setStringList(keys);
134 }
135
136
137 void QCitation::upKey(QModelIndex const & index)
138 {
139         QStringList keys = selected_keys_.stringList();
140         int pos = index.row();
141         keys.swap(pos, pos - 1);
142         selected_keys_.setStringList(keys);
143 }
144
145
146 void QCitation::downKey(QModelIndex const & index)
147 {
148         QStringList keys = selected_keys_.stringList();
149         int pos = index.row();
150         keys.swap(pos, pos + 1);
151         selected_keys_.setStringList(keys);
152 }
153
154
155 QStringList QCitation::citationStyles(int sel)
156 {
157         string const key = fromqstr(selected_keys_.stringList()[sel]);
158         return toQStringList(getCiteStrings(key));
159 }
160
161
162 QString QCitation::getKeyInfo(QString const & sel)
163 {
164         if (!bibkeysInfo().empty())
165                 return toqstr(biblio::getInfo(bibkeysInfo(), fromqstr(sel) ));
166
167         return QString();
168 }
169
170
171 } // namespace frontend
172 } // namespace lyx