• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.0 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kfile
kicondialog.cpp
Go to the documentation of this file.
1 /* vi: ts=8 sts=4 sw=4
2  *
3  * This file is part of the KDE project, module kfile.
4  * Copyright (C) 2000 Geert Jansen <jansen@kde.org>
5  * (C) 2000 Kurt Granroth <granroth@kde.org>
6  * (C) 1997 Christoph Neerfeld <chris@kde.org>
7  * (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
8  *
9  * This is free software; it comes under the GNU Library General
10  * Public License, version 2. See the file "COPYING.LIB" for the
11  * exact licensing terms.
12  */
13 
14 #include "kicondialog.h"
15 
16 #include <kio/kio_export.h>
17 
18 #include <kcombobox.h>
19 #include <klistwidgetsearchline.h>
20 #include <klocale.h>
21 #include <kstandarddirs.h>
22 #include <kiconloader.h>
23 #include <kfiledialog.h>
24 #include <kimagefilepreview.h>
25 #ifndef _WIN32_WCE
26 #include <ksvgrenderer.h>
27 #endif
28 
29 #include <QtGui/QApplication>
30 #include <QtGui/QGroupBox>
31 #include <QtGui/QLayout>
32 #include <QtGui/QLabel>
33 #include <QtCore/QTimer>
34 #include <QtGui/QRadioButton>
35 #include <QtCore/QFileInfo>
36 #include <QtGui/QProgressBar>
37 #include <QtGui/QPainter>
38 #include <QtGui/QScrollBar>
39 
40 
46 class KIconCanvasDelegate : public QAbstractItemDelegate
47 {
48 public:
49  KIconCanvasDelegate(KIconCanvas *parent, QAbstractItemDelegate *defaultDelegate);
50  ~KIconCanvasDelegate() {};
51  void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
52  QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
53 private:
54  KIconCanvas *m_iconCanvas;
55  QAbstractItemDelegate *m_defaultDelegate;
56  static const int HORIZONTAL_EDGE_PAD = 3;
57 };
58 
59 KIconCanvasDelegate::KIconCanvasDelegate(KIconCanvas *parent, QAbstractItemDelegate *defaultDelegate)
60  : QAbstractItemDelegate(parent)
61 {
62  m_iconCanvas = parent;
63  m_defaultDelegate = defaultDelegate;
64 }
65 
66 void KIconCanvasDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
67 {
68  const int GRID_WIDTH = m_iconCanvas->gridSize().width();
69  QStyleOptionViewItem newOption = option;
70  // Manipulate the width available.
71  newOption.rect.setX((option.rect.x() / GRID_WIDTH) * GRID_WIDTH + HORIZONTAL_EDGE_PAD);
72  newOption.rect.setWidth(GRID_WIDTH - 2 * HORIZONTAL_EDGE_PAD);
73 
74  m_defaultDelegate->paint(painter, newOption, index);
75 }
76 
77 QSize KIconCanvasDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
78 {
79  QSize size = m_defaultDelegate->sizeHint(option, index);
80  const int GRID_WIDTH = m_iconCanvas->gridSize().width();
81  size.setWidth(GRID_WIDTH - 2 * HORIZONTAL_EDGE_PAD);
82  return size;
83 }
84 
85 class KIconCanvas::KIconCanvasPrivate
86 {
87  public:
88  KIconCanvasPrivate(KIconCanvas *qq) { q = qq; m_bLoading = false; }
89  ~KIconCanvasPrivate() {}
90  KIconCanvas *q;
91  bool m_bLoading;
92  QStringList mFiles;
93  QTimer *mpTimer;
94  KIconCanvasDelegate *mpDelegate;
95 
96  // slots
97  void _k_slotLoadFiles();
98  void _k_slotCurrentChanged(QListWidgetItem *item);
99 };
100 
104 class IconPath : public QString
105 {
106 protected:
107  QString m_iconName;
108 
109 public:
110  IconPath(const QString &ip) : QString (ip)
111  {
112  int n = lastIndexOf('/');
113  m_iconName = (n==-1) ? static_cast<QString>(*this) : mid(n+1);
114  }
115 
116 
117  IconPath() : QString ()
118  { }
119 
120  bool operator== (const IconPath &ip) const
121  { return m_iconName == ip.m_iconName; }
122 
123  bool operator< (const IconPath &ip) const
124  { return m_iconName < ip.m_iconName; }
125 
126 };
127 
128 /*
129  * KIconCanvas: Iconview for the iconloader dialog.
130  */
131 
132 KIconCanvas::KIconCanvas(QWidget *parent)
133  : KListWidget(parent), d(new KIconCanvasPrivate(this))
134 {
135  setViewMode(IconMode);
136  setUniformItemSizes(true);
137  setMovement(Static);
138  setIconSize(QSize(60, 60));
139  d->mpTimer = new QTimer(this);
140  connect(d->mpTimer, SIGNAL(timeout()), this, SLOT(_k_slotLoadFiles()));
141  connect(this, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
142  this, SLOT(_k_slotCurrentChanged(QListWidgetItem*)));
143  setGridSize(QSize(100,80));
144 
145  d->mpDelegate = new KIconCanvasDelegate(this, itemDelegate());
146  setItemDelegate(d->mpDelegate);
147 }
148 
149 KIconCanvas::~KIconCanvas()
150 {
151  delete d->mpTimer;
152  delete d->mpDelegate;
153  delete d;
154 }
155 
156 void KIconCanvas::loadFiles(const QStringList& files)
157 {
158  clear();
159  d->mFiles = files;
160  emit startLoading(d->mFiles.count());
161  d->mpTimer->setSingleShot(true);
162  d->mpTimer->start(10);
163  d->m_bLoading = false;
164 }
165 
166 void KIconCanvas::KIconCanvasPrivate::_k_slotLoadFiles()
167 {
168  q->setResizeMode(QListWidget::Fixed);
169  QApplication::setOverrideCursor(Qt::WaitCursor);
170 
171  // disable updates to not trigger paint events when adding child items,
172  // but force an initial paint so that we do not get garbage
173  q->repaint();
174  q->setUpdatesEnabled(false);
175 
176  // Cache these as we will call them frequently.
177  const int canvasIconWidth = q->iconSize().width();
178  const int canvasIconHeight = q->iconSize().width();
179  const bool uniformIconSize = q->uniformItemSizes();
180 
181  m_bLoading = true;
182  int i;
183  QStringList::ConstIterator it;
184  uint emitProgress = 10; // so we will emit it once in the beginning
185  QStringList::ConstIterator end(mFiles.constEnd());
186  for (it=mFiles.constBegin(), i=0; it!=end; ++it, i++)
187  {
188  if ( emitProgress >= 10 ) {
189  emit q->progress(i);
190  emitProgress = 0;
191  }
192 
193  emitProgress++;
194 
195  if (!m_bLoading) { // user clicked on a button that will load another set of icons
196  break;
197  }
198  QImage img;
199 
200  // Use the extension as the format. Works for XPM and PNG, but not for SVG
201  QString path= *it;
202  QString ext = path.right(3).toUpper();
203 
204  if (ext != "SVG" && ext != "VGZ")
205  img.load(*it);
206  else {
207 #ifndef _WIN32_WCE
208  // Special stuff for SVG icons
209  img = QImage(canvasIconWidth, canvasIconHeight, QImage::Format_ARGB32_Premultiplied);
210  img.fill(0);
211  QSvgRenderer renderer(*it);
212  if (renderer.isValid()) {
213  QPainter p(&img);
214  renderer.render(&p);
215  }
216 #endif
217  }
218 
219  if (img.isNull())
220  continue;
221  if (img.width() > canvasIconWidth || img.height() > canvasIconHeight)
222  {
223  if (img.width() / (float)canvasIconWidth > img.height() / (float)canvasIconHeight)
224  {
225  int height = (int) (((float)canvasIconWidth / img.width()) * img.height());
226  img = img.scaled(canvasIconWidth, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
227  } else
228  {
229  int width = (int) (((float)canvasIconHeight / img.height()) * img.width());
230  img = img.scaled(width, canvasIconHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
231  }
232  }
233 
234  if (uniformIconSize && (img.width() != canvasIconWidth || img.height() != canvasIconHeight))
235  {
236  // Image is smaller than desired. Draw onto a transparent QImage of the required dimensions.
237  // (Unpleasant glitches occur if we break the uniformIconSizes() contract).
238  QImage paddedImage = QImage(canvasIconWidth, canvasIconHeight, QImage::Format_ARGB32_Premultiplied);
239  paddedImage.fill(0);
240  QPainter painter(&paddedImage);
241  painter.drawImage( (canvasIconWidth - img.width()) / 2, (canvasIconHeight - img.height()) / 2, img);
242  img = paddedImage;
243  }
244 
245  QPixmap pm = QPixmap::fromImage(img);
246  QFileInfo fi(*it);
247  QListWidgetItem *item = new QListWidgetItem(pm, fi.completeBaseName(), q);
248  item->setData(Qt::UserRole, *it);
249  item->setToolTip(fi.completeBaseName());
250  }
251 
252  // enable updates since we have to draw the whole view now
253  q->setUpdatesEnabled(true);
254 
255  QApplication::restoreOverrideCursor();
256  m_bLoading = false;
257  emit q->finished();
258  q->setResizeMode(QListWidget::Adjust);
259 }
260 
261 QString KIconCanvas::getCurrent() const
262 {
263  if (!currentItem())
264  return QString();
265  return currentItem()->data(Qt::UserRole).toString();
266 }
267 
268 void KIconCanvas::stopLoading()
269 {
270  d->m_bLoading = false;
271 }
272 
273 void KIconCanvas::KIconCanvasPrivate::_k_slotCurrentChanged(QListWidgetItem *item)
274 {
275  emit q->nameChanged((item != 0L) ? item->text() : QString());
276 }
277 
278 class KIconDialog::KIconDialogPrivate
279 {
280  public:
281  KIconDialogPrivate(KIconDialog *qq) {
282  q = qq;
283  m_bStrictIconSize = true;
284  m_bLockUser = false;
285  m_bLockCustomDir = false;
286  searchLine = 0;
287  mNumOfSteps = 1;
288  }
289  ~KIconDialogPrivate() {}
290 
291  void init();
292  void showIcons();
293  void setContext( KIconLoader::Context context );
294 
295  // slots
296  void _k_slotContext(int);
297  void _k_slotStartLoading(int);
298  void _k_slotProgress(int);
299  void _k_slotFinished();
300  void _k_slotAcceptIcons();
301  void _k_slotBrowse();
302  void _k_slotOtherIconClicked();
303  void _k_slotSystemIconClicked();
304 
305  KIconDialog *q;
306 
307  int mGroupOrSize;
308  KIconLoader::Context mContext;
309 
310  QStringList mFileList;
311  KComboBox *mpCombo;
312  QPushButton *mpBrowseBut;
313  QRadioButton *mpSystemIcons, *mpOtherIcons;
314  QProgressBar *mpProgress;
315  int mNumOfSteps;
316  KIconLoader *mpLoader;
317  KIconCanvas *mpCanvas;
318  int mNumContext;
319  KIconLoader::Context mContextMap[ 12 ]; // must match KIcon::Context size, code has assert
320 
321  bool m_bStrictIconSize, m_bLockUser, m_bLockCustomDir;
322  QString custom;
323  QString customLocation;
324  KListWidgetSearchLine *searchLine;
325 };
326 
327 /*
328  * KIconDialog: Dialog for selecting icons. Both system and user
329  * specified icons can be chosen.
330  */
331 
332 KIconDialog::KIconDialog(QWidget *parent)
333  : KDialog(parent), d(new KIconDialogPrivate(this))
334 {
335  setModal( true );
336  setCaption( i18n("Select Icon") );
337  setButtons( Ok | Cancel );
338  setDefaultButton( Ok );
339 
340  d->mpLoader = KIconLoader::global();
341  d->init();
342 }
343 
344 KIconDialog::KIconDialog(KIconLoader *loader, QWidget *parent)
345  : KDialog(parent), d(new KIconDialogPrivate(this))
346 {
347  setModal( true );
348  setCaption( i18n("Select Icon") );
349  setButtons( Ok | Cancel );
350  setDefaultButton( Ok );
351 
352  d->mpLoader = loader;
353  d->init();
354 }
355 
356 void KIconDialog::KIconDialogPrivate::init()
357 {
358  mGroupOrSize = KIconLoader::Desktop;
359  mContext = KIconLoader::Any;
360  mFileList = KGlobal::dirs()->findAllResources("appicon", QLatin1String("*.png"));
361 
362  QWidget *main = new QWidget(q);
363  q->setMainWidget(main);
364 
365  QVBoxLayout *top = new QVBoxLayout(main);
366  top->setMargin(0);
367 
368  QGroupBox *bgroup = new QGroupBox(main);
369  bgroup->setTitle(i18n("Icon Source"));
370 
371  QVBoxLayout *vbox = new QVBoxLayout;
372  bgroup->setLayout( vbox );
373  top->addWidget(bgroup);
374 
375  QGridLayout *grid = new QGridLayout();
376  vbox->addLayout(grid);
377 
378  mpSystemIcons = new QRadioButton(i18n("S&ystem icons:"), bgroup);
379  connect(mpSystemIcons, SIGNAL(clicked()), q, SLOT(_k_slotSystemIconClicked()));
380  grid->addWidget(mpSystemIcons, 1, 0);
381  mpCombo = new KComboBox(bgroup);
382  mpCombo->setMaxVisibleItems(12);
383  connect(mpCombo, SIGNAL(activated(int)), q, SLOT(_k_slotContext(int)));
384  grid->addWidget(mpCombo, 1, 1);
385  mpOtherIcons = new QRadioButton(i18n("O&ther icons:"), bgroup);
386  connect(mpOtherIcons, SIGNAL(clicked()), q, SLOT(_k_slotOtherIconClicked()));
387  grid->addWidget(mpOtherIcons, 2, 0);
388  mpBrowseBut = new QPushButton(i18n("&Browse..."), bgroup);
389  connect(mpBrowseBut, SIGNAL(clicked()), q, SLOT(_k_slotBrowse()));
390  grid->addWidget(mpBrowseBut, 2, 1);
391 
392  //
393  // ADD SEARCHLINE
394  //
395  QHBoxLayout *searchLayout = new QHBoxLayout();
396  searchLayout->setMargin(0);
397  top->addLayout(searchLayout);
398 
399  QLabel *searchLabel = new QLabel(i18n("&Search:"), main);
400  searchLayout->addWidget(searchLabel);
401 
402  searchLine = new KListWidgetSearchLine(main);
403  searchLayout->addWidget(searchLine);
404  searchLabel->setBuddy(searchLine);
405 
406  QString wtstr = i18n("Search interactively for icon names (e.g. folder).");
407  searchLabel->setWhatsThis(wtstr);
408  searchLine->setWhatsThis(wtstr);
409 
410 
411  mpCanvas = new KIconCanvas(main);
412  connect(mpCanvas, SIGNAL(itemActivated(QListWidgetItem*)), q, SLOT(_k_slotAcceptIcons()));
413  top->addWidget(mpCanvas);
414  searchLine->setListWidget(mpCanvas);
415 
416  // Compute width of canvas with 4 icons displayed in a row
417  QStyleOption opt;
418  opt.initFrom(mpCanvas);
419  int width = 4 * mpCanvas->gridSize().width() + 1;
420  width += mpCanvas->verticalScrollBar()->sizeHint().width();
421  width += 2 * mpCanvas->frameWidth();
422  if (mpCanvas->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &opt, mpCanvas)) {
423  width += mpCanvas->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &opt, mpCanvas);
424  }
425  mpCanvas->setMinimumSize(width, 125);
426 
427  mpProgress = new QProgressBar(main);
428  top->addWidget(mpProgress);
429  connect(mpCanvas, SIGNAL(startLoading(int)), q, SLOT(_k_slotStartLoading(int)));
430  connect(mpCanvas, SIGNAL(progress(int)), q, SLOT(_k_slotProgress(int)));
431  connect(mpCanvas, SIGNAL(finished()), q, SLOT(_k_slotFinished()));
432 
433  // When pressing Ok or Cancel, stop loading icons
434  connect(q, SIGNAL(hidden()), mpCanvas, SLOT(stopLoading()));
435 
436  static const char* const context_text[] = {
437  I18N_NOOP( "Actions" ),
438  I18N_NOOP( "Animations" ),
439  I18N_NOOP( "Applications" ),
440  I18N_NOOP( "Categories" ),
441  I18N_NOOP( "Devices" ),
442  I18N_NOOP( "Emblems" ),
443  I18N_NOOP( "Emotes" ),
444  I18N_NOOP( "Filesystems" ),
445  I18N_NOOP( "International" ),
446  I18N_NOOP( "Mimetypes" ),
447  I18N_NOOP( "Places" ),
448  I18N_NOOP( "Status" ) };
449  static const KIconLoader::Context context_id[] = {
450  KIconLoader::Action,
451  KIconLoader::Animation,
452  KIconLoader::Application,
453  KIconLoader::Category,
454  KIconLoader::Device,
455  KIconLoader::Emblem,
456  KIconLoader::Emote,
457  KIconLoader::FileSystem,
458  KIconLoader::International,
459  KIconLoader::MimeType,
460  KIconLoader::Place,
461  KIconLoader::StatusIcon };
462  mNumContext = 0;
463  int cnt = sizeof( context_text ) / sizeof( context_text[ 0 ] );
464  // check all 3 arrays have same sizes
465  Q_ASSERT( cnt == sizeof( context_id ) / sizeof( context_id[ 0 ] )
466  && cnt == sizeof( mContextMap ) / sizeof( mContextMap[ 0 ] ));
467  for( int i = 0;
468  i < cnt;
469  ++i )
470  {
471  if( mpLoader->hasContext( context_id[ i ] ))
472  {
473  mpCombo->addItem(i18n( context_text[ i ] ));
474  mContextMap[ mNumContext++ ] = context_id[ i ];
475  }
476  }
477  mpCombo->setFixedSize(mpCombo->sizeHint());
478 
479  mpBrowseBut->setFixedWidth(mpCombo->width());
480 
481  // Make the dialog a little taller
482  q->incrementInitialSize(QSize(0,100));
483  connect(q, SIGNAL(okClicked()), q, SLOT(slotOk()));
484 }
485 
486 
487 KIconDialog::~KIconDialog()
488 {
489  delete d;
490 }
491 
492 void KIconDialog::KIconDialogPrivate::_k_slotAcceptIcons()
493 {
494  custom.clear();
495  q->slotOk();
496 }
497 
498 void KIconDialog::KIconDialogPrivate::showIcons()
499 {
500  mpCanvas->clear();
501  QStringList filelist;
502  if (mpSystemIcons->isChecked())
503  if (m_bStrictIconSize)
504  filelist=mpLoader->queryIcons(mGroupOrSize, mContext);
505  else
506  filelist=mpLoader->queryIconsByContext(mGroupOrSize, mContext);
507  else if (!customLocation.isNull()) {
508  filelist = mpLoader->queryIconsByDir(customLocation);
509  }
510  else
511  filelist=mFileList;
512 
513  QList<IconPath> iconlist;
514  QStringList::const_iterator it;
515  foreach (const QString &it, filelist) {
516  iconlist.append(IconPath(it));
517  }
518 
519  qSort(iconlist);
520  filelist.clear();
521 
522  foreach (const IconPath &ip, iconlist) {
523  filelist.append(ip);
524  }
525 
526  searchLine->clear();
527 
528  // The KIconCanvas has uniformItemSizes set which really expects
529  // all added icons to be the same size, otherwise weirdness ensues :)
530  // Ensure all SVGs are scaled to the desired size and that as few icons
531  // need to be padded as possible by specifying a sensible size.
532  if (mGroupOrSize < -1) // mGroupOrSize can be -1 if NoGroup is chosen.
533  {
534  // Explicit size.
535  mpCanvas->setIconSize(QSize(-mGroupOrSize, -mGroupOrSize));
536  }
537  else
538  {
539  // Icon group.
540  int groupSize = mpLoader->currentSize((KIconLoader::Group)mGroupOrSize);
541  mpCanvas->setIconSize(QSize(groupSize, groupSize));
542  }
543 
544  mpCanvas->loadFiles(filelist);
545 }
546 
547 void KIconDialog::setStrictIconSize(bool b)
548 {
549  d->m_bStrictIconSize=b;
550 }
551 
552 bool KIconDialog::strictIconSize() const
553 {
554  return d->m_bStrictIconSize;
555 }
556 
557 void KIconDialog::setIconSize( int size )
558 {
559  // see KIconLoader, if you think this is weird
560  if (size == 0) {
561  d->mGroupOrSize = KIconLoader::Desktop; // default Group
562  } else {
563  d->mGroupOrSize = -size; // yes, KIconLoader::queryIconsByContext is weird
564  }
565 }
566 
567 int KIconDialog::iconSize() const
568 {
569  // 0 or any other value ==> mGroupOrSize is a group, so we return 0
570  return (d->mGroupOrSize < 0) ? -d->mGroupOrSize : 0;
571 }
572 
573 void KIconDialog::setup(KIconLoader::Group group, KIconLoader::Context context,
574  bool strictIconSize, int iconSize, bool user,
575  bool lockUser, bool lockCustomDir )
576 {
577  d->m_bStrictIconSize = strictIconSize;
578  d->m_bLockUser = lockUser;
579  d->m_bLockCustomDir = lockCustomDir;
580  if (iconSize == 0)
581  {
582  if (group == KIconLoader::NoGroup)
583  {
584  // NoGroup has numeric value -1, which should
585  // not really be used with KIconLoader::queryIcons*(...);
586  // pick a proper group.
587  d->mGroupOrSize = KIconLoader::Small;
588  }
589  else
590  {
591  d->mGroupOrSize = group;
592  }
593  }
594  else
595  {
596  d->mGroupOrSize = -iconSize;
597  }
598 
599  d->mpSystemIcons->setChecked(!user);
600  d->mpSystemIcons->setEnabled(!lockUser || !user);
601  d->mpOtherIcons->setChecked(user);
602  d->mpOtherIcons->setEnabled(!lockUser || user);
603  d->mpCombo->setEnabled(!user);
604  d->mpBrowseBut->setEnabled(user && !lockCustomDir);
605  d->setContext(context);
606 }
607 
608 void KIconDialog::KIconDialogPrivate::setContext(KIconLoader::Context context)
609 {
610  mContext = context;
611  for( int i = 0;
612  i < mNumContext;
613  ++i )
614  if( mContextMap[ i ] == context )
615  {
616  mpCombo->setCurrentIndex( i );
617  return;
618  }
619 }
620 
621 void KIconDialog::setCustomLocation( const QString& location )
622 {
623  d->customLocation = location;
624 }
625 
626 QString KIconDialog::openDialog()
627 {
628  d->showIcons();
629  d->searchLine->setFocus();
630 
631  if ( exec() == Accepted )
632  {
633  if (!d->custom.isNull())
634  return d->custom;
635  QString name = d->mpCanvas->getCurrent();
636  if (name.isEmpty() || d->mpOtherIcons->isChecked()) {
637  return name;
638  }
639  QFileInfo fi(name);
640  return fi.completeBaseName();
641  }
642  return QString();
643 }
644 
645 void KIconDialog::showDialog()
646 {
647  setModal(false);
648  d->showIcons();
649  d->searchLine->setFocus();
650  show();
651 }
652 
653 void KIconDialog::slotOk()
654 {
655  QString name;
656  if (!d->custom.isNull())
657  {
658  name = d->custom;
659  }
660  else
661  {
662  name = d->mpCanvas->getCurrent();
663  if (!name.isEmpty() && d->mpSystemIcons->isChecked()) {
664  QFileInfo fi(name);
665  name = fi.baseName();
666  }
667  }
668 
669  emit newIconName(name);
670  KDialog::accept();
671 }
672 
673 QString KIconDialog::getIcon(KIconLoader::Group group, KIconLoader::Context context,
674  bool strictIconSize, int iconSize, bool user,
675  QWidget *parent, const QString &caption)
676 {
677  KIconDialog dlg(parent);
678  dlg.setup( group, context, strictIconSize, iconSize, user );
679  if (!caption.isNull())
680  dlg.setCaption(caption);
681 
682  return dlg.openDialog();
683 }
684 
685 void KIconDialog::KIconDialogPrivate::_k_slotBrowse()
686 {
687  // Create a file dialog to select a PNG, XPM or SVG file,
688  // with the image previewer shown.
689  // KFileDialog::getImageOpenURL doesn't allow svg.
690  KUrl emptyUrl;
691  KFileDialog dlg(emptyUrl, i18n("*.png *.xpm *.svg *.svgz|Icon Files (*.png *.xpm *.svg *.svgz)"), q);
692  dlg.setOperationMode( KFileDialog::Opening );
693  dlg.setCaption( i18n("Open") );
694  dlg.setMode( KFile::File );
695 
696  KImageFilePreview *ip = new KImageFilePreview( &dlg );
697  dlg.setPreviewWidget( ip );
698  dlg.exec();
699 
700  QString file = dlg.selectedFile();
701  if (!file.isEmpty())
702  {
703  custom = file;
704  if (mpSystemIcons->isChecked()) {
705  customLocation = QFileInfo(file).absolutePath();
706  }
707  q->slotOk();
708  }
709 }
710 
711 void KIconDialog::KIconDialogPrivate::_k_slotSystemIconClicked()
712 {
713  mpBrowseBut->setEnabled(false);
714  mpCombo->setEnabled(true);
715  showIcons();
716 }
717 
718 void KIconDialog::KIconDialogPrivate::_k_slotOtherIconClicked()
719 {
720  mpBrowseBut->setEnabled(!m_bLockCustomDir);
721  mpCombo->setEnabled(false);
722  showIcons();
723 }
724 
725 void KIconDialog::KIconDialogPrivate::_k_slotContext(int id)
726 {
727  mContext = static_cast<KIconLoader::Context>( mContextMap[ id ] );
728  showIcons();
729 }
730 
731 void KIconDialog::KIconDialogPrivate::_k_slotStartLoading(int steps)
732 {
733  if (steps < 10)
734  mpProgress->hide();
735  else
736  {
737  mNumOfSteps = steps;
738  mpProgress->setValue(0);
739  mpProgress->show();
740  }
741 }
742 
743 void KIconDialog::KIconDialogPrivate::_k_slotProgress(int p)
744 {
745  mpProgress->setValue(static_cast<int>(100.0 * (double)p / (double)mNumOfSteps));
746 }
747 
748 void KIconDialog::KIconDialogPrivate::_k_slotFinished()
749 {
750  mNumOfSteps = 1;
751  mpProgress->hide();
752 }
753 
754 class KIconButton::KIconButtonPrivate
755 {
756  public:
757  KIconButtonPrivate(KIconButton *qq, KIconLoader *loader);
758  ~KIconButtonPrivate();
759 
760  // slots
761  void _k_slotChangeIcon();
762  void _k_newIconName(const QString&);
763 
764  KIconButton *q;
765 
766  int iconSize;
767  int buttonIconSize;
768  bool m_bStrictIconSize;
769 
770  bool mbUser;
771  KIconLoader::Group mGroup;
772  KIconLoader::Context mContext;
773 
774  QString mIcon;
775  KIconDialog *mpDialog;
776  KIconLoader *mpLoader;
777 };
778 
779 
780 /*
781  * KIconButton: A "choose icon" pushbutton.
782  */
783 
784 KIconButton::KIconButton(QWidget *parent)
785  : QPushButton(parent), d(new KIconButtonPrivate(this, KIconLoader::global()))
786 {
787  QPushButton::setIconSize(QSize(48, 48));
788 }
789 
790 KIconButton::KIconButton(KIconLoader *loader, QWidget *parent)
791  : QPushButton(parent), d(new KIconButtonPrivate(this, loader))
792 {
793  QPushButton::setIconSize(QSize(48, 48));
794 }
795 
796 KIconButton::KIconButtonPrivate::KIconButtonPrivate(KIconButton *qq, KIconLoader *loader)
797  : q(qq)
798 {
799  m_bStrictIconSize = false;
800  iconSize = 0; // let KIconLoader choose the default
801  buttonIconSize = -1; //When buttonIconSize is -1, iconSize will be used for the button
802 
803  mGroup = KIconLoader::Desktop;
804  mContext = KIconLoader::Application;
805  mbUser = false;
806 
807  mpLoader = loader;
808  mpDialog = 0L;
809  connect(q, SIGNAL(clicked()), q, SLOT(_k_slotChangeIcon()));
810 }
811 
812 KIconButton::KIconButtonPrivate::~KIconButtonPrivate()
813 {
814  delete mpDialog;
815 }
816 
817 KIconButton::~KIconButton()
818 {
819  delete d;
820 }
821 
822 void KIconButton::setStrictIconSize(bool b)
823 {
824  d->m_bStrictIconSize=b;
825 }
826 
827 bool KIconButton::strictIconSize() const
828 {
829  return d->m_bStrictIconSize;
830 }
831 
832 void KIconButton::setIconSize( int size )
833 {
834  if (d->buttonIconSize == -1) {
835  QPushButton::setIconSize(QSize(size, size));
836  }
837 
838  d->iconSize = size;
839 }
840 
841 int KIconButton::iconSize() const
842 {
843  return d->iconSize;
844 }
845 
846 void KIconButton::setButtonIconSize( int size )
847 {
848  QPushButton::setIconSize(QSize(size, size));
849  d->buttonIconSize = size;
850 }
851 
852 int KIconButton::buttonIconSize() const
853 {
854  return QPushButton::iconSize().height();
855 }
856 
857 void KIconButton::setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user)
858 {
859  d->mGroup = group;
860  d->mContext = context;
861  d->mbUser = user;
862 }
863 
864 void KIconButton::setIcon(const QString& icon)
865 {
866  d->mIcon = icon;
867  setIcon(KIcon(d->mIcon));
868 
869  if (!d->mpDialog) {
870  d->mpDialog = new KIconDialog(d->mpLoader, this);
871  connect(d->mpDialog, SIGNAL(newIconName(QString)), this, SLOT(_k_newIconName(QString)));
872  }
873 
874  if (d->mbUser) {
875  d->mpDialog->setCustomLocation(QFileInfo(d->mpLoader->iconPath(d->mIcon, d->mGroup, true) ).absolutePath());
876  }
877 }
878 
879 void KIconButton::setIcon(const QIcon& icon)
880 {
881  QPushButton::setIcon(icon);
882 }
883 
884 void KIconButton::resetIcon()
885 {
886  d->mIcon.clear();
887  setIcon(QIcon());
888 }
889 
890 const QString &KIconButton::icon() const
891 {
892  return d->mIcon;
893 }
894 
895 void KIconButton::KIconButtonPrivate::_k_slotChangeIcon()
896 {
897  if (!mpDialog)
898  {
899  mpDialog = new KIconDialog(mpLoader, q);
900  connect(mpDialog, SIGNAL(newIconName(QString)), q, SLOT(_k_newIconName(QString)));
901  }
902 
903  mpDialog->setup(mGroup, mContext, m_bStrictIconSize, iconSize, mbUser);
904  mpDialog->showDialog();
905 }
906 
907 void KIconButton::KIconButtonPrivate::_k_newIconName(const QString& name)
908 {
909  if (name.isEmpty())
910  return;
911 
912  q->setIcon(KIcon(name));
913  mIcon = name;
914 
915  if (mbUser) {
916  mpDialog->setCustomLocation(QFileInfo(mpLoader->iconPath(mIcon, mGroup, true)).absolutePath());
917  }
918 
919  emit q->iconChanged(name);
920 }
921 
922 #include "kicondialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Feb 9 2013 12:13:37 by doxygen 1.8.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.10.0 API Reference

Skip menu "kdelibs-4.10.0 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal