moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kvoctrain / kvoctrain / option-dialogs / PasteOptPage.cpp
blob89b42945069efe76f7cb5c95ad4647a35b34bc1e
1 /***************************************************************************
3 $Id$
5 clipboard options dialog page
7 -----------------------------------------------------------------------
9 begin : Sun Jun 27 11:07:24 1999
11 copyright : (C) 1999-2001 Ewald Arnold
12 (C) 2001 The KDE-EDU team
14 email : kvoctrain@ewald-arnold.de
16 -----------------------------------------------------------------------
19 ***************************************************************************/
21 /***************************************************************************
22 * *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
27 * *
28 ***************************************************************************/
30 #include <qlistbox.h>
31 #include <qlabel.h>
32 #include <qpushbutton.h>
33 #include <qcombobox.h>
34 #include <qcheckbox.h>
36 #include <iostream>
37 using namespace std;
39 #include <klocale.h>
42 #include <langset.h>
44 #include "PasteOptPage.h"
45 #include "GeneralOptionsDlg.h"
47 static const char *separator_name[] = {
48 ";", // 0
49 "#", // 1
50 "!", // 2
51 "|", // 3
52 ",", // 4
53 I18N_NOOP("TAB"), // 5
54 I18N_NOOP(">= 2 SPACES"), // 6
55 " : ", // 7
56 " :: ", // 8
61 static const char *separator_id[] = {
62 ";", // 0
63 "#", // 1
64 "!", // 2
65 "|", // 3
66 ",", // 4
67 "\t", // 5
68 " ", // 6
69 ":", // 7
70 "::", // 8
75 PasteOptPage::PasteOptPage
77 QString _sep,
78 LangSet &_langset,
79 QStringList &paste_list,
80 bool _useCurrent,
81 kvoctrainDoc *_doc,
82 QWidget *parent,
83 const char *name
86 PasteOptPageForm( parent, name ),
87 langset(_langset),
88 doc(_doc)
90 connect( kcfg_useCurrent, SIGNAL(toggled(bool)), SLOT(slotCurrentDoc(bool)) );
91 connect( item_down, SIGNAL(clicked()), SLOT(slotItemDown()) );
92 connect( item_skip, SIGNAL(clicked()), SLOT(slotItemSkip()) );
93 connect( item_up, SIGNAL(clicked()), SLOT(slotItemUp()) );
94 connect( kcfg_separator, SIGNAL(highlighted(int)), SLOT(slotSelectSep(int)) );
95 connect( order_list, SIGNAL(highlighted(int)), SLOT(slotSelectOrder(int)) );
98 setCaption(i18n("Options" ));
99 sep = _sep;
100 useCurrent = _useCurrent;
102 int sel = 0;
103 const char **seps = separator_name;
104 const char **sepid = separator_id;
105 while (*seps) {
106 if (*sepid == sep)
107 sel = seps - separator_name;
108 kcfg_separator->insertItem (i18n(*seps));
109 seps++;
110 sepid++;
112 kcfg_separator->setSizeLimit( seps - separator_name );
113 kcfg_separator->setCurrentItem(sel);
114 label_sep->setBuddy(kcfg_separator);//could be done in the ui file
116 order_list->clear();
117 for (int i = 0; i < (int) paste_list.count(); i++) {
118 QString codename = langset.findLongId(paste_list[i]);
120 if (codename.isEmpty())
121 codename = paste_list[i];
122 order_list->insertItem (codename);
125 // any new languages to add to order-list ?
126 for (int i = 0; i < (int) langset.size(); i++) {
127 bool found = false;
128 for (int j = 0; j < (int) order_list->count(); j++)
129 if (order_list->text(j) == langset.longId(i))
130 found = true;
132 if (!found)
133 order_list->insertItem(langset.longId(i));
136 order_list->setCurrentItem (0);
137 kcfg_useCurrent->setChecked (useCurrent);
138 order_list->setEnabled(!useCurrent);
139 item_down->setEnabled(!useCurrent);
140 item_skip->setEnabled(!useCurrent);
141 item_up->setEnabled(!useCurrent);
142 slotEnableCursor();
146 void PasteOptPage::initFocus() const
148 kcfg_separator->setFocus();
152 void PasteOptPage::slotSyncLangSet(const QString& dlg_name)
154 // skip "skip" elements at tail
155 if (dlg_name == i18n("&Clipboard")) {
156 // order_list contains doublettes??
157 for (int j = (int) order_list->count()-1; j > 1; j--) {
158 QString comp = order_list->text(j);
159 for (int i = (int) j-1; i >=0; i--) {
160 if ( order_list->text(i) == comp
161 && !order_list->text(i).isEmpty()) {
162 // cout << "remove " << j << " " << order_list->text(j).latin1() << endl;
163 order_list->removeItem(j);
164 break;
168 // order_list contains elements unknown in language set? remove them
169 for (int i = (int) order_list->count()-1; i >=0; i--) {
170 // cout << order_list->text(i).latin1() << endl;
171 if (!order_list->text(i).isEmpty())
172 if (langset.indexLongId(order_list->text(i)) < 0) {
173 order_list->removeItem(i);
176 // language set contains new elements? append them
177 for (int i = 0; i < (int) langset.size(); i++) {
178 // cout << langset.longId(i).latin1() << endl;
179 // cout << langset.shortId(i).latin1() << endl;
180 bool found = false;
181 for (int j = 0; j < (int) order_list->count(); j++)
182 if (langset.longId(i) == order_list->text(j))
183 found = true;
185 if (!found) {
186 if (langset.longId(i).isEmpty())
187 order_list->insertItem (langset.shortId(i));
188 else
189 order_list->insertItem (langset.longId(i));
194 int i;
195 for (i = (int) order_list->count()-1;
196 i >= 0 && order_list->text(i).isEmpty();
197 i--) {
198 if (order_list->currentItem() > (int) order_list->count()-2)
199 order_list->setCurrentItem(order_list->count()-2);
200 order_list->removeItem(i);
205 void PasteOptPage::slotSelectSep(int idx)
207 sep = separator_id[idx];
211 void PasteOptPage::slotSelectOrder(int )
213 slotEnableCursor();
217 QStringList PasteOptPage::getPasteOrder()
219 slotSyncLangSet(i18n("&Clipboard"));
220 QStringList sl;
221 // skip "skip" elements at tail
222 int i;
223 for (i = (int) order_list->count()-1;
224 i >= 0 && order_list->text(i).isEmpty();
225 i--);
227 // insert language codes and "skips" between them
228 for ( /**/; i >= 0; i--) {
229 QString code = langset.findShortId(order_list->text(i));
230 if (code.isEmpty())
231 code = order_list->text(i); // no longId previously available
232 if (order_list->text(i).isEmpty())
233 sl.insert (sl.begin(), "");
234 else
235 sl.insert (sl.begin(), code);
237 return sl;
241 void PasteOptPage::slotItemSkip()
243 int pos = order_list->currentItem();
244 order_list->insertItem("", order_list->currentItem());
245 order_list->setCurrentItem(pos);
246 slotSyncLangSet(i18n("&Clipboard"));
247 order_list->setFocus();
248 slotEnableCursor();
252 void PasteOptPage::slotEnableCursor()
254 order_list->setEnabled(!useCurrent);
255 if (order_list->count() > 1 && !useCurrent) {
256 item_down->setEnabled(order_list->currentItem() < (int) order_list->count()-1);
257 item_skip->setEnabled(true);
258 item_up->setEnabled(order_list->currentItem() != 0);
260 else {
261 item_down->setEnabled(false);
262 item_skip->setEnabled(false);
263 item_up->setEnabled(false);
268 void PasteOptPage::slotItemUp()
270 if (order_list->currentItem() > 0
271 && (int) order_list->count() > 1) {
272 int pos = order_list->currentItem();
273 QString item = order_list->text(pos);
274 order_list->removeItem(pos);
275 order_list->insertItem(item, pos-1);
276 order_list->setCurrentItem(pos-1);
277 slotSyncLangSet(i18n("&Clipboard"));
278 order_list->setFocus();
280 slotEnableCursor();
284 void PasteOptPage::slotItemDown()
286 if ((int)order_list->currentItem() < (int)order_list->count()-1
287 && (int) order_list->count() > 1) {
288 int pos = order_list->currentItem();
289 QString item = order_list->text(pos);
290 order_list->removeItem(pos);
291 order_list->insertItem(item, pos+1);
292 order_list->setCurrentItem(pos+1);
293 slotSyncLangSet(i18n("&Clipboard"));
294 order_list->setFocus();
296 slotEnableCursor();
300 void PasteOptPage::slotCurrentDoc(bool use)
303 order_list->clear();
304 order_list->insertItem (langset.findLongId(doc->getOriginalIdent()));
305 for (int i = 1; i < (int) doc->numLangs(); i++)
306 order_list->insertItem (langset.findLongId(doc->getIdent(i)));
308 // any other languages to append besides those from doc?
309 for (int i = 0; i < (int) langset.size(); i++) {
310 bool found = false;
311 for (int j = 0; j < (int) order_list->count(); j++)
312 if (order_list->text(j) == langset.longId(i))
313 found = true;
315 if (!found)
316 order_list->insertItem(langset.longId(i));
319 order_list->setCurrentItem (0);
321 useCurrent = use;
323 order_list->setEnabled(!useCurrent);
324 item_down->setEnabled(!useCurrent);
325 item_skip->setEnabled(!useCurrent);
326 item_up->setEnabled(!useCurrent);
328 slotEnableCursor();
332 void PasteOptPage::keyPressEvent( QKeyEvent *e )
334 if (e->state() & AltButton & ControlButton & ShiftButton == 0) {
335 if ( e->key() == Key_Escape )
336 emit reject();
337 else if ( e->key() == Key_Enter
338 ||e->key() == Key_Return)
339 emit accept();
340 else
341 e->ignore();
343 else
344 e->ignore();
346 #include "PasteOptPage.moc"