]> git.lyx.org Git - features.git/blob - src/frontends/qt2/QBibtexDialog.C
introduce namespace lyx::support
[features.git] / src / frontends / qt2 / QBibtexDialog.C
1 /**
2  * \file QBibtexDialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
14 #include "ControlBibtex.h"
15 #include "qt_helpers.h"
16 #include "debug.h"
17
18 #include "support/filetools.h"
19 #include "support/lyxalgo.h" // eliminate_duplicates
20
21 #include <qwidget.h>
22 #include <qpushbutton.h>
23 #include <qcombobox.h>
24 #include <qlineedit.h>
25 #include <qlistbox.h>
26 #include <qcheckbox.h>
27 #include <qfiledialog.h>
28
29 #include "ui/QBibtexAddDialogBase.h"
30 #include "QBibtexDialog.h"
31 #include "QBibtex.h"
32
33 #include <vector>
34
35 using namespace lyx::support;
36
37 QBibtexDialog::QBibtexDialog(QBibtex * form)
38         : QBibtexDialogBase(0, 0, false, 0),
39         form_(form)
40 {
41         connect(okPB, SIGNAL(clicked()),
42                 form, SLOT(slotOK()));
43         connect(closePB, SIGNAL(clicked()),
44                 form, SLOT(slotClose()));
45
46         add_ = new QBibtexAddDialogBase(this, "", true);
47         connect(add_->addPB, SIGNAL(clicked()), this, SLOT(addDatabase()));
48         connect(add_->addPB, SIGNAL(clicked()), this, SLOT(addDatabase()));
49         connect(add_->bibLB, SIGNAL(selected(QListBoxItem *)), this, SLOT(addDatabase()));
50         connect(add_->bibLB, SIGNAL(selected(QListBoxItem *)), add_, SLOT(accept()));
51         connect(add_->bibLB, SIGNAL(currentChanged(QListBoxItem *)), this, SLOT(availableChanged()));
52         connect(add_->browsePB, SIGNAL(clicked()), this, SLOT(browseBibPressed()));
53 }
54
55
56 void QBibtexDialog::change_adaptor()
57 {
58         form_->changed();
59 }
60
61
62 void QBibtexDialog::browsePressed()
63 {
64         QString const file =
65                 QFileDialog::getOpenFileName(QString::null,
66                                              qt_("BibTeX style files (*.bst)"),
67                                              this,
68                                              0,
69                                              qt_("Select a BibTeX style"));
70         if (!file.isNull()) {
71                 string const filen = ChangeExtension(fromqstr(file), "");
72                 bool present = false;
73                 unsigned int pres = 0;
74
75                 for (int i = 0; i != styleCB->count(); ++i) {
76                         if (fromqstr(styleCB->text(i)) == filen) {
77                                 present = true;
78                                 pres = i;
79                         }
80                 }
81
82                 if (!present)
83                         styleCB->insertItem(toqstr(filen),0);
84
85                 styleCB->setCurrentItem(pres);
86                 form_->changed();
87         }
88 }
89
90
91 void QBibtexDialog::browseBibPressed()
92 {
93         QString const file = QFileDialog::getOpenFileName(QString::null,
94                 qt_("BibTeX database files (*.bib)"), add_, 0, qt_("Select a BibTeX database to add"));
95
96         if (!file.isNull()) {
97                 string const f = ChangeExtension(fromqstr(file), "");
98                 bool present = false;
99
100                 for (unsigned int i = 0; i != add_->bibLB->count(); i++) {
101                         if (fromqstr(add_->bibLB->text(i)) == f)
102                                 present = true;
103                 }
104
105                 if (!present) {
106                         add_->bibLB->insertItem(toqstr(f));
107                         form_->changed();
108                 }
109
110                 add_->bibED->setText(toqstr(f));
111         }
112 }
113
114 void QBibtexDialog::addPressed()
115 {
116         add_->exec();
117 }
118
119
120 void QBibtexDialog::addDatabase()
121 {
122         QString const file = add_->bibED->text();
123
124         if (!file.isNull()) {
125                 string const f = ChangeExtension(fromqstr(file), "");
126                 bool present = false;
127                 for (unsigned int i = 0; i != databaseLB->count(); ++i) {
128                         if (fromqstr(databaseLB->text(i)) == f)
129                                 present = true;
130
131                 }
132                 if (!present) {
133                         databaseLB->insertItem(f.c_str());
134                         form_->changed();
135                 }
136         }
137 }
138
139
140 void QBibtexDialog::deletePressed()
141 {
142         databaseLB->removeItem(databaseLB->currentItem());
143 }
144
145
146
147 void QBibtexDialog::databaseChanged()
148 {
149         deletePB->setEnabled(!form_->readOnly() && databaseLB->currentItem() != -1);
150 }
151
152
153 void QBibtexDialog::availableChanged()
154 {
155         add_->bibED->setText(add_->bibLB->currentText());
156 }
157
158
159 void QBibtexDialog::closeEvent(QCloseEvent *e)
160 {
161         form_->slotWMHide();
162         e->accept();
163 }