• Skip to content
  • Skip to link menu
KDE 4.2 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     q->setUpdatesEnabled(false);
00170 
00171     // Cache these as we will call them frequently.
00172     const int canvasIconWidth = q->iconSize().width();
00173     const int canvasIconHeight = q->iconSize().width();
00174     const bool uniformIconSize = q->uniformItemSizes();
00175 
00176     m_bLoading = true;
00177     int i;
00178     QStringList::ConstIterator it;
00179     uint emitProgress = 10; // so we will emit it once in the beginning
00180     QStringList::ConstIterator end(mFiles.constEnd());
00181     for (it=mFiles.constBegin(), i=0; it!=end; ++it, i++)
00182     {
00183     if ( emitProgress >= 10 ) {
00184             emit q->progress(i);
00185             emitProgress = 0;
00186         }
00187 
00188         emitProgress++;
00189 
00190         if (!m_bLoading) { // user clicked on a button that will load another set of icons
00191             break;
00192         }
00193     QImage img;
00194 
00195     // Use the extension as the format. Works for XPM and PNG, but not for SVG
00196     QString path= *it;
00197     QString ext = path.right(3).toUpper();
00198 
00199     if (ext != "SVG" && ext != "VGZ")
00200         img.load(*it);
00201     else {
00202             // Special stuff for SVG icons
00203             img = QImage(canvasIconWidth, canvasIconHeight, QImage::Format_ARGB32_Premultiplied);
00204             img.fill(0);
00205             KSvgRenderer renderer(*it);
00206             if (renderer.isValid()) {
00207                 QPainter p(&img);
00208                 renderer.render(&p);
00209             }
00210         }
00211 
00212     if (img.isNull())
00213         continue;
00214     if (img.width() > canvasIconWidth || img.height() > canvasIconHeight)
00215     {
00216         if (img.width() / (float)canvasIconWidth  > img.height() / (float)canvasIconHeight)
00217         {
00218         int height = (int) (((float)canvasIconWidth / img.width()) * img.height());
00219         img = img.scaled(canvasIconWidth, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
00220         } else
00221         {
00222         int width = (int) (((float)canvasIconHeight / img.height()) * img.width());
00223         img = img.scaled(width, canvasIconHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
00224         }
00225     }
00226     
00227     if (uniformIconSize && (img.width() != canvasIconWidth || img.height() != canvasIconHeight))
00228     {
00229        // Image is smaller than desired.  Draw onto a transparent QImage of the required dimensions.
00230        // (Unpleasant glitches occur if we break the uniformIconSizes() contract).
00231        QImage paddedImage = QImage(canvasIconWidth, canvasIconHeight, QImage::Format_ARGB32_Premultiplied);
00232        paddedImage.fill(0);
00233        QPainter painter(&paddedImage);
00234        painter.drawImage( (canvasIconWidth - img.width()) / 2, (canvasIconHeight - img.height()) / 2, img);
00235        img = paddedImage;
00236     }
00237     
00238     QPixmap pm = QPixmap::fromImage(img);
00239     QFileInfo fi(*it);
00240         QListWidgetItem *item = new QListWidgetItem(pm, fi.completeBaseName(), q);
00241     item->setData(Qt::UserRole, *it);
00242         item->setToolTip(fi.completeBaseName());
00243     }
00244 
00245     // enable updates since we have to draw the whole view now
00246     q->setUpdatesEnabled(true);
00247 
00248     QApplication::restoreOverrideCursor();
00249     m_bLoading = false;
00250     emit q->finished();
00251     q->setResizeMode(QListWidget::Adjust);
00252 }
00253 
00254 QString KIconCanvas::getCurrent() const
00255 {
00256     if (!currentItem())
00257     return QString();
00258     return currentItem()->data(Qt::UserRole).toString();
00259 }
00260 
00261 void KIconCanvas::stopLoading()
00262 {
00263     d->m_bLoading = false;
00264 }
00265 
00266 void KIconCanvas::KIconCanvasPrivate::_k_slotCurrentChanged(QListWidgetItem *item)
00267 {
00268     emit q->nameChanged((item != 0L) ? item->text() : QString());
00269 }
00270 
00271 class KIconDialog::KIconDialogPrivate
00272 {
00273   public:
00274     KIconDialogPrivate(KIconDialog *qq) {
00275         q = qq;
00276         m_bStrictIconSize = true;
00277     m_bLockUser = false;
00278     m_bLockCustomDir = false;
00279     searchLine = 0;
00280         mNumOfSteps = 1;
00281     }
00282     ~KIconDialogPrivate() {}
00283 
00284     void init();
00285     void showIcons();
00286     void setContext( KIconLoader::Context context );
00287 
00288     // slots
00289     void _k_slotContext(int);
00290     void _k_slotStartLoading(int);
00291     void _k_slotProgress(int);
00292     void _k_slotFinished();
00293     void _k_slotAcceptIcons();
00294     void _k_slotBrowse();
00295     void _k_slotOtherIconClicked();
00296     void _k_slotSystemIconClicked();
00297 
00298     KIconDialog *q;
00299 
00300     int mGroupOrSize;
00301     KIconLoader::Context mContext;
00302 
00303     QStringList mFileList;
00304     KComboBox *mpCombo;
00305     QPushButton *mpBrowseBut;
00306     QRadioButton *mpSystemIcons, *mpOtherIcons;
00307     QProgressBar *mpProgress;
00308     int mNumOfSteps;
00309     KIconLoader *mpLoader;
00310     KIconCanvas *mpCanvas;
00311     int mNumContext;
00312     KIconLoader::Context mContextMap[ 12 ]; // must match KIcon::Context size, code has assert
00313 
00314     bool m_bStrictIconSize, m_bLockUser, m_bLockCustomDir;
00315     QString custom;
00316     QString customLocation;
00317     KListWidgetSearchLine *searchLine;
00318 };
00319 
00320 /*
00321  * KIconDialog: Dialog for selecting icons. Both system and user
00322  * specified icons can be chosen.
00323  */
00324 
00325 KIconDialog::KIconDialog(QWidget *parent)
00326     : KDialog(parent), d(new KIconDialogPrivate(this))
00327 {
00328     setModal( true );
00329     setCaption( i18n("Select Icon") );
00330     setButtons( Ok | Cancel );
00331     setDefaultButton( Ok );
00332 
00333     d->mpLoader = KIconLoader::global();
00334     d->init();
00335 }
00336 
00337 KIconDialog::KIconDialog(KIconLoader *loader, QWidget *parent)
00338     : KDialog(parent), d(new KIconDialogPrivate(this))
00339 {
00340     setModal( true );
00341     setCaption( i18n("Select Icon") );
00342     setButtons( Ok | Cancel );
00343     setDefaultButton( Ok );
00344 
00345     d->mpLoader = loader;
00346     d->init();
00347 }
00348 
00349 void KIconDialog::KIconDialogPrivate::init()
00350 {
00351     mGroupOrSize = KIconLoader::Desktop;
00352     mContext = KIconLoader::Any;
00353     mFileList = KGlobal::dirs()->findAllResources("appicon", QLatin1String("*.png"));
00354 
00355     QWidget *main = new QWidget(q);
00356     q->setMainWidget(main);
00357 
00358     QVBoxLayout *top = new QVBoxLayout(main);
00359     top->setMargin(0);
00360 
00361     QGroupBox *bgroup = new QGroupBox(main);
00362     bgroup->setTitle(i18n("Icon Source"));
00363 
00364     QVBoxLayout *vbox = new QVBoxLayout;
00365     bgroup->setLayout( vbox );
00366     top->addWidget(bgroup);
00367 
00368     QGridLayout *grid = new QGridLayout();
00369     grid->setSpacing(KDialog::spacingHint());
00370     bgroup->layout()->addItem(grid);
00371 
00372     mpSystemIcons = new QRadioButton(i18n("S&ystem icons:"), bgroup);
00373     connect(mpSystemIcons, SIGNAL(clicked()), q, SLOT(_k_slotSystemIconClicked()));
00374     grid->addWidget(mpSystemIcons, 1, 0);
00375     mpCombo = new KComboBox(bgroup);
00376     connect(mpCombo, SIGNAL(activated(int)), q, SLOT(_k_slotContext(int)));
00377     grid->addWidget(mpCombo, 1, 1);
00378     mpOtherIcons = new QRadioButton(i18n("O&ther icons:"), bgroup);
00379     connect(mpOtherIcons, SIGNAL(clicked()), q, SLOT(_k_slotOtherIconClicked()));
00380     grid->addWidget(mpOtherIcons, 2, 0);
00381     mpBrowseBut = new QPushButton(i18n("&Browse..."), bgroup);
00382     connect(mpBrowseBut, SIGNAL(clicked()), q, SLOT(_k_slotBrowse()));
00383     grid->addWidget(mpBrowseBut, 2, 1);
00384 
00385     //
00386     // ADD SEARCHLINE
00387     //
00388     QHBoxLayout *searchLayout = new QHBoxLayout();
00389     searchLayout->setMargin(0);
00390     searchLayout->setSpacing(KDialog::spacingHint());
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 
00614     if ( exec() == Accepted )
00615     {
00616         if (!d->custom.isNull())
00617             return d->custom;
00618         QString name = d->mpCanvas->getCurrent();
00619         if (name.isEmpty() || d->mpOtherIcons->isChecked()) {
00620             return name;
00621         }
00622         QFileInfo fi(name);
00623         return fi.baseName();
00624     }
00625     return QString();
00626 }
00627 
00628 void KIconDialog::showDialog()
00629 {
00630     setModal(false);
00631     d->showIcons();
00632     show();
00633 }
00634 
00635 void KIconDialog::slotOk()
00636 {
00637     QString name;
00638     if (!d->custom.isNull())
00639     {
00640         name = d->custom;
00641     }
00642     else
00643     {
00644         name = d->mpCanvas->getCurrent();
00645         if (!name.isEmpty() && d->mpSystemIcons->isChecked()) {
00646             QFileInfo fi(name);
00647             name = fi.baseName();
00648         }
00649     }
00650 
00651     emit newIconName(name);
00652     KDialog::accept();
00653 }
00654 
00655 QString KIconDialog::getIcon(KIconLoader::Group group, KIconLoader::Context context,
00656                              bool strictIconSize, int iconSize, bool user,
00657                              QWidget *parent, const QString &caption)
00658 {
00659     KIconDialog dlg(parent);
00660     dlg.setup( group, context, strictIconSize, iconSize, user );
00661     if (!caption.isNull())
00662         dlg.setCaption(caption);
00663 
00664     return dlg.openDialog();
00665 }
00666 
00667 void KIconDialog::KIconDialogPrivate::_k_slotBrowse()
00668 {
00669     // Create a file dialog to select a PNG, XPM or SVG file,
00670     // with the image previewer shown.
00671     // KFileDialog::getImageOpenURL doesn't allow svg.
00672     KUrl emptyUrl;
00673     KFileDialog dlg(emptyUrl, i18n("*.png *.xpm *.svg *.svgz|Icon Files (*.png *.xpm *.svg *.svgz)"), q);
00674     dlg.setOperationMode( KFileDialog::Opening );
00675     dlg.setCaption( i18n("Open") );
00676     dlg.setMode( KFile::File );
00677 
00678     KImageFilePreview *ip = new KImageFilePreview( &dlg );
00679     dlg.setPreviewWidget( ip );
00680     dlg.exec();
00681 
00682     QString file = dlg.selectedFile();
00683     if (!file.isEmpty())
00684     {
00685         custom = file;
00686         if (mpSystemIcons->isChecked()) {
00687             customLocation = QFileInfo(file).absolutePath();
00688         }
00689         q->slotOk();
00690     }
00691 }
00692 
00693 void KIconDialog::KIconDialogPrivate::_k_slotSystemIconClicked()
00694 {
00695     mpBrowseBut->setEnabled(false);
00696     mpCombo->setEnabled(true);
00697     showIcons();
00698 }
00699 
00700 void KIconDialog::KIconDialogPrivate::_k_slotOtherIconClicked()
00701 {
00702     mpBrowseBut->setEnabled(!m_bLockCustomDir);
00703     mpCombo->setEnabled(false);
00704     showIcons();
00705 }
00706 
00707 void KIconDialog::KIconDialogPrivate::_k_slotContext(int id)
00708 {
00709     mContext = static_cast<KIconLoader::Context>( mContextMap[ id ] );
00710     showIcons();
00711 }
00712 
00713 void KIconDialog::KIconDialogPrivate::_k_slotStartLoading(int steps)
00714 {
00715     if (steps < 10)
00716     mpProgress->hide();
00717     else
00718     {
00719         mNumOfSteps = steps;
00720         mpProgress->setValue(0);
00721         mpProgress->show();
00722     }
00723 }
00724 
00725 void KIconDialog::KIconDialogPrivate::_k_slotProgress(int p)
00726 {
00727     mpProgress->setValue(static_cast<int>(100.0 * (double)p / (double)mNumOfSteps));
00728 }
00729 
00730 void KIconDialog::KIconDialogPrivate::_k_slotFinished()
00731 {
00732     mNumOfSteps = 1;
00733     mpProgress->hide();
00734 }
00735 
00736 class KIconButton::KIconButtonPrivate
00737 {
00738   public:
00739     KIconButtonPrivate(KIconButton *qq, KIconLoader *loader);
00740     ~KIconButtonPrivate();
00741 
00742     // slots
00743     void _k_slotChangeIcon();
00744     void _k_newIconName(const QString&);
00745 
00746     KIconButton *q;
00747 
00748     int iconSize;
00749     int buttonIconSize;
00750     bool m_bStrictIconSize;
00751 
00752     bool mbUser;
00753     KIconLoader::Group mGroup;
00754     KIconLoader::Context mContext;
00755 
00756     QString mIcon;
00757     KIconDialog *mpDialog;
00758     KIconLoader *mpLoader;
00759 };
00760 
00761 
00762 /*
00763  * KIconButton: A "choose icon" pushbutton.
00764  */
00765 
00766 KIconButton::KIconButton(QWidget *parent)
00767     : QPushButton(parent), d(new KIconButtonPrivate(this, KIconLoader::global()))
00768 {
00769     QPushButton::setIconSize(QSize(48, 48));
00770 }
00771 
00772 KIconButton::KIconButton(KIconLoader *loader, QWidget *parent)
00773     : QPushButton(parent), d(new KIconButtonPrivate(this, loader))
00774 {
00775     QPushButton::setIconSize(QSize(48, 48));
00776 }
00777 
00778 KIconButton::KIconButtonPrivate::KIconButtonPrivate(KIconButton *qq, KIconLoader *loader)
00779     : q(qq)
00780 {
00781     m_bStrictIconSize = false;
00782     iconSize = 0; // let KIconLoader choose the default
00783     buttonIconSize = -1; //When buttonIconSize is -1, iconSize will be used for the button
00784 
00785     mGroup = KIconLoader::Desktop;
00786     mContext = KIconLoader::Application;
00787     mbUser = false;
00788 
00789     mpLoader = loader;
00790     mpDialog = 0L;
00791     connect(q, SIGNAL(clicked()), q, SLOT(_k_slotChangeIcon()));
00792 }
00793 
00794 KIconButton::KIconButtonPrivate::~KIconButtonPrivate()
00795 {
00796     delete mpDialog;
00797 }
00798 
00799 KIconButton::~KIconButton()
00800 {
00801     delete d;
00802 }
00803 
00804 void KIconButton::setStrictIconSize(bool b)
00805 {
00806     d->m_bStrictIconSize=b;
00807 }
00808 
00809 bool KIconButton::strictIconSize() const
00810 {
00811     return d->m_bStrictIconSize;
00812 }
00813 
00814 void KIconButton::setIconSize( int size )
00815 {
00816     if (d->buttonIconSize == -1) {
00817         QPushButton::setIconSize(QSize(size, size));
00818     }
00819 
00820     d->iconSize = size;
00821 }
00822 
00823 int KIconButton::iconSize() const
00824 {
00825     return d->iconSize;
00826 }
00827 
00828 void KIconButton::setButtonIconSize( int size )
00829 {
00830     QPushButton::setIconSize(QSize(size, size));
00831     d->buttonIconSize = size;
00832 }
00833 
00834 int KIconButton::buttonIconSize() const
00835 {
00836     return QPushButton::iconSize().height();
00837 }
00838 
00839 void KIconButton::setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user)
00840 {
00841     d->mGroup = group;
00842     d->mContext = context;
00843     d->mbUser = user;
00844 }
00845 
00846 void KIconButton::setIcon(const QString& icon)
00847 {
00848     d->mIcon = icon;
00849     setIcon(KIcon(d->mIcon));
00850 
00851     if (!d->mpDialog) {
00852         d->mpDialog = new KIconDialog(d->mpLoader, this);
00853         connect(d->mpDialog, SIGNAL(newIconName(const QString&)), this, SLOT(_k_newIconName(const QString&)));
00854     }
00855 
00856     if (d->mbUser) {
00857         d->mpDialog->setCustomLocation(QFileInfo(d->mpLoader->iconPath(d->mIcon, d->mGroup, true) ).absolutePath());
00858     }
00859 }
00860 
00861 void KIconButton::setIcon(const QIcon& icon)
00862 {
00863     QPushButton::setIcon(icon);
00864 }
00865 
00866 void KIconButton::resetIcon()
00867 {
00868     d->mIcon.clear();
00869     setIcon(QIcon());
00870 }
00871 
00872 const QString &KIconButton::icon() const
00873 {
00874     return d->mIcon;
00875 }
00876 
00877 void KIconButton::KIconButtonPrivate::_k_slotChangeIcon()
00878 {
00879     if (!mpDialog)
00880     {
00881         mpDialog = new KIconDialog(mpLoader, q);
00882         connect(mpDialog, SIGNAL(newIconName(const QString&)), q, SLOT(_k_newIconName(const QString&)));
00883     }
00884 
00885     mpDialog->setup(mGroup, mContext, m_bStrictIconSize, iconSize, mbUser);
00886     mpDialog->showDialog();
00887 }
00888 
00889 void KIconButton::KIconButtonPrivate::_k_newIconName(const QString& name)
00890 {
00891     if (name.isEmpty())
00892         return;
00893 
00894     q->setIcon(KIcon(name));
00895     mIcon = name;
00896 
00897     if (mbUser) {
00898         mpDialog->setCustomLocation(QFileInfo(mpLoader->iconPath(mIcon, mGroup, true)).absolutePath());
00899     }
00900 
00901     emit q->iconChanged(name);
00902 }
00903 
00904 #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
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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