summaryrefslogtreecommitdiffstats
path: root/kbabel/kbabeldict/dictchooser.cpp
blob: 796cf51fe2ec07d9b2e87851e34dd20495e199e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* ****************************************************************************
  This file is part of KBabel

  Copyright (C) 1999-2001 by Matthias Kiefer
                            <matthias.kiefer@gmx.de>
		2003	  by Stanislav Visnovsky
			    <visnovsky@kde.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  In addition, as a special exception, the copyright holders give
  permission to link the code of this program with any edition of
  the TQt library by Trolltech AS, Norway (or with modified versions
  of TQt that use the same license as TQt), and distribute linked
  combinations including the two.  You must obey the GNU General
  Public License in all respects for all of the code used other than
  TQt. If you modify this file, you may extend this exception to
  your version of the file, but you are not obligated to do so.  If
  you do not wish to do so, delete this exception statement from
  your version.

**************************************************************************** */
#include "dictchooser.h"
#include "resources.h"

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tdeconfig.h>
#include <tdelistbox.h>
#include <klocale.h>
#include <kdialog.h>
#include <ktempfile.h>

DictChooser::DictChooser(KBabelDictBox*b, TQStringList selected
                    , TQWidget *parent, const char *name)
           : TQWidget(parent,name), box(b)
{
    tempConfig.clear();
    tempConfig.setAutoDelete(true);

    dictList = box->moduleInfos();
    TQGridLayout *layout = new TQGridLayout(this);
    layout->setSpacing(KDialog::spacingHint());


    TQLabel *label = new TQLabel(i18n("dictionary to not use","Do not use:"),this);
    layout->addWidget(label,0,0);

    label = new TQLabel(i18n("dictionary to use","Use:"),this);
    layout->addWidget(label,0,2);

    unselectedBox = new TDEListBox(this,"unselectedBox");
    layout->addWidget(unselectedBox,1,0);


    TQVBoxLayout *bLayout = new TQVBoxLayout();
    selectBtn = new TQPushButton("  &>>  ", this);
    selectBtn->setEnabled(false);
    bLayout->addWidget(selectBtn);
    unselectBtn = new TQPushButton("  &<<  ", this);
    unselectBtn->setEnabled(false);
    bLayout->addWidget(unselectBtn);
    bLayout->addStretch();
    layout->addLayout(bLayout,1,1);

    selectedBox = new TDEListBox(this,"selectedBox");
    layout->addWidget(selectedBox,1,2);


    bLayout = new TQVBoxLayout();
    upBtn = new TQPushButton(i18n("Move &Up"), this);
    upBtn->setEnabled(false);
    bLayout->addWidget(upBtn);
    downBtn = new TQPushButton(i18n("Move &Down"), this);
    downBtn->setEnabled(false);
    bLayout->addWidget(downBtn);
    configureBtn = new TQPushButton(i18n("Con&figure..."), this);
    bLayout->addWidget(configureBtn);
    bLayout->addStretch();
    layout->addLayout(bLayout,1,3);


    dictList.setAutoDelete(true);
    ModuleInfo *mi;
    for(TQStringList::Iterator it=selected.begin(); it!=selected.end();
            ++it)
    {
        for(mi = dictList.first(); mi != 0; mi = dictList.next())
        {
            if(mi->id==*it)
            {
                selectedBox->insertItem(mi->name);
            }
        }
    }

    for(mi = dictList.first(); mi != 0; mi = dictList.next())
    {
        if(!selected.contains(mi->id))
        {
            unselectedBox->insertItem(mi->name);
        }
    }

    if(selectedBox->count() == 0 && unselectedBox->count() > 0)
    {
        selectedBox->insertItem(unselectedBox->text(0));
        unselectedBox->removeItem(0);
    }


    connect(selectedBox,TQT_SIGNAL(highlighted(int)), this
            , TQT_SLOT(selectedMarked(int)));
    connect(unselectedBox,TQT_SIGNAL(highlighted(int)), this
            , TQT_SLOT(unselectedMarked(int)));

    connect(selectBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(select()));
    connect(unselectBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(unselect()));
    connect(upBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(up()));
    connect(downBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(down()));
    connect(configureBtn,TQT_SIGNAL(clicked()), this
            , TQT_SLOT(configureSelected()));


    selectedBox->installEventFilter(this);
    unselectedBox->installEventFilter(this);

    int min = minimumHeight();
    if(min < 100)
        setMinimumHeight(100);
}


TQStringList DictChooser::selectedDicts()
{
    TQStringList selected;

    for(int i = 0; i < (int)selectedBox->count(); i++)
    {
        TQString text = selectedBox->text(i);

        ModuleInfo *mi;
        for(mi = dictList.first(); mi!=0; mi = dictList.next())
        {
            if(mi->name == text)
            {
                selected.append(mi->id);
            }
        }
    }

    return selected;
}


void DictChooser::selectedMarked(int index)
{
    unselectedBox->selectAll(false);

    unselectBtn->setEnabled(selectedBox->count() > 1);
    selectBtn->setEnabled(false);
    downBtn->setEnabled((int)selectedBox->count()-1 > index);
    upBtn->setEnabled(index > (int)0);
}

void DictChooser::unselectedMarked(int)
{
    selectedBox->selectAll(false);

    selectBtn->setEnabled(true);
    unselectBtn->setEnabled(false);
    downBtn->setEnabled(false);
    upBtn->setEnabled(false);
}


void DictChooser::select()
{
    int i = unselectedBox->currentItem();
    if(i >= 0)
    {
        TQString text = unselectedBox->text(i);
        selectedBox->insertItem(text);

        unselectedBox->removeItem(i);

        if(unselectedBox->count() == 0)
        {
            selectBtn->setEnabled(false);
        }
        else
        {
            unselectedBox->setSelected(i,true);
        }
    }
}


void DictChooser::unselect()
{
    int i = selectedBox->currentItem();
    if(i >= 0 && selectedBox->count() > 1)
    {
        TQString text = selectedBox->text(i);
        unselectedBox->insertItem(text);

        selectedBox->removeItem(i);

        if(i < (int)selectedBox->count()-1)
        {
            selectedBox->setSelected(i,true);
        }
        else
        {
            selectedBox->setSelected(i-1,true);
        }
    }
}


void DictChooser::up()
{
    int i = selectedBox->currentItem();
    if(i > 0)
    {
        TQString text = selectedBox->text(i);
        selectedBox->changeItem(selectedBox->text(i-1),i);
        selectedBox->changeItem(text,i-1);
        selectedBox->setSelected(i-1,true);
    }
}


void DictChooser::down()
{
    int i = selectedBox->currentItem();
    if(i < (int)selectedBox->count()-1)
    {
        TQString text = selectedBox->text(i);
        selectedBox->changeItem(selectedBox->text(i+1),i);
        selectedBox->changeItem(text,i+1);
        selectedBox->setSelected(i+1,true);
    }
}


bool DictChooser::eventFilter(TQObject *object, TQEvent *event)
{
    if(event->type() == TQEvent::FocusIn)
    {
        if(TQT_BASE_OBJECT(object)==TQT_BASE_OBJECT(selectedBox))
        {
            int i = selectedBox->currentItem();
            selectedBox->setSelected(i,true);
            selectedMarked(i);
            unselectedBox->clearSelection();
        }
        else
        {
            int i = unselectedBox->currentItem();
            if(i >= 0)
            {
                unselectedBox->setSelected(i,true);
                unselectedMarked(i);
                selectedBox->clearSelection();
            }
        }

        return true;
    }

    return false;
}

void DictChooser::configureSelected()
{
    int ci = selectedBox->currentItem();
    if( ci == -1 ) ci = 0; // there is always at least one item
    TQString module = selectedBox->text(ci);

    ModuleInfo *mi;
    for(mi = dictList.first(); mi!=0; mi = dictList.next())
    {
        if(mi->name == module)
        {
	    // do backup first
	    if( !tempConfig[mi->id])
	    {
		KTempFile* tmp=new KTempFile();
		tmp->setAutoDelete(true);
		tempConfig.insert(mi->id ,tmp);

		kdDebug(KBABEL_SEARCH) << "Temp file: " << tmp->name() << endl;
		TDEConfig config(tmp->name());
		config.setGroup(mi->id);
		box->saveSettings(mi->id, &config);
	    }

	    // now let user change configuration
	    box->configure(mi->id,true);
	    break;
        }
    }
}

void DictChooser::restoreConfig()
{
    kdDebug( KBABEL_SEARCH ) << "Cleanup" << endl;
    // cleanup
    TQDictIterator<KTempFile> it( tempConfig ); // See TQDictIterator
    for( ; it.current(); ++it )
    {
        TDEConfig config( it.current()->name() );
	config.setGroup( it.currentKey() );
	box->readSettings( it.currentKey(), &config);
    }

    // there is no temporary configs
    tempConfig.clear();
}

#include "dictchooser.moc"