• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

kicondialog.cpp

Go to the documentation of this file.
00001 /* vi: ts=8 sts=4 sw=4
00002  *
00003  * This file is part of the KDE project, module kfile.
00004  * Copyright (C) 2000 Geert Jansen <jansen@kde.org>
00005  *           (C) 2000 Kurt Granroth <granroth@kde.org>
00006  *           (C) 1997 Christoph Neerfeld <chris@kde.org>
00007  *           (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
00008  *
00009  * This is free software; it comes under the GNU Library General
00010  * Public License, version 2. See the file "COPYING.LIB" for the
00011  * exact licensing terms.
00012  */
00013 
00014 #include "kicondialog.h"
00015 
00016 #include <kio/kio_export.h>
00017 
00018 #include <kbuttongroup.h>
00019 #include <kcombobox.h>
00020 #include <klistwidgetsearchline.h>
00021 #include <kapplication.h>
00022 #include <klocale.h>
00023 #include <kstandarddirs.h>
00024 #include <kiconloader.h>
00025 #include <kfiledialog.h>
00026 #include <kimagefilepreview.h>
00027 #include <ksvgrenderer.h>
00028 
00029 #include <QtGui/QLayout>
00030 #include <QtGui/QLabel>
00031 #include <QtCore/QTimer>
00032 #include <QtGui/QRadioButton>
00033 #include <QtCore/QFileInfo>
00034 #include <QtGui/QProgressBar>
00035 #include <QtGui/QPainter>
00036 
00037 
00043 class KIconCanvasDelegate : public QAbstractItemDelegate
00044 {
00045 public:
00046     KIconCanvasDelegate(KIconCanvas *parent, QAbstractItemDelegate *defaultDelegate);
00047     ~KIconCanvasDelegate() {};
00048     void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const; 
00049     QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
00050 private:
00051     KIconCanvas *m_iconCanvas;
00052     QAbstractItemDelegate *m_defaultDelegate;
00053     static const int HORIZONTAL_EDGE_PAD = 3;
00054 };
00055 
00056 KIconCanvasDelegate::KIconCanvasDelegate(KIconCanvas *parent, QAbstractItemDelegate *defaultDelegate)
00057     : QAbstractItemDelegate(parent)
00058 {
00059     m_iconCanvas = parent;
00060     m_defaultDelegate = defaultDelegate;
00061 }
00062 
00063 void KIconCanvasDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
00064 {
00065     const int GRID_WIDTH = m_iconCanvas->gridSize().width();
00066     QStyleOptionViewItem newOption = option;
00067     // Manipulate the width available.
00068     newOption.rect.setX((option.rect.x() / GRID_WIDTH) * GRID_WIDTH + HORIZONTAL_EDGE_PAD);
00069     newOption.rect.setWidth(GRID_WIDTH - 2 * HORIZONTAL_EDGE_PAD);
00070 
00071     m_defaultDelegate->paint(painter, newOption, index);
00072 }
00073 
00074 QSize KIconCanvasDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
00075 {
00076     QSize size = m_defaultDelegate->sizeHint(option, index);
00077     const int GRID_WIDTH = m_iconCanvas->gridSize().width();
00078     size.setWidth(GRID_WIDTH - 2 * HORIZONTAL_EDGE_PAD);
00079     return size;
00080 }
00081 
00082 class KIconCanvas::KIconCanvasPrivate
00083 {
00084   public:
00085     KIconCanvasPrivate(KIconCanvas *qq) { q = qq; m_bLoading = false; }
00086     ~KIconCanvasPrivate() {}
00087     KIconCanvas *q;
00088     bool m_bLoading;
00089     QStringList mFiles;
00090     QTimer *mpTimer;
00091     KIconCanvasDelegate *mpDelegate;
00092 
00093     // slots
00094     void _k_slotLoadFiles();
00095     void _k_slotCurrentChanged(QListWidgetItem *item);
00096 };
00097 
00101 class IconPath : public QString
00102 {
00103 protected:
00104  QString m_iconName;
00105 
00106 public:
00107  IconPath(const QString &ip) : QString (ip)
00108  {
00109    int n = lastIndexOf('/');
00110    m_iconName = (n==-1) ? static_cast<QString>(*this) : mid(n+1);
00111  }
00112 
00113 
00114  IconPath() : QString ()
00115  { }
00116 
00117  bool operator== (const IconPath &ip) const
00118  { return m_iconName == ip.m_iconName; }
00119 
00120  bool operator< (const IconPath &ip) const
00121  { return m_iconName < ip.m_iconName; }
00122 
00123 };
00124 
00125 /*
00126  * KIconCanvas: Iconview for the iconloader dialog.
00127  */
00128 
00129 KIconCanvas::KIconCanvas(QWidget *parent)
00130     : KListWidget(parent), d(new KIconCanvasPrivate(this))
00131 {
00132     setViewMode(IconMode);
00133     setUniformItemSizes(true);
00134     setMovement(Static);
00135     setIconSize(QSize(60, 60));
00136     d->mpTimer = new QTimer(this);
00137     connect(d->mpTimer, SIGNAL(timeout()), this, SLOT(_k_slotLoadFiles()));
00138     connect(this, SIGNAL( currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
00139             this, SLOT(_k_slotCurrentChanged(QListWidgetItem *)));
00140     setGridSize(QSize(100,80));
00141 
00142     d->mpDelegate = new KIconCanvasDelegate(this, itemDelegate());
00143     setItemDelegate(d->mpDelegate);
00144 }
00145 
00146 KIconCanvas::~KIconCanvas()
00147 {
00148     delete d->mpTimer;
00149     delete d->mpDelegate;
00150     delete d;
00151 }
00152 
00153 void KIconCanvas::loadFiles(const QStringList& files)
00154 {
00155     clear();
00156     d->mFiles = files;
00157     emit startLoading(d->mFiles.count());
00158     d->mpTimer->setSingleShot(true);
00159     d->mpTimer->start(10);
00160     d->m_bLoading = false;
00161 }
00162 
00163 void KIconCanvas::KIconCanvasPrivate::_k_slotLoadFiles()
00164 {
00165     q->setResizeMode(QListWidget::Fixed);
00166     QApplication::setOverrideCursor(Qt::WaitCursor);
00167 
00168     // disable updates to not trigger paint events when adding child items,
00169     // but force an initial paint so that we do not get garbage
00170     q->repaint();
00171     q->setUpdatesEnabled(false);
00172 
00173     // Cache these as we will call them frequently.
00174     const int canvasIconWidth = q->iconSize().width();
00175     const int canvasIconHeight = q->iconSize().width();
00176     const bool uniformIconSize = q->uniformItemSizes();
00177 
00178     m_bLoading = true;
00179     int i;
00180     QStringList::ConstIterator it;
00181     uint emitProgress = 10; // so we will emit it once in the beginning
00182     QStringList::ConstIterator end(mFiles.constEnd());
00183     for (it=mFiles.constBegin(), i=0; it!=end; ++it, i++)
00184     {
00185     if ( emitProgress >= 10 ) {
00186             emit q->progress(i);
00187             emitProgress = 0;
00188         }
00189 
00190         emitProgress++;
00191 
00192         if (!m_bLoading) { // user clicked on a button that will load another set of icons
00193             break;
00194         }
00195     QImage img;
00196 
00197     // Use the extension as the format. Works for XPM and PNG, but not for SVG
00198     QString path= *it;
00199     QString ext = path.right(3).toUpper();
00200 
00201     if (ext != "SVG" && ext != "VGZ")
00202         img.load(*it);
00203     else {
00204             // Special stuff for SVG icons
00205             img = QImage(canvasIconWidth, canvasIconHeight, QImage::Format_ARGB32_Premultiplied);
00206             img.fill(0);
00207             KSvgRenderer renderer(*it);
00208             if (renderer.isValid()) {
00209                 QPainter p(&img);
00210                 renderer.render(&p);
00211             }
00212         }
00213 
00214     if (img.isNull())
00215         continue;
00216     if (img.width() > canvasIconWidth || img.height() > canvasIconHeight)
00217     {
00218         if (img.width() / (float)canvasIconWidth  > img.height() / (float)canvasIconHeight)
00219         {
00220         int height = (int) (((float)canvasIconWidth / img.width()) * img.height());
00221         img = img.scaled(canvasIconWidth, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
00222         } else
00223         {
00224         int width = (int) (((float)canvasIconHeight / img.height()) * img.width());
00225         img = img.scaled(width, canvasIconHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
00226         }
00227     }
00228     
00229     if (uniformIconSize && (img.width() != canvasIconWidth || img.height() != canvasIconHeight))
00230     {
00231        // Image is smaller than desired.  Draw onto a transparent QImage of the required dimensions.
00232        // (Unpleasant glitches occur if we break the uniformIconSizes() contract).
00233        QImage paddedImage = QImage(canvasIconWidth, canvasIconHeight, QImage::Format_ARGB32_Premultiplied);
00234        paddedImage.fill(0);
00235        QPainter painter(&paddedImage);
00236        painter.drawImage( (canvasIconWidth - img.width()) / 2, (canvasIconHeight - img.height()) / 2, img);
00237        img = paddedImage;
00238     }
00239     
00240     QPixmap pm = QPixmap::fromImage(img);
00241     QFileInfo fi(*it);
00242         QListWidgetItem *item = new QListWidgetItem(pm, fi.completeBaseName(), q);
00243     item->setData(Qt::UserRole, *it);
00244         item->setToolTip(fi.completeBaseName());
00245     }
00246 
00247     // enable updates since we have to draw the whole view now
00248     q->setUpdatesEnabled(true);
00249 
00250     QApplication::restoreOverrideCursor();
00251     m_bLoading = false;
00252     emit q->finished();
00253     q->setResizeMode(QListWidget::Adjust);
00254 }
00255 
00256 QString KIconCanvas::getCurrent() const
00257 {
00258     if (!currentItem())
00259     return QString();
00260     return currentItem()->data(Qt::UserRole).toString();
00261 }
00262 
00263 void KIconCanvas::stopLoading()
00264 {
00265     d->m_bLoading = false;
00266 }
00267 
00268 void KIconCanvas::KIconCanvasPrivate::_k_slotCurrentChanged(QListWidgetItem *item)
00269 {
00270     emit q->nameChanged((item != 0L) ? item->text() : QString());
00271 }
00272 
00273 class KIconDialog::KIconDialogPrivate
00274 {
00275   public:
00276     KIconDialogPrivate(KIconDialog *qq) {
00277         q = qq;
00278         m_bStrictIconSize = true;
00279     m_bLockUser = false;
00280     m_bLockCustomDir = false;
00281     searchLine = 0;
00282         mNumOfSteps = 1;
00283     }
00284     ~KIconDialogPrivate() {}
00285 
00286     void init();
00287     void showIcons();
00288     void setContext( KIconLoader::Context context );
00289 
00290     // slots
00291     void _k_slotContext(int);
00292     void _k_slotStartLoading(int);
00293     void _k_slotProgress(int);
00294     void _k_slotFinished();
00295     void _k_slotAcceptIcons();
00296     void _k_slotBrowse();
00297     void _k_slotOtherIconClicked();
00298     void _k_slotSystemIconClicked();
00299 
00300     KIconDialog *q;
00301 
00302     int mGroupOrSize;
00303     KIconLoader::Context mContext;
00304 
00305     QStringList mFileList;
00306     KComboBox *mpCombo;
00307     QPushButton *mpBrowseBut;
00308     QRadioButton *mpSystemIcons, *mpOtherIcons;
00309     QProgressBar *mpProgress;
00310     int mNumOfSteps;
00311     KIconLoader *mpLoader;
00312     KIconCanvas *mpCanvas;
00313     int mNumContext;
00314     KIconLoader::Context mContextMap[ 12 ]; // must match KIcon::Context size, code has assert
00315 
00316     bool m_bStrictIconSize, m_bLockUser, m_bLockCustomDir;
00317     QString custom;
00318     QString customLocation;
00319     KListWidgetSearchLine *searchLine;
00320 };
00321 
00322 /*
00323  * KIconDialog: Dialog for selecting icons. Both system and user
00324  * specified icons can be chosen.
00325  */
00326 
00327 KIconDialog::KIconDialog(QWidget *parent)
00328     : KDialog(parent), d(new KIconDialogPrivate(this))
00329 {
00330     setModal( true );
00331     setCaption( i18n("Select Icon") );
00332     setButtons( Ok | Cancel );
00333     setDefaultButton( Ok );
00334 
00335     d->mpLoader = KIconLoader::global();
00336     d->init();
00337 }
00338 
00339 KIconDialog::KIconDialog(KIconLoader *loader, QWidget *parent)
00340     : KDialog(parent), d(new KIconDialogPrivate(this))
00341 {
00342     setModal( true );
00343     setCaption( i18n("Select Icon") );
00344     setButtons( Ok | Cancel );
00345     setDefaultButton( Ok );
00346 
00347     d->mpLoader = loader;
00348     d->init();
00349 }
00350 
00351 void KIconDialog::KIconDialogPrivate::init()
00352 {
00353     mGroupOrSize = KIconLoader::Desktop;
00354     mContext = KIconLoader::Any;
00355     mFileList = KGlobal::dirs()->findAllResources("appicon", QLatin1String("*.png"));
00356 
00357     QWidget *main = new QWidget(q);
00358     q->setMainWidget(main);
00359 
00360     QVBoxLayout *top = new QVBoxLayout(main);
00361     top->setMargin(0);
00362 
00363     QGroupBox *bgroup = new QGroupBox(main);
00364     bgroup->setTitle(i18n("Icon Source"));
00365 
00366     QVBoxLayout *vbox = new QVBoxLayout;
00367     bgroup->setLayout( vbox );
00368     top->addWidget(bgroup);
00369 
00370     QGridLayout *grid = new QGridLayout();
00371     bgroup->layout()->addItem(grid);
00372 
00373     mpSystemIcons = new QRadioButton(i18n("S&ystem icons:"), bgroup);
00374     connect(mpSystemIcons, SIGNAL(clicked()), q, SLOT(_k_slotSystemIconClicked()));
00375     grid->addWidget(mpSystemIcons, 1, 0);
00376     mpCombo = new KComboBox(bgroup);
00377     connect(mpCombo, SIGNAL(activated(int)), q, SLOT(_k_slotContext(int)));
00378     grid->addWidget(mpCombo, 1, 1);
00379     mpOtherIcons = new QRadioButton(i18n("O&ther icons:"), bgroup);
00380     connect(mpOtherIcons, SIGNAL(clicked()), q, SLOT(_k_slotOtherIconClicked()));
00381     grid->addWidget(mpOtherIcons, 2, 0);
00382     mpBrowseBut = new QPushButton(i18n("&Browse..."), bgroup);
00383     connect(mpBrowseBut, SIGNAL(clicked()), q, SLOT(_k_slotBrowse()));
00384     grid->addWidget(mpBrowseBut, 2, 1);
00385 
00386     //
00387     // ADD SEARCHLINE
00388     //
00389     QHBoxLayout *searchLayout = new QHBoxLayout();
00390     searchLayout->setMargin(0);
00391     top->addLayout(searchLayout);
00392 
00393     QLabel *searchLabel = new QLabel(i18n("&Search:"), main);
00394     searchLayout->addWidget(searchLabel);
00395 
00396     searchLine = new KListWidgetSearchLine(main);
00397     searchLayout->addWidget(searchLine);
00398     searchLabel->setBuddy(searchLine);
00399 
00400     QString wtstr = i18n("Search interactively for icon names (e.g. folder).");
00401     searchLabel->setWhatsThis(wtstr);
00402     searchLine->setWhatsThis(wtstr);
00403 
00404 
00405     mpCanvas = new KIconCanvas(main);
00406     connect(mpCanvas, SIGNAL(itemActivated(QListWidgetItem *)), q, SLOT(_k_slotAcceptIcons()));
00407     mpCanvas->setMinimumSize(425, 125);
00408     top->addWidget(mpCanvas);
00409     searchLine->setListWidget(mpCanvas);
00410 
00411     mpProgress = new QProgressBar(main);
00412     top->addWidget(mpProgress);
00413     connect(mpCanvas, SIGNAL(startLoading(int)), q, SLOT(_k_slotStartLoading(int)));
00414     connect(mpCanvas, SIGNAL(progress(int)), q, SLOT(_k_slotProgress(int)));
00415     connect(mpCanvas, SIGNAL(finished()), q, SLOT(_k_slotFinished()));
00416 
00417     // When pressing Ok or Cancel, stop loading icons
00418     connect(q, SIGNAL(hidden()), mpCanvas, SLOT(stopLoading()));
00419 
00420     static const char* const context_text[] = {
00421         I18N_NOOP( "Actions" ),
00422         I18N_NOOP( "Animations" ),
00423         I18N_NOOP( "Applications" ),
00424         I18N_NOOP( "Categories" ),
00425         I18N_NOOP( "Devices" ),
00426         I18N_NOOP( "Emblems" ),
00427         I18N_NOOP( "Emotes" ),
00428         I18N_NOOP( "Filesystems" ),
00429         I18N_NOOP( "International" ),
00430         I18N_NOOP( "Mimetypes" ),
00431         I18N_NOOP( "Places" ),
00432         I18N_NOOP( "Status" ) };
00433     static const KIconLoader::Context context_id[] = {
00434         KIconLoader::Action,
00435         KIconLoader::Animation,
00436         KIconLoader::Application,
00437         KIconLoader::Category,
00438         KIconLoader::Device,
00439         KIconLoader::Emblem,
00440         KIconLoader::Emote,
00441         KIconLoader::FileSystem,
00442         KIconLoader::International,
00443         KIconLoader::MimeType,
00444         KIconLoader::Place,
00445         KIconLoader::StatusIcon };
00446     mNumContext = 0;
00447     int cnt = sizeof( context_text ) / sizeof( context_text[ 0 ] );
00448     // check all 3 arrays have same sizes
00449     Q_ASSERT( cnt == sizeof( context_id ) / sizeof( context_id[ 0 ] )
00450             && cnt == sizeof( mContextMap ) / sizeof( mContextMap[ 0 ] ));
00451     for( int i = 0;
00452          i < cnt;
00453          ++i )
00454     {
00455         if( mpLoader->hasContext( context_id[ i ] ))
00456         {
00457             mpCombo->addItem(i18n( context_text[ i ] ));
00458             mContextMap[ mNumContext++ ] = context_id[ i ];
00459         }
00460     }
00461     mpCombo->setFixedSize(mpCombo->sizeHint());
00462 
00463     mpBrowseBut->setFixedWidth(mpCombo->width());
00464 
00465     // Make the dialog a little taller
00466     q->incrementInitialSize(QSize(0,100));
00467     connect(q, SIGNAL(okClicked()), q, SLOT(slotOk()));
00468 }
00469 
00470 
00471 KIconDialog::~KIconDialog()
00472 {
00473     delete d;
00474 }
00475 
00476 void KIconDialog::KIconDialogPrivate::_k_slotAcceptIcons()
00477 {
00478     custom.clear();
00479     q->slotOk();
00480 }
00481 
00482 void KIconDialog::KIconDialogPrivate::showIcons()
00483 {
00484     mpCanvas->clear();
00485     QStringList filelist;
00486     if (mpSystemIcons->isChecked())
00487         if (m_bStrictIconSize)
00488             filelist=mpLoader->queryIcons(mGroupOrSize, mContext);
00489         else
00490             filelist=mpLoader->queryIconsByContext(mGroupOrSize, mContext);
00491     else if (!customLocation.isNull()) {
00492         filelist = mpLoader->queryIconsByDir(customLocation);
00493     }
00494     else
00495     filelist=mFileList;
00496 
00497     QList<IconPath> iconlist;
00498     QStringList::const_iterator it;
00499     foreach (const QString &it, filelist) {
00500        iconlist.append(IconPath(it));
00501     }
00502 
00503     qSort(iconlist);
00504     filelist.clear();
00505 
00506     foreach (const IconPath &ip, iconlist) {
00507        filelist.append(ip);
00508     }
00509 
00510     searchLine->clear();
00511 
00512     // The KIconCanvas has uniformItemSizes set which really expects
00513     // all added icons to be the same size, otherwise weirdness ensues :)
00514     // Ensure all SVGs are scaled to the desired size and that as few icons
00515     // need to be padded as possible by specifying a sensible size.
00516     if (mGroupOrSize < -1) // mGroupOrSize can be -1 if NoGroup is chosen.
00517     {
00518         // Explicit size.
00519         mpCanvas->setIconSize(QSize(-mGroupOrSize, -mGroupOrSize));
00520     }
00521     else
00522     {
00523         // Icon group.
00524         int groupSize = mpLoader->currentSize((KIconLoader::Group)mGroupOrSize);
00525         mpCanvas->setIconSize(QSize(groupSize, groupSize));
00526     }
00527 
00528     mpCanvas->loadFiles(filelist);
00529 }
00530 
00531 void KIconDialog::setStrictIconSize(bool b)
00532 {
00533     d->m_bStrictIconSize=b;
00534 }
00535 
00536 bool KIconDialog::strictIconSize() const
00537 {
00538     return d->m_bStrictIconSize;
00539 }
00540 
00541 void KIconDialog::setIconSize( int size )
00542 {
00543     // see KIconLoader, if you think this is weird
00544     if (size == 0) {
00545         d->mGroupOrSize = KIconLoader::Desktop; // default Group
00546     } else {
00547         d->mGroupOrSize = -size; // yes, KIconLoader::queryIconsByContext is weird
00548     }
00549 }
00550 
00551 int KIconDialog::iconSize() const
00552 {
00553     // 0 or any other value ==> mGroupOrSize is a group, so we return 0
00554     return (d->mGroupOrSize < 0) ? -d->mGroupOrSize : 0;
00555 }
00556 
00557 void KIconDialog::setup(KIconLoader::Group group, KIconLoader::Context context,
00558                         bool strictIconSize, int iconSize, bool user,
00559                         bool lockUser, bool lockCustomDir )
00560 {
00561     d->m_bStrictIconSize = strictIconSize;
00562     d->m_bLockUser = lockUser;
00563     d->m_bLockCustomDir = lockCustomDir;
00564     if (iconSize == 0)
00565     {
00566         if (group == KIconLoader::NoGroup)
00567         {
00568             // NoGroup has numeric value -1, which should
00569             // not really be used with KIconLoader::queryIcons*(...);
00570             // pick a proper group.
00571             d->mGroupOrSize = KIconLoader::Small;
00572         }
00573         else
00574         {
00575             d->mGroupOrSize = group;
00576         }
00577     }
00578     else
00579     {
00580         d->mGroupOrSize = -iconSize;
00581     }
00582     
00583     d->mpSystemIcons->setChecked(!user);
00584     d->mpSystemIcons->setEnabled(!lockUser || !user);
00585     d->mpOtherIcons->setChecked(user);
00586     d->mpOtherIcons->setEnabled(!lockUser || user);
00587     d->mpCombo->setEnabled(!user);
00588     d->mpBrowseBut->setEnabled(user && !lockCustomDir);
00589     d->setContext(context);
00590 }
00591 
00592 void KIconDialog::KIconDialogPrivate::setContext(KIconLoader::Context context)
00593 {
00594     mContext = context;
00595     for( int i = 0;
00596          i < mNumContext;
00597          ++i )
00598         if( mContextMap[ i ] == context )
00599         {
00600             mpCombo->setCurrentIndex( i );
00601             return;
00602         }
00603 }
00604 
00605 void KIconDialog::setCustomLocation( const QString& location )
00606 {
00607     d->customLocation = location;
00608 }
00609 
00610 QString KIconDialog::openDialog()
00611 {
00612     d->showIcons();
00613     d->searchLine->setFocus();
00614 
00615     if ( exec() == Accepted )
00616     {
00617         if (!d->custom.isNull())
00618             return d->custom;
00619         QString name = d->mpCanvas->getCurrent();
00620         if (name.isEmpty() || d->mpOtherIcons->isChecked()) {
00621             return name;
00622         }
00623         QFileInfo fi(name);
00624         return fi.baseName();
00625     }
00626     return QString();
00627 }
00628 
00629 void KIconDialog::showDialog()
00630 {
00631     setModal(false);
00632     d->showIcons();
00633     d->searchLine->setFocus();
00634     show();
00635 }
00636 
00637 void KIconDialog::slotOk()
00638 {
00639     QString name;
00640     if (!d->custom.isNull())
00641     {
00642         name = d->custom;
00643     }
00644     else
00645     {
00646         name = d->mpCanvas->getCurrent();
00647         if (!name.isEmpty() && d->mpSystemIcons->isChecked()) {
00648             QFileInfo fi(name);
00649             name = fi.baseName();
00650         }
00651     }
00652 
00653     emit newIconName(name);
00654     KDialog::accept();
00655 }
00656 
00657 QString KIconDialog::getIcon(KIconLoader::Group group, KIconLoader::Context context,
00658                              bool strictIconSize, int iconSize, bool user,
00659                              QWidget *parent, const QString &caption)
00660 {
00661     KIconDialog dlg(parent);
00662     dlg.setup( group, context, strictIconSize, iconSize, user );
00663     if (!caption.isNull())
00664         dlg.setCaption(caption);
00665 
00666     return dlg.openDialog();
00667 }
00668 
00669 void KIconDialog::KIconDialogPrivate::_k_slotBrowse()
00670 {
00671     // Create a file dialog to select a PNG, XPM or SVG file,
00672     // with the image previewer shown.
00673     // KFileDialog::getImageOpenURL doesn't allow svg.
00674     KUrl emptyUrl;
00675     KFileDialog dlg(emptyUrl, i18n("*.png *.xpm *.svg *.svgz|Icon Files (*.png *.xpm *.svg *.svgz)"), q);
00676     dlg.setOperationMode( KFileDialog::Opening );
00677     dlg.setCaption( i18n("Open") );
00678     dlg.setMode( KFile::File );
00679 
00680     KImageFilePreview *ip = new KImageFilePreview( &dlg );
00681     dlg.setPreviewWidget( ip );
00682     dlg.exec();
00683 
00684     QString file = dlg.selectedFile();
00685     if (!file.isEmpty())
00686     {
00687         custom = file;
00688         if (mpSystemIcons->isChecked()) {
00689             customLocation = QFileInfo(file).absolutePath();
00690         }
00691         q->slotOk();
00692     }
00693 }
00694 
00695 void KIconDialog::KIconDialogPrivate::_k_slotSystemIconClicked()
00696 {
00697     mpBrowseBut->setEnabled(false);
00698     mpCombo->setEnabled(true);
00699     showIcons();
00700 }
00701 
00702 void KIconDialog::KIconDialogPrivate::_k_slotOtherIconClicked()
00703 {
00704     mpBrowseBut->setEnabled(!m_bLockCustomDir);
00705     mpCombo->setEnabled(false);
00706     showIcons();
00707 }
00708 
00709 void KIconDialog::KIconDialogPrivate::_k_slotContext(int id)
00710 {
00711     mContext = static_cast<KIconLoader::Context>( mContextMap[ id ] );
00712     showIcons();
00713 }
00714 
00715 void KIconDialog::KIconDialogPrivate::_k_slotStartLoading(int steps)
00716 {
00717     if (steps < 10)
00718     mpProgress->hide();
00719     else
00720     {
00721         mNumOfSteps = steps;
00722         mpProgress->setValue(0);
00723         mpProgress->show();
00724     }
00725 }
00726 
00727 void KIconDialog::KIconDialogPrivate::_k_slotProgress(int p)
00728 {
00729     mpProgress->setValue(static_cast<int>(100.0 * (double)p / (double)mNumOfSteps));
00730 }
00731 
00732 void KIconDialog::KIconDialogPrivate::_k_slotFinished()
00733 {
00734     mNumOfSteps = 1;
00735     mpProgress->hide();
00736 }
00737 
00738 class KIconButton::KIconButtonPrivate
00739 {
00740   public:
00741     KIconButtonPrivate(KIconButton *qq, KIconLoader *loader);
00742     ~KIconButtonPrivate();
00743 
00744     // slots
00745     void _k_slotChangeIcon();
00746     void _k_newIconName(const QString&);
00747 
00748     KIconButton *q;
00749 
00750     int iconSize;
00751     int buttonIconSize;
00752     bool m_bStrictIconSize;
00753 
00754     bool mbUser;
00755     KIconLoader::Group mGroup;
00756     KIconLoader::Context mContext;
00757 
00758     QString mIcon;
00759     KIconDialog *mpDialog;
00760     KIconLoader *mpLoader;
00761 };
00762 
00763 
00764 /*
00765  * KIconButton: A "choose icon" pushbutton.
00766  */
00767 
00768 KIconButton::KIconButton(QWidget *parent)
00769     : QPushButton(parent), d(new KIconButtonPrivate(this, KIconLoader::global()))
00770 {
00771     QPushButton::setIconSize(QSize(48, 48));
00772 }
00773 
00774 KIconButton::KIconButton(KIconLoader *loader, QWidget *parent)
00775     : QPushButton(parent), d(new KIconButtonPrivate(this, loader))
00776 {
00777     QPushButton::setIconSize(QSize(48, 48));
00778 }
00779 
00780 KIconButton::KIconButtonPrivate::KIconButtonPrivate(KIconButton *qq, KIconLoader *loader)
00781     : q(qq)
00782 {
00783     m_bStrictIconSize = false;
00784     iconSize = 0; // let KIconLoader choose the default
00785     buttonIconSize = -1; //When buttonIconSize is -1, iconSize will be used for the button
00786 
00787     mGroup = KIconLoader::Desktop;
00788     mContext = KIconLoader::Application;
00789     mbUser = false;
00790 
00791     mpLoader = loader;
00792     mpDialog = 0L;
00793     connect(q, SIGNAL(clicked()), q, SLOT(_k_slotChangeIcon()));
00794 }
00795 
00796 KIconButton::KIconButtonPrivate::~KIconButtonPrivate()
00797 {
00798     delete mpDialog;
00799 }
00800 
00801 KIconButton::~KIconButton()
00802 {
00803     delete d;
00804 }
00805 
00806 void KIconButton::setStrictIconSize(bool b)
00807 {
00808     d->m_bStrictIconSize=b;
00809 }
00810 
00811 bool KIconButton::strictIconSize() const
00812 {
00813     return d->m_bStrictIconSize;
00814 }
00815 
00816 void KIconButton::setIconSize( int size )
00817 {
00818     if (d->buttonIconSize == -1) {
00819         QPushButton::setIconSize(QSize(size, size));
00820     }
00821 
00822     d->iconSize = size;
00823 }
00824 
00825 int KIconButton::iconSize() const
00826 {
00827     return d->iconSize;
00828 }
00829 
00830 void KIconButton::setButtonIconSize( int size )
00831 {
00832     QPushButton::setIconSize(QSize(size, size));
00833     d->buttonIconSize = size;
00834 }
00835 
00836 int KIconButton::buttonIconSize() const
00837 {
00838     return QPushButton::iconSize().height();
00839 }
00840 
00841 void KIconButton::setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user)
00842 {
00843     d->mGroup = group;
00844     d->mContext = context;
00845     d->mbUser = user;
00846 }
00847 
00848 void KIconButton::setIcon(const QString& icon)
00849 {
00850     d->mIcon = icon;
00851     setIcon(KIcon(d->mIcon));
00852 
00853     if (!d->mpDialog) {
00854         d->mpDialog = new KIconDialog(d->mpLoader, this);
00855         connect(d->mpDialog, SIGNAL(newIconName(const QString&)), this, SLOT(_k_newIconName(const QString&)));
00856     }
00857 
00858     if (d->mbUser) {
00859         d->mpDialog->setCustomLocation(QFileInfo(d->mpLoader->iconPath(d->mIcon, d->mGroup, true) ).absolutePath());
00860     }
00861 }
00862 
00863 void KIconButton::setIcon(const QIcon& icon)
00864 {
00865     QPushButton::setIcon(icon);
00866 }
00867 
00868 void KIconButton::resetIcon()
00869 {
00870     d->mIcon.clear();
00871     setIcon(QIcon());
00872 }
00873 
00874 const QString &KIconButton::icon() const
00875 {
00876     return d->mIcon;
00877 }
00878 
00879 void KIconButton::KIconButtonPrivate::_k_slotChangeIcon()
00880 {
00881     if (!mpDialog)
00882     {
00883         mpDialog = new KIconDialog(mpLoader, q);
00884         connect(mpDialog, SIGNAL(newIconName(const QString&)), q, SLOT(_k_newIconName(const QString&)));
00885     }
00886 
00887     mpDialog->setup(mGroup, mContext, m_bStrictIconSize, iconSize, mbUser);
00888     mpDialog->showDialog();
00889 }
00890 
00891 void KIconButton::KIconButtonPrivate::_k_newIconName(const QString& name)
00892 {
00893     if (name.isEmpty())
00894         return;
00895 
00896     q->setIcon(KIcon(name));
00897     mIcon = name;
00898 
00899     if (mbUser) {
00900         mpDialog->setCustomLocation(QFileInfo(mpLoader->iconPath(mIcon, mGroup, true)).absolutePath());
00901     }
00902 
00903     emit q->iconChanged(name);
00904 }
00905 
00906 #include "kicondialog.moc"

KIO

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal