2
* iconwidget.cpp - misc. Iconset- and Icon-aware widgets
3
* Copyright (C) 2003 Michail Pishchagin
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include "iconwidget.h"
23
#include <qapplication.h>
34
static const char *cancel_xpm[] = {
126
//----------------------------------------------------------------------------
128
//----------------------------------------------------------------------------
130
class IconsetSelectItem : public IconWidgetItem
134
static const int margin;
135
static const int displayNumIcons;
136
#ifndef WIDGET_PLUGIN
138
QMap<Icon*, QRect> iconRects;
141
mutable int fullW, fullH;
144
IconsetSelectItem(QListBox *parent, IconsetSelectItem *after, const Iconset &_iconset)
145
: IconWidgetItem(parent, after)
147
#ifndef WIDGET_PLUGIN
149
setText( iss.name() );
156
QPtrListIterator<Icon> it = iss.iterator();
157
for (count = 0; it.current(); ++it) {
158
if ( count++ >= displayNumIcons )
159
break; // display only first displayNumIcons icons
161
QPixmap pix = it.current()->pixmap();
163
iconRects[it.current()] = QRect( w, margin, pix.width(), pix.height() );
165
w += pix.width() + margin;
166
h = QMAX( h, pix.height() + 2*margin );
168
connect (it.current(), SIGNAL(pixmapChanged(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
169
it.current()->activated(false); // start animation
172
QMap<Icon*, QRect>::Iterator it2;
173
for (it2 = iconRects.begin(); it2 != iconRects.end(); it2++) {
174
QRect r = it2.data();
175
it2.data() = QRect( r.x(), (h - r.height())/2, r.width(), r.height() );
182
#ifndef WIDGET_PLUGIN
183
QMap<Icon*, QRect>::Iterator it;
184
for (it = iconRects.begin(); it != iconRects.end(); it++)
189
const Iconset *iconset() const
191
#ifndef WIDGET_PLUGIN
198
int height( const QListBox *lb ) const
200
fullH = lb->fontMetrics().lineSpacing() + 2 + h;
201
return QMAX( fullH, QApplication::globalStrut().height() );
204
int width( const QListBox *lb ) const
206
fullW = QMAX(lb->fontMetrics().width( text() ) + 6, w + 10);
207
return QMAX( fullW, QApplication::globalStrut().width() );
210
void paint(QPainter *painter)
212
#ifndef WIDGET_PLUGIN
213
QFontMetrics fm = painter->fontMetrics();
214
painter->drawText( 3, fm.ascent() + (fm.leading()+1)/2 + 1, text() );
216
QMap<Icon*, QRect>::Iterator it;
217
for (it = iconRects.begin(); it != iconRects.end(); it++) {
218
Icon *icon = it.key();
220
painter->drawPixmap(QPoint(10 + r.left(), fm.lineSpacing() + 2 + r.top()), icon->pixmap());
228
void iconUpdated(const QPixmap &)
230
#ifndef WIDGET_PLUGIN
231
IconsetSelect *issel = (IconsetSelect *)listBox();
232
issel->updateItem (this);
236
const int IconsetSelectItem::margin = 3;
237
const int IconsetSelectItem::displayNumIcons = 10;
239
class IconsetSelect::Private
247
IconsetSelectItem *lastItem;
250
IconsetSelect::IconsetSelect(QWidget *parent, const char *name)
251
: QListBox(parent, name)
256
IconsetSelect::~IconsetSelect()
261
void IconsetSelect::insert(const Iconset &iconset)
263
#ifndef WIDGET_PLUGIN
264
IconsetSelectItem *item = new IconsetSelectItem(this, d->lastItem, iconset);
271
void IconsetSelect::moveItemUp()
273
if ( currentItem() < 1 )
276
IconsetSelectItem *i = (IconsetSelectItem *)item(currentItem());
279
QListBoxItem *prev = i->prev()->prev();
281
insertItem (i, prev);
282
setSelected (i, true);
286
void IconsetSelect::moveItemDown()
288
if ( currentItem() == -1 || currentItem() > (int)count() - 2 )
291
IconsetSelectItem *i = (IconsetSelectItem *)item(currentItem());
294
QListBoxItem *next = i->next();
296
insertItem (i, next);
300
const Iconset *IconsetSelect::iconset() const
302
IconsetSelectItem *i = (IconsetSelectItem *)selectedItem();
304
i = (IconsetSelectItem *)item(currentItem());
310
void IconsetSelect::paintCell(QPainter *p, int row, int col)
312
// we'll do some caching to avoid flicker
313
QListBoxItem *item = QListBox::item(row);
316
QListBox::paintCell(p, row, col);
320
int w = contentsWidth();
321
int h = item->height(this);
325
QListBox::paintCell(&p2, row, col);
328
p->drawPixmap(QPoint(0, 0), pix);
332
//----------------------------------------------------------------------------
334
//----------------------------------------------------------------------------
336
class IconsetDisplayItem : public IconWidgetItem
340
static const int margin;
345
IconsetDisplayItem(QListBox *parent, IconsetDisplayItem *after, Icon *i, int iconW)
346
: IconWidgetItem(parent, after)
348
#ifndef WIDGET_PLUGIN
352
connect (icon, SIGNAL(pixmapChanged(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
353
icon->activated(false);
355
h = icon->pixmap().height();
358
QDictIterator<QString> it ( icon->text() );
359
for ( ; it.current(); ++it) {
360
if ( !str.isEmpty() )
364
if ( !str.isEmpty() )
367
setText(tr("Name: '%1'").arg(icon->name()));
371
~IconsetDisplayItem()
373
#ifndef WIDGET_PLUGIN
378
int height( const QListBox *lb ) const
380
int hh = QMAX(h + 2*margin, lb->fontMetrics().lineSpacing() + 2);
381
return QMAX( hh, QApplication::globalStrut().height() );
384
int width( const QListBox *lb ) const
386
int ww = lb->fontMetrics().width( text() ) + 6 + w + 2*margin;
387
return QMAX( ww, QApplication::globalStrut().width() );
390
void paint(QPainter *painter)
392
#ifndef WIDGET_PLUGIN
393
painter->drawPixmap(QPoint((2*margin+w - icon->pixmap().width())/2, margin), icon->pixmap());
394
QFontMetrics fm = painter->fontMetrics();
395
//int hh = QMAX(h + 2*margin, fm.lineSpacing() + 2);
396
painter->drawText( w + 2*margin + 3, fm.ascent() + (fm.leading()+1)/2 + 1, text() );
397
//painter->drawText( w + 2*margin + 3, (hh - fm.lineSpacing())/2, text() );
404
void iconUpdated(const QPixmap &)
406
IconsetDisplay *issel = (IconsetDisplay *)listBox();
407
issel->updateItem (this);
410
const int IconsetDisplayItem::margin = 3;
412
class IconsetDisplay::Private
420
IconsetDisplayItem *lastItem;
423
IconsetDisplay::IconsetDisplay(QWidget *parent, const char *name)
424
: QListBox(parent, name, WStaticContents | WResizeNoErase | WRepaintNoErase)
429
IconsetDisplay::~IconsetDisplay()
434
void IconsetDisplay::setIconset(const Iconset &iconset)
436
#ifndef WIDGET_PLUGIN
438
QPtrListIterator<Icon> it = iconset.iterator();
439
for ( ; it.current(); ++it) {
440
w = QMAX(w, it.current()->pixmap().width());
443
it = iconset.iterator();
444
for ( ; it.current(); ++it) {
445
IconsetDisplayItem *item = new IconsetDisplayItem(this, d->lastItem, it.current(), w);
453
void IconsetDisplay::paintCell(QPainter *p, int row, int col)
455
// we'll do some caching to avoid flicker
456
QListBoxItem *item = QListBox::item(row);
459
QListBox::paintCell(p, row, col);
463
int w = contentsWidth();
464
int h = item->height(this);
468
QListBox::paintCell(&p2, row, col);
471
p->drawPixmap(QPoint(0, 0), pix);
475
//----------------------------------------------------------------------------
477
//----------------------------------------------------------------------------
479
class IconButton::Private : public QObject
486
bool activate, forced;
492
Private(IconButton *b)
505
void setIcon(Icon *i)
507
#ifndef WIDGET_PLUGIN
517
#ifndef WIDGET_PLUGIN
519
connect(icon, SIGNAL(pixmapChanged(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
521
icon->activated(true); // FIXME: should icon play sound when it's activated on button?
522
iconUpdated( icon->pixmap() );
525
iconUpdated( QPixmap() );
531
#ifndef WIDGET_PLUGIN
533
disconnect(icon, 0, this, 0 );
545
#ifndef WIDGET_PLUGIN
547
iconUpdated( icon->pixmap() );
552
void iconUpdated(const QPixmap &pix)
554
button->setUpdatesEnabled(FALSE);
556
button->setIconSet(pix);
558
button->setPixmap(pix);
559
button->setUpdatesEnabled(TRUE);
564
IconButton::IconButton(QWidget *parent, const char *name)
565
: QPushButton(parent, name)
567
setWFlags(getWFlags() | WRepaintNoErase); // no nasty flicker anymore :)
568
d = new Private(this);
571
IconButton::~IconButton()
576
void IconButton::setIcon(const QPixmap &p)
578
QPushButton::setIcon(p);
581
void IconButton::forceSetIcon(const Icon *i, bool activate)
583
d->activate = activate;
584
d->setIcon ((Icon *)i);
588
void IconButton::setIcon(const Icon *i, bool activate)
591
if ( !text().isEmpty() )
595
forceSetIcon(i, activate);
599
void IconButton::setIcon(const QString &name)
601
#ifndef WIDGET_PLUGIN
602
setIcon( IconsetFactory::iconPtr(name) );
606
if ( !name.isEmpty() ) {
607
QPixmap pix((char **)cancel_xpm);
608
d->iconUpdated(QPixmap( pix ));
611
d->iconUpdated(QPixmap());
615
const QString &IconButton::iconName() const
617
#ifndef WIDGET_PLUGIN
619
return d->icon->name();
620
return QString::null;
626
void IconButton::setText(const QString &text)
633
QPushButton::setText( text );
636
bool IconButton::textVisible() const
638
return d->textVisible;
641
void IconButton::setTextVisible(bool v)
647
void IconButton::drawButtonLabel(QPainter *p)
649
QPushButton::drawButtonLabel(p);
652
//----------------------------------------------------------------------------
654
//----------------------------------------------------------------------------
656
class IconToolButton::Private : public QObject
661
IconToolButton *button;
668
Private(IconToolButton *b)
679
void setIcon(Icon *i)
681
#ifndef WIDGET_PLUGIN
691
#ifndef WIDGET_PLUGIN
693
connect(icon, SIGNAL(pixmapChanged(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
695
icon->activated(true); // FIXME: should icon play sound when it's activated on button?
696
iconUpdated( icon->pixmap() );
699
iconUpdated( QPixmap() );
705
#ifndef WIDGET_PLUGIN
707
disconnect(icon, 0, this, 0 );
719
#ifndef WIDGET_PLUGIN
721
iconUpdated( icon->pixmap() );
726
void iconUpdated(const QPixmap &pix)
728
button->setUpdatesEnabled(FALSE);
730
button->setIconSet(pix);
732
// button->setPixmap(pix);
733
button->setUpdatesEnabled(TRUE);
738
IconToolButton::IconToolButton(QWidget *parent, const char *name)
739
: QToolButton(parent, name)
741
setWFlags(getWFlags() | WRepaintNoErase);
742
d = new Private(this);
745
IconToolButton::~IconToolButton()
750
void IconToolButton::setIcon(const QPixmap &p)
752
QToolButton::setIcon(p);
755
void IconToolButton::setIcon(const Icon *i, bool activate)
757
d->activate = activate;
758
d->setIcon ((Icon *)i);
761
void IconToolButton::setIcon(const QString &name)
763
#ifndef WIDGET_PLUGIN
764
setIcon( IconsetFactory::iconPtr(name) );
770
const QString &IconToolButton::iconName() const
772
#ifndef WIDGET_PLUGIN
774
return d->icon->name();
775
return QString::null;
781
void IconToolButton::drawButtonLabel(QPainter *p)
783
QToolButton::drawButtonLabel(p);
786
#include "iconwidget.moc"