~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/image/qicon.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the painting module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qicon.h"
 
30
#include "qiconengine.h"
 
31
#include "qiconengineplugin.h"
 
32
#include "private/qfactoryloader_p.h"
 
33
#include "qapplication.h"
 
34
#include "qstyleoption.h"
 
35
#include "qpainter.h"
 
36
#include "qfileinfo.h"
 
37
#include "qstyle.h"
 
38
#include "qpixmapcache.h"
 
39
#include "qvariant.h"
 
40
#include "qdebug.h"
 
41
 
 
42
/*!
 
43
    \enum QIcon::Mode
 
44
 
 
45
    This enum type describes the mode for which a pixmap is intended
 
46
    to be used. The currently defined modes are:
 
47
 
 
48
    \value Normal
 
49
         Display the pixmap when the user is
 
50
        not interacting with the icon, but the
 
51
        functionality represented by the icon is available.
 
52
    \value Disabled
 
53
         Display the pixmap when the
 
54
        functionality represented by the icon is not available.
 
55
    \value Active
 
56
         Display the pixmap when the
 
57
        functionality represented by the icon is available and
 
58
        the user is interacting with the icon, for example, moving the
 
59
        mouse over it or clicking it.
 
60
*/
 
61
 
 
62
/*!
 
63
  \enum QIcon::State
 
64
 
 
65
  This enum describes the state for which a pixmap is intended to be
 
66
  used. The \e state can be:
 
67
 
 
68
  \value Off  Display the pixmap when the widget is in an "off" state
 
69
  \value On  Display the pixmap when the widget is in an "on" state
 
70
*/
 
71
 
 
72
static int serialNumCounter = 0;
 
73
 
 
74
class QIconPrivate
 
75
{
 
76
public:
 
77
    QIconPrivate():ref(1),engine(0),serialNum(++serialNumCounter){}
 
78
    ~QIconPrivate() { delete engine; }
 
79
    QAtomic ref;
 
80
    QIconEngine *engine;
 
81
    int serialNum;
 
82
};
 
83
 
 
84
 
 
85
struct QPixmapIconEngineEntry
 
86
{
 
87
    QPixmapIconEngineEntry():mode(QIcon::Normal), state(QIcon::Off){}
 
88
    QPixmapIconEngineEntry(const QPixmap &pm, QIcon::Mode m = QIcon::Normal, QIcon::State s = QIcon::Off)
 
89
        :pixmap(pm), size(pm.size()), mode(m), state(s){}
 
90
    QPixmapIconEngineEntry(const QString &file, const QSize &sz = QSize(), QIcon::Mode m = QIcon::Normal, QIcon::State s = QIcon::Off)
 
91
        :fileName(file), size(sz), mode(m), state(s){}
 
92
    QPixmap pixmap;
 
93
    QString fileName;
 
94
    QSize size;
 
95
    QIcon::Mode mode;
 
96
    QIcon::State state;
 
97
    bool isNull() const {return (fileName.isEmpty() && pixmap.isNull()); }
 
98
};
 
99
 
 
100
class QPixmapIconEngine : public QIconEngine{
 
101
public:
 
102
    QPixmapIconEngine();
 
103
    ~QPixmapIconEngine();
 
104
    void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state);
 
105
    QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state);
 
106
    QPixmapIconEngineEntry *bestMatch(const QSize &size, QIcon::Mode mode, QIcon::State state, bool sizeOnly);
 
107
    QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state);
 
108
    void addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state);
 
109
    void addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state);
 
110
private:
 
111
    QPixmapIconEngineEntry *tryMatch(const QSize &size, QIcon::Mode mode, QIcon::State state);
 
112
    QVector<QPixmapIconEngineEntry> pixmaps;
 
113
};
 
114
 
 
115
QPixmapIconEngine::QPixmapIconEngine()
 
116
{
 
117
}
 
118
 
 
119
QPixmapIconEngine::~QPixmapIconEngine()
 
120
{
 
121
}
 
122
 
 
123
void QPixmapIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
 
124
{
 
125
    painter->drawPixmap(rect, pixmap(rect.size(), mode, state));
 
126
}
 
127
 
 
128
static inline int area(const QSize &s) { return s.width() * s.height(); }
 
129
 
 
130
// returns the smallest of the two that is still larger than or equal to size.
 
131
static QPixmapIconEngineEntry *bestSizeMatch( const QSize &size, QPixmapIconEngineEntry *pa, QPixmapIconEngineEntry *pb)
 
132
{
 
133
    int s = area(size);
 
134
    if (pa->size == QSize() && pa->pixmap.isNull()) {
 
135
        pa->pixmap = QPixmap(pa->fileName);
 
136
        pa->size = pa->pixmap.size();
 
137
    }
 
138
    int a = area(pa->size);
 
139
    if (pb->size == QSize() && pb->pixmap.isNull()) {
 
140
        pb->pixmap = QPixmap(pb->fileName);
 
141
        pb->size = pb->pixmap.size();
 
142
    }
 
143
    int b = area(pb->size);
 
144
    int res = a;
 
145
    if (qMin(a,b) >= s)
 
146
        res = qMin(a,b);
 
147
    else
 
148
        res = qMax(a,b);
 
149
    if (res == a)
 
150
        return pa;
 
151
    return pb;
 
152
}
 
153
 
 
154
QPixmapIconEngineEntry *QPixmapIconEngine::tryMatch(const QSize &size, QIcon::Mode mode, QIcon::State state)
 
155
{
 
156
    QPixmapIconEngineEntry *pe = 0;
 
157
    for (int i = 0; i < pixmaps.count(); ++i)
 
158
        if (pixmaps.at(i).mode == mode && pixmaps.at(i).state == state) {
 
159
            if (pe)
 
160
                pe = bestSizeMatch(size, &pixmaps[i], pe);
 
161
            else
 
162
                pe = &pixmaps[i];
 
163
        }
 
164
    return pe;
 
165
}
 
166
 
 
167
 
 
168
QPixmapIconEngineEntry *QPixmapIconEngine::bestMatch(const QSize &size, QIcon::Mode mode, QIcon::State state, bool sizeOnly)
 
169
{
 
170
    QPixmapIconEngineEntry *pe = tryMatch(size, mode, state);
 
171
    while (!pe){
 
172
        QIcon::State oppositeState = (state == QIcon::On) ? QIcon::Off : QIcon::On;
 
173
        if (mode == QIcon::Disabled) {
 
174
            if ((pe = tryMatch(size, QIcon::Normal, state)))
 
175
                break;
 
176
            if ((pe = tryMatch(size, QIcon::Active, state)))
 
177
                break;
 
178
            if ((pe = tryMatch(size, QIcon::Disabled, oppositeState)))
 
179
                break;
 
180
            if ((pe = tryMatch(size, QIcon::Normal, oppositeState)))
 
181
                break;
 
182
            if ((pe = tryMatch(size, QIcon::Active, oppositeState)))
 
183
                break;
 
184
        } else {
 
185
            QIcon::Mode oppositeMode = (mode == QIcon::Normal) ? QIcon::Active : QIcon::Normal;
 
186
            if ((pe = tryMatch(size, oppositeMode, state)))
 
187
                break;
 
188
            if ((pe = tryMatch(size, mode, oppositeState)))
 
189
                break;
 
190
            if ((pe = tryMatch(size, oppositeMode, oppositeState)))
 
191
                break;
 
192
            if ((pe = tryMatch(size, QIcon::Disabled, state)))
 
193
                break;
 
194
            if ((pe = tryMatch(size, QIcon::Disabled, oppositeState)))
 
195
                break;
 
196
        }
 
197
 
 
198
        if (!pe)
 
199
            return pe;
 
200
    }
 
201
 
 
202
    if (sizeOnly ? (pe->size.isNull() || !pe->size.isValid()) : pe->pixmap.isNull()) {
 
203
        pe->pixmap = QPixmap(pe->fileName);
 
204
        if (!pe->pixmap.isNull())
 
205
            pe->size = pe->pixmap.size();
 
206
    }
 
207
 
 
208
    return pe;
 
209
}
 
210
 
 
211
QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
 
212
{
 
213
    QPixmap pm;
 
214
    QPixmapIconEngineEntry *pe = bestMatch(size, mode, state, false);
 
215
    if (pe)
 
216
        pm = pe->pixmap;
 
217
 
 
218
    if (pm.isNull())
 
219
        return pm;
 
220
 
 
221
    QSize actualSize = pm.size();
 
222
    if (!actualSize.isNull() && (actualSize.width() > size.width() && actualSize.height() > size.height()))
 
223
        actualSize.scale(size, Qt::KeepAspectRatio);
 
224
 
 
225
    QString key = QLatin1String("$qt_icon_")
 
226
                  + QString::number(pm.serialNumber())
 
227
                  + QString::number(actualSize.width())
 
228
                  + QLatin1Char('_')
 
229
                  + QString::number(actualSize.height())
 
230
                  + QLatin1Char('_');
 
231
 
 
232
 
 
233
    if (mode == QIcon::Active) {
 
234
        if (QPixmapCache::find(key + QString::number(mode), pm))
 
235
            return pm; // horray
 
236
        if (QPixmapCache::find(key + QString::number(QIcon::Normal), pm)) {
 
237
            QStyleOption opt(0);
 
238
            opt.palette = QApplication::palette();
 
239
            QPixmap active = QApplication::style()->generatedIconPixmap(QIcon::Active, pm, &opt);
 
240
            if (pm.serialNumber() == active.serialNumber())
 
241
                return pm;
 
242
        }
 
243
    }
 
244
 
 
245
    if (!QPixmapCache::find(key + QString::number(mode), pm)) {
 
246
        if (pe->mode != mode && mode != QIcon::Normal && pe->mode != QIcon::Disabled) {
 
247
            QStyleOption opt(0);
 
248
            opt.palette = QApplication::palette();
 
249
            QPixmap generated = QApplication::style()->generatedIconPixmap(mode, pm, &opt);
 
250
            if (!generated.isNull())
 
251
                pm = generated;
 
252
        }
 
253
        if (pm.size() != actualSize)
 
254
            pm = pm.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
 
255
        QPixmapCache::insert(key + QString::number(mode), pm);
 
256
    }
 
257
    return pm;
 
258
}
 
259
 
 
260
QSize QPixmapIconEngine::actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
 
261
{
 
262
    QSize actualSize;
 
263
    if (QPixmapIconEngineEntry *pe = bestMatch(size, mode, state, true))
 
264
        actualSize = pe->size;
 
265
 
 
266
    if (actualSize.isNull())
 
267
        return actualSize;
 
268
 
 
269
    if (!actualSize.isNull() && (actualSize.width() > size.width() && actualSize.height() > size.height()))
 
270
        actualSize.scale(size, Qt::KeepAspectRatio);
 
271
    return actualSize;
 
272
}
 
273
 
 
274
void QPixmapIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state)
 
275
{
 
276
    if (!pixmap.isNull())
 
277
        pixmaps += QPixmapIconEngineEntry(pixmap, mode, state);
 
278
}
 
279
 
 
280
void QPixmapIconEngine::addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state)
 
281
{
 
282
    if (!fileName.isEmpty()) {
 
283
        QString abs = fileName;
 
284
        if (fileName.at(0) != QLatin1Char(':'))
 
285
            abs = QFileInfo(fileName).absoluteFilePath();
 
286
        pixmaps += QPixmapIconEngineEntry(abs, size, mode, state);
 
287
    }
 
288
}
 
289
 
 
290
 
 
291
 
 
292
#ifndef QT_NO_COMPONENT
 
293
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
 
294
    (QIconEngineFactoryInterface_iid, QCoreApplication::libraryPaths(), "/iconengines", Qt::CaseInsensitive))
 
