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