]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCitation.C
This new citation dialog follows a new design similar to lyx-1.3:
[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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QCitation.h"
15 #include "QCitationDialog.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 #include <iostream>
29 using std::cout;
30 using std::endl;
31
32 using std::vector;
33 using std::string;
34
35 void toQStringList(QStringList & qlist, vector<string> const & v)
36 {
37         qlist.clear();
38         for (size_t i=0; i != v.size(); ++i) {
39                 if (v[i].empty())
40                         continue;
41                 qlist.append(toqstr(v[i]));
42         }
43 }
44
45 void toVector(vector<string> & v, const QStringList & qlist)
46 {
47         v.clear();
48
49         for (size_t i=0; i != qlist.size(); ++i)
50                 v.push_back(fromqstr(qlist[i]));
51 }
52
53 namespace lyx {
54 namespace frontend {
55
56 typedef QController<ControlCitation, QView<QCitationDialog> > base_class;
57
58
59 QCitation::QCitation(Dialog & parent)
60         : base_class(parent, _("Citation"))
61 {
62 }
63
64
65 void QCitation::apply()
66 {
67         InsetCommandParams & params = controller().params();
68         dialog_->update(params);
69
70         params.setContents(fromqstr(selected_keys_.stringList().join("'")));
71 }
72
73
74 void QCitation::build_dialog()
75 {
76         dialog_.reset(new QCitationDialog(this));
77 }
78
79
80 void QCitation::update_contents()
81 {
82         QStringList keys;
83         
84         // Make the list of all available bibliography keys
85         toQStringList(keys,
86                 biblio::getKeys(controller().bibkeysInfo()));
87         available_keys_.setStringList(keys);
88
89         // Ditto for the keys cited in this inset
90         QString str = toqstr(controller().params().getContents());
91         if (!str.isEmpty()) {
92                 keys = str.split(",");
93                 selected_keys_.setStringList(keys);
94         }
95
96         dialog_->update(controller().params());
97
98         bc().valid(isValid());
99 }
100
101 void QCitation::hide()
102 {
103         QDialogView::hide();
104 }
105
106 bool QCitation::isValid()
107 {
108         return selected_keys_.rowCount() > 0;
109 }
110
111 QModelIndex QCitation::findKey(QString const & str, QModelIndex const & index) const
112 {
113         QStringList const avail = available_keys_.stringList();
114         int const pos = avail.indexOf(str, index.row());
115         if (pos == -1)
116                 return index;
117         return available_keys_.index(pos);
118 }
119
120 QModelIndex QCitation::findKey(QString const & str) const
121 {
122         QStringList const avail = available_keys_.stringList();
123         int const pos = avail.indexOf(str);
124         if (pos == -1)
125                 return QModelIndex();
126         return available_keys_.index(pos);
127 }
128
129 void QCitation::addKeys(QModelIndexList const & indexes)
130 {
131         // = selectionModel->selectedIndexes();
132
133         QModelIndex index;
134
135         if (indexes.empty())
136                 return;
137
138         QStringList keys = selected_keys_.stringList();
139         
140         foreach(index, indexes) {
141                 if (keys.indexOf(index.data().toString()) == -1)
142                         keys.append(index.data().toString());
143         }
144
145         selected_keys_.setStringList(keys);
146         
147         changed();
148 }
149
150 void QCitation::deleteKeys(QModelIndexList const & indexes)
151 {
152         QModelIndex index;
153
154         if (indexes.empty())
155                 return;
156
157         QStringList keys = selected_keys_.stringList();
158         
159         foreach(index, indexes) {
160                 int const pos = keys.indexOf(index.data().toString());
161                 if (pos != -1)
162                         keys.removeAt(pos);
163         }
164
165         selected_keys_.setStringList(keys);
166         
167         changed();
168 }
169
170 void QCitation::upKey(QModelIndexList const & indexes)
171 {
172         if (indexes.empty() || indexes.size() > 1)
173                 return;
174
175         int pos = indexes[0].row();
176         if (pos < 1)
177                 return;
178
179         QStringList keys = selected_keys_.stringList();
180         keys.swap(pos, pos-1);
181         selected_keys_.setStringList(keys);
182         
183         changed();
184 }
185
186 void QCitation::downKey(QModelIndexList const & indexes)
187 {
188         if (indexes.empty() || indexes.size() > 1)
189                 return;
190
191         int pos = indexes[0].row();
192         if (pos >= selected_keys_.rowCount() - 1)
193                 return;
194
195         QStringList keys = selected_keys_.stringList();
196         keys.swap(pos, pos+1);
197         selected_keys_.setStringList(keys);
198         
199         changed();
200 }
201
202
203
204
205
206
207 } // namespace frontend
208 } // namespace lyx