295
#endif
 
296
 
 
297
 
 
298
 
 
299
/*!
 
300
  \class QIcon
 
301
 
 
302
  \brief The QIcon class provides scalable icons in different modes
 
303
  and states.
 
304
 
 
305
  \ingroup multimedia
 
306
  \ingroup shared
 
307
  \mainclass
 
308
 
 
309
  A QIcon can generate smaller, larger, active, and disabled pixmaps
 
310
  from the set of pixmaps it is given. Such pixmaps are used by Qt
 
311
  widgets to show an icon representing a particular action.
 
312
 
 
313
  The simplest use of QIcon is to create one from a QPixmap file or
 
314
  resource, and then use it, allowing Qt to work out all the required
 
315
  icon styles and sizes. For example:
 
316
 
 
317
  \code
 
318
    QToolButton *button = new QToolButton;
 
319
    button->setIcon(QIcon("open.xpm"));
 
320
  \endcode
 
321
 
 
322
  When you retrieve a pixmap using pixmap(QSize, Mode, State), and no
 
323
  pixmap for this given size, mode and state has been added with
 
324
  addFile() or addPixmap(), then QIcon will generate one on the
 
325
  fly. This pixmap generation happens in a QIconEngine. The default
 
326
  engine scales pixmaps down if required, but never up, and it uses
 
327
  the current style to calculate a disabled appearance. By using
 
328
  custom icon engines, you can customize every aspect of generated
 
329
  icons. With QIconEnginePlugin it is possible to register different
 
330
  icon engines for different file suffixes, so you could provide a SVG
 
331
  icon engine or any other scalable format.
 
332
 
 
333
  \section1 Making Classes that Use QIcon
 
334
 
 
335
  If you write your own widgets that have an option to set a small
 
336
  pixmap, consider allowing a QIcon to be set for that pixmap.  The
 
337
  Qt class QToolButton is an example of such a widget.
 
338
 
 
339
  Provide a method to set a QIcon, and when you draw the icon, choose
 
340
  whichever pixmap is appropriate for the current state of your widget.
 
341
  For example:
 
342
  \code
 
343
    void MyWidget::drawIcon(QPainter *painter, QPoint pos)
 
344
    {
 
345
        QPixmap pixmap = icon.pixmap(QSize(22, 22),
 
346
                                       isEnabled() ? QIcon::Normal
 
347
                                                   : QIcon::Disabled,
 
348
                                       isOn() ? QIcon::On
 
349
                                              : QIcon::Off);
 
350
        painter->drawPixmap(pos, pixmap);
 
351
    }
 
352
  \endcode
 
353
 
 
354
  You might also make use of the \c Active mode, perhaps making your
 
355
  widget \c Active when the mouse is over the widget (see \l
 
356
  QWidget::enterEvent()), while the mouse is pressed pending the
 
357
  release that will activate the function, or when it is the currently
 
358
  selected item. If the widget can be toggled, the "On" mode might be
 
359
  used to draw a different icon.
 
360
 
 
361
  \img icon.png QIcon
 
362
 
 
363
  \sa {fowler}{GUI Design Handbook: Iconic Label}
 
364
*/
 
365
 
 
366
 
 
367
/*!
 
368
  Constructs a null icon.
 
369
*/
 
370
QIcon::QIcon()
 
371
    : d(0)
 
372
{
 
373
}
 
374
 
 
375
/*!
 
376
  Constructs an icon from a \a pixmap.
 
377
 */
 
378
QIcon::QIcon(const QPixmap &pixmap)
 
379
    :d(0)
 
380
{
 
381
    addPixmap(pixmap);
 
382
}
 
383
 
 
384
/*!
 
385
  Constructs a copy of \a other. This is very fast.
 
386
*/
 
387
QIcon::QIcon(const QIcon &other)
 
388
    :d(other.d)
 
389
{
 
390
    if (d)
 
391
        d->ref.ref();
 
392
}
 
393
 
 
394
/*!
 
395
    Constructs an icon from the file with the given \a fileName. The
 
396
    file will be loaded on demand. If the file does not exist or is of
 
397
    an unknown format, the icon becomes a null icon.
 
398
 
 
399
    If \a fileName contains a relative path (e.g. the filename only)
 
400
    the relevant file must be found relative to the runtime working
 
401
    directory.
 
402
 
 
403
    The file name can be either refer to an actual file on disk or to
 
404
    one of the application's embedded resources. See the
 
405
    \l{resources.html}{Resource System} overview for details on how to
 
406
    embed images and other resource files in the application's
 
407
    executable.
 
408
*/
 
409
QIcon::QIcon(const QString &fileName)
 
410
    : d(0)
 
411
{
 
412
    QFileInfo info(fileName);
 
413
    QString suffix = info.suffix();
 
414
    if (!suffix.isEmpty())
 
415
        if (QIconEngineFactoryInterface *factory = qobject_cast<QIconEngineFactoryInterface*>(loader()->instance(suffix)))
 
416
            if (QIconEngine *engine = factory->create(fileName)) {
 
417
                d = new QIconPrivate;
 
418
                d->engine = engine;
 
419
                return;
 
420
            }
 
421
    addFile(fileName);
 
422
}
 
423
 
 
424
 
 
425
/*!
 
426
    Creates an icon with a specific icon \a engine. The icon takes
 
427
    ownership of the engine.
 
428
*/
 
429
QIcon::QIcon(QIconEngine *engine)
 
430
    :d(new QIconPrivate)
 
431
{
 
432
    d->engine = engine;
 
433
}
 
434
 
 
435
/*!
 
436
    Destroys the icon.
 
437
*/
 
438
QIcon::~QIcon()
 
439
{
 
440
    if (d && !d->ref.deref())
 
441
        delete d;
 
442
}
 
443
 
 
444
/*!
 
445
    Assigns the \a other icon to this icon and returns a reference to
 
446
    this icon.
 
447
*/
 
448
QIcon &QIcon::operator=(const QIcon &other)
 
449
{
 
450
    QIconPrivate *x = other.d;
 
451
    if (x)
 
452
        x->ref.ref();
 
453
    x = qAtomicSetPtr(&d, x);
 
454
    if (x && !x->ref.deref())
 
455
        delete x;
 
456
    return *this;
 
457
}
 
458
 
 
459
/*!
 
460
   Returns the icon as a QVariant.
 
461
*/
 
462
QIcon::operator QVariant() const
 
463
{
 
464
    return QVariant(QVariant::Icon, this);
 
465
}
 
466
 
 
467
/*!
 
468
    Returns a number that uniquely identifies the contents of this
 
469
    QIcon object. This means that multiple QIcon objects can have
 
470
    the same serial number as long as they refer to the same contents.
 
471
 
 
472
    A null icon always has a serial number of 0.
 
473
 
 
474
    \sa QPixmap::serialNumber()
 
475
*/
 
476
 
 
477
int QIcon::serialNumber() const
 
478
{
 
479
    return d ? d->serialNum : 0;
 
480
}
 
481
 
 
482
/*!  Returns a pixmap with the requested \a size, \a mode, and \a
 
483
  state, generating one if necessary. The pixmap might be smaller than
 
484
  requested, but never larger.
 
485
 
 
486
  \sa actualSize(), paint()
 
487
*/
 
488
QPixmap QIcon::pixmap(const QSize &size, Mode mode, State state) const
 
489
{
 
490
    if (!d)
 
491
        return QPixmap();
 
492
    return d->engine->pixmap(size, mode, state);
 
493
}
 
494
 
 
495
/*!
 
496
    \fn QPixmap QIcon::pixmap(int w, int h, Mode mode = Normal, State state = Off) const
 
497
 
 
498
    \overload
 
499
 
 
500
    Returns a pixmap of size QSize(\a w, \a h).
 
501
*/
 
502
 
 
503
/*!
 
504
    \fn QPixmap QIcon::pixmap(int extent, Mode mode = Normal, State state = Off) const
 
505
 
 
506
    \overload
 
507
 
 
508
    Returns a pixmap of size QSize(\a extent, \a extent).
 
509
*/
 
510
 
 
511
/*!  Returns the actual size of the icon for the requested \a size, \a
 
512
  mode, and \a state. The result might be smaller than requested, but
 
513
  never larger.
 
514
 
 
515
  \sa pixmap(), paint()
 
516
*/
 
517
QSize QIcon::actualSize(const QSize &size, Mode mode, State state) const
 
518
{
 
519
    if (!d)
 
520
        return QSize();
 
521
    return d->engine->actualSize(size, mode, state);
 
522
}
 
523
 
 
524
 
 
525
/*!
 
526
    Uses the \a painter to paint the icon with specified \a alignment,
 
527
    required \a mode, and \a state into the rectangle \a rect.
 
528
 
 
529
    \sa actualSize(), pixmap()
 
530
*/
 
531
void QIcon::paint(QPainter *painter, const QRect &rect, Qt::Alignment alignment, Mode mode, State state) const
 
532
{
 
533
    if (!d || !painter)
 
534
        return;
 
535
    QRect alignedRect = QStyle::alignedRect(painter->layoutDirection(), alignment, d->engine->actualSize(rect.size(), mode, state), rect);
 
536
    d->engine->paint(painter, alignedRect, mode, state);
 
537
}
 
538
 
 
539
/*!
 
540
    \fn void QIcon::paint(QPainter *painter, int x, int y, int w, int h, Qt::Alignment alignment,
 
541
                          Mode mode, State state) const
 
542
 
 
543
    \overload
 
544
 
 
545
    Paints the icon into the rectangle QRect(\a x, \a y, \a w, \a h).
 
546
*/
 
547
 
 
548
/*!
 
549
    Returns true if the icon is empty; otherwise returns false.
 
550
*/
 
551
bool QIcon::isNull() const
 
552
{
 
553
    return !d;
 
554
}
 
555
 
 
556
/*!\internal
 
557
 */
 
558
bool QIcon::isDetached() const
 
559
{
 
560
    return !d || d->ref == 1;
 
561
}
 
562
 
 
563
 
 
564
/*!
 
565
    Adds \a pixmap to the icon, as a specialization for \a mode and
 
566
    \a state.
 
567
 
 
568
    Custom icon engines are free to ignore additionally added
 
569
    pixmaps.
 
570
 
 
571
    \sa addFile()
 
572
*/
 
573
void QIcon::addPixmap(const QPixmap &pixmap, Mode mode, State state)
 
574
{
 
575
    if (pixmap.isNull())
 
576
        return;
 
577
    if (!d) {
 
578
        d = new QIconPrivate;
 
579
        d->engine = new QPixmapIconEngine;
 
580
    }
 
581
    d->engine->addPixmap(pixmap, mode, state);
 
582
}
 
583
 
 
584
 
 
585
/*!  Adds a pixmap from the file with the given \a fileName to the
 
586
     icon, as a specialization for \a size, \a mode and \a state. The
 
587
     file will be loaded on demand. Note: custom icon engines are free
 
588
     to ignore additionally added pixmaps.
 
589
 
 
590
     If \a fileName contains a relative path (e.g. the filename only)
 
591
     the relevant file must be found relative to the runtime working
 
592
     directory.
 
593
 
 
594
    The file name can be either refer to an actual file on disk or to
 
595
    one of the application's embedded resources. See the
 
596
    \l{resources.html}{Resource System} overview for details on how to
 
597
    embed images and other resource files in the application's
 
598
    executable.
 
599
 
 
600
    \sa addPixmap()
 
601
 */
 
602
void QIcon::addFile(const QString &fileName, const QSize &size, Mode mode, State state)
 
603
{
 
604
    if (fileName.isEmpty())
 
605
        return;
 
606
    if (!d) {
 
607
        d = new QIconPrivate;
 
608
        d->engine = new QPixmapIconEngine;
 
609
    }
 
610
    d->engine->addFile(fileName, size, mode, state);
 
611
}
 
612
 
 
613
 
 
614
 
 
615
#ifdef QT3_SUPPORT
 
616
 
 
617
static int widths[2] = { 22, 32 };
 
618
static int heights[2] = { 22, 32 };
 
619
 
 
620
static QSize pixmapSize(QIcon::Size which) {
 
621
    int i = 0;
 
622
    if (which == QIcon::Large)
 
623
        i = 1;
 
624
    return QSize(widths[i], heights[i]);
 
625
}
 
626
 
 
627
/*!
 
628
    \enum QIcon::Size
 
629
    \compat
 
630
 
 
631
    \value Small  Use QStyle::pixelMetric(QStyle::PM_SmallIconSize) instead.
 
632
    \value Large  Use QStyle::pixelMetric(QStyle::PM_LargeIconSize) instead.
 
633
    \value Automatic  N/A.
 
634
*/
 
635
 
 
636
/*!
 
637
    Use pixmap(QSize(...), \a mode, \a state), where the first
 
638
    argument is an appropriate QSize instead of a \l Size value.
 
639
 
 
640
    \sa pixmapSize()
 
641
*/
 
642
QPixmap QIcon::pixmap(Size size, Mode mode, State state) const
 
643
{ return pixmap(::pixmapSize(size), mode, state); }
 
644
 
 
645
/*!
 
646
    Use pixmap(QSize(...), mode, \a state), where the first argument
 
647
    is an appropriate QSize instead of a \l Size value, and the
 
648
    second argument is QIcon::Normal or QIcon::Disabled, depending on
 
649
    the value of \a enabled.
 
650
 
 
651
    \sa pixmapSize()
 
652
*/
 
653
QPixmap QIcon::pixmap(Size size, bool enabled, State state) const
 
654
{ return pixmap(::pixmapSize(size), enabled ? Normal : Disabled, state); }
 
655
 
 
656
/*!
 
657
    Use one of the other pixmap() overloads.
 
658
*/
 
659
QPixmap QIcon::pixmap() const
 
660
{ return pixmap(::pixmapSize(Small), Normal, Off); }
 
661
 
 
662
/*!
 
663
    The pixmap() function now takes a QSize instead of a QIcon::Size,
 
664
    so there is no need for this function in new code.
 
665
*/
 
666
void QIcon::setPixmapSize(Size which, const QSize &size)
 
667
{
 
668
    int i = 0;
 
669
    if (which == Large)
 
670
        i = 1;
 
671
    widths[i] = size.width();
 
672
    heights[i] = size.height();
 
673
}
 
674
 
 
675
/*!
 
676
    Use QStyle::pixelMetric() with QStyle::PM_SmallIconSize or
 
677
    QStyle::PM_LargeIconSize as the first argument, depending on \a
 
678
    which.
 
679
*/
 
680
QSize QIcon::pixmapSize(Size which)
 
681
{
 
682
    return ::pixmapSize(which);
 
683
}
 
684
 
 
685
/*!
 
686
    \fn void QIcon::reset(const QPixmap &pixmap, Size size)
 
687
 
 
688
    Use the constructor that takes a QPixmap and operator=().
 
689
*/
 
690
 
 
691
/*!
 
692
    \fn void QIcon::setPixmap(const QPixmap &pixmap, Size size, Mode mode, State state)
 
693
 
 
694
    Use addPixmap(\a pixmap, \a mode, \a state) instead. The \a size
 
695
    parameter is ignored.
 
696
*/
 
697
 
 
698
/*!
 
699
    \fn void QIcon::setPixmap(const QString &fileName, Size size, Mode mode, State state)
 
700
 
 
701
    Use addFile(\a fileName, \a mode, \a state) instead. The \a size
 
702
    parameter is ignored.
 
703
*/
 
704
 
 
705
#endif // QT3_SUPPORT