14 #include "kicondialog.h"
20 #include <kiconviewsearchline.h>
22 #include <tdeapplication.h>
23 #include <tdelocale.h>
24 #include <tdeglobal.h>
25 #include <kstandarddirs.h>
26 #include <kiconloader.h>
27 #include <kprogress.h>
28 #include <kiconview.h>
29 #include <tdefiledialog.h>
30 #include <kimagefilepreview.h>
34 #include <tqstringlist.h>
35 #include <tqsortedlist.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, TQT_SIGNAL(timeout()), TQT_SLOT(slotLoadFiles()));
97 connect(
this, TQT_SIGNAL(currentChanged(TQIconViewItem *)),
98 TQT_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(tqwaitCursor);
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(), 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, Qt::Vertical, i18n(
"Icon Source"), main);
271 bgroup->layout()->setSpacing(KDialog::spacingHint());
272 bgroup->layout()->setMargin(KDialog::marginHint());
273 top->addWidget(bgroup);
274 connect(bgroup, TQT_SIGNAL(clicked(
int)), TQT_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, TQT_SIGNAL(activated(
int)), TQT_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, TQT_SIGNAL(clicked()), d->searchLine, TQT_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, TQT_SIGNAL(executed(TQIconViewItem *)), TQT_SLOT(slotAcceptIcons()));
315 connect(mpCanvas, TQT_SIGNAL(returnPressed(TQIconViewItem *)), TQT_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, TQT_SIGNAL(startLoading(
int)), TQT_SLOT(slotStartLoading(
int)));
323 connect(mpCanvas, TQT_SIGNAL(progress(
int)), TQT_SLOT(slotProgress(
int)));
324 connect(mpCanvas, TQT_SIGNAL(finished()), TQT_SLOT(slotFinished()));
327 connect(
this, TQT_SIGNAL(hidden()), mpCanvas, TQT_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 TQSortedList <IconPath>iconlist;
405 iconlist.setAutoDelete(
true);
406 TQStringList::Iterator it;
407 for( it = filelist.begin(); it != filelist.end(); ++it )
408 iconlist.append(
new IconPath(*it));
413 for ( IconPath *ip=iconlist.first(); ip != 0; ip=iconlist.next() )
414 filelist.append(*ip);
416 d->searchLine->clear();
422 d->m_bStrictIconSize=b;
427 return d->m_bStrictIconSize;
434 mGroupOrSize = TDEIcon::Desktop;
436 mGroupOrSize = -size;
442 return (mGroupOrSize < 0) ? -mGroupOrSize : 0;
445 #ifndef KDE_NO_COMPAT
448 setup( group, context,
false, 0, user );
454 bool strictIconSize,
int iconSize,
bool user )
457 mGroupOrSize = (iconSize == 0) ? group : -iconSize;
458 mType = user ? 1 : 0;
459 mpRb1->setChecked(!user);
460 mpRb2->setChecked(user);
461 mpCombo->setEnabled(!user);
462 mpBrowseBut->setEnabled(user);
463 setContext( context );
467 bool strictIconSize,
int iconSize,
bool user,
468 bool lockUser,
bool lockCustomDir )
471 d->m_bLockUser = lockUser;
472 d->m_bLockCustomDir = lockCustomDir;
473 mGroupOrSize = (iconSize == 0) ? group : -iconSize;
474 mType = user ? 1 : 0;
475 mpRb1->setChecked(!user);
476 mpRb1->setEnabled( !lockUser || !user );
477 mpRb2->setChecked(user);
478 mpRb2->setEnabled( !lockUser || user );
479 mpCombo->setEnabled(!user);
480 mpBrowseBut->setEnabled( user && !lockCustomDir );
481 setContext( context );
484 void TDEIconDialog::setContext( TDEIcon::Context context )
490 if( mContextMap[ i ] == context )
492 mpCombo->setCurrentItem( i );
499 d->customLocation = location;
506 if ( exec() == Accepted )
508 if (!d->custom.isNull())
511 if (name.isEmpty() || (mType == 1))
514 return fi.baseName();
516 return TQString::null;
526 void TDEIconDialog::slotOk()
529 if (!d->custom.isNull())
536 if (!name.isEmpty() && (mType != 1))
539 name = fi.baseName();
543 emit newIconName(name);
544 KDialogBase::slotOk();
548 bool strictIconSize,
int iconSize,
bool user,
549 TQWidget *parent,
const TQString &caption)
552 dlg.
setup( group, context, strictIconSize, iconSize, user );
553 if (!caption.isNull())
554 dlg.setCaption(caption);
559 void TDEIconDialog::slotButtonClicked(
int id)
569 mpBrowseBut->setEnabled(
false);
570 mpCombo->setEnabled(
true);
579 mpBrowseBut->setEnabled( !d->m_bLockCustomDir );
580 mpCombo->setEnabled(
false);
589 KFileDialog dlg(TQString::null, i18n(
"*.png *.xpm *.svg *.svgz|Icon Files (*.png *.xpm *.svg *.svgz)"),
590 this,
"filedialog",
true);
591 dlg.setOperationMode( KFileDialog::Opening );
592 dlg.setCaption( i18n(
"Open") );
593 dlg.setMode( KFile::File );
596 dlg.setPreviewWidget( ip );
599 file = dlg.selectedFile();
604 d->customLocation = TQFileInfo( file ).dirPath(
true );
612 void TDEIconDialog::slotContext(
int id)
614 mContext =
static_cast<TDEIcon::Context
>( mContextMap[ id ] );
618 void TDEIconDialog::slotStartLoading(
int steps)
624 mpProgress->setTotalSteps(steps);
625 mpProgress->setProgress(0);
630 void TDEIconDialog::slotProgress(
int p)
632 mpProgress->setProgress(p);
638 void TDEIconDialog::slotFinished()
643 class TDEIconButton::TDEIconButtonPrivate
646 TDEIconButtonPrivate() {
647 m_bStrictIconSize =
false;
650 ~TDEIconButtonPrivate() {}
651 bool m_bStrictIconSize;
661 : TQPushButton(parent, name)
663 init( TDEGlobal::iconLoader() );
667 TQWidget *parent,
const char *name)
668 : TQPushButton(parent, name)
673 void TDEIconButton::init( TDEIconLoader *loader )
675 d =
new TDEIconButtonPrivate;
676 mGroup = TDEIcon::Desktop;
677 mContext = TDEIcon::Application;
682 connect(
this, TQT_SIGNAL(clicked()), TQT_SLOT(slotChangeIcon()));
693 d->m_bStrictIconSize=b;
698 return d->m_bStrictIconSize;
721 setIconSet(mpLoader->loadIconSet(mIcon, mGroup, d->iconSize));
726 connect(mpDialog, TQT_SIGNAL(newIconName(
const TQString&)), TQT_SLOT(newIconName(
const TQString&)));
730 mpDialog->
setCustomLocation( TQFileInfo( mpLoader->iconPath(mIcon, mGroup,
true) ).dirPath(
true ) );
735 mIcon = TQString::null;
736 setIconSet(TQIconSet());
739 void TDEIconButton::slotChangeIcon()
744 connect(mpDialog, TQT_SIGNAL(newIconName(
const TQString&)), TQT_SLOT(newIconName(
const TQString&)));
747 mpDialog->
setup( mGroup, mContext, d->m_bStrictIconSize, d->iconSize, mbUser );
751 void TDEIconButton::newIconName(
const TQString& name)
756 TQIconSet iconset = mpLoader->loadIconSet(name, mGroup, d->iconSize);
761 mpDialog->
setCustomLocation( TQFileInfo( mpLoader->iconPath(mIcon, mGroup,
true) ).dirPath(
true ) );
766 void TDEIconCanvas::virtual_hook(
int id,
void* data )
767 { TDEIconView::virtual_hook(
id, data ); }
769 void TDEIconDialog::virtual_hook(
int id,
void* data )
770 { KDialogBase::virtual_hook(
id, data ); }
772 #include "kicondialog.moc"
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
void showDialog()
show()es this dialog and emits a newIcon(const TQString&) signal when successful. ...
void setStrictIconSize(bool b)
Sets a strict icon size policy for allowed icons.
void nameChanged(TQString)
Emitted when the current icon has changed.
bool strictIconSize() const
Returns true if a strict icon size policy is set.
int iconSize() const
Returns the iconsize set via setIconSize() or 0, if the default iconsize will be used.
Provides a user (and developer) friendly way to select files and directories.
void setIconSize(int size)
Sets the size of the icons to be shown / selected.
TQString openDialog()
exec()utes this modal dialog and returns the name of the selected icon, or TQString::null if the dial...
TQString selectIcon(TDEIcon::Group group=TDEIcon::Desktop, TDEIcon::Context context=TDEIcon::Application, bool user=false)
TDEIconDialog(TQWidget *parent=0L, const char *name=0L)
Constructs an icon selection dialog using the global iconloader.
Image preview widget for the file dialog.
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 loadFiles(const TQStringList &files)
Load icons into the canvas.
Dialog for interactive selection of icons.
TQString getCurrent() const
Returns the current icon.
Icon canvas for TDEIconDialog.
~TDEIconDialog()
Destructs the dialog.