summaryrefslogtreecommitdiffstats
path: root/kbabel/kbabeldict/dictchooser.cpp
blob: f9360b6146cbbebd5b9489bb77d46de8c8d906b0 (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 Qt library by Trolltech AS, Norway (or with modified versions
  of Qt that use the same license as Qt), 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
  Qt. 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 <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <kconfig.h>
#include <klistbox.h>
#include <klocale.h>
#include <kdialog.h>
#include <ktempfile.h>

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

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


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

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

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


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

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


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


    dictList.setAutoDelete(true);
    ModuleInfo *mi;
    for(QStringList::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,SIGNAL(highlighted(int)), this
            , SLOT(selectedMarked(int)));
    connect(unselectedBox,SIGNAL(highlighted(int)), this
            , SLOT(unselectedMarked(int)));

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


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

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


QStringList DictChooser::selectedDicts()
{
    QStringList selected;

    for(int i = 0; i < (int)selectedBox->count(); i++)
    {
        QString 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)
    {
        QString 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)
    {
        QString 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)
    {
        QString 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)
    {
        QString text = selectedBox->text(i);
        selectedBox->changeItem(selectedBox->text(i+1),i);
        selectedBox->changeItem(text,i+1);
        selectedBox->setSelected(i+1,true);
    }
}


bool DictChooser::eventFilter(QObject *object, QEvent *event)
{
    if(event->type() == QEvent::FocusIn)
    {
        if(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
    QString 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;
		KConfig 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
    QDictIterator<KTempFile> it( tempConfig ); // See QDictIterator
    for( ; it.current(); ++it )
    {
        KConfig config( it.current()->name() );
	config.setGroup( it.currentKey() );
	box->readSettings( it.currentKey(), &config);
    }

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

#include "dictchooser.moc"