14 #include <config-kleopatra.h>
18 #include <kbuttongroup.h>
19 #include <klistwidgetsearchline.h>
20 #include <kapplication.h>
22 #include <kstandarddirs.h>
23 #include <kiconloader.h>
28 #include <QtCore/QTimer>
29 #include <QRadioButton>
30 #include <QtCore/QFileInfo>
31 #include <QProgressBar>
33 #include <QFileDialog>
34 #include <QtSvg/QSvgRenderer>
36 using namespace Kleo::KioAvoidance;
38 class KIconCanvas::KIconCanvasPrivate
41 KIconCanvasPrivate(
KIconCanvas *qq) {
q = qq; m_bLoading =
false; }
42 ~KIconCanvasPrivate() {}
49 void _k_slotLoadFiles();
50 void _k_slotCurrentChanged(QListWidgetItem *item);
56 class IconPath :
public QString
62 IconPath(
const QString &ip) : QString (ip)
64 int n = lastIndexOf(
'/');
65 m_iconName = (n==-1) ? static_cast<QString>(*
this) : mid(n+1);
69 IconPath() : QString ()
73 {
return m_iconName == ip.m_iconName; }
75 bool operator< (
const IconPath &ip)
const
76 {
return m_iconName < ip.m_iconName; }
87 setViewMode(IconMode);
88 setUniformItemSizes(
true);
90 setIconSize(QSize(60, 60));
91 d->mpTimer =
new QTimer(
this);
92 connect(d->mpTimer, SIGNAL(timeout()),
this, SLOT(_k_slotLoadFiles()));
93 connect(
this, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
94 this, SLOT(_k_slotCurrentChanged(QListWidgetItem*)));
95 setGridSize(QSize(80,80));
109 d->mpTimer->setSingleShot(
true);
110 d->mpTimer->start(10);
111 d->m_bLoading =
false;
114 void KIconCanvas::KIconCanvasPrivate::_k_slotLoadFiles()
116 q->setResizeMode(QListWidget::Fixed);
118 QApplication::setOverrideCursor(Qt::WaitCursor);
122 q->setUpdatesEnabled(
false);
126 QStringList::ConstIterator it;
127 uint emitProgress = 10;
128 QStringList::ConstIterator end(mFiles.end());
129 for (it=mFiles.begin(), i=0; it!=end; ++it, i++)
131 if ( emitProgress >= 10 ) {
145 QString ext = path.right(3).toUpper();
147 if (ext !=
"SVG" && ext !=
"VGZ")
149 #ifndef QT_NO_SVGRENDERER
152 img = QImage(60, 60, QImage::Format_ARGB32_Premultiplied);
154 QSvgRenderer renderer(*it);
155 if (renderer.isValid()) {
160 #endif // QT_NO_SVGRENDERER
164 if (img.width() > 60 || img.height() > 60)
166 if (img.width() > img.height())
168 int height = (int) ((60.0 / img.width()) * img.height());
169 img = img.scaled(60, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
172 int width = (int) ((60.0 / img.height()) * img.width());
173 img = img.scaled(width, 60, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
176 QPixmap pm = QPixmap::fromImage(img);
178 QListWidgetItem *item =
new QListWidgetItem(pm, fi.completeBaseName(),
q);
179 item->setData(Qt::UserRole, *it);
180 item->setToolTip(fi.completeBaseName());
184 q->setUpdatesEnabled(
true);
187 QApplication::restoreOverrideCursor();
191 q->setResizeMode(QListWidget::Adjust);
198 return currentItem()->data(Qt::UserRole).toString();
203 d->m_bLoading =
false;
206 void KIconCanvas::KIconCanvasPrivate::_k_slotCurrentChanged(QListWidgetItem *item)
208 emit
q->nameChanged((item != 0L) ? item->text() : QString());
211 class KIconDialog::KIconDialogPrivate
216 m_bStrictIconSize =
true;
218 m_bLockCustomDir =
false;
222 ~KIconDialogPrivate() {}
226 void setContext( KIconLoader::Context context );
229 void _k_slotContext(
int);
230 void _k_slotStartLoading(
int);
231 void _k_slotProgress(
int);
232 void _k_slotFinished();
233 void _k_slotAcceptIcons();
234 #ifndef QT_NO_FILEDIALOG
235 void _k_slotBrowse();
237 void _k_slotOtherIconClicked();
238 void _k_slotSystemIconClicked();
243 KIconLoader::Context mContext;
245 QStringList mFileList;
247 #ifndef QT_NO_FILEDIALOG
248 QPushButton *mpBrowseBut;
250 QRadioButton *mpSystemIcons, *mpOtherIcons;
251 QProgressBar *mpProgress;
253 KIconLoader *mpLoader;
256 KIconLoader::Context mContextMap[ 12 ];
258 bool m_bStrictIconSize, m_bLockUser, m_bLockCustomDir;
260 QString customLocation;
261 KListWidgetSearchLine *searchLine;
270 :
KDialog(parent),
d(new KIconDialogPrivate(this))
273 setCaption( i18n(
"Select Icon") );
274 setButtons(
Ok | Cancel );
275 setDefaultButton(
Ok );
277 d->mpLoader = KIconLoader::global();
282 :
KDialog(parent),
d(new KIconDialogPrivate(this))
285 setCaption( i18n(
"Select Icon") );
286 setButtons(
Ok | Cancel );
287 setDefaultButton(
Ok );
289 d->mpLoader = loader;
293 void KIconDialog::KIconDialogPrivate::init()
295 mGroupOrSize = KIconLoader::Desktop;
296 mContext = KIconLoader::Any;
297 mFileList = KGlobal::dirs()->findAllResources(
"appicon", QLatin1String(
"*.png"));
300 q->setMainWidget(main);
302 QVBoxLayout *top =
new QVBoxLayout(main);
305 QGroupBox *bgroup =
new QGroupBox(main);
306 bgroup->setTitle(i18n(
"Icon Source"));
308 QVBoxLayout *vbox =
new QVBoxLayout;
309 bgroup->setLayout( vbox );
310 top->addWidget(bgroup);
312 QGridLayout *grid =
new QGridLayout();
313 grid->setSpacing(KDialog::spacingHint());
314 bgroup->layout()->addItem(grid);
316 mpSystemIcons =
new QRadioButton(i18n(
"S&ystem icons:"), bgroup);
317 connect(mpSystemIcons, SIGNAL(clicked()),
q, SLOT(_k_slotSystemIconClicked()));
318 grid->addWidget(mpSystemIcons, 1, 0);
319 mpCombo =
new QComboBox(bgroup);
320 connect(mpCombo, SIGNAL(activated(
int)),
q, SLOT(_k_slotContext(
int)));
321 grid->addWidget(mpCombo, 1, 1);
322 mpOtherIcons =
new QRadioButton(i18n(
"O&ther icons:"), bgroup);
323 connect(mpOtherIcons, SIGNAL(clicked()),
q, SLOT(_k_slotOtherIconClicked()));
324 grid->addWidget(mpOtherIcons, 2, 0);
325 #ifndef QT_NO_FILEDIALOG
326 mpBrowseBut =
new QPushButton(i18n(
"&Browse..."), bgroup);
327 connect(mpBrowseBut, SIGNAL(clicked()),
q, SLOT(_k_slotBrowse()));
328 grid->addWidget(mpBrowseBut, 2, 1);
329 #endif // QT_NO_FILEDIALOG
334 QHBoxLayout *searchLayout =
new QHBoxLayout();
335 searchLayout->setMargin(0);
336 searchLayout->setSpacing(KDialog::spacingHint());
337 top->addLayout(searchLayout);
339 QLabel *searchLabel =
new QLabel(i18n(
"&Search:"), main);
340 searchLayout->addWidget(searchLabel);
342 searchLine =
new KListWidgetSearchLine(main);
343 searchLayout->addWidget(searchLine);
344 searchLabel->setBuddy(searchLine);
346 QString wtstr = i18n(
"Search interactively for icon names (e.g. folder).");
347 searchLabel->setWhatsThis(wtstr);
348 searchLine->setWhatsThis(wtstr);
352 connect(mpCanvas, SIGNAL(itemActivated(QListWidgetItem*)),
q, SLOT(_k_slotAcceptIcons()));
353 mpCanvas->setMinimumSize(400, 125);
354 top->addWidget(mpCanvas);
355 searchLine->setListWidget(mpCanvas);
357 mpProgress =
new QProgressBar(main);
358 top->addWidget(mpProgress);
359 connect(mpCanvas, SIGNAL(startLoading(
int)),
q, SLOT(_k_slotStartLoading(
int)));
360 connect(mpCanvas, SIGNAL(progress(
int)),
q, SLOT(_k_slotProgress(
int)));
361 connect(mpCanvas, SIGNAL(finished()),
q, SLOT(_k_slotFinished()));
364 connect(
q, SIGNAL(hidden()), mpCanvas, SLOT(stopLoading()));
366 static const char*
const context_text[] = {
367 I18N_NOOP(
"Actions" ),
368 I18N_NOOP(
"Animations" ),
369 I18N_NOOP(
"Applications" ),
370 I18N_NOOP(
"Categories" ),
371 I18N_NOOP(
"Devices" ),
372 I18N_NOOP(
"Emblems" ),
373 I18N_NOOP(
"Emotes" ),
374 I18N_NOOP(
"Filesystems" ),
375 I18N_NOOP(
"International" ),
376 I18N_NOOP(
"Mimetypes" ),
377 I18N_NOOP(
"Places" ),
378 I18N_NOOP(
"Status" ) };
379 static const KIconLoader::Context context_id[] = {
381 KIconLoader::Animation,
382 KIconLoader::Application,
383 KIconLoader::Category,
387 KIconLoader::FileSystem,
388 KIconLoader::International,
389 KIconLoader::MimeType,
391 KIconLoader::StatusIcon };
393 int cnt =
sizeof( context_text ) /
sizeof( context_text[ 0 ] );
395 Q_ASSERT( cnt ==
sizeof( context_id ) /
sizeof( context_id[ 0 ] )
396 && cnt ==
sizeof( mContextMap ) /
sizeof( mContextMap[ 0 ] ));
401 if( mpLoader->hasContext( context_id[ i ] ))
403 mpCombo->addItem(i18n( context_text[ i ] ));
404 mContextMap[ mNumContext++ ] = context_id[ i ];
407 mpCombo->setFixedSize(mpCombo->sizeHint());
409 #ifndef QT_NO_FILEDIALOG
410 mpBrowseBut->setFixedWidth(mpCombo->width());
414 q->incrementInitialSize(QSize(0,100));
415 connect(
q, SIGNAL(okClicked()),
q, SLOT(slotOk()));
424 void KIconDialog::KIconDialogPrivate::_k_slotAcceptIcons()
430 void KIconDialog::KIconDialogPrivate::showIcons()
433 QStringList filelist;
434 if (mpSystemIcons->isChecked())
435 if (m_bStrictIconSize)
436 filelist=mpLoader->queryIcons(mGroupOrSize, mContext);
438 filelist=mpLoader->queryIconsByContext(mGroupOrSize, mContext);
439 else if (!customLocation.isNull()) {
440 filelist = mpLoader->queryIconsByDir(customLocation);
446 QStringList::Iterator it;
447 foreach (
const QString &it, filelist) {
448 iconlist.append(IconPath(it));
454 foreach (
const IconPath &ip, iconlist) {
459 mpCanvas->loadFiles(filelist);
464 d->m_bStrictIconSize=b;
469 return d->m_bStrictIconSize;
476 d->mGroupOrSize = KIconLoader::Desktop;
478 d->mGroupOrSize = -size;
485 return (d->mGroupOrSize < 0) ? -d->mGroupOrSize : 0;
489 bool strictIconSize,
int iconSize,
bool user,
490 bool lockUser,
bool lockCustomDir )
493 d->m_bLockUser = lockUser;
494 d->m_bLockCustomDir = lockCustomDir;
495 d->mGroupOrSize = (iconSize == 0) ? group : -iconSize;
496 d->mpSystemIcons->setChecked(!user);
497 d->mpSystemIcons->setEnabled(!lockUser || !user);
498 d->mpOtherIcons->setChecked(user);
499 d->mpOtherIcons->setEnabled(!lockUser || user);
500 d->mpCombo->setEnabled(!user);
501 #ifndef QT_NO_FILEDIALOG
502 d->mpBrowseBut->setEnabled(user && !lockCustomDir);
504 d->setContext(context);
507 void KIconDialog::KIconDialogPrivate::setContext(KIconLoader::Context context)
513 if( mContextMap[ i ] == context )
515 mpCombo->setCurrentIndex( i );
522 d->customLocation = location;
529 if ( exec() == Accepted )
531 if (!d->custom.isNull())
533 QString
name = d->mpCanvas->getCurrent();
534 if (
name.isEmpty() || d->mpOtherIcons->isChecked()) {
538 return fi.baseName();
553 if (!d->custom.isNull())
559 name = d->mpCanvas->getCurrent();
560 if (!name.isEmpty() && d->mpSystemIcons->isChecked()) {
562 name = fi.baseName();
571 bool strictIconSize,
int iconSize,
bool user,
572 QWidget *parent,
const QString &caption)
575 dlg.
setup( group, context, strictIconSize, iconSize, user );
576 if (!caption.isNull())
577 dlg.setCaption(caption);
582 #ifndef QT_NO_FILEDIALOG
583 void KIconDialog::KIconDialogPrivate::_k_slotBrowse()
589 if (mpSystemIcons->isChecked()) {
590 customLocation = QFileInfo(file).absolutePath();
597 void KIconDialog::KIconDialogPrivate::_k_slotSystemIconClicked()
599 #ifndef QT_NO_FILEDIALOG
600 mpBrowseBut->setEnabled(
false);
602 mpCombo->setEnabled(
true);
606 void KIconDialog::KIconDialogPrivate::_k_slotOtherIconClicked()
608 #ifndef QT_NO_FILEDIALOG
609 mpBrowseBut->setEnabled(!m_bLockCustomDir);
611 mpCombo->setEnabled(
false);
615 void KIconDialog::KIconDialogPrivate::_k_slotContext(
int id)
617 mContext =
static_cast<KIconLoader::Context
>( mContextMap[ id ] );
621 void KIconDialog::KIconDialogPrivate::_k_slotStartLoading(
int steps)
628 mpProgress->setValue(0);
633 void KIconDialog::KIconDialogPrivate::_k_slotProgress(
int p)
635 mpProgress->setValue(static_cast<int>(100.0 * (
double)p / (
double)mNumOfSteps));
638 void KIconDialog::KIconDialogPrivate::_k_slotFinished()
644 class KIconButton::KIconButtonPrivate
647 KIconButtonPrivate(
KIconButton *qq, KIconLoader *loader);
648 ~KIconButtonPrivate();
651 void _k_slotChangeIcon();
652 void _k_newIconName(
const QString&);
657 bool m_bStrictIconSize;
660 KIconLoader::Group mGroup;
661 KIconLoader::Context mContext;
665 KIconLoader *mpLoader;
674 : QPushButton(parent),
d(new KIconButtonPrivate(this, KIconLoader::global()))
676 QPushButton::setIconSize(QSize(48, 48));
680 : QPushButton(parent),
d(new KIconButtonPrivate(this, loader))
682 QPushButton::setIconSize(QSize(48, 48));
685 KIconButton::KIconButtonPrivate::KIconButtonPrivate(
KIconButton *qq, KIconLoader *loader)
688 m_bStrictIconSize =
false;
691 mGroup = KIconLoader::Desktop;
692 mContext = KIconLoader::Application;
697 connect(
q, SIGNAL(clicked()),
q, SLOT(_k_slotChangeIcon()));
700 KIconButton::KIconButtonPrivate::~KIconButtonPrivate()
712 d->m_bStrictIconSize=b;
717 return d->m_bStrictIconSize;
722 QPushButton::setIconSize(QSize(size, size));
734 d->mContext = context;
745 connect(d->mpDialog, SIGNAL(newIconName(QString)),
this, SLOT(_k_newIconName(QString)));
749 d->mpDialog->setCustomLocation(QFileInfo(d->mpLoader->iconPath(d->mIcon, d->mGroup,
true) ).absolutePath());
755 QPushButton::setIcon(icon);
769 void KIconButton::KIconButtonPrivate::_k_slotChangeIcon()
774 connect(mpDialog, SIGNAL(newIconName(QString)),
q, SLOT(_k_newIconName(QString)));
777 mpDialog->setup(mGroup, mContext, m_bStrictIconSize, iconSize, mbUser);
778 mpDialog->showDialog();
781 void KIconButton::KIconButtonPrivate::_k_newIconName(
const QString&
name)
786 q->setIcon(KIcon(name));
790 mpDialog->setCustomLocation(QFileInfo(mpLoader->iconPath(mIcon, mGroup,
true)).absolutePath());
793 emit
q->iconChanged(name);
796 #include "kleo_kicondialog.moc"
int iconSize() const
Returns the iconsize set via setIconSize() or 0, if the default iconsize will be used.
QString getOpenFileName(QWidget *parent=0, const QString &caption=QString(), const QString &dirID=QString(), const QString &filter=QString())
void stopLoading()
Call this slot to stop the loading of the icons.
Icon canvas for KIconDialog.
void newIconName(const QString &)
~KIconDialog()
Destructs the dialog.
bool strictIconSize() const
Returns true if a strict icon size policy is set.
KIconCanvas(QWidget *parent=0L)
Creates a new icon canvas.
~KIconCanvas()
Destroys the icon canvas.
QString getCurrent() const
Returns the current icon.
static QString getIcon(KIconLoader::Group group=KIconLoader::Desktop, KIconLoader::Context context=KIconLoader::Application, bool strictIconSize=false, int iconSize=0, bool user=false, QWidget *parent=0, const QString &caption=QString())
Pops up the dialog an lets the user select an icon.
Dialog for interactive selection of icons.
static bool operator==(const AddrSpec &lhs, const AddrSpec &rhs)
void showDialog()
show()es this dialog and emits a newIcon(const QString&) signal when successful.
void setStrictIconSize(bool b)
Sets a strict icon size policy for allowed icons.
QString openDialog()
exec()utes this modal dialog and returns the name of the selected icon, or QString() if the dialog wa...
void setCustomLocation(const QString &location)
sets a custom icon directory
int main(int argc, char **argv)
void setIconSize(int size)
Sets the size of the icons to be shown / selected.
void setup(KIconLoader::Group group, KIconLoader::Context context=KIconLoader::Application, bool strictIconSize=false, int iconSize=0, bool user=false, bool lockUser=false, bool lockCustomDir=false)
Allows you to set the same parameters as in the class method getIcon(), as well as two additional par...
KIconDialog(QWidget *parent=0L)
Constructs an icon selection dialog using the global iconloader.
void loadFiles(const QStringList &files)
Load icons into the canvas.
void startLoading(int count)
This signal is emitted when the loading of the icons has started.