2
* fancylabel.cpp - the FancyLabel widget
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 "fancylabel.h"
29
#include <qobjectlist.h>
35
//----------------------------------------------------------------------------
37
//----------------------------------------------------------------------------
39
class IconLabel::Private : public QObject
62
if ( icon && copyIcon )
67
void setIcon(const Icon *i, bool _copyIcon)
86
disconnect(icon, 0, this, 0);
96
connect(icon, SIGNAL(pixmapChanged(const QPixmap &)), SLOT(iconUpdated(const QPixmap &)));
97
icon->activated(false); // TODO: should icon play sound when it's activated on icon?
98
iconUpdated( icon->pixmap() );
101
iconUpdated( QPixmap() );
106
void iconUpdated(const QPixmap &pix)
108
label->setPixmap(pix);
112
IconLabel::IconLabel(QWidget *parent, const char *name)
113
: QLabel(parent, name, WRepaintNoErase | WResizeNoErase)
115
d = new Private(this);
118
IconLabel::~IconLabel()
123
void IconLabel::drawContents (QPainter *paint)
125
const QPixmap *background = paletteBackgroundPixmap();
126
QRect r = contentsRect();
127
QPoint start( r.topLeft() );
129
QWidget *pw = (QWidget *)parent();
130
if ( pw && (!background || pw->inherits("MyFancyFrame")) ) { // nasty hack
131
background = pw->paletteBackgroundPixmap();
132
start = mapToParent( r.topLeft() );
136
erase(0, 0, width(), height());
137
QLabel::drawContents(paint);
141
// try to eliminate flicker
142
QPixmap pix(r.width(), r.height());
145
p.drawTiledPixmap (r, *background, start);
146
QLabel::drawContents ( &p );
149
paint->drawPixmap (r.topLeft(), pix );
152
const Icon *IconLabel::icon () const
157
const QString &IconLabel::iconName () const
159
#ifndef WIDGET_PLUGIN
161
return d->icon->name();
162
return QString::null;
168
void IconLabel::setIcon (const Icon *i, bool copyIcon)
170
d->setIcon(i, copyIcon);
173
void IconLabel::setIcon (const QString &name)
175
#ifndef WIDGET_PLUGIN
176
setIcon( IconsetFactory::iconPtr(name) );
179
setText("<qt>icon:<br><small>" + name + "</small></qt>");
183
void IconLabel::setIcon(const QPixmap &p)
188
void IconLabel::setScaledContents(int width, int height)
190
QLabel::setScaledContents(true);
191
setMinimumSize(width, height);
192
setMaximumSize(width, height);
195
//----------------------------------------------------------------------------
196
// MyFancyFrame -- internal
197
//----------------------------------------------------------------------------
199
class MyFancyFrame : public QFrame
203
void drawContents (QPainter *paint)
205
paint->drawPixmap (contentsRect().topLeft(), background);
208
void resizeEvent (QResizeEvent *e)
210
QFrame::resizeEvent (e);
212
QRect rect = contentsRect();
213
int w = rect.width();
215
if ( rect.height() <= 0 || w <= 0 )
216
return; // avoid crash
219
from->rgb (&r1, &g1, &b1);
221
to->rgb (&r2, &g2, &b2);
223
float stepR = (float)(r2 - r1) / w;
224
float stepG = (float)(g2 - g1) / w;
225
float stepB = (float)(b2 - b1) / w;
227
QPixmap pix (rect.width(), rect.height());
230
for (int i = 0; i < w; i++) {
231
int r = (int)((float)r1 + stepR*i);
232
int g = (int)((float)g1 + stepG*i);
233
int b = (int)((float)b1 + stepB*i);
235
p.setPen ( QColor( r, g, b ) );
236
p.drawLine ( i, 0, i, rect.height() );
240
QObjectList *l = queryList( "QLabel" );
241
QObjectListIt it( *l );
242
while ( it.current() ) {
243
QLabel *child = (QLabel *)it.current();
250
setUpdatesEnabled(false);
251
setPaletteBackgroundPixmap(pix);
252
setUpdatesEnabled(true);
263
MyFancyFrame (QWidget *parent, QColor *_from, QColor *_to, const char *name = 0, WFlags f = 0)
264
: QFrame (parent, name, f)
270
void repaintBackground()
272
QResizeEvent e( size(), size() );
277
//----------------------------------------------------------------------------
279
//----------------------------------------------------------------------------
281
class FancyLabel::Private : public QObject
285
IconLabel *text, *help, *pix;
286
QColor from, to, font;
287
QString textStr, helpStr;
288
static int smallFontSize;
290
Private (FancyLabel *parent)
291
: QObject(parent), from(72, 172, 243), to(255, 255, 255), font(0, 0, 0)
293
QHBoxLayout *mainbox = new QHBoxLayout( parent, 0, 0 );
295
frame = new MyFancyFrame ( parent, &from, &to, "fancy_frame" );
296
frame->setFrameShape( QFrame::StyledPanel );
297
frame->setFrameShadow( QFrame::Raised );
299
QHBoxLayout *frameLayout = new QHBoxLayout( frame, 3, 0 );
300
QVBoxLayout *layout = new QVBoxLayout( 0, 0, 0 );
301
frameLayout->addLayout( layout );
303
text = new IconLabel( frame, "text_label" );
304
text->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)5, 0, 0, text->sizePolicy().hasHeightForWidth() ) );
305
layout->addWidget( text );
307
help = new IconLabel( frame, "help_label" );
308
layout->addWidget( help );
310
QFont font = help->font();
311
font.setPointSize(smallFontSize);
314
pix = new IconLabel( frame, "pixmap_label" );
315
pix->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)5, 0, 0, help->sizePolicy().hasHeightForWidth() ) );
316
frameLayout->addWidget( pix );
318
mainbox->addWidget( frame );
322
int FancyLabel::Private::smallFontSize = 0;
324
FancyLabel::FancyLabel (QWidget *parent, const char *name)
325
: QWidget (parent, name)
327
d = new Private (this);
330
FancyLabel::~FancyLabel ()
334
void FancyLabel::setText (const QString &text)
337
d->text->setText (QString("<font color=\"%1\"><b>").arg(d->font.name()) + text + "</b></font>");
340
void FancyLabel::setHelp (const QString &help)
344
QString f1 = "<small>";
345
QString f2 = "</small>";
346
if ( d->smallFontSize ) {
351
d->help->setText (QString("<font color=\"%1\">").arg(d->font.name()) + f1 + help + f2 + "</font>");
354
void FancyLabel::setPixmap (const QPixmap &pix)
356
d->pix->setPixmap( pix );
359
void FancyLabel::setColorFrom (const QColor &col)
362
d->frame->repaintBackground();
365
void FancyLabel::setColorTo (const QColor &col)
368
d->frame->repaintBackground();
371
void FancyLabel::setColorFont (const QColor &col)
374
d->frame->repaintBackground();
377
const QString &FancyLabel::text () const
382
const QString &FancyLabel::help () const
387
const QPixmap *FancyLabel::pixmap () const
389
return d->pix->pixmap();
392
const QColor &FancyLabel::colorFrom () const
397
const QColor &FancyLabel::colorTo () const
402
const QColor &FancyLabel::colorFont () const
407
const Icon *FancyLabel::icon () const
409
return d->pix->icon();
412
void FancyLabel::setIcon (const Icon *i)
417
void FancyLabel::setIcon (const QString &name)
419
d->pix->setIcon(name);
422
void FancyLabel::setIcon(const QPixmap &p)
427
const QString &FancyLabel::iconName () const
429
return d->pix->iconName();
432
FancyLabel::Shape FancyLabel::frameShape () const
434
return (FancyLabel::Shape)(int)d->frame->frameShape();
437
void FancyLabel::setFrameShape (FancyLabel::Shape v)
439
d->frame->setFrameShape( (QFrame::Shape)(int)v );
440
d->frame->repaintBackground();
443
FancyLabel::Shadow FancyLabel::frameShadow () const
445
return (FancyLabel::Shadow)(int)d->frame->frameShadow();
448
void FancyLabel::setFrameShadow (FancyLabel::Shadow v)
450
d->frame->setFrameShadow( (QFrame::Shadow)(int)v );
451
d->frame->repaintBackground();
454
int FancyLabel::lineWidth () const
456
return d->frame->lineWidth();
459
void FancyLabel::setLineWidth (int v)
461
d->frame->setLineWidth(v);
462
d->frame->repaintBackground();
465
int FancyLabel::midLineWidth () const
467
return d->frame->midLineWidth();
470
void FancyLabel::setMidLineWidth (int v)
472
d->frame->setMidLineWidth(v);
473
d->frame->repaintBackground();
476
void FancyLabel::setScaledContents(int width, int height)
478
d->pix->setScaledContents(width, height);
481
void FancyLabel::setSmallFontSize(int s)
483
Private::smallFontSize = s;
486
#include "fancylabel.moc"