]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/FormCitation.C
use anon namespace, somewhat better comp. handling of minipages, not quite there yet
[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 std::vector;
39 using std::pair;
40 using std::find;
41
42 FormCitation::FormCitation(LyXView *v, Dialogs *d)
43     : dialog_(0), lv_(v), d_(d), inset_(0), ih_(0)
44 {
45     // let the dialog be shown
46     // This is a permanent connection so we won't bother
47     // storing a copy because we won't be disconnecting.
48     d->showCitation.connect(slot(this, &FormCitation::showCitation));
49     d->createCitation.connect(slot(this, &FormCitation::createCitation));
50 }
51
52 FormCitation::~FormCitation()
53 {
54     delete dialog_;
55 }
56
57
58 void FormCitation::showCitation(InsetCommand * inset)
59 {
60     if (inset == 0) return;  // maybe we should Assert this?
61     
62     // If connected to another inset, disconnect from it.
63     if (inset_)
64         ih_.disconnect();
65     
66     inset_    = inset;
67     params    = inset->params();
68     ih_ = inset->hideDialog.connect(slot(this, &FormCitation::hide));
69     show();
70 }
71
72
73 void FormCitation::createCitation(string const & arg)
74 {
75     if (inset_) {       
76         ih_.disconnect();
77         inset_ = 0;
78     }
79     
80     params.setFromString(arg);
81     show();
82 }
83
84
85 void FormCitation::hide()
86 {
87     if( dialog_ )
88         dialog_->hide();
89 }
90
91
92
93 void FormCitation::show()
94 {
95     if (!dialog_)
96         dialog_ = new FormCitationDialogImpl(this, 0, _("LyX: Citation Reference"), false);
97
98     dialog_->show();
99     
100     update();
101 }
102
103
104 void FormCitation::update()
105 {
106     bibkeys.clear();
107     bibkeysInfo.clear();
108
109     vector<pair<string,string> > blist =
110         lv_->buffer()->getBibkeyList();
111     sort(blist.begin(), blist.end());
112
113     for (unsigned int i = 0; i < blist.size(); ++i) {
114         bibkeys.push_back(blist[i].first);
115         bibkeysInfo.push_back(blist[i].second);
116     }
117     blist.clear();
118
119     updateBrowser(dialog_->bibliographyKeysLB, bibkeys);
120     
121     // Ditto for the keys cited in this inset
122     citekeys.clear();
123     string tmp, keys(params.getContents());
124     keys = frontStrip(split(keys, tmp, ','));
125     while (!tmp.empty()) {
126         citekeys.push_back(tmp);
127         keys = frontStrip(split(keys, tmp, ','));
128     }
129     updateBrowser(dialog_->insetKeysLB, citekeys);
130
131     // No keys have been selected yet, so...
132     dialog_->infoML->clear();
133     setBibButtons(OFF);
134     setCiteButtons(OFF);
135
136     dialog_->textAfterED->setText( params.getOptions().c_str() );
137 }
138
139
140 void FormCitation::updateBrowser( QListBox* listbox,
141                                   vector<string> const & keys) const
142 {
143     listbox->clear();
144
145     for (unsigned int i = 0; i < keys.size(); ++i)
146         listbox->insertItem( keys[i].c_str() );
147 }
148
149
150 void FormCitation::setBibButtons(State status) const
151 {
152     dialog_->leftPB->setEnabled( status == ON );
153 }
154
155
156 void FormCitation::setCiteButtons(State status) const
157 {
158     int const sel     = dialog_->insetKeysLB->currentItem();
159     int const maxline = dialog_->insetKeysLB->count();
160
161     bool const activate      = (status == ON);
162     bool const activate_up   = (activate && sel != 1);
163     bool const activate_down = (activate && sel != maxline);
164     
165     dialog_->stopPB->setEnabled(activate);
166     dialog_->upPB->setEnabled(activate_up);
167     dialog_->downPB->setEnabled(activate_down);
168 }
169
170
171 void FormCitation::apply()
172 {
173     if (lv_->buffer()->isReadonly()) return;
174
175     string contents;
176     for (unsigned int i = 0; i < citekeys.size(); ++i) {
177         if (i > 0) contents += ",";
178         contents += citekeys[i];
179     }
180
181     params.setContents(contents);
182     params.setOptions( dialog_->textAfterED->text().latin1() );
183     
184     if (inset_ != 0) {
185         // Only update if contents have changed
186         if (params != inset_->params()) {
187             inset_->setParams(params);
188             lv_->view()->updateInset(inset_, true);
189         }
190     } else {
191         lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
192                                     params.getAsString());
193     }   
194 }
195
196