]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/FormCitation.C
- Compiles and links again
[lyx.git] / src / frontends / qt2 / FormCitation.C
1 /*
2  * FormCitation.C
3  * (C) 2000 LyX Team
4  * John Levon, moz@compsoc.man.ac.uk
5  * Changed for Qt2 implementation by Kalle Dalheimer, kalle@klaralvdalens-datakonsult.se
6  */
7
8 /***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  ***************************************************************************/
16
17 #include <config.h>
18
19 #include <algorithm>
20 #include <support/lstrings.h>
21
22 #include "FormCitationDialogImpl.h"
23 #undef emit
24
25 #include "Dialogs.h"
26 #include "FormCitation.h"
27 #include "gettext.h"
28 #include "buffer.h"
29 #include "LyXView.h"
30 #include "lyxfunc.h"
31 #include "lyxfont.h"
32
33 #include <qlineedit.h>
34 #include <qlistbox.h>
35 #include <qmultilineedit.h>
36 #include <qpushbutton.h>
37
38 using SigC::slot;
39 using std::vector;
40 using std::pair;
41 using std::find;
42 using std::sort;
43
44 FormCitation::FormCitation(LyXView *v, Dialogs *d)
45     : dialog_(0), lv_(v), d_(d), inset_(0), ih_(0)
46 {
47     // let the dialog be shown
48     // This is a permanent connection so we won't bother
49     // storing a copy because we won't be disconnecting.
50     d->showCitation.connect(slot(this, &FormCitation::showCitation));
51     d->createCitation.connect(slot(this, &FormCitation::createCitation));
52 }
53
54 FormCitation::~FormCitation()
55 {
56     delete dialog_;
57 }
58
59
60 void FormCitation::showCitation(InsetCommand * inset)
61 {
62     if (inset == 0) return;  // maybe we should Assert this?
63
64     // If connected to another inset, disconnect from it.
65     if (inset_)
66         ih_.disconnect();
67
68     inset_    = inset;
69     params    = inset->params();
70     ih_ = inset->hideDialog.connect(slot(this, &FormCitation::hide));
71     show();
72 }
73
74
75 void FormCitation::createCitation(string const & arg)
76 {
77     if (inset_) {
78         ih_.disconnect();
79         inset_ = 0;
80     }
81
82     params.setFromString(arg);
83     show();
84 }
85
86
87 void FormCitation::hide()
88 {
89     if( dialog_ )
90         dialog_->hide();
91 }
92
93
94
95 void FormCitation::show()
96 {
97     if (!dialog_)
98         dialog_ = new FormCitationDialogImpl(this, 0, _("LyX: Citation Reference"), false);
99
100     dialog_->show();
101
102     update();
103 }
104
105
106 void FormCitation::update()
107 {
108     bibkeys.clear();
109     bibkeysInfo.clear();
110
111     vector<pair<string,string> > blist =
112         lv_->buffer()->getBibkeyList();
113     sort(blist.begin(), blist.end());
114
115     for (unsigned int i = 0; i < blist.size(); ++i) {
116         bibkeys.push_back(blist[i].first);
117         bibkeysInfo.push_back(blist[i].second);
118     }
119     blist.clear();
120
121     updateBrowser(dialog_->bibliographyKeysLB, bibkeys);
122
123     // Ditto for the keys cited in this inset
124     citekeys.clear();
125     string tmp, keys(params.getContents());
126     keys = frontStrip(split(keys, tmp, ','));
127     while (!tmp.empty()) {
128         citekeys.push_back(tmp);
129         keys = frontStrip(split(keys, tmp, ','));
130     }
131     updateBrowser(dialog_->insetKeysLB, citekeys);
132
133     // No keys have been selected yet, so...
134     dialog_->infoML->clear();
135     setBibButtons(OFF);
136     setCiteButtons(OFF);
137
138     dialog_->textAfterED->setText( params.getOptions().c_str() );
139 }
140
141
142 void FormCitation::updateBrowser( QListBox* listbox,
143                                   vector<string> const & keys) const
144 {
145     listbox->clear();
146
147     for (unsigned int i = 0; i < keys.size(); ++i)
148         listbox->insertItem( keys[i].c_str() );
149 }
150
151
152 void FormCitation::setBibButtons(State status) const
153 {
154     dialog_->leftPB->setEnabled( status == ON );
155 }
156
157
158 void FormCitation::setCiteButtons(State status) const
159 {
160     int const sel     = dialog_->insetKeysLB->currentItem();
161     int const maxline = dialog_->insetKeysLB->count();
162
163     bool const activate      = (status == ON);
164     bool const activate_up   = (activate && sel != 1);
165     bool const activate_down = (activate && sel != maxline);
166
167     dialog_->stopPB->setEnabled(activate);
168     dialog_->upPB->setEnabled(activate_up);
169     dialog_->downPB->setEnabled(activate_down);
170 }
171
172
173 void FormCitation::apply()
174 {
175     if (lv_->buffer()->isReadonly()) return;
176
177     string contents;
178     for (unsigned int i = 0; i < citekeys.size(); ++i) {
179         if (i > 0) contents += ",";
180         contents += citekeys[i];
181     }
182
183     params.setContents(contents);
184     params.setOptions( dialog_->textAfterED->text().latin1() );
185
186     if (inset_ != 0) {
187         // Only update if contents have changed
188         if (params != inset_->params()) {
189             inset_->setParams(params);
190             lv_->view()->updateInset(inset_, true);
191         }
192     } else {
193         lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
194                                     params.getAsString());
195     }
196 }
197
198