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