1 /* -------------------------------------------------------------
3 sets.cpp (part of The KDE Dictionary Client)
5 Copyright (C) 2000-2001 Christian Gebauer <gebauer@kde.org>
7 This file is distributed under the Artistic License.
8 See LICENSE for details.
10 -------------------------------------------------------------
12 DbSetsDialog dialog for editing the user defined database sets
14 ------------------------------------------------------------- */
16 #include <qpushbutton.h>
19 #include <qcombobox.h>
22 #include <QVBoxLayout>
24 #include <QHBoxLayout>
25 #include <QGridLayout>
27 #include <kiconloader.h>
28 #include <kseparator.h>
35 //********* DbSetsDialog ******************************************
38 DbSetsDialog::DbSetsDialog(QWidget
*parent
)
41 setCaption(i18n("Database Sets"));
42 setButtons(Close
| Help
);
43 setDefaultButton(Close
);
45 QFrame
*page
= new QFrame(this);
49 for(unsigned int i
=1;i
<global
->databaseSets
.count()+1;i
++)
50 sets
.append(global
->databases
[i
]);
52 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
53 topLayout
->setSpacing(0);
54 topLayout
->setMargin(0);
56 QHBoxLayout
* subLayout1
= new QHBoxLayout
;
57 subLayout1
->setSpacing(5);
58 topLayout
->addLayout(subLayout1
,0);
60 w_set
= new QComboBox(true,page
);
61 w_set
->setFixedHeight(w_set
->sizeHint().height());
62 w_set
->setInsertionPolicy (QComboBox::NoInsertion
);
63 w_set
->insertStringList(sets
);
64 connect(w_set
, SIGNAL(activated(int)),this, SLOT(activateSet(int)));
65 QLabel
*l
= new QLabel(w_set
, i18n("&Set:"),page
);
66 l
->setMinimumSize(l
->sizeHint());
67 subLayout1
->addWidget(l
,0);
68 subLayout1
->addWidget(w_set
,1);
70 subLayout1
->addSpacing(8);
72 w_save
= new QPushButton(i18n("S&ave"),page
);
73 connect(w_save
,SIGNAL(clicked()),this, SLOT(transferSet()));
74 subLayout1
->addWidget(w_save
,0);
76 QPushButton
*btn
= new QPushButton(i18n("&New"),page
);
77 btn
->setMinimumSize(btn
->sizeHint());
78 connect(btn
, SIGNAL(clicked()),this, SLOT(newPressed()));
79 subLayout1
->addWidget(btn
,0);
81 w_delete
= new QPushButton(i18n("&Delete"),page
);
82 w_delete
->setMinimumSize(w_delete
->sizeHint());
83 connect(w_delete
, SIGNAL(clicked()),this, SLOT(deletePressed()));
84 subLayout1
->addWidget(w_delete
,0);
86 topLayout
->addSpacing(8);
88 KSeparator
*sep
= new KSeparator(page
);
89 topLayout
->addWidget(sep
,0);
91 topLayout
->addSpacing(8);
93 QGridLayout
* subLayout2
= new QGridLayout();
94 subLayout2
->setSpacing(6);
95 topLayout
->addLayout(subLayout2
,1);
97 w_leftBox
= new Q3ListBox(page
);
98 connect(w_leftBox
, SIGNAL(selected(int)),this, SLOT(leftSelected(int)));
99 connect(w_leftBox
, SIGNAL(highlighted(int)),this, SLOT(leftHighlighted(int)));
100 QLabel
*leftLabel
= new QLabel(w_leftBox
, i18n("S&elected databases:"),page
);
101 leftLabel
->setMinimumSize(leftLabel
->sizeHint());
102 subLayout2
->addWidget(leftLabel
,0,0);
103 subLayout2
->addMultiCellWidget(w_leftBox
,1,6,0,0);
105 w_allLeft
= new QPushButton(page
);
106 w_allLeft
->setIconSet(BarIconSet("arrow-left-double"));
107 connect(w_allLeft
, SIGNAL(clicked()),this, SLOT(allLeftPressed()));
108 subLayout2
->addWidget(w_allLeft
,2,1);
110 w_left
= new QPushButton(page
);
111 w_left
->setIconSet(BarIconSet("arrow-left"));
112 connect(w_left
, SIGNAL(clicked()),this, SLOT(leftPressed()));
113 subLayout2
->addWidget(w_left
,3,1);
115 w_right
= new QPushButton(page
);
116 w_right
->setIconSet(BarIconSet("arrow-right"));
117 connect(w_right
, SIGNAL(clicked()),this, SLOT(rightPressed()));
118 subLayout2
->addWidget(w_right
,4,1);
120 w_allRight
= new QPushButton(page
);
121 w_allRight
->setIconSet(BarIconSet("arrow-right-double"));
122 connect(w_allRight
, SIGNAL(clicked()),this, SLOT(allRightPressed()));
123 subLayout2
->addWidget(w_allRight
,5,1);
125 w_rightBox
= new Q3ListBox(page
);
126 connect(w_rightBox
, SIGNAL(selected(int)),this, SLOT(rightSelected(int)));
127 connect(w_rightBox
, SIGNAL(highlighted(int)),this, SLOT(rightHighlighted(int)));
128 QLabel
*rightLabel
= new QLabel(w_rightBox
, i18n("A&vailable databases:"),page
);
129 rightLabel
->setMinimumSize(rightLabel
->sizeHint());
130 subLayout2
->addWidget(rightLabel
,0,2);
131 subLayout2
->addMultiCellWidget(w_rightBox
,1,6,2,2);
133 subLayout2
->setRowStretch(1,1);
134 subLayout2
->setRowStretch(6,1);
135 subLayout2
->setColStretch(0,1);
136 subLayout2
->setColStretch(2,1);
138 setHelp("database-sets");
140 if (global
->setsSize
.isValid())
141 resize(global
->setsSize
);
145 if ((global
->currentDatabase
>=1)&&(global
->currentDatabase
<=global
->databaseSets
.count()))
146 activateSet(global
->currentDatabase
-1);
153 void DbSetsDialog::hideEvent(QHideEvent
*)
155 global
->setsSize
= size();
156 emit(dialogClosed());
160 void DbSetsDialog::newPressed()
162 QStringList
*temp
= new QStringList
;
163 temp
->append(i18n("New Set"));
164 global
->databaseSets
.append(temp
);
165 global
->databases
.insert(global
->databaseSets
.count(),i18n("New Set"));
166 if (global
->currentDatabase
>= global
->databaseSets
.count())
167 global
->currentDatabase
++;
169 QStringList sets
; // reread sets, because w_sets internal list is not correct in all cases
170 for(unsigned int i
=1;i
<global
->databaseSets
.count()+1;i
++)
171 sets
.append(global
->databases
[i
]);
173 w_set
->insertStringList(sets
);
175 activateSet(global
->databaseSets
.count()-1);
180 void DbSetsDialog::deletePressed()
182 int pos
= w_set
->currentItem();
184 global
->databaseSets
.remove(global
->databaseSets
.at(pos
));
185 global
->databases
.remove(global
->databases
.at(pos
+1));
186 if ((int)global
->currentDatabase
>= pos
+1)
187 global
->currentDatabase
--;
188 w_set
->removeItem(pos
);
189 if (pos
>= w_set
->count())
198 void DbSetsDialog::allLeftPressed()
200 while (w_rightBox
->count()) {
201 w_leftBox
->insertItem(w_rightBox
->text(0));
202 w_rightBox
->removeItem(0);
209 void DbSetsDialog::leftPressed()
211 int pos
= w_rightBox
->currentItem();
213 w_leftBox
->insertItem(w_rightBox
->text(pos
));
215 w_rightBox
->removeItem(pos
);
216 if (pos
>= (int)w_rightBox
->count())
219 w_rightBox
->setCurrentItem(pos
);
225 void DbSetsDialog::rightPressed()
227 int pos
= w_leftBox
->currentItem();
229 w_rightBox
->insertItem(w_leftBox
->text(pos
));
231 w_leftBox
->removeItem(pos
);
232 if (pos
>= (int)w_leftBox
->count())
235 w_leftBox
->setCurrentItem(pos
);
241 void DbSetsDialog::allRightPressed()
243 while (w_leftBox
->count()) {
244 w_rightBox
->insertItem(w_leftBox
->text(0));
245 w_leftBox
->removeItem(0);
252 void DbSetsDialog::closePressed()
255 global
->setsSize
= size();
256 emit(dialogClosed());
260 void DbSetsDialog::transferSet()
262 global
->databaseSets
.at(w_set
->currentItem())->clear();
263 global
->databaseSets
.at(w_set
->currentItem())->append(w_set
->currentText());
264 for (unsigned int i
= 0;i
<w_leftBox
->count();i
++)
265 global
->databaseSets
.at(w_set
->currentItem())->append(w_leftBox
->text(i
));
266 global
->databases
.replace(w_set
->currentItem()+1,w_set
->currentText());
267 w_set
->changeItem(w_set
->currentText(),w_set
->currentItem());
272 void DbSetsDialog::activateSet(int num
)
277 if ((num
< 0)||(num
>=(int)global
->databaseSets
.count())) {
279 w_delete
->setEnabled(false);
280 w_save
->setEnabled(false);
281 w_rightBox
->repaint(true); // Workaround for repaint-bug
282 w_leftBox
->repaint(true); // Workaround for repaint-bug
284 w_set
->setCurrentItem(num
);
285 for (int i
=0;i
<global
->serverDatabases
.count();i
++)
286 if (global
->databaseSets
.at(num
)->findIndex(global
->serverDatabases
[i
])>0)
287 w_leftBox
->insertItem(global
->serverDatabases
[i
]);
289 w_rightBox
->insertItem(global
->serverDatabases
[i
]);
292 w_delete
->setEnabled(true);
293 w_save
->setEnabled(true);
294 if (w_rightBox
->count()==0)
295 w_rightBox
->repaint(true); // Workaround for repaint-bug
296 if (w_leftBox
->count()==0)
297 w_leftBox
->repaint(true); // Workaround for repaint-bug
298 w_leftBox
->clearSelection();
299 w_leftBox
->centerCurrentItem();
300 w_rightBox
->clearSelection();
301 w_rightBox
->centerCurrentItem();
307 void DbSetsDialog::leftSelected(int)
313 void DbSetsDialog::rightSelected(int)
319 void DbSetsDialog::leftHighlighted(int)
321 w_right
->setEnabled(true);
325 void DbSetsDialog::rightHighlighted(int)
327 w_left
->setEnabled(true);
330 void DbSetsDialog::checkButtons()
332 w_allLeft
->setEnabled((w_rightBox
->count()>0));
333 w_allRight
->setEnabled((w_leftBox
->count()>0));
334 w_right
->setEnabled((w_leftBox
->currentItem()>=0));
335 w_left
->setEnabled((w_rightBox
->currentItem()>=0));
338 //--------------------------------