• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 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
46class KIconCanvasDelegate : public QAbstractItemDelegate
47{
48public:
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;
53private:
54 KIconCanvas *m_iconCanvas;
55 QAbstractItemDelegate *m_defaultDelegate;
56 static const int HORIZONTAL_EDGE_PAD = 3;
57};
58
59KIconCanvasDelegate::KIconCanvasDelegate(KIconCanvas *parent, QAbstractItemDelegate *defaultDelegate)
60 : QAbstractItemDelegate(parent)
61{
62 m_iconCanvas = parent;
63 m_defaultDelegate = defaultDelegate;
64}
65
66void 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
77QSize 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
85class 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
104class IconPath : public QString
105{
106protected:
107 QString m_iconName;
108
109public:
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
132KIconCanvas::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
149KIconCanvas::~KIconCanvas()
150{
151 delete d->mpTimer;
152 delete d->mpDelegate;
153 delete d;
154}
155
156void 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
166void 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 if (emitProgress >= 10) {
188 emit q->progress(i);
189 emitProgress = 0;
190 }
191
192 emitProgress++;
193
194 if (!m_bLoading) { // user clicked on a button that will load another set of icons
195 break;
196 }
197 QImage img;
198
199 // Use the extension as the format. Works for XPM and PNG, but not for SVG
200 QString path= *it;
201 QString ext = path.right(3).toUpper();
202
203 if (ext != "SVG" && ext != "VGZ") {
204 img.load(*it);
205 } else {
206#ifndef _WIN32_WCE
207 // Special stuff for SVG icons
208 img = QImage(canvasIconWidth, canvasIconHeight, QImage::Format_ARGB32_Premultiplied);
209 img.fill(0);
210 QSvgRenderer renderer(*it);
211 if (renderer.isValid()) {
212 QPainter p(&img);
213 renderer.render(&p);
214 }
215#endif
216 }
217
218 if (img.isNull()) {
219 continue;
220 }
221
222 if (img.width() > canvasIconWidth || img.height() > canvasIconHeight) {
223 if (img.width() / (float)canvasIconWidth > img.height() / (float)canvasIconHeight) {
224 int height = (int) (((float)canvasIconWidth / img.width()) * img.height());
225 img = img.scaled(canvasIconWidth, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
226 } else {
227 int width = (int) (((float)canvasIconHeight / img.height()) * img.width());
228 img = img.scaled(width, canvasIconHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
229 }
230 }
231
232 if (uniformIconSize && (img.width() != canvasIconWidth || img.height() != canvasIconHeight)) {
233 // Image is smaller than desired. Draw onto a transparent QImage of the required dimensions.
234 // (Unpleasant glitches occur if we break the uniformIconSizes() contract).
235 QImage paddedImage = QImage(canvasIconWidth, canvasIconHeight, QImage::Format_ARGB32_Premultiplied);
236 paddedImage.fill(0);
237 QPainter painter(&paddedImage);
238 painter.drawImage( (canvasIconWidth - img.width()) / 2, (canvasIconHeight - img.height()) / 2, img);
239 img = paddedImage;
240 }
241
242 QPixmap pm = QPixmap::fromImage(img);
243 QFileInfo fi(*it);
244 QListWidgetItem *item = new QListWidgetItem(pm, fi.completeBaseName(), q);
245 item->setData(Qt::UserRole, *it);
246 item->setToolTip(fi.completeBaseName());
247 }
248
249 // enable updates since we have to draw the whole view now
250 q->setUpdatesEnabled(true);
251
252 QApplication::restoreOverrideCursor();
253 m_bLoading = false;
254 emit q->finished();
255 q->setResizeMode(QListWidget::Adjust);
256}
257
258QString KIconCanvas::getCurrent() const
259{
260 if (!currentItem())
261 return QString();
262 return currentItem()->data(Qt::UserRole).toString();
263}
264
265void KIconCanvas::stopLoading()
266{
267 d->m_bLoading = false;
268}
269
270void KIconCanvas::KIconCanvasPrivate::_k_slotCurrentChanged(QListWidgetItem *item)
271{
272 emit q->nameChanged((item != 0L) ? item->text() : QString());
273}
274
275class KIconDialog::KIconDialogPrivate
276{
277 public:
278 KIconDialogPrivate(KIconDialog *qq) {
279 q = qq;
280 m_bStrictIconSize = true;
281 m_bLockUser = false;
282 m_bLockCustomDir = false;
283 searchLine = 0;
284 mNumOfSteps = 1;
285 }
286 ~KIconDialogPrivate() {}
287
288 void init();
289 void showIcons();
290 void setContext( KIconLoader::Context context );
291
292 // slots
293 void _k_slotContext(int);
294 void _k_slotStartLoading(int);
295 void _k_slotProgress(int);
296 void _k_slotFinished();
297 void _k_slotAcceptIcons();
298 void _k_slotBrowse();
299 void _k_slotOtherIconClicked();
300 void _k_slotSystemIconClicked();
301
302 KIconDialog *q;
303
304 int mGroupOrSize;
305 KIconLoader::Context mContext;
306
307 QStringList mFileList;
308 KComboBox *mpCombo;
309 QPushButton *mpBrowseBut;
310 QRadioButton *mpSystemIcons, *mpOtherIcons;
311 QProgressBar *mpProgress;
312 int mNumOfSteps;
313 KIconLoader *mpLoader;
314 KIconCanvas *mpCanvas;
315 int mNumContext;
316 KIconLoader::Context mContextMap[ 12 ]; // must match KIcon::Context size, code has assert
317
318 bool m_bStrictIconSize, m_bLockUser, m_bLockCustomDir;
319 QString custom;
320 QString customLocation;
321 KListWidgetSearchLine *searchLine;
322};
323
324/*
325 * KIconDialog: Dialog for selecting icons. Both system and user
326 * specified icons can be chosen.
327 */
328
329KIconDialog::KIconDialog(QWidget *parent)
330 : KDialog(parent), d(new KIconDialogPrivate(this))
331{
332 setModal( true );
333 setCaption( i18n("Select Icon") );
334 setButtons( Ok | Cancel );
335 setDefaultButton( Ok );
336
337 d->mpLoader = KIconLoader::global();
338 d->init();
339}
340
341KIconDialog::KIconDialog(KIconLoader *loader, QWidget *parent)
342 : KDialog(parent), d(new KIconDialogPrivate(this))
343{
344 setModal( true );
345 setCaption( i18n("Select Icon") );
346 setButtons( Ok | Cancel );
347 setDefaultButton( Ok );
348
349 d->mpLoader = loader;
350 d->init();
351}
352
353void KIconDialog::KIconDialogPrivate::init()
354{
355 mGroupOrSize = KIconLoader::Desktop;
356 mContext = KIconLoader::Any;
357 mFileList = KGlobal::dirs()->findAllResources("appicon", QLatin1String("*.png"));
358
359 QWidget *main = new QWidget(q);
360 q->setMainWidget(main);
361
362 QVBoxLayout *top = new QVBoxLayout(main);
363 top->setMargin(0);
364
365 QGroupBox *bgroup = new QGroupBox(main);
366 bgroup->setTitle(i18n("Icon Source"));
367
368 QVBoxLayout *vbox = new QVBoxLayout;
369 bgroup->setLayout( vbox );
370 top->addWidget(bgroup);
371
372 QGridLayout *grid = new QGridLayout();
373 vbox->addLayout(grid);
374
375 mpSystemIcons = new QRadioButton(i18n("S&ystem icons:"), bgroup);
376 connect(mpSystemIcons, SIGNAL(clicked()), q, SLOT(_k_slotSystemIconClicked()));
377 grid->addWidget(mpSystemIcons, 1, 0);
378 mpCombo = new KComboBox(bgroup);
379 mpCombo->setMaxVisibleItems(12);
380 connect(mpCombo, SIGNAL(activated(int)), q, SLOT(_k_slotContext(int)));
381 grid->addWidget(mpCombo, 1, 1);
382 mpOtherIcons = new QRadioButton(i18n("O&ther icons:"), bgroup);
383 connect(mpOtherIcons, SIGNAL(clicked()), q, SLOT(_k_slotOtherIconClicked()));
384 grid->addWidget(mpOtherIcons, 2, 0);
385 mpBrowseBut = new QPushButton(i18n("&Browse..."), bgroup);
386 connect(mpBrowseBut, SIGNAL(clicked()), q, SLOT(_k_slotBrowse()));
387 grid->addWidget(mpBrowseBut, 2, 1);
388
389 //
390 // ADD SEARCHLINE
391 //
392 QHBoxLayout *searchLayout = new QHBoxLayout();
393 searchLayout->setMargin(0);
394 top->addLayout(searchLayout);
395
396 QLabel *searchLabel = new QLabel(i18n("&Search:"), main);
397 searchLayout->addWidget(searchLabel);
398
399 searchLine = new KListWidgetSearchLine(main);
400 searchLayout->addWidget(searchLine);
401 searchLabel->setBuddy(searchLine);
402
403 QString wtstr = i18n("Search interactively for icon names (e.g. folder).");
404 searchLabel->setWhatsThis(wtstr);
405 searchLine->setWhatsThis(wtstr);
406
407
408 mpCanvas = new KIconCanvas(main);
409 connect(mpCanvas, SIGNAL(itemActivated(QListWidgetItem*)), q, SLOT(_k_slotAcceptIcons()));
410 top->addWidget(mpCanvas);
411 searchLine->setListWidget(mpCanvas);
412
413 // Compute width of canvas with 4 icons displayed in a row
414 QStyleOption opt;
415 opt.initFrom(mpCanvas);
416 int width = 4 * mpCanvas->gridSize().width() + 1;
417 width += mpCanvas->verticalScrollBar()->sizeHint().width();
418 width += 2 * mpCanvas->frameWidth();
419 if (mpCanvas->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &opt, mpCanvas)) {
420 width += mpCanvas->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &opt, mpCanvas);
421 }
422 mpCanvas->setMinimumSize(width, 125);
423
424 mpProgress = new QProgressBar(main);
425 top->addWidget(mpProgress);
426 connect(mpCanvas, SIGNAL(startLoading(int)), q, SLOT(_k_slotStartLoading(int)));
427 connect(mpCanvas, SIGNAL(progress(int)), q, SLOT(_k_slotProgress(int)));
428 connect(mpCanvas, SIGNAL(finished()), q, SLOT(_k_slotFinished()));
429
430 // When pressing Ok or Cancel, stop loading icons
431 connect(q, SIGNAL(hidden()), mpCanvas, SLOT(stopLoading()));
432
433 static const char* const context_text[] = {
434 I18N_NOOP( "Actions" ),
435 I18N_NOOP( "Animations" ),
436 I18N_NOOP( "Applications" ),
437 I18N_NOOP( "Categories" ),
438 I18N_NOOP( "Devices" ),
439 I18N_NOOP( "Emblems" ),
440 I18N_NOOP( "Emotes" ),
441 I18N_NOOP( "Filesystems" ),
442 I18N_NOOP( "International" ),
443 I18N_NOOP( "Mimetypes" ),
444 I18N_NOOP( "Places" ),
445 I18N_NOOP( "Status" ) };
446 static const KIconLoader::Context context_id[] = {
447 KIconLoader::Action,
448 KIconLoader::Animation,
449 KIconLoader::Application,
450 KIconLoader::Category,
451 KIconLoader::Device,
452 KIconLoader::Emblem,
453 KIconLoader::Emote,
454 KIconLoader::FileSystem,
455 KIconLoader::International,
456 KIconLoader::MimeType,
457 KIconLoader::Place,
458 KIconLoader::StatusIcon };
459 mNumContext = 0;
460 int cnt = sizeof( context_text ) / sizeof( context_text[ 0 ] );
461 // check all 3 arrays have same sizes
462 Q_ASSERT( cnt == sizeof( context_id ) / sizeof( context_id[ 0 ] )
463 && cnt == sizeof( mContextMap ) / sizeof( mContextMap[ 0 ] ));
464 for (int i = 0; i < cnt; ++i) {
465 if (mpLoader->hasContext( context_id[ i ])) {
466 mpCombo->addItem(i18n( context_text[ i ] ));
467 mContextMap[ mNumContext++ ] = context_id[ i ];
468 }
469 }
470 mpCombo->setFixedSize(mpCombo->sizeHint());
471
472 mpBrowseBut->setFixedWidth(mpCombo->width());
473
474 // Make the dialog a little taller
475 q->incrementInitialSize(QSize(0,100));
476 connect(q, SIGNAL(okClicked()), q, SLOT(slotOk()));
477}
478
479
480KIconDialog::~KIconDialog()
481{
482 delete d;
483}
484
485void KIconDialog::KIconDialogPrivate::_k_slotAcceptIcons()
486{
487 custom.clear();
488 q->slotOk();
489}
490
491void KIconDialog::KIconDialogPrivate::showIcons()
492{
493 mpCanvas->clear();
494 QStringList filelist;
495 if (mpSystemIcons->isChecked()) {
496 if (m_bStrictIconSize) {
497 filelist = mpLoader->queryIcons(mGroupOrSize, mContext);
498 } else {
499 filelist = mpLoader->queryIconsByContext(mGroupOrSize, mContext);
500 }
501 } else if (!customLocation.isEmpty()) {
502 filelist = mpLoader->queryIconsByDir(customLocation);
503 } else {
504 filelist = mFileList;
505 }
506
507 QList<IconPath> iconlist;
508 QStringList::const_iterator it;
509 foreach (const QString &it, filelist) {
510 iconlist.append(IconPath(it));
511 }
512
513 qSort(iconlist);
514 filelist.clear();
515
516 foreach (const IconPath &ip, iconlist) {
517 filelist.append(ip);
518 }
519
520 searchLine->clear();
521
522 // The KIconCanvas has uniformItemSizes set which really expects
523 // all added icons to be the same size, otherwise weirdness ensues :)
524 // Ensure all SVGs are scaled to the desired size and that as few icons
525 // need to be padded as possible by specifying a sensible size.
526 if (mGroupOrSize < -1) {
527 // mGroupOrSize can be -1 if NoGroup is chosen.
528 // Explicit size.
529 mpCanvas->setIconSize(QSize(-mGroupOrSize, -mGroupOrSize));
530 } else {
531 // Icon group.
532 int groupSize = mpLoader->currentSize((KIconLoader::Group)mGroupOrSize);
533 mpCanvas->setIconSize(QSize(groupSize, groupSize));
534 }
535
536 mpCanvas->loadFiles(filelist);
537}
538
539void KIconDialog::setStrictIconSize(bool b)
540{
541 d->m_bStrictIconSize=b;
542}
543
544bool KIconDialog::strictIconSize() const
545{
546 return d->m_bStrictIconSize;
547}
548
549void KIconDialog::setIconSize( int size )
550{
551 // see KIconLoader, if you think this is weird
552 if (size == 0) {
553 d->mGroupOrSize = KIconLoader::Desktop; // default Group
554 } else {
555 d->mGroupOrSize = -size; // yes, KIconLoader::queryIconsByContext is weird
556 }
557}
558
559int KIconDialog::iconSize() const
560{
561 // 0 or any other value ==> mGroupOrSize is a group, so we return 0
562 return (d->mGroupOrSize < 0) ? -d->mGroupOrSize : 0;
563}
564
565void KIconDialog::setup(KIconLoader::Group group, KIconLoader::Context context,
566 bool strictIconSize, int iconSize, bool user,
567 bool lockUser, bool lockCustomDir )
568{
569 d->m_bStrictIconSize = strictIconSize;
570 d->m_bLockUser = lockUser;
571 d->m_bLockCustomDir = lockCustomDir;
572 if (iconSize == 0) {
573 if (group == KIconLoader::NoGroup) {
574 // NoGroup has numeric value -1, which should
575 // not really be used with KIconLoader::queryIcons*(...);
576 // pick a proper group.
577 d->mGroupOrSize = KIconLoader::Small;
578 } else {
579 d->mGroupOrSize = group;
580 }
581 } else {
582 d->mGroupOrSize = -iconSize;
583 }
584
585 d->mpSystemIcons->setChecked(!user);
586 d->mpSystemIcons->setEnabled(!lockUser || !user);
587 d->mpOtherIcons->setChecked(user);
588 d->mpOtherIcons->setEnabled(!lockUser || user);
589 d->mpCombo->setEnabled(!user);
590 d->mpBrowseBut->setEnabled(user && !lockCustomDir);
591 d->setContext(context);
592}
593
594void KIconDialog::KIconDialogPrivate::setContext(KIconLoader::Context context)
595{
596 mContext = context;
597 for (int i = 0; i < mNumContext; ++i) {
598 if( mContextMap[ i ] == context ) {
599 mpCombo->setCurrentIndex(i);
600 return;
601 }
602 }
603}
604
605void KIconDialog::setCustomLocation( const QString& location )
606{
607 d->customLocation = location;
608}
609
610QString KIconDialog::openDialog()
611{
612 d->showIcons();
613 d->searchLine->setFocus();
614
615 if (exec() == Accepted) {
616 if (!d->custom.isEmpty()) {
617 return d->custom;
618 }
619
620 QString name = d->mpCanvas->getCurrent();
621 if (name.isEmpty() || d->mpOtherIcons->isChecked()) {
622 return name;
623 }
624
625 QFileInfo fi(name);
626 return fi.completeBaseName();
627 }
628
629 return QString();
630}
631
632void KIconDialog::showDialog()
633{
634 setModal(false);
635 d->showIcons();
636 d->searchLine->setFocus();
637 show();
638}
639
640void KIconDialog::slotOk()
641{
642 QString name;
643 if (!d->custom.isEmpty()) {
644 name = d->custom;
645 } else {
646 name = d->mpCanvas->getCurrent();
647 if (!name.isEmpty() && d->mpSystemIcons->isChecked()) {
648 const QFileInfo fi(name);
649 name = fi.completeBaseName();
650 }
651 }
652
653 emit newIconName(name);
654 KDialog::accept();
655}
656
657QString KIconDialog::getIcon(KIconLoader::Group group, KIconLoader::Context context,
658 bool strictIconSize, int iconSize, bool user,
659 QWidget *parent, const QString &caption)
660{
661 KIconDialog dlg(parent);
662 dlg.setup(group, context, strictIconSize, iconSize, user);
663 if (!caption.isEmpty()) {
664 dlg.setCaption(caption);
665 }
666
667 return dlg.openDialog();
668}
669
670void KIconDialog::KIconDialogPrivate::_k_slotBrowse()
671{
672 // Create a file dialog to select a PNG, XPM or SVG file,
673 // with the image previewer shown.
674 // KFileDialog::getImageOpenURL doesn't allow svg.
675 KUrl emptyUrl;
676 KFileDialog dlg(emptyUrl, i18n("*.png *.xpm *.svg *.svgz|Icon Files (*.png *.xpm *.svg *.svgz)"), q);
677 dlg.setOperationMode( KFileDialog::Opening );
678 dlg.setCaption( i18n("Open") );
679 dlg.setMode( KFile::File );
680
681 KImageFilePreview *ip = new KImageFilePreview( &dlg );
682 dlg.setPreviewWidget( ip );
683 dlg.exec();
684
685 QString file = dlg.selectedFile();
686 if (!file.isEmpty())
687 {
688 custom = file;
689 if (mpSystemIcons->isChecked()) {
690 customLocation = QFileInfo(file).absolutePath();
691 }
692 q->slotOk();
693 }
694}
695
696void KIconDialog::KIconDialogPrivate::_k_slotSystemIconClicked()
697{
698 mpBrowseBut->setEnabled(false);
699 mpCombo->setEnabled(true);
700 showIcons();
701}
702
703void KIconDialog::KIconDialogPrivate::_k_slotOtherIconClicked()
704{
705 mpBrowseBut->setEnabled(!m_bLockCustomDir);
706 mpCombo->setEnabled(false);
707 showIcons();
708}
709
710void KIconDialog::KIconDialogPrivate::_k_slotContext(int id)
711{
712 mContext = static_cast<KIconLoader::Context>( mContextMap[ id ] );
713 showIcons();
714}
715
716void KIconDialog::KIconDialogPrivate::_k_slotStartLoading(int steps)
717{
718 if (steps < 10)
719 mpProgress->hide();
720 else
721 {
722 mNumOfSteps = steps;
723 mpProgress->setValue(0);
724 mpProgress->show();
725 }
726}
727
728void KIconDialog::KIconDialogPrivate::_k_slotProgress(int p)
729{
730 mpProgress->setValue(static_cast<int>(100.0 * (double)p / (double)mNumOfSteps));
731}
732
733void KIconDialog::KIconDialogPrivate::_k_slotFinished()
734{
735 mNumOfSteps = 1;
736 mpProgress->hide();
737}
738
739class KIconButton::KIconButtonPrivate
740{
741 public:
742 KIconButtonPrivate(KIconButton *qq, KIconLoader *loader);
743 ~KIconButtonPrivate();
744
745 // slots
746 void _k_slotChangeIcon();
747 void _k_newIconName(const QString&);
748
749 KIconButton *q;
750
751 int iconSize;
752 int buttonIconSize;
753 bool m_bStrictIconSize;
754
755 bool mbUser;
756 KIconLoader::Group mGroup;
757 KIconLoader::Context mContext;
758
759 QString mIcon;
760 KIconDialog *mpDialog;
761 KIconLoader *mpLoader;
762};
763
764
765/*
766 * KIconButton: A "choose icon" pushbutton.
767 */
768
769KIconButton::KIconButton(QWidget *parent)
770 : QPushButton(parent), d(new KIconButtonPrivate(this, KIconLoader::global()))
771{
772 QPushButton::setIconSize(QSize(48, 48));
773}
774
775KIconButton::KIconButton(KIconLoader *loader, QWidget *parent)
776 : QPushButton(parent), d(new KIconButtonPrivate(this, loader))
777{
778 QPushButton::setIconSize(QSize(48, 48));
779}
780
781KIconButton::KIconButtonPrivate::KIconButtonPrivate(KIconButton *qq, KIconLoader *loader)
782 : q(qq)
783{
784 m_bStrictIconSize = false;
785 iconSize = 0; // let KIconLoader choose the default
786 buttonIconSize = -1; //When buttonIconSize is -1, iconSize will be used for the button
787
788 mGroup = KIconLoader::Desktop;
789 mContext = KIconLoader::Application;
790 mbUser = false;
791
792 mpLoader = loader;
793 mpDialog = 0L;
794 connect(q, SIGNAL(clicked()), q, SLOT(_k_slotChangeIcon()));
795}
796
797KIconButton::KIconButtonPrivate::~KIconButtonPrivate()
798{
799 delete mpDialog;
800}
801
802KIconButton::~KIconButton()
803{
804 delete d;
805}
806
807void KIconButton::setStrictIconSize(bool b)
808{
809 d->m_bStrictIconSize=b;
810}
811
812bool KIconButton::strictIconSize() const
813{
814 return d->m_bStrictIconSize;
815}
816
817void KIconButton::setIconSize( int size )
818{
819 if (d->buttonIconSize == -1) {
820 QPushButton::setIconSize(QSize(size, size));
821 }
822
823 d->iconSize = size;
824}
825
826int KIconButton::iconSize() const
827{
828 return d->iconSize;
829}
830
831void KIconButton::setButtonIconSize( int size )
832{
833 QPushButton::setIconSize(QSize(size, size));
834 d->buttonIconSize = size;
835}
836
837int KIconButton::buttonIconSize() const
838{
839 return QPushButton::iconSize().height();
840}
841
842void KIconButton::setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user)
843{
844 d->mGroup = group;
845 d->mContext = context;
846 d->mbUser = user;
847}
848
849void KIconButton::setIcon(const QString& icon)
850{
851 d->mIcon = icon;
852 setIcon(KIcon(d->mIcon));
853
854 if (!d->mpDialog) {
855 d->mpDialog = new KIconDialog(d->mpLoader, this);
856 connect(d->mpDialog, SIGNAL(newIconName(QString)), this, SLOT(_k_newIconName(QString)));
857 }
858
859 if (d->mbUser) {
860 d->mpDialog->setCustomLocation(QFileInfo(d->mpLoader->iconPath(d->mIcon, d->mGroup, true) ).absolutePath());
861 }
862}
863
864void KIconButton::setIcon(const QIcon& icon)
865{
866 QPushButton::setIcon(icon);
867}
868
869void KIconButton::resetIcon()
870{
871 d->mIcon.clear();
872 setIcon(QIcon());
873}
874
875const QString &KIconButton::icon() const
876{
877 return d->mIcon;
878}
879
880void KIconButton::KIconButtonPrivate::_k_slotChangeIcon()
881{
882 if (!mpDialog)
883 {
884 mpDialog = new KIconDialog(mpLoader, q);
885 connect(mpDialog, SIGNAL(newIconName(QString)), q, SLOT(_k_newIconName(QString)));
886 }
887
888 mpDialog->setup(mGroup, mContext, m_bStrictIconSize, iconSize, mbUser);
889 mpDialog->showDialog();
890}
891
892void KIconButton::KIconButtonPrivate::_k_newIconName(const QString& name)
893{
894 if (name.isEmpty()) {
895 return;
896 }
897
898 q->setIcon(KIcon(name));
899 mIcon = name;
900
901 if (mbUser) {
902 mpDialog->setCustomLocation(QFileInfo(mpLoader->iconPath(mIcon, mGroup, true)).absolutePath());
903 }
904
905 emit q->iconChanged(name);
906}
907
908#include "kicondialog.moc"
KComboBox
KDialog
KDialog::setButtons
void setButtons(ButtonCodes buttonMask)
KDialog::Ok
Ok
KDialog::Cancel
Cancel
KDialog::setDefaultButton
void setDefaultButton(ButtonCode id)
KDialog::setCaption
virtual void setCaption(const QString &caption)
KFileDialog
Provides a user (and developer) friendly way to select files and directories.
Definition: kfiledialog.h:69
KFileDialog::Opening
@ Opening
Definition: kfiledialog.h:85
KFile::File
@ File
Definition: kfile.h:45
KIconButton
A pushbutton for choosing an icon.
Definition: kicondialog.h:244
KIconButton::setIcon
void setIcon(const QString &icon)
Sets the button's initial icon.
Definition: kicondialog.cpp:849
KIconButton::strictIconSize
bool strictIconSize
Definition: kicondialog.h:248
KIconButton::setStrictIconSize
void setStrictIconSize(bool b)
Sets a strict icon size policy for allowed icons.
Definition: kicondialog.cpp:807
KIconButton::~KIconButton
~KIconButton()
Destructs the button.
Definition: kicondialog.cpp:802
KIconButton::KIconButton
KIconButton(QWidget *parent=0L)
Constructs a KIconButton using the global iconloader.
Definition: kicondialog.cpp:769
KIconButton::setIconType
void setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user=false)
Sets the icon group and context.
Definition: kicondialog.cpp:842
KIconButton::buttonIconSize
int buttonIconSize() const
Returns the Button's Icon-Size.
Definition: kicondialog.cpp:837
KIconButton::setButtonIconSize
void setButtonIconSize(int size)
Sets the size of the icon to be shown on the button.
Definition: kicondialog.cpp:831
KIconButton::iconSize
int iconSize
Definition: kicondialog.h:247
KIconButton::icon
QString icon
Definition: kicondialog.h:246
KIconButton::resetIcon
void resetIcon()
Resets the icon (reverts to an empty button).
Definition: kicondialog.cpp:869
KIconButton::setIconSize
void setIconSize(int size)
Sets the size of the icon to be shown / selected.
Definition: kicondialog.cpp:817
KIconCanvas
Icon canvas for KIconDialog.
Definition: kicondialog.h:31
KIconCanvas::loadFiles
void loadFiles(const QStringList &files)
Load icons into the canvas.
Definition: kicondialog.cpp:156
KIconCanvas::getCurrent
QString getCurrent() const
Returns the current icon.
Definition: kicondialog.cpp:258
KIconCanvas::startLoading
void startLoading(int count)
This signal is emitted when the loading of the icons has started.
KIconCanvas::~KIconCanvas
~KIconCanvas()
Destroys the icon canvas.
Definition: kicondialog.cpp:149
KIconCanvas::stopLoading
void stopLoading()
Call this slot to stop the loading of the icons.
Definition: kicondialog.cpp:265
KIconCanvas::KIconCanvas
KIconCanvas(QWidget *parent=0L)
Creates a new icon canvas.
Definition: kicondialog.cpp:132
KIconDialog
Dialog for interactive selection of icons.
Definition: kicondialog.h:108
KIconDialog::strictIconSize
bool strictIconSize() const
Returns true if a strict icon size policy is set.
Definition: kicondialog.cpp:544
KIconDialog::setup
void setup(KIconLoader::Group group, KIconLoader::Context context=KIconLoader::Application, bool strictIconSize=false, int iconSize=0, bool user=false, bool lockUser=false, bool lockCustomDir=false)
Allows you to set the same parameters as in the class method getIcon(), as well as two additional par...
Definition: kicondialog.cpp:565
KIconDialog::~KIconDialog
~KIconDialog()
Destructs the dialog.
Definition: kicondialog.cpp:480
KIconDialog::KIconDialog
KIconDialog(QWidget *parent=0L)
Constructs an icon selection dialog using the global iconloader.
Definition: kicondialog.cpp:329
KIconDialog::iconSize
int iconSize() const
Returns the iconsize set via setIconSize() or 0, if the default iconsize will be used.
Definition: kicondialog.cpp:559
KIconDialog::newIconName
void newIconName(const QString &)
KIconDialog::setStrictIconSize
void setStrictIconSize(bool b)
Sets a strict icon size policy for allowed icons.
Definition: kicondialog.cpp:539
KIconDialog::showDialog
void showDialog()
show()es this dialog and emits a newIcon(const QString&) signal when successful.
Definition: kicondialog.cpp:632
KIconDialog::openDialog
QString openDialog()
exec()utes this modal dialog and returns the name of the selected icon, or QString() if the dialog wa...
Definition: kicondialog.cpp:610
KIconDialog::setIconSize
void setIconSize(int size)
Sets the size of the icons to be shown / selected.
Definition: kicondialog.cpp:549
KIconDialog::getIcon
static QString getIcon(KIconLoader::Group group=KIconLoader::Desktop, KIconLoader::Context context=KIconLoader::Application, bool strictIconSize=false, int iconSize=0, bool user=false, QWidget *parent=0, const QString &caption=QString())
Pops up the dialog an lets the user select an icon.
Definition: kicondialog.cpp:657
KIconDialog::setCustomLocation
void setCustomLocation(const QString &location)
sets a custom icon directory
Definition: kicondialog.cpp:605
KIconDialog::slotOk
void slotOk()
Definition: kicondialog.cpp:640
KIconLoader
KIconLoader::Group
Group
KIconLoader::Small
Small
KIconLoader::Desktop
Desktop
KIconLoader::NoGroup
NoGroup
KIconLoader::global
static KIconLoader * global()
KIconLoader::Context
Context
KIconLoader::Category
Category
KIconLoader::Emblem
Emblem
KIconLoader::StatusIcon
StatusIcon
KIconLoader::Application
Application
KIconLoader::FileSystem
FileSystem
KIconLoader::Emote
Emote
KIconLoader::Any
Any
KIconLoader::Place
Place
KIconLoader::MimeType
MimeType
KIconLoader::Action
Action
KIconLoader::International
International
KIconLoader::Animation
Animation
KIconLoader::Device
Device
KIcon
KImageFilePreview
Image preview widget for the file dialog.
Definition: kimagefilepreview.h:28
KListWidgetSearchLine
KListWidget
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter, SearchOptions options, QStringList &relPaths) const
KUrl
QAbstractItemDelegate
QGroupBox
QLabel
QList
QPushButton
QSvgRenderer
QWidget
ip
static const char ip[]
Definition: des.cpp:56
kcombobox.h
kfiledialog.h
kicondialog.h
kiconloader.h
kimagefilepreview.h
kio_export.h
timeout
int timeout
klistwidgetsearchline.h
klocale.h
i18n
QString i18n(const char *text)
I18N_NOOP
#define I18N_NOOP(x)
kstandarddirs.h
ksvgrenderer.h
KGlobal::dirs
KStandardDirs * dirs()
caption
QString caption()
group
group
name
const char * name(StandardAction id)
clear
KAction * clear(const QObject *recvr, const char *slot, QObject *parent)
end
const KShortcut & end()
main
int main(int argc, char **argv)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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.14.38 API Reference

Skip menu "kdelibs-4.14.38 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