14 #include "kicondialog.h"
21 #include <kiconviewsearchline.h>
23 #include <tdeapplication.h>
24 #include <tdelocale.h>
25 #include <tdeglobal.h>
26 #include <kstandarddirs.h>
27 #include <kiconloader.h>
28 #include <kprogress.h>
29 #include <kiconview.h>
30 #include <tdefiledialog.h>
31 #include <kimagefilepreview.h>
35 #include <tqstringlist.h>
39 #include <tqcombobox.h>
41 #include <tqbuttongroup.h>
42 #include <tqradiobutton.h>
43 #include <tqfileinfo.h>
44 #include <tqtoolbutton.h>
45 #include <tqwhatsthis.h>
48 #include <svgicons/ksvgiconengine.h>
49 #include <svgicons/ksvgiconpainter.h>
52 class TDEIconCanvas::TDEIconCanvasPrivate
55 TDEIconCanvasPrivate() { m_bLoading =
false; }
56 ~TDEIconCanvasPrivate() {}
63 class IconPath :
public TQString
69 IconPath(
const TQString &ip) : TQString (ip)
72 m_iconName = (n==-1) ?
static_cast<TQString
>(*
this) : mid(n+1);
76 IconPath() : TQString ()
79 bool operator== (
const IconPath &ip)
const
80 {
return m_iconName == ip.m_iconName; }
82 bool operator< (
const IconPath &ip)
const
83 {
return m_iconName < ip.m_iconName; }
91 TDEIconCanvas::TDEIconCanvas(TQWidget *parent,
const char *name)
92 : TDEIconView(parent, name)
94 d =
new TDEIconCanvasPrivate;
95 mpTimer =
new TQTimer(
this);
96 connect(mpTimer, TQ_SIGNAL(timeout()), TQ_SLOT(slotLoadFiles()));
97 connect(
this, TQ_SIGNAL(currentChanged(TQIconViewItem *)),
98 TQ_SLOT(slotCurrentChanged(TQIconViewItem *)));
100 setWordWrapIconText(
false);
101 setShowToolTips(
true);
104 TDEIconCanvas::~TDEIconCanvas()
114 emit startLoading(mFiles.count());
115 mpTimer->start(10,
true);
116 d->m_bLoading =
false;
119 void TDEIconCanvas::slotLoadFiles()
121 setResizeMode(Fixed);
122 TQApplication::setOverrideCursor(TQt::waitCursor);
125 setUpdatesEnabled(
false );
128 KSVGIconEngine *svgEngine =
new KSVGIconEngine();
131 d->m_bLoading =
true;
133 TQStringList::ConstIterator it;
134 uint emitProgress = 10;
135 TQStringList::ConstIterator end(mFiles.end());
136 for (it=mFiles.begin(), i=0; it!=end; ++it, i++)
143 if ( emitProgress >= 10 ) {
150 if ( !d->m_bLoading )
156 TQString ext = path.right(3).upper();
158 if (ext !=
"SVG" && ext !=
"VGZ")
162 if (svgEngine->load(60, 60, *it))
163 img = *svgEngine->painter()->image();
168 if (img.width() > 60 || img.height() > 60)
170 if (img.width() > img.height())
172 int height = (int) ((60.0 / img.width()) * img.height());
173 img = img.smoothScale(60, height);
176 int width = (int) ((60.0 / img.height()) * img.width());
177 img = img.smoothScale(width, 60);
181 pm.convertFromImage(img);
183 TQIconViewItem *item =
new TQIconViewItem(
this, fi.baseName(
true), pm);
185 item->setDragEnabled(
false);
186 item->setDropEnabled(
false);
194 setUpdatesEnabled(
true );
196 TQApplication::restoreOverrideCursor();
197 d->m_bLoading =
false;
199 setResizeMode(Adjust);
205 return TQString::null;
206 return currentItem()->key();
209 void TDEIconCanvas::stopLoading()
211 d->m_bLoading =
false;
214 void TDEIconCanvas::slotCurrentChanged(TQIconViewItem *item)
216 emit
nameChanged((item != 0L) ? item->text() : TQString::null);
219 class TDEIconDialog::TDEIconDialogPrivate
222 TDEIconDialogPrivate() {
223 m_bStrictIconSize =
true;
225 m_bLockCustomDir =
false;
228 ~TDEIconDialogPrivate() {}
229 bool m_bStrictIconSize, m_bLockUser, m_bLockCustomDir;
231 TQString customLocation;
232 TDEIconViewSearchLine *searchLine;
241 : KDialogBase(parent, name, true, i18n(
"Select Icon"), Ok|Cancel, Ok)
243 d =
new TDEIconDialogPrivate;
244 mpLoader = TDEGlobal::iconLoader();
250 : KDialogBase(parent, name, true, i18n(
"Select Icon"), Ok|Cancel, Ok)
252 d =
new TDEIconDialogPrivate;
257 void TDEIconDialog::init()
259 mGroupOrSize = TDEIcon::Desktop;
260 mContext = TDEIcon::Any;
262 mFileList = TDEGlobal::dirs()->findAllResources(
"appicon", TQString::fromLatin1(
"*.png"));
264 TQWidget *main =
new TQWidget(
this );
267 TQVBoxLayout *top =
new TQVBoxLayout(main);
268 top->setSpacing( spacingHint() );
270 TQButtonGroup *bgroup =
new TQButtonGroup(0, TQt::Vertical, i18n(
"Icon Source"), main);
271 bgroup->layout()->setSpacing(KDialog::spacingHint());
272 bgroup->layout()->setMargin(KDialog::marginHint());
273 top->addWidget(bgroup);
274 connect(bgroup, TQ_SIGNAL(clicked(
int)), TQ_SLOT(slotButtonClicked(
int)));
275 TQGridLayout *grid =
new TQGridLayout(bgroup->layout(), 3, 2);
276 mpRb1 =
new TQRadioButton(i18n(
"S&ystem icons:"), bgroup);
277 grid->addWidget(mpRb1, 1, 0);
278 mpCombo =
new TQComboBox(bgroup);
279 connect(mpCombo, TQ_SIGNAL(activated(
int)), TQ_SLOT(slotContext(
int)));
280 grid->addWidget(mpCombo, 1, 1);
281 mpRb2 =
new TQRadioButton(i18n(
"O&ther icons:"), bgroup);
282 grid->addWidget(mpRb2, 2, 0);
283 mpBrowseBut =
new TQPushButton(i18n(
"&Browse..."), bgroup);
284 grid->addWidget(mpBrowseBut, 2, 1);
289 TQHBoxLayout *searchLayout =
new TQHBoxLayout(0, 0, KDialog::spacingHint());
290 top->addLayout(searchLayout);
292 TQToolButton *clearSearch =
new TQToolButton(main);
293 clearSearch->setTextLabel(i18n(
"Clear Search"),
true);
294 clearSearch->setIconSet(SmallIconSet(TQApplication::reverseLayout() ?
"clear_left" :
"locationbar_erase"));
295 searchLayout->addWidget(clearSearch);
297 TQLabel *searchLabel =
new TQLabel(i18n(
"&Search:"), main);
298 searchLayout->addWidget(searchLabel);
300 d->searchLine =
new TDEIconViewSearchLine(main,
"searchLine");
301 searchLayout->addWidget(d->searchLine);
302 searchLabel->setBuddy(d->searchLine);
306 connect(clearSearch, TQ_SIGNAL(clicked()), d->searchLine, TQ_SLOT(clear()));
308 TQString wtstr = i18n(
"Search interactively for icon names (e.g. folder).");
309 TQWhatsThis::add(searchLabel, wtstr);
310 TQWhatsThis::add(d->searchLine, wtstr);
314 connect(mpCanvas, TQ_SIGNAL(executed(TQIconViewItem *)), TQ_SLOT(slotAcceptIcons()));
315 connect(mpCanvas, TQ_SIGNAL(returnPressed(TQIconViewItem *)), TQ_SLOT(slotAcceptIcons()));
316 mpCanvas->setMinimumSize(400, 125);
317 top->addWidget(mpCanvas);
318 d->searchLine->setIconView(mpCanvas);
320 mpProgress =
new KProgress(main);
321 top->addWidget(mpProgress);
322 connect(mpCanvas, TQ_SIGNAL(startLoading(
int)), TQ_SLOT(slotStartLoading(
int)));
323 connect(mpCanvas, TQ_SIGNAL(progress(
int)), TQ_SLOT(slotProgress(
int)));
324 connect(mpCanvas, TQ_SIGNAL(finished()), TQ_SLOT(slotFinished()));
327 connect(
this, TQ_SIGNAL(hidden()), mpCanvas, TQ_SLOT(stopLoading()));
329 static const char*
const context_text[] = {
330 I18N_NOOP(
"Actions" ),
331 I18N_NOOP(
"Animations" ),
332 I18N_NOOP(
"Applications" ),
333 I18N_NOOP(
"Categories" ),
334 I18N_NOOP(
"Devices" ),
335 I18N_NOOP(
"Emblems" ),
336 I18N_NOOP(
"Emotes" ),
337 I18N_NOOP(
"Filesystems" ),
338 I18N_NOOP(
"International" ),
339 I18N_NOOP(
"Mimetypes" ),
340 I18N_NOOP(
"Places" ),
341 I18N_NOOP(
"Status" ) };
342 static const TDEIcon::Context context_id[] = {
345 TDEIcon::Application,
351 TDEIcon::International,
354 TDEIcon::StatusIcon };
356 int cnt =
sizeof( context_text ) /
sizeof( context_text[ 0 ] );
358 assert( cnt ==
sizeof( context_id ) /
sizeof( context_id[ 0 ] )
359 && cnt ==
sizeof( mContextMap ) /
sizeof( mContextMap[ 0 ] ));
364 if( mpLoader->hasContext( context_id[ i ] ))
366 mpCombo->insertItem(i18n( context_text[ i ] ));
367 mContextMap[ mNumContext++ ] = context_id[ i ];
370 mpCombo->setFixedSize(mpCombo->sizeHint());
372 mpBrowseBut->setFixedWidth(mpCombo->width());
375 incInitialSize(TQSize(0,100));
384 void TDEIconDialog::slotAcceptIcons()
386 d->custom=TQString::null;
390 void TDEIconDialog::showIcons()
393 TQStringList filelist;
395 if (d->m_bStrictIconSize)
396 filelist=mpLoader->queryIcons(mGroupOrSize, mContext);
398 filelist=mpLoader->queryIconsByContext(mGroupOrSize, mContext);
399 else if ( !d->customLocation.isNull() )
400 filelist=mpLoader->queryIconsByDir( d->customLocation );
404 std::list<IconPath> iconlist;
405 TQStringList::Iterator it;
406 for( it = filelist.begin(); it != filelist.end(); ++it )
407 iconlist.push_back(IconPath(*it));
412 for (
const IconPath &ip : iconlist)
415 d->searchLine->clear();
421 d->m_bStrictIconSize=b;
426 return d->m_bStrictIconSize;
433 mGroupOrSize = TDEIcon::Desktop;
435 mGroupOrSize = -size;
441 return (mGroupOrSize < 0) ? -mGroupOrSize : 0;
444 #ifndef KDE_NO_COMPAT
447 setup( group, context,
false, 0, user );
453 bool strictIconSize,
int iconSize,
bool user )
457 mType = user ? 1 : 0;
458 mpRb1->setChecked(!user);
459 mpRb2->setChecked(user);
460 mpCombo->setEnabled(!user);
461 mpBrowseBut->setEnabled(user);
462 setContext( context );
466 bool strictIconSize,
int iconSize,
bool user,
467 bool lockUser,
bool lockCustomDir )
470 d->m_bLockUser = lockUser;
471 d->m_bLockCustomDir = lockCustomDir;
473 mType = user ? 1 : 0;
474 mpRb1->setChecked(!user);
475 mpRb1->setEnabled( !lockUser || !user );
476 mpRb2->setChecked(user);
477 mpRb2->setEnabled( !lockUser || user );
478 mpCombo->setEnabled(!user);
479 mpBrowseBut->setEnabled( user && !lockCustomDir );
480 setContext( context );
483 void TDEIconDialog::setContext( TDEIcon::Context context )
489 if( mContextMap[ i ] == context )
491 mpCombo->setCurrentItem( i );
498 d->customLocation = location;
505 if ( exec() == Accepted )
507 if (!d->custom.isNull())
510 if (name.isEmpty() || (mType == 1))
513 return fi.baseName(
true);
515 return TQString::null;
525 void TDEIconDialog::slotOk()
528 if (!d->custom.isNull())
535 if (!name.isEmpty() && (mType != 1))
538 name = fi.baseName(
true);
542 emit newIconName(name);
543 KDialogBase::slotOk();
547 bool strictIconSize,
int iconSize,
bool user,
548 TQWidget *parent,
const TQString &caption)
552 if (!caption.isNull())
553 dlg.setCaption(caption);
558 void TDEIconDialog::slotButtonClicked(
int id)
568 mpBrowseBut->setEnabled(
false);
569 mpCombo->setEnabled(
true);
578 mpBrowseBut->setEnabled( !d->m_bLockCustomDir );
579 mpCombo->setEnabled(
false);
588 KFileDialog dlg(TQString::null, i18n(
"*.png *.xpm *.svg *.svgz|Icon Files (*.png *.xpm *.svg *.svgz)"),
589 this,
"filedialog",
true);
590 dlg.setOperationMode( KFileDialog::Opening );
591 dlg.setCaption( i18n(
"Open") );
592 dlg.setMode( KFile::File );
595 dlg.setPreviewWidget( ip );
598 file = dlg.selectedFile();
603 d->customLocation = TQFileInfo( file ).dirPath(
true );
611 void TDEIconDialog::slotContext(
int id)
613 mContext =
static_cast<TDEIcon::Context
>( mContextMap[ id ] );
617 void TDEIconDialog::slotStartLoading(
int steps)
623 mpProgress->setTotalSteps(steps);
624 mpProgress->setProgress(0);
629 void TDEIconDialog::slotProgress(
int p)
631 mpProgress->setProgress(p);
637 void TDEIconDialog::slotFinished()
642 class TDEIconButton::TDEIconButtonPrivate
645 TDEIconButtonPrivate() {
646 m_bStrictIconSize =
false;
649 ~TDEIconButtonPrivate() {}
650 bool m_bStrictIconSize;
660 : TQPushButton(parent, name)
662 init( TDEGlobal::iconLoader() );
666 TQWidget *parent,
const char *name)
667 : TQPushButton(parent, name)
672 void TDEIconButton::init( TDEIconLoader *loader )
674 d =
new TDEIconButtonPrivate;
675 mGroup = TDEIcon::Desktop;
676 mContext = TDEIcon::Application;
681 connect(
this, TQ_SIGNAL(clicked()), TQ_SLOT(slotChangeIcon()));
692 d->m_bStrictIconSize=b;
697 return d->m_bStrictIconSize;
720 setIconSet(mpLoader->loadIconSet(mIcon, mGroup, d->iconSize));
725 connect(mpDialog, TQ_SIGNAL(newIconName(
const TQString&)), TQ_SLOT(newIconName(
const TQString&)));
729 mpDialog->
setCustomLocation( TQFileInfo( mpLoader->iconPath(mIcon, mGroup,
true) ).dirPath(
true ) );
734 mIcon = TQString::null;
735 setIconSet(TQIconSet());
738 void TDEIconButton::slotChangeIcon()
743 connect(mpDialog, TQ_SIGNAL(newIconName(
const TQString&)), TQ_SLOT(newIconName(
const TQString&)));
746 mpDialog->
setup( mGroup, mContext, d->m_bStrictIconSize, d->iconSize, mbUser );
750 void TDEIconButton::newIconName(
const TQString& name)
755 TQIconSet iconset = mpLoader->loadIconSet(name, mGroup, d->iconSize);
760 mpDialog->
setCustomLocation( TQFileInfo( mpLoader->iconPath(mIcon, mGroup,
true) ).dirPath(
true ) );
765 void TDEIconCanvas::virtual_hook(
int id,
void* data )
766 { TDEIconView::virtual_hook(
id, data ); }
768 void TDEIconDialog::virtual_hook(
int id,
void* data )
769 { KDialogBase::virtual_hook(
id, data ); }
771 #include "kicondialog.moc"
Provides a user (and developer) friendly way to select files and directories.
Image preview widget for the file dialog.
Icon canvas for TDEIconDialog.
void loadFiles(const TQStringList &files)
Load icons into the canvas.
void nameChanged(TQString)
Emitted when the current icon has changed.
TQString getCurrent() const
Returns the current icon.
Dialog for interactive selection of icons.
int iconSize() const
Returns the iconsize set via setIconSize() or 0, if the default iconsize will be used.
TDEIconDialog(TQWidget *parent=0L, const char *name=0L)
Constructs an icon selection dialog using the global iconloader.
TQString openDialog()
exec()utes this modal dialog and returns the name of the selected icon, or TQString::null if the dial...
void setIconSize(int size)
Sets the size of the icons to be shown / selected.
void setup(TDEIcon::Group group, TDEIcon::Context context=TDEIcon::Application, bool strictIconSize=false, int iconSize=0, bool user=false)
Allows you to set the same parameters as in the class method getIcon().
void showDialog()
show()es this dialog and emits a newIcon(const TQString&) signal when successful.
TQString selectIcon(TDEIcon::Group group=TDEIcon::Desktop, TDEIcon::Context context=TDEIcon::Application, bool user=false)
void setStrictIconSize(bool b)
Sets a strict icon size policy for allowed icons.
~TDEIconDialog()
Destructs the dialog.
bool strictIconSize() const
Returns true if a strict icon size policy is set.
static TQString getIcon(TDEIcon::Group group=TDEIcon::Desktop, TDEIcon::Context context=TDEIcon::Application, bool strictIconSize=false, int iconSize=0, bool user=false, TQWidget *parent=0, const TQString &caption=TQString::null)
Pops up the dialog an lets the user select an icon.
void setCustomLocation(const TQString &location)
sets a custom icon directory