summaryrefslogtreecommitdiffstats
path: root/src/kicondialog.cpp
blob: a32f5cefd1a71a04bab93e0969045414cb52587f (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* vi: ts=8 sts=4 sw=4
 * kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle;
 *
 * This file is part of the KDE project, module tdefile.
 * Copyright (C) 2006 Luke Sandell <lasandell@gmail.com>
 *           (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
 *           (C) 2000 Geert Jansen <jansen@kde.org>
 *           (C) 2000 Kurt Granroth <granroth@kde.org>
 *           (C) 1997 Christoph Neerfeld <chris@kde.org>
 *
 * This is free software; it comes under the GNU Library General
 * Public License, version 2. See the file "COPYING.LIB" for the
 * exact licensing terms.
 */

#include "kicondialogui.h"
#include "kicondialog.h"
#include "kiconcanvas.h"

#include <config.h>

#include <kiconviewsearchline.h>

#include <kdebug.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kprogress.h>
#include <kiconview.h>
#include <tdefiledialog.h>
#include <kimagefilepreview.h>
#include <kpushbutton.h>
#include <tdemessagebox.h>

#include <tqstring.h>
#include <tqstringlist.h>
#include <tqsortedlist.h>
#include <tqimage.h>
#include <tqpixmap.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqtimer.h>
#include <tqbuttongroup.h>
#include <tqradiobutton.h>
#include <tqfileinfo.h>
#include <tqtoolbutton.h>
#include <tqwhatsthis.h>
#include <tqhbuttongroup.h>
#include <tqdragobject.h>

/* NOTE: Must be in the same order as listbox */
enum ExtendedContext
{
    ALL = 0,
    RECENT = 1,
    // Action thru MimeType, subtract 1 to convert to TDEIcon::Context
    OTHER = 7
};

class TDEIconDialog::TDEIconDialogPrivate
{
  public:
    TDEIconDialogPrivate() {
       	m_bStrictIconSize = true;
    }
    ~TDEIconDialogPrivate() {}
    bool m_bStrictIconSize;
    TQString custom;
    TQString customLocation;
    int recentMax;
    TQStringList recentList;
    ExtendedContext extendedContext;
    TDEIconDialogUI *ui; /* FIXME: KDE4 probably move this to the main class */
};

/*
 * TDEIconDialog: Dialog for selecting icons. Both system and user
 * specified icons can be chosen.
 */

TDEIconDialog::TDEIconDialog(TQWidget *parent, const char*)
    : KDialogBase(parent, "IconDialog", true, i18n("Select Icon"), Ok|Cancel, Ok)
{
    d = new TDEIconDialogPrivate;
    mpLoader = TDEGlobal::iconLoader();
    init();
    resize(minimumSize());
}

TDEIconDialog::TDEIconDialog(TDEIconLoader *loader, TQWidget *parent,
	const char *name)
    : KDialogBase(parent, name, true, i18n("Select Icon"), Ok|Cancel, Ok)
{
    d = new TDEIconDialogPrivate;
    mpLoader = loader;
    init();
}

void TDEIconDialog::init()
{
    mGroupOrSize = TDEIcon::Desktop;
    d->extendedContext = ALL;
    mType = 0;
    setCustomLocation(TQString()); // Initialize mFileList

    // Read configuration
    TDEConfig *config = TDEGlobal::config();
    TDEConfigGroupSaver saver(config, "TDEIconDialog");
    d->recentMax = config->readNumEntry("RecentMax", 10);
    d->recentList = config->readPathListEntry("RecentIcons");

    d->ui = new TDEIconDialogUI( this );
    setMainWidget(d->ui);

    d->ui->searchLine->setIconView(d->ui->iconCanvas);
    d->ui->searchLine->setCaseSensitive(false);

    // Hack standard Gui item, as KDevDesigner won't let us
    d->ui->browseButton->setText(i18n("&Browse..."));

    connect(d->ui->browseButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotBrowse()));
    connect(d->ui->listBox, TQT_SIGNAL(highlighted(int)), TQT_SLOT(slotContext(int)));
    connect(d->ui->iconCanvas, TQT_SIGNAL(executed(TQIconViewItem *)), TQT_SLOT(slotOk()));
    connect(d->ui->iconCanvas, TQT_SIGNAL(returnPressed(TQIconViewItem *)), TQT_SLOT(slotOk()));
    connect(d->ui->iconCanvas, TQT_SIGNAL(startLoading(int)), TQT_SLOT(slotStartLoading(int)));
    connect(d->ui->iconCanvas, TQT_SIGNAL(progress(int)), TQT_SLOT(slotProgress(int)));
    connect(d->ui->iconCanvas, TQT_SIGNAL(finished()), TQT_SLOT(slotFinished()));
    connect(this, TQT_SIGNAL(hidden()), d->ui->iconCanvas, TQT_SLOT(stopLoading()));

    // NOTE: this must be consistent with the IconType enum (see above)
    d->ui->listBox->insertItem(i18n("(All Icons)"));
    d->ui->listBox->insertItem(i18n("(Recent)"));
    d->ui->listBox->insertItem(i18n("Actions"));
    d->ui->listBox->insertItem(i18n("Applications"));
    d->ui->listBox->insertItem(i18n("Devices"));
    d->ui->listBox->insertItem(i18n("Filesystem"));
    d->ui->listBox->insertItem(i18n("File Types"));
    d->ui->listBox->insertItem(i18n("Miscellaneous"));
}

TDEIconDialog::~TDEIconDialog()
{
    // Write configuration
    TDEConfig *config = TDEGlobal::config();
    TDEConfigGroupSaver saver(config, "TDEIconDialog");
    config->writeEntry("RecentMax", d->recentMax, true, true);
    config->writePathEntry("RecentIcons", d->recentList, ',', true, true);

    delete d;
}

void TDEIconDialog::slotAcceptIcons()
{
    //FIXME: not used anymore
}

void TDEIconDialog::showIcons()
{
    d->ui->iconCanvas->clear();
    TQStringList filelist;

    TDEIcon::Context context = static_cast<TDEIcon::Context>(d->extendedContext - 1);
    switch (d->extendedContext)
    {
        case RECENT:
            filelist = d->recentList;
            break;
        case OTHER:
            filelist = mFileList;
            break;
        case ALL:
            filelist = mFileList;
            context = TDEIcon::Any;
            // ** Fallthrough to next case **
        default:
            TQStringList list;
            if (d->m_bStrictIconSize)
                list=mpLoader->queryIcons(mGroupOrSize, context);
            else
                list=mpLoader->queryIconsByContext(mGroupOrSize, context);

            // Remove path & extension
            for ( TQStringList::iterator it = list.begin(); it != list.end(); ++it)
                filelist.append(TQFileInfo(*it).baseName());
    }

    // Remove duplicate icons FIXME: TQt4 we can just use TQSet
    filelist.sort();
    TQString prev;
    for ( TQStringList::iterator it = filelist.begin(); it != filelist.end(); )
    {
        if (*it == prev)
        {
            it = filelist.remove(it);
        }
        else
        {
            prev = *it;
            ++it;
        }
    }

    d->ui->iconCanvas->setGroupOrSize(mGroupOrSize);
    d->ui->iconCanvas->loadFiles(filelist);
}

void TDEIconDialog::setStrictIconSize(bool b)
{
    d->m_bStrictIconSize=b;
}

bool TDEIconDialog::strictIconSize() const
{
    return d->m_bStrictIconSize;
}

void TDEIconDialog::setIconSize( int size )
{
    // see TDEIconLoader, if you think this is weird
    if ( size == 0 )
        mGroupOrSize = TDEIcon::Desktop; // default Group
    else
        mGroupOrSize = -size; // yes, TDEIconLoader::queryIconsByContext is weird
}

int TDEIconDialog::iconSize() const
{
    // 0 or any other value ==> mGroupOrSize is a group, so we return 0
    return (mGroupOrSize < 0) ? -mGroupOrSize : 0;
}

#ifndef KDE_NO_COMPAT
TQString TDEIconDialog::selectIcon(TDEIcon::Group group, TDEIcon::Context context, bool user)
{
    setup( group, context, false, 0, user );
    return openDialog();
}
#endif

void TDEIconDialog::setup(TDEIcon::Group group, TDEIcon::Context context,
                        bool strictIconSize, int iconSize, bool user )
{
    setup(group, context, strictIconSize, iconSize, user, false, false);
}

void TDEIconDialog::setup(TDEIcon::Group group, TDEIcon::Context context,
                        bool strictIconSize, int iconSize, bool user,
                        bool lockContext, bool lockBrowse )
{
    d->m_bStrictIconSize = strictIconSize;
    d->ui->iconCanvas->setStrictIconSize(strictIconSize);
    mGroupOrSize = (iconSize == 0) ? group : -iconSize;
    mType = user;

    d->extendedContext = static_cast<ExtendedContext>( ( context == TDEIcon::Any ) ? ALL : context + 1 );

    // We cannot change layout because it is protected ;-(
    // FIXME: TQt4 we will be able to inherit from both TQDialog and our GUI
    d->ui->listBox->setEnabled(!lockContext);
    d->ui->browseButton->setEnabled(!lockBrowse);
    d->ui->listBox->setHidden(lockContext && lockBrowse);
    d->ui->browseButton->setHidden(lockContext && lockBrowse);

    d->ui->listBox->setCurrentItem(d->extendedContext);
}

const TQString & TDEIconDialog::customLocation( ) const
{
    return d->customLocation;
}

void TDEIconDialog::setCustomLocation( const TQString& location )
{
    d->customLocation = location;

    if (location.isEmpty())
    {
        mFileList = TDEGlobal::dirs()->findAllResources("appicon", TQString::fromLatin1("*.png"));
    } else {
        mFileList = mpLoader->queryIconsByDir(location);
    }
}

TQString TDEIconDialog::openDialog()
{
    showIcons();

    if ( exec() == Accepted )
    {
         if (!d->custom.isEmpty())
             return d->custom;
         else
             return d->ui->iconCanvas->getCurrent();
    }
    else
    {
        return TQString();
    }
}

void TDEIconDialog::showDialog()
{
    d->custom = TQString();

    // Make it so minimumSize returns correct value
    d->ui->filterLabel->hide();
    d->ui->searchLine->hide();
    d->ui->progressBar->show();

    setModal(false);
    show();

    // FIXME: this should be before show() but it doesn't work ;-(
    resize(minimumSize());

    showIcons();
}

void TDEIconDialog::slotOk()
{
    TQString key = !d->custom.isEmpty() ? d->custom : d->ui->iconCanvas->getCurrent();

    // Append to list of recent icons
    if (!d->recentList.contains(key))
    {
        d->recentList.push_back(key);

        // Limit recent list in size
        while ( (int)d->recentList.size() > d->recentMax )
            d->recentList.pop_front();
    }

    emit newIconName(key);
    KDialogBase::slotOk();
}

TQString TDEIconDialog::getIcon(TDEIcon::Group group, TDEIcon::Context context,
                             bool strictIconSize, int iconSize, bool user,
                             TQWidget *parent, const TQString &caption)
{
    TDEIconDialog dlg(parent, "icon dialog");
    dlg.setup( group, context, strictIconSize, iconSize, user );
    if (!caption.isNull())
        dlg.setCaption(caption);

    return dlg.openDialog();
}

void TDEIconDialog::slotBrowse()
{
    // Create a file dialog to select a PNG, XPM or SVG file,
    // with the image previewer shown.
    // KFileDialog::getImageOpenURL doesn't allow svg.
    KFileDialog dlg(TQString(), i18n("*.png *.xpm *.svg *.svgz|Icon Files (*.png *.xpm *.svg *.svgz)"),
                    this, "filedialog", true);
    dlg.setOperationMode( KFileDialog::Opening );
    dlg.setCaption( i18n("Open") );
    dlg.setMode( KFile::File | KFile::ExistingOnly | KFile::LocalOnly );
    KImageFilePreview *ip = new KImageFilePreview( &dlg );
    dlg.setPreviewWidget( ip );
    dlg.exec();

    TQString file = dlg.selectedFile();
    if (!file.isEmpty())
    {
        d->custom = file;
        if ( mType == 1 )
            setCustomLocation(TQFileInfo( file ).dirPath( true ));
        slotOk();
    }
}

void TDEIconDialog::slotContext(int id)
{
    d->extendedContext = static_cast<ExtendedContext>(id);
    showIcons();
}

void TDEIconDialog::slotStartLoading(int steps)
{
    if (steps < 10)
    	d->ui->progressBar->hide();
    else
    {
        d->ui->progressBar->setTotalSteps(steps);
        d->ui->progressBar->setProgress(0);
        d->ui->progressBar->show();
        d->ui->filterLabel->hide();
        d->ui->searchLine->hide();
    }
}

void TDEIconDialog::slotProgress(int p)
{
    d->ui->progressBar->setProgress(p);
}

void TDEIconDialog::slotFinished()
{
    d->ui->progressBar->hide();
    d->ui->filterLabel->show();
    d->ui->searchLine->show();
}

class TDEIconButton::TDEIconButtonPrivate
{
  public:
    TDEIconButtonPrivate() {
        m_bStrictIconSize = false;
        iconSize = 0; // let TDEIconLoader choose the default
    }
    ~TDEIconButtonPrivate() {}
    bool m_bStrictIconSize;
    bool lockUser;
    bool lockCustom;
    int iconSize;
};


/*
 * TDEIconButton: A "choose icon" pushbutton.
 */

TDEIconButton::TDEIconButton(TQWidget *parent, const char *name)
    : TQPushButton(parent, name)
{
    init( TDEGlobal::iconLoader() );
}

TDEIconButton::TDEIconButton(TDEIconLoader *loader,
	TQWidget *parent, const char *name)
    : TQPushButton(parent, name)
{
    init( loader );
}

void TDEIconButton::init( TDEIconLoader *loader )
{
    d = new TDEIconButtonPrivate;
    mGroup = TDEIcon::Desktop;
    mContext = TDEIcon::Application;
    mbUser = false;

    mpLoader = loader;
    mpDialog = 0L;
    connect(this, TQT_SIGNAL(clicked()), TQT_SLOT(slotChangeIcon()));
}

TDEIconButton::~TDEIconButton()
{
    delete mpDialog;
    delete d;
}

void TDEIconButton::setStrictIconSize(bool b)
{
    d->m_bStrictIconSize=b;
}

bool TDEIconButton::strictIconSize() const
{
    return d->m_bStrictIconSize;
}

void TDEIconButton::setIconSize( int size )
{
    d->iconSize = size;
}

int TDEIconButton::iconSize() const
{
    return d->iconSize;
}

void TDEIconButton::setIconType(TDEIcon::Group group, TDEIcon::Context context, bool user)
{
    mGroup = group;
    mContext = context;
    mbUser = user;
    d->lockUser = false;
    d->lockCustom = false;
}

void TDEIconButton::setIconType(TDEIcon::Group group, TDEIcon::Context context, bool user, bool lockUser, bool lockCustom)
{
    mGroup = group;
    mContext = context;
    mbUser = user;
    d->lockUser = lockUser;
    d->lockCustom = lockCustom;
}

void TDEIconButton::setIcon(const TQString& icon)
{
    mIcon = icon;
    setIconSet(mpLoader->loadIconSet(mIcon, mGroup, d->iconSize));

    if (!mpDialog)
    {
        mpDialog = new TDEIconDialog(mpLoader, this);
        connect(mpDialog, TQT_SIGNAL(newIconName(const TQString&)), TQT_SLOT(newIconName(const TQString&)));
    }
}

const TQString & TDEIconButton::customLocation( ) const
{
    return mpDialog ? mpDialog->customLocation() : TQString();
}

void TDEIconButton::setCustomLocation(const TQString &custom)
{
    if (!mpDialog)
    {
        mpDialog = new TDEIconDialog(mpLoader, this);
        connect(mpDialog, TQT_SIGNAL(newIconName(const TQString&)), TQT_SLOT(newIconName(const TQString&)));
    }

    mpDialog->setCustomLocation(custom);
}

void TDEIconButton::resetIcon()
{
    mIcon = TQString();
    setIconSet(TQIconSet());
}

void TDEIconButton::slotChangeIcon()
{
    if (!mpDialog)
    {
        mpDialog = new TDEIconDialog(mpLoader, this);
        connect(mpDialog, TQT_SIGNAL(newIconName(const TQString&)), TQT_SLOT(newIconName(const TQString&)));
    }

    mpDialog->setup( mGroup, mContext, d->m_bStrictIconSize, d->iconSize, mbUser, d->lockUser, d->lockCustom );
    mpDialog->showDialog();
}

void TDEIconButton::newIconName(const TQString& name)
{
    if (name.isEmpty())
        return;

    TQIconSet iconset = mpLoader->loadIconSet(name, mGroup, d->iconSize);
    setIconSet(iconset);
    mIcon = name;

    emit iconChanged(name);
}

void TDEIconCanvas::virtual_hook( int id, void* data )
{ TDEIconView::virtual_hook( id, data ); }

void TDEIconDialog::virtual_hook( int id, void* data )
{ KDialogBase::virtual_hook( id, data ); }

#include "kicondialog.moc"