]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QBibtexDialog.C
Wrap most of the frontend code up inside namespace lyx::frontend.
[lyx.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 #include "qt_helpers.h"
14
15 #include "support/filetools.h"
16
17 #include <qpushbutton.h>
18 #include <qcombobox.h>
19 #include <qlineedit.h>
20 #include <qlistbox.h>
21 #include <qfiledialog.h>
22
23 #include "ui/QBibtexAddDialogBase.h"
24 #include "QBibtexDialog.h"
25 #include "QBibtex.h"
26
27 using lyx::support::ChangeExtension;
28
29 using std::string;
30
31 namespace lyx {
32 namespace frontend {
33
34 QBibtexDialog::QBibtexDialog(QBibtex * form)
35         : QBibtexDialogBase(0, 0, false, 0),
36         form_(form)
37 {
38         connect(okPB, SIGNAL(clicked()),
39                 form, SLOT(slotOK()));
40         connect(closePB, SIGNAL(clicked()),
41                 form, SLOT(slotClose()));
42
43         add_ = new QBibtexAddDialogBase(this, "", true);
44         connect(add_->addPB, SIGNAL(clicked()), this, SLOT(addDatabase()));
45         connect(add_->addPB, SIGNAL(clicked()), this, SLOT(addDatabase()));
46         connect(add_->bibLB, SIGNAL(selected(QListBoxItem *)), this, SLOT(addDatabase()));
47         connect(add_->bibLB, SIGNAL(selected(QListBoxItem *)), add_, SLOT(accept()));
48         connect(add_->bibLB, SIGNAL(currentChanged(QListBoxItem *)), this, SLOT(availableChanged()));
49         connect(add_->browsePB, SIGNAL(clicked()), this, SLOT(browseBibPressed()));
50 }
51
52
53 void QBibtexDialog::change_adaptor()
54 {
55         form_->changed();
56 }
57
58
59 void QBibtexDialog::browsePressed()
60 {
61         QString const file =
62                 QFileDialog::getOpenFileName(QString::null,
63                                              qt_("BibTeX style files (*.bst)"),
64                                              this,
65                                              0,
66                                              qt_("Select a BibTeX style"));
67         if (!file.isNull()) {
68                 string const filen = ChangeExtension(fromqstr(file), "");
69                 bool present = false;
70                 unsigned int pres = 0;
71
72                 for (int i = 0; i != styleCB->count(); ++i) {
73                         if (fromqstr(styleCB->text(i)) == filen) {
74                                 present = true;
75                                 pres = i;
76                         }
77                 }
78
79                 if (!present)
80                         styleCB->insertItem(toqstr(filen),0);
81
82                 styleCB->setCurrentItem(pres);
83                 form_->changed();
84         }
85 }
86
87
88 void QBibtexDialog::browseBibPressed()
89 {
90         QString const file = QFileDialog::getOpenFileName(QString::null,
91                 qt_("BibTeX database files (*.bib)"), add_, 0, qt_("Select a BibTeX database to add"));
92
93         if (!file.isNull()) {
94                 string const f = ChangeExtension(fromqstr(file), "");
95                 bool present = false;
96
97                 for (unsigned int i = 0; i != add_->bibLB->count(); i++) {
98                         if (fromqstr(add_->bibLB->text(i)) == f)
99                                 present = true;
100                 }
101
102                 if (!present) {
103                         add_->bibLB->insertItem(toqstr(f));
104                         form_->changed();
105                 }
106
107                 add_->bibED->setText(toqstr(f));
108         }
109 }
110
111 void QBibtexDialog::addPressed()
112 {
113         add_->exec();
114 }
115
116
117 void QBibtexDialog::addDatabase()
118 {
119         int const sel = add_->bibLB->currentItem();
120         QString const file = add_->bibED->text();
121
122         if (sel < 0 && file.isNull())
123                 return;
124
125         // Add the selected browser_bib keys to browser_database
126         // multiple selections are possible
127         for (unsigned int i = 0; i != add_->bibLB->count(); i++) {
128                 if (add_->bibLB->isSelected(i)) {
129                         // do not allow duplicates
130                         if ((databaseLB->findItem(add_->bibLB->text(i))) == 0)
131                                 databaseLB->insertItem(add_->bibLB->text(i));
132                 }
133         }
134
135         if (!file.isEmpty()) {
136                 QString const f = toqstr(ChangeExtension(fromqstr(file), ""));
137                 if ((databaseLB->findItem(f)) == 0)
138                         databaseLB->insertItem(f);
139         }
140
141         form_->changed();
142 }
143
144
145 void QBibtexDialog::deletePressed()
146 {
147         databaseLB->removeItem(databaseLB->currentItem());
148 }
149
150
151
152 void QBibtexDialog::databaseChanged()
153 {
154         deletePB->setEnabled(!form_->readOnly() && databaseLB->currentItem() != -1);
155 }
156
157
158 void QBibtexDialog::availableChanged()
159 {
160         form_->changed();
161 }
162
163
164 void QBibtexDialog::closeEvent(QCloseEvent *e)
165 {
166         form_->slotWMHide();
167         e->accept();
168 }
169
170 } // namespace frontend
171 } // namespace lyx