Ran am2cmake.
[basket4.git] / src / kicondialog.cpp
blob5c372b7dce7c71407d673df739f2c44258af37d5
1 /* vi: ts=8 sts=4 sw=4
2 * kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle;
4 * This file is part of the KDE project, module kfile.
5 * Copyright (C) 2006 Luke Sandell <lasandell@gmail.com>
6 * (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
7 * (C) 2000 Geert Jansen <jansen@kde.org>
8 * (C) 2000 Kurt Granroth <granroth@kde.org>
9 * (C) 1997 Christoph Neerfeld <chris@kde.org>
11 * This is free software; it comes under the GNU Library General
12 * Public License, version 2. See the file "COPYING.LIB" for the
13 * exact licensing terms.
16 #include "kicondialogui.h"
17 #include "kicondialog.h"
18 #include "kiconcanvas.h"
20 #include <config.h>
22 #include <kiconviewsearchline.h>
24 #include <kdebug.h>
25 #include <kapplication.h>
26 #include <klocale.h>
27 #include <kglobal.h>
28 #include <kstandarddirs.h>
29 #include <kiconloader.h>
30 #include <kprogress.h>
31 #include <kiconview.h>
32 #include <kfiledialog.h>
33 #include <kimagefilepreview.h>
34 #include <kpushbutton.h>
35 #include <kmessagebox.h>
37 #include <qstring.h>
38 #include <qstringlist.h>
39 #include <q3sortedlist.h>
40 #include <qimage.h>
41 #include <qpixmap.h>
42 #include <qlabel.h>
43 #include <qcombobox.h>
44 #include <qtimer.h>
45 #include <q3buttongroup.h>
46 #include <qradiobutton.h>
47 #include <qfileinfo.h>
48 #include <qtoolbutton.h>
49 #include <q3whatsthis.h>
50 #include <qhbuttongroup.h>
51 #include <q3dragobject.h>
53 /* NOTE: Must be in the same order as listbox */
54 enum ExtendedContext
56 ALL = 0,
57 RECENT = 1,
58 // Action thru MimeType, subtract 1 to convert to KIcon::Context
59 OTHER = 7
62 class KIconDialog::KIconDialogPrivate
64 public:
65 KIconDialogPrivate() {
66 m_bStrictIconSize = true;
68 ~KIconDialogPrivate() {}
69 bool m_bStrictIconSize;
70 QString custom;
71 QString customLocation;
72 int recentMax;
73 QStringList recentList;
74 ExtendedContext extendedContext;
75 KIconDialogUI *ui; /* FIXME: KDE4 probably move this to the main class */
79 * KIconDialog: Dialog for selecting icons. Both system and user
80 * specified icons can be chosen.
83 KIconDialog::KIconDialog(QWidget *parent, const char*)
84 : KDialogBase(parent, "IconDialog", true, i18n("Select Icon"), Ok|Cancel, Ok)
86 d = new KIconDialogPrivate;
87 mpLoader = KGlobal::iconLoader();
88 init();
89 resize(minimumSize());
92 KIconDialog::KIconDialog(KIconLoader *loader, QWidget *parent,
93 const char *name)
94 : KDialogBase(parent, name, true, i18n("Select Icon"), Ok|Cancel, Ok)
96 d = new KIconDialogPrivate;
97 mpLoader = loader;
98 init();
101 void KIconDialog::init()
103 mGroupOrSize = KIcon::Desktop;
104 d->extendedContext = ALL;
105 mType = 0;
106 setCustomLocation(QString::null); // Initialize mFileList
108 // Read configuration
109 KConfig *config = KGlobal::config();
110 KConfigGroupSaver saver(config, "KIconDialog");
111 d->recentMax = config->readNumEntry("RecentMax", 10);
112 d->recentList = config->readPathListEntry("RecentIcons");
114 d->ui = new KIconDialogUI( this );
115 setMainWidget(d->ui);
117 d->ui->searchLine->setIconView(d->ui->iconCanvas);
118 d->ui->searchLine->setCaseSensitive(false);
120 // Hack standard Gui item, as KDevDesigner won't let us
121 d->ui->browseButton->setText(i18n("&Browse..."));
123 connect(d->ui->browseButton, SIGNAL(clicked()), SLOT(slotBrowse()));
124 connect(d->ui->listBox, SIGNAL(highlighted(int)), SLOT(slotContext(int)));
125 connect(d->ui->iconCanvas, SIGNAL(executed(Q3IconViewItem *)), SLOT(slotOk()));
126 connect(d->ui->iconCanvas, SIGNAL(returnPressed(Q3IconViewItem *)), SLOT(slotOk()));
127 connect(d->ui->iconCanvas, SIGNAL(startLoading(int)), SLOT(slotStartLoading(int)));
128 connect(d->ui->iconCanvas, SIGNAL(progress(int)), SLOT(slotProgress(int)));
129 connect(d->ui->iconCanvas, SIGNAL(finished()), SLOT(slotFinished()));
130 connect(this, SIGNAL(hidden()), d->ui->iconCanvas, SLOT(stopLoading()));
132 // NOTE: this must be consistent with the IconType enum (see above)
133 d->ui->listBox->insertItem(i18n("(All Icons)"));
134 d->ui->listBox->insertItem(i18n("(Recent)"));
135 d->ui->listBox->insertItem(i18n("Actions"));
136 d->ui->listBox->insertItem(i18n("Applications"));
137 d->ui->listBox->insertItem(i18n("Devices"));
138 d->ui->listBox->insertItem(i18n("Filesystem"));
139 d->ui->listBox->insertItem(i18n("File Types"));
140 d->ui->listBox->insertItem(i18n("Miscellaneous"));
143 KIconDialog::~KIconDialog()
145 // Write configuration
146 KConfig *config = KGlobal::config();
147 KConfigGroupSaver saver(config, "KIconDialog");
148 config->writeEntry("RecentMax", d->recentMax, true, true);
149 config->writePathEntry("RecentIcons", d->recentList, ',', true, true);
151 delete d;
154 void KIconDialog::slotAcceptIcons()
156 //FIXME: not used anymore
159 void KIconDialog::showIcons()
161 d->ui->iconCanvas->clear();
162 QStringList filelist;
164 KIcon::Context context = static_cast<KIcon::Context>(d->extendedContext - 1);
165 switch (d->extendedContext)
167 case RECENT:
168 filelist = d->recentList;
169 break;
170 case OTHER:
171 filelist = mFileList;
172 break;
173 case ALL:
174 filelist = mFileList;
175 context = KIcon::Any;
176 // ** Fallthrough to next case **
177 default:
178 QStringList list;
179 if (d->m_bStrictIconSize)
180 list=mpLoader->queryIcons(mGroupOrSize, context);
181 else
182 list=mpLoader->queryIconsByContext(mGroupOrSize, context);
184 // Remove path & extension
185 for ( QStringList::iterator it = list.begin(); it != list.end(); ++it)
186 filelist.append(QFileInfo(*it).baseName());
189 // Remove duplicate icons FIXME: Qt4 we can just use QSet
190 filelist.sort();
191 QString prev;
192 for ( QStringList::iterator it = filelist.begin(); it != filelist.end(); )
194 if (*it == prev)
196 it = filelist.remove(it);
198 else
200 prev = *it;
201 ++it;
205 d->ui->iconCanvas->setGroupOrSize(mGroupOrSize);
206 d->ui->iconCanvas->loadFiles(filelist);
209 void KIconDialog::setStrictIconSize(bool b)
211 d->m_bStrictIconSize=b;
214 bool KIconDialog::strictIconSize() const
216 return d->m_bStrictIconSize;
219 void KIconDialog::setIconSize( int size )
221 // see KIconLoader, if you think this is weird
222 if ( size == 0 )
223 mGroupOrSize = KIcon::Desktop; // default Group
224 else
225 mGroupOrSize = -size; // yes, KIconLoader::queryIconsByContext is weird
228 int KIconDialog::iconSize() const
230 // 0 or any other value ==> mGroupOrSize is a group, so we return 0
231 return (mGroupOrSize < 0) ? -mGroupOrSize : 0;
234 #ifndef KDE_NO_COMPAT
235 QString KIconDialog::selectIcon(KIcon::Group group, KIcon::Context context, bool user)
237 setup( group, context, false, 0, user );
238 return openDialog();
240 #endif
242 void KIconDialog::setup(KIcon::Group group, KIcon::Context context,
243 bool strictIconSize, int iconSize, bool user )
245 setup(group, context, strictIconSize, iconSize, user, false, false);
248 void KIconDialog::setup(KIcon::Group group, KIcon::Context context,
249 bool strictIconSize, int iconSize, bool user,
250 bool lockContext, bool lockBrowse )
252 d->m_bStrictIconSize = strictIconSize;
253 d->ui->iconCanvas->setStrictIconSize(strictIconSize);
254 mGroupOrSize = (iconSize == 0) ? group : -iconSize;
255 mType = user;
257 d->extendedContext = static_cast<ExtendedContext>( ( context == KIcon::Any ) ? ALL : context + 1 );
259 // We cannot change layout because it is protected ;-(
260 // FIXME: Qt4 we will be able to inherit from both QDialog and our GUI
261 d->ui->listBox->setEnabled(!lockContext);
262 d->ui->browseButton->setEnabled(!lockBrowse);
263 d->ui->listBox->setHidden(lockContext && lockBrowse);
264 d->ui->browseButton->setHidden(lockContext && lockBrowse);
266 d->ui->listBox->setCurrentItem(d->extendedContext);
269 const QString & KIconDialog::customLocation( ) const
271 return d->customLocation;
274 void KIconDialog::setCustomLocation( const QString& location )
276 d->customLocation = location;
278 if (location.isEmpty())
280 mFileList = KGlobal::dirs()->findAllResources("appicon", QString::fromLatin1("*.png"));
281 } else {
282 mFileList = mpLoader->queryIconsByDir(location);
286 QString KIconDialog::openDialog()
288 showIcons();
290 if ( exec() == Accepted )
292 if (!d->custom.isEmpty())
293 return d->custom;
294 else
295 return d->ui->iconCanvas->getCurrent();
297 else
299 return QString::null;
303 void KIconDialog::showDialog()
305 d->custom = QString::null;
307 // Make it so minimumSize returns correct value
308 d->ui->filterLabel->hide();
309 d->ui->searchLine->hide();
310 d->ui->progressBar->show();
312 setModal(false);
313 show();
315 // FIXME: this should be before show() but it doesn't work ;-(
316 resize(minimumSize());
318 showIcons();
321 void KIconDialog::slotOk()
323 QString key = !d->custom.isEmpty() ? d->custom : d->ui->iconCanvas->getCurrent();
325 // Append to list of recent icons
326 if (!d->recentList.contains(key))
328 d->recentList.push_back(key);
330 // Limit recent list in size
331 while ( (int)d->recentList.size() > d->recentMax )
332 d->recentList.pop_front();
335 emit newIconName(key);
336 KDialogBase::slotOk();
339 QString KIconDialog::getIcon(KIcon::Group group, KIcon::Context context,
340 bool strictIconSize, int iconSize, bool user,
341 QWidget *parent, const QString &caption)
343 KIconDialog dlg(parent, "icon dialog");
344 dlg.setup( group, context, strictIconSize, iconSize, user );
345 if (!caption.isNull())
346 dlg.setCaption(caption);
348 return dlg.openDialog();
351 void KIconDialog::slotBrowse()
353 // Create a file dialog to select a PNG, XPM or SVG file,
354 // with the image previewer shown.
355 // KFileDialog::getImageOpenURL doesn't allow svg.
356 KFileDialog dlg(QString::null, i18n("*.png *.xpm *.svg *.svgz|Icon Files (*.png *.xpm *.svg *.svgz)"),
357 this, "filedialog", true);
358 dlg.setOperationMode( KFileDialog::Opening );
359 dlg.setCaption( i18n("Open") );
360 dlg.setMode( KFile::File | KFile::ExistingOnly | KFile::LocalOnly );
361 KImageFilePreview *ip = new KImageFilePreview( &dlg );
362 dlg.setPreviewWidget( ip );
363 dlg.exec();
365 QString file = dlg.selectedFile();
366 if (!file.isEmpty())
368 d->custom = file;
369 if ( mType == 1 )
370 setCustomLocation(QFileInfo( file ).dirPath( true ));
371 slotOk();
375 void KIconDialog::slotContext(int id)
377 d->extendedContext = static_cast<ExtendedContext>(id);
378 showIcons();
381 void KIconDialog::slotStartLoading(int steps)
383 if (steps < 10)
384 d->ui->progressBar->hide();
385 else
387 d->ui->progressBar->setTotalSteps(steps);
388 d->ui->progressBar->setProgress(0);
389 d->ui->progressBar->show();
390 d->ui->filterLabel->hide();
391 d->ui->searchLine->hide();
395 void KIconDialog::slotProgress(int p)
397 d->ui->progressBar->setProgress(p);
400 void KIconDialog::slotFinished()
402 d->ui->progressBar->hide();
403 d->ui->filterLabel->show();
404 d->ui->searchLine->show();
407 class KIconButton::KIconButtonPrivate
409 public:
410 KIconButtonPrivate() {
411 m_bStrictIconSize = false;
412 iconSize = 0; // let KIconLoader choose the default
414 ~KIconButtonPrivate() {}
415 bool m_bStrictIconSize;
416 bool lockUser;
417 bool lockCustom;
418 int iconSize;
423 * KIconButton: A "choose icon" pushbutton.
426 KIconButton::KIconButton(QWidget *parent, const char *name)
427 : QPushButton(parent, name)
429 init( KGlobal::iconLoader() );
432 KIconButton::KIconButton(KIconLoader *loader,
433 QWidget *parent, const char *name)
434 : QPushButton(parent, name)
436 init( loader );
439 void KIconButton::init( KIconLoader *loader )
441 d = new KIconButtonPrivate;
442 mGroup = KIcon::Desktop;
443 mContext = KIcon::Application;
444 mbUser = false;
446 mpLoader = loader;
447 mpDialog = 0L;
448 connect(this, SIGNAL(clicked()), SLOT(slotChangeIcon()));
451 KIconButton::~KIconButton()
453 delete mpDialog;
454 delete d;
457 void KIconButton::setStrictIconSize(bool b)
459 d->m_bStrictIconSize=b;
462 bool KIconButton::strictIconSize() const
464 return d->m_bStrictIconSize;
467 void KIconButton::setIconSize( int size )
469 d->iconSize = size;
472 int KIconButton::iconSize() const
474 return d->iconSize;
477 void KIconButton::setIconType(KIcon::Group group, KIcon::Context context, bool user)
479 mGroup = group;
480 mContext = context;
481 mbUser = user;
482 d->lockUser = false;
483 d->lockCustom = false;
486 void KIconButton::setIconType(KIcon::Group group, KIcon::Context context, bool user, bool lockUser, bool lockCustom)
488 mGroup = group;
489 mContext = context;
490 mbUser = user;
491 d->lockUser = lockUser;
492 d->lockCustom = lockCustom;
495 void KIconButton::setIcon(const QString& icon)
497 mIcon = icon;
498 setIconSet(mpLoader->loadIconSet(mIcon, mGroup, d->iconSize));
500 if (!mpDialog)
502 mpDialog = new KIconDialog(mpLoader, this);
503 connect(mpDialog, SIGNAL(newIconName(const QString&)), SLOT(newIconName(const QString&)));
507 const QString & KIconButton::customLocation( ) const
509 return mpDialog ? mpDialog->customLocation() : QString::null;
512 void KIconButton::setCustomLocation(const QString &custom)
514 if (!mpDialog)
516 mpDialog = new KIconDialog(mpLoader, this);
517 connect(mpDialog, SIGNAL(newIconName(const QString&)), SLOT(newIconName(const QString&)));
520 mpDialog->setCustomLocation(custom);
523 void KIconButton::resetIcon()
525 mIcon = QString::null;
526 setIconSet(QIcon());
529 void KIconButton::slotChangeIcon()
531 if (!mpDialog)
533 mpDialog = new KIconDialog(mpLoader, this);
534 connect(mpDialog, SIGNAL(newIconName(const QString&)), SLOT(newIconName(const QString&)));
537 mpDialog->setup( mGroup, mContext, d->m_bStrictIconSize, d->iconSize, mbUser, d->lockUser, d->lockCustom );
538 mpDialog->showDialog();
541 void KIconButton::newIconName(const QString& name)
543 if (name.isEmpty())
544 return;
546 QIcon iconset = mpLoader->loadIconSet(name, mGroup, d->iconSize);
547 setIconSet(iconset);
548 mIcon = name;
550 emit iconChanged(name);
553 void KIconCanvas::virtual_hook( int id, void* data )
554 { KIconView::virtual_hook( id, data ); }
556 void KIconDialog::virtual_hook( int id, void* data )
557 { KDialogBase::virtual_hook( id, data ); }
559 #include "kicondialog.moc"