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() );
178
Q_UNUSED( _iconset );
184
#ifndef WIDGET_PLUGIN
185
QMap<Icon*, QRect>::Iterator it;
186
for (it = iconRects.begin(); it != iconRects.end(); it++)
191
const Iconset *iconset() const
193
#ifndef WIDGET_PLUGIN
200
int height( const QListBox *lb ) const
202
fullH = lb->fontMetrics().lineSpacing() + 2 + h;
203
return QMAX( fullH, QApplication::globalStrut().height() );
206
int width( const QListBox *lb ) const
208
fullW = QMAX(lb->fontMetrics().width( text() ) + 6, w + 10);
209
return QMAX( fullW, QApplication::globalStrut().width() );
212
void paint(QPainter *painter)
214
#ifndef WIDGET_PLUGIN
215
QFontMetrics fm = painter->fontMetrics();
216
painter->drawText( 3, fm.ascent() + (fm.leading()+1)/2 + 1, text() );
218
QMap<Icon*, QRect>::Iterator it;
219
for (it = iconRects.begin(); it != iconRects.end(); it++) {
220
Icon *icon = it.key();
222
painter->drawPixmap(QPoint(10 + r.left(), fm.lineSpacing() + 2 + r.top()), icon->pixmap());
230
void iconUpdated(const QPixmap &)
232
#ifndef WIDGET_PLUGIN
233
IconsetSelect *issel = (IconsetSelect *)listBox();
234
issel->updateItem (this);
238
const int IconsetSelectItem::margin = 3;
239
const int IconsetSelectItem::displayNumIcons = 10;
241
class IconsetSelect::Private
249
IconsetSelectItem *lastItem;
252
IconsetSelect::IconsetSelect(QWidget *parent, const char *name)
253
: QListBox(parent, name)
258
IconsetSelect::~IconsetSelect()
263
void IconsetSelect::insert(const Iconset &iconset)
265
#ifndef WIDGET_PLUGIN
266
IconsetSelectItem *item = new IconsetSelectItem(this, d->lastItem, iconset);
273
void IconsetSelect::moveItemUp()
275
if ( currentItem() < 1 )
278
IconsetSelectItem *i = (IconsetSelectItem *)item(currentItem());
281
QListBoxItem *prev = i->prev()->prev();
283
insertItem (i, prev);
284
setSelected (i, true);
288
void IconsetSelect::moveItemDown()
290
if ( currentItem() == -1 || currentItem() > (int)count() - 2 )
293
IconsetSelectItem *i = (IconsetSelectItem *)item(currentItem());
296
QListBoxItem *next = i->next();
298
insertItem (i, next);
302
const Iconset *IconsetSelect::iconset() const
304
IconsetSelectItem *i = (IconsetSelectItem *)selectedItem();
306
i = (IconsetSelectItem *)item(currentItem());
312
void IconsetSelect::paintCell(QPainter *p, int row, int col)
314
// we'll do some caching to avoid flicker
315
QListBoxItem *item = QListBox::item(row);
318
QListBox::paintCell(p, row, col);
322
int w = contentsWidth();
323
int h = item->height(this);
327
QListBox::paintCell(&p2, row, col);
330
p->drawPixmap(QPoint(0, 0), pix);
334
//----------------------------------------------------------------------------
336
//----------------------------------------------------------------------------
338
class IconsetDisplayItem : public IconWidgetItem
342
static const int margin;
347
IconsetDisplayItem(QListBox *parent, IconsetDisplayItem *after, Icon *i, int iconW)
348
: IconWidgetItem(parent, after)
350
#ifndef WIDGET_PLUGIN
354
connect (icon, SIGNAL(pixmapChanged(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
355
icon->activated(false);
357
h = icon->pixmap().height();
360
QDictIterator<QString> it ( icon->text() );
361
for ( ; it.current(); ++it) {
362
if ( !str.isEmpty() )
366
if ( !str.isEmpty() )
369
setText(tr("Name: '%1'").arg(icon->name()));
376
~IconsetDisplayItem()
378
#ifndef WIDGET_PLUGIN
383
int height( const QListBox *lb ) const
385
int hh = QMAX(h + 2*margin, lb->fontMetrics().lineSpacing() + 2);
386
return QMAX( hh, QApplication::globalStrut().height() );
389
int width( const QListBox *lb ) const
391
int ww = lb->fontMetrics().width( text() ) + 6 + w + 2*margin;
392
return QMAX( ww, QApplication::globalStrut().width() );
395
void paint(QPainter *painter)
397
#ifndef WIDGET_PLUGIN
398
painter->drawPixmap(QPoint((2*margin+w - icon->pixmap().width())/2, margin), icon->pixmap());
399
QFontMetrics fm = painter->fontMetrics();
400
//int hh = QMAX(h + 2*margin, fm.lineSpacing() + 2);
401
painter->drawText( w + 2*margin + 3, fm.ascent() + (fm.leading()+1)/2 + 1, text() );
402
//painter->drawText( w + 2*margin + 3, (hh - fm.lineSpacing())/2, text() );
409
void iconUpdated(const QPixmap &)
411
IconsetDisplay *issel = (IconsetDisplay *)listBox();
412
issel->updateItem (this);
415
const int IconsetDisplayItem::margin = 3;
417
class IconsetDisplay::Private
425
IconsetDisplayItem *lastItem;
428
IconsetDisplay::IconsetDisplay(QWidget *parent, const char *name)
429
: QListBox(parent, name, WStaticContents | WResizeNoErase | WRepaintNoErase)
434
IconsetDisplay::~IconsetDisplay()
439
void IconsetDisplay::setIconset(const Iconset &iconset)
441
#ifndef WIDGET_PLUGIN
443
QPtrListIterator<Icon> it = iconset.iterator();
444
for ( ; it.current(); ++it) {
445
w = QMAX(w, it.current()->pixmap().width());
448
it = iconset.iterator();
449
for ( ; it.current(); ++it) {
450
IconsetDisplayItem *item = new IconsetDisplayItem(this, d->lastItem, it.current(), w);
458
void IconsetDisplay::paintCell(QPainter *p, int row, int col)
460
// we'll do some caching to avoid flicker
461
QListBoxItem *item = QListBox::item(row);
464
QListBox::paintCell(p, row, col);
468
int w = contentsWidth();
469
int h = item->height(this);
473
QListBox::paintCell(&p2, row, col);
476
p->drawPixmap(QPoint(0, 0), pix);
480
//----------------------------------------------------------------------------
482
//----------------------------------------------------------------------------
484
class IconButton::Private : public QObject
491
bool activate, forced;
497
Private(IconButton *b)
510
void setIcon(Icon *i)
512
#ifndef WIDGET_PLUGIN
524
#ifndef WIDGET_PLUGIN
526
connect(icon, SIGNAL(pixmapChanged(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
528
icon->activated(true); // FIXME: should icon play sound when it's activated on button?
537
#ifndef WIDGET_PLUGIN
539
disconnect(icon, 0, this, 0 );
551
#ifndef WIDGET_PLUGIN
553
iconUpdated( icon->pixmap() );
559
#ifndef WIDGET_PLUGIN
561
iconUpdated( icon->pixmap() );
563
iconUpdated( QPixmap() );
568
void iconUpdated(const QPixmap &pix)
570
button->setUpdatesEnabled(FALSE);
571
if ( textVisible || button->text().isEmpty() )
572
button->setIconSet(pix);
574
button->setPixmap(pix);
575
button->setUpdatesEnabled(TRUE);
580
IconButton::IconButton(QWidget *parent, const char *name)
581
: QPushButton(parent, name)
583
setWFlags(getWFlags() | WRepaintNoErase); // no nasty flicker anymore :)
584
d = new Private(this);
587
IconButton::~IconButton()
592
void IconButton::setIcon(const QPixmap &p)
594
QPushButton::setIcon(p);
597
void IconButton::forceSetIcon(const Icon *i, bool activate)
599
d->activate = activate;
600
d->setIcon ((Icon *)i);
604
void IconButton::setIcon(const Icon *i, bool activate)
607
if ( !text().isEmpty() )
611
forceSetIcon(i, activate);
615
void IconButton::setIcon(const QString &name)
617
#ifndef WIDGET_PLUGIN
618
setIcon( IconsetFactory::iconPtr(name) );
622
if ( !name.isEmpty() ) {
623
QPixmap pix((const char **)cancel_xpm);
624
d->iconUpdated(QPixmap( pix ));
627
d->iconUpdated(QPixmap());
631
const QString &IconButton::iconName() const
633
#ifndef WIDGET_PLUGIN
635
return d->icon->name();
636
return QString::null;
642
void IconButton::setText(const QString &text)
649
QPushButton::setText( text );
653
bool IconButton::textVisible() const
655
return d->textVisible;
658
void IconButton::setTextVisible(bool v)
664
void IconButton::drawButtonLabel(QPainter *p)
666
QPushButton::drawButtonLabel(p);
669
//----------------------------------------------------------------------------
671
//----------------------------------------------------------------------------
673
class IconToolButton::Private : public QObject
678
IconToolButton *button;
685
Private(IconToolButton *b)
696
void setIcon(Icon *i)
698
#ifndef WIDGET_PLUGIN
710
#ifndef WIDGET_PLUGIN
712
connect(icon, SIGNAL(pixmapChanged(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
714
icon->activated(true); // FIXME: should icon play sound when it's activated on button?
715
iconUpdated( icon->pixmap() );
718
iconUpdated( QPixmap() );
724
#ifndef WIDGET_PLUGIN
726
disconnect(icon, 0, this, 0 );
738
#ifndef WIDGET_PLUGIN
740
iconUpdated( icon->pixmap() );
745
void iconUpdated(const QPixmap &pix)
747
button->setUpdatesEnabled(FALSE);
749
button->setIconSet(pix);
751
// button->setPixmap(pix);
752
button->setUpdatesEnabled(TRUE);
757
IconToolButton::IconToolButton(QWidget *parent, const char *name)
758
: QToolButton(parent, name)
760
setWFlags(getWFlags() | WRepaintNoErase);
761
d = new Private(this);
764
IconToolButton::~IconToolButton()
769
void IconToolButton::setIcon(const QPixmap &p)
771
QToolButton::setIcon(p);
774
void IconToolButton::setIcon(const Icon *i, bool activate)
776
d->activate = activate;
777
d->setIcon ((Icon *)i);
780
void IconToolButton::setIcon(const QString &name)
782
#ifndef WIDGET_PLUGIN
783
setIcon( IconsetFactory::iconPtr(name) );
789
const QString &IconToolButton::iconName() const
791
#ifndef WIDGET_PLUGIN
793
return d->icon->name();
794
return QString::null;
800
void IconToolButton::drawButtonLabel(QPainter *p)
802
QToolButton::drawButtonLabel(p);
805
#include "iconwidget.moc"