source: psi/trunk/libpsi/psiwidgets/fancylabel.cpp@ 150

Last change on this file since 150 was 2, checked in by dmik, 19 years ago

Imported original Psi 0.10 sources from Affinix

File size: 9.7 KB
Line 
1/*
2 * fancylabel.cpp - the FancyLabel widget
3 * Copyright (C) 2003 Michail Pishchagin
4 *
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.
9 *
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.
14 *
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
18 *
19 */
20
21#include "fancylabel.h"
22
23#include <qpixmap.h>
24#include <qcolor.h>
25#include <qlayout.h>
26#include <qlabel.h>
27#include <qframe.h>
28#include <qpainter.h>
29#include <qobjectlist.h>
30
31#ifndef WIDGET_PLUGIN
32#include "iconset.h"
33#endif
34
35//----------------------------------------------------------------------------
36// IconLabel
37//----------------------------------------------------------------------------
38
39class IconLabel::Private : public QObject
40{
41 Q_OBJECT
42
43public:
44
45 IconLabel *label;
46 Icon *icon;
47 bool copyIcon;
48#ifdef WIDGET_PLUGIN
49 QString iconName;
50#endif
51
52 Private(IconLabel *l)
53 {
54 label = l;
55 icon = 0;
56 }
57
58 ~Private()
59 {
60 stopIcon();
61#ifndef WIDGET_PLUGIN
62 if ( icon && copyIcon )
63 delete icon;
64#endif
65 }
66
67 void setIcon(const Icon *i, bool _copyIcon)
68 {
69 copyIcon = _copyIcon;
70
71 stopIcon();
72#ifndef WIDGET_PLUGIN
73 if ( i && copyIcon )
74 icon = new Icon(*i);
75 else
76 icon = (Icon *)i;
77#endif
78 startIcon();
79 }
80
81protected:
82 void stopIcon()
83 {
84#ifndef WIDGET_PLUGIN
85 if ( icon ) {
86 disconnect(icon, 0, this, 0);
87 icon->stop();
88 }
89#endif
90 }
91
92 void startIcon()
93 {
94#ifndef WIDGET_PLUGIN
95 if ( icon ) {
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() );
99 }
100 else
101 iconUpdated( QPixmap() );
102#endif
103 }
104
105private slots:
106 void iconUpdated(const QPixmap &pix)
107 {
108 label->setPixmap(pix);
109 }
110};
111
112IconLabel::IconLabel(QWidget *parent, const char *name)
113: QLabel(parent, name, WRepaintNoErase | WResizeNoErase)
114{
115 d = new Private(this);
116}
117
118IconLabel::~IconLabel()
119{
120 delete d;
121}
122
123void IconLabel::drawContents (QPainter *paint)
124{
125 const QPixmap *background = paletteBackgroundPixmap();
126 QRect r = contentsRect();
127 QPoint start( r.topLeft() );
128
129 QWidget *pw = (QWidget *)parent();
130 if ( pw && (!background || pw->inherits("MyFancyFrame")) ) { // nasty hack
131 background = pw->paletteBackgroundPixmap();
132 start = mapToParent( r.topLeft() );
133 }
134
135 if ( !background ) {
136 erase(0, 0, width(), height());
137 QLabel::drawContents(paint);
138 return;
139 }
140
141 // try to eliminate flicker
142 QPixmap pix(r.width(), r.height());
143 QPainter p;
144 p.begin(&pix);
145 p.drawTiledPixmap (r, *background, start);
146 QLabel::drawContents ( &p );
147 p.end();
148
149 paint->drawPixmap (r.topLeft(), pix );
150}
151
152const Icon *IconLabel::icon () const
153{
154 return d->icon;
155}
156
157const QString &IconLabel::iconName () const
158{
159#ifndef WIDGET_PLUGIN
160 if ( d->icon )
161 return d->icon->name();
162 return QString::null;
163#else
164 return d->iconName;
165#endif
166}
167
168void IconLabel::setIcon (const Icon *i, bool copyIcon)
169{
170 d->setIcon(i, copyIcon);
171}
172
173void IconLabel::setIcon (const QString &name)
174{
175#ifndef WIDGET_PLUGIN
176 setIcon( IconsetFactory::iconPtr(name) );
177#else
178 d->iconName = name;
179 setText("<qt>icon:<br><small>" + name + "</small></qt>");
180#endif
181}
182
183void IconLabel::setIcon(const QPixmap &p)
184{
185 QLabel::setIcon(p);
186}
187
188void IconLabel::setScaledContents(int width, int height)
189{
190 QLabel::setScaledContents(true);
191 setMinimumSize(width, height);
192 setMaximumSize(width, height);
193}
194
195//----------------------------------------------------------------------------
196// MyFancyFrame -- internal
197//----------------------------------------------------------------------------
198
199class MyFancyFrame : public QFrame
200{
201 Q_OBJECT
202protected:
203 void drawContents (QPainter *paint)
204 {
205 paint->drawPixmap (contentsRect().topLeft(), background);
206 }
207
208 void resizeEvent (QResizeEvent *e)
209 {
210 QFrame::resizeEvent (e);
211
212 QRect rect = contentsRect();
213 int w = rect.width();
214
215 if ( rect.height() <= 0 || w <= 0 )
216 return; // avoid crash
217
218 int r1, g1, b1;
219 from->rgb (&r1, &g1, &b1);
220 int r2, g2, b2;
221 to->rgb (&r2, &g2, &b2);
222
223 float stepR = (float)(r2 - r1) / w;
224 float stepG = (float)(g2 - g1) / w;
225 float stepB = (float)(b2 - b1) / w;
226
227 QPixmap pix (rect.width(), rect.height());
228 QPainter p;
229 p.begin (&pix);
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);
234
235 p.setPen ( QColor( r, g, b ) );
236 p.drawLine ( i, 0, i, rect.height() );
237 }
238 p.end ();
239
240 QObjectList *l = queryList( "QLabel" );
241 QObjectListIt it( *l );
242 while ( it.current() ) {
243 QLabel *child = (QLabel *)it.current();
244 child->update();
245
246 ++it;
247 }
248 delete l;
249
250 setUpdatesEnabled(false);
251 setPaletteBackgroundPixmap(pix);
252 setUpdatesEnabled(true);
253
254 background = pix;
255 update ();
256 }
257
258private:
259 QColor *from, *to;
260 QPixmap background;
261
262public:
263 MyFancyFrame (QWidget *parent, QColor *_from, QColor *_to, const char *name = 0, WFlags f = 0)
264 : QFrame (parent, name, f)
265 {
266 from = _from;
267 to = _to;
268 }
269
270 void repaintBackground()
271 {
272 QResizeEvent e( size(), size() );
273 resizeEvent( &e );
274 }
275};
276
277//----------------------------------------------------------------------------
278// FancyLabel
279//----------------------------------------------------------------------------
280
281class FancyLabel::Private : public QObject
282{
283public:
284 MyFancyFrame *frame;
285 IconLabel *text, *help, *pix;
286 QColor from, to, font;
287 QString textStr, helpStr;
288 static int smallFontSize;
289
290 Private (FancyLabel *parent)
291 : QObject(parent), from(72, 172, 243), to(255, 255, 255), font(0, 0, 0)
292 {
293 QHBoxLayout *mainbox = new QHBoxLayout( parent, 0, 0 );
294
295 frame = new MyFancyFrame ( parent, &from, &to, "fancy_frame" );
296 frame->setFrameShape( QFrame::StyledPanel );
297 frame->setFrameShadow( QFrame::Raised );
298
299 QHBoxLayout *frameLayout = new QHBoxLayout( frame, 3, 0 );
300 QVBoxLayout *layout = new QVBoxLayout( 0, 0, 0 );
301 frameLayout->addLayout( layout );
302
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 );
306
307 help = new IconLabel( frame, "help_label" );
308 layout->addWidget( help );
309
310 QFont font = help->font();
311 font.setPointSize(smallFontSize);
312 help->setFont(font);
313
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 );
317
318 mainbox->addWidget( frame );
319 }
320};
321
322int FancyLabel::Private::smallFontSize = 0;
323
324FancyLabel::FancyLabel (QWidget *parent, const char *name)
325: QWidget (parent, name)
326{
327 d = new Private (this);
328}
329
330FancyLabel::~FancyLabel ()
331{
332}
333
334void FancyLabel::setText (const QString &text)
335{
336 d->textStr = text;
337 d->text->setText (QString("<font color=\"%1\"><b>").arg(d->font.name()) + text + "</b></font>");
338}
339
340void FancyLabel::setHelp (const QString &help)
341{
342 d->helpStr = help;
343
344 QString f1 = "<small>";
345 QString f2 = "</small>";
346 if ( d->smallFontSize ) {
347 f1 = "<font>";
348 f2 = "</font>";
349 }
350
351 d->help->setText (QString("<font color=\"%1\">").arg(d->font.name()) + f1 + help + f2 + "</font>");
352}
353
354void FancyLabel::setPixmap (const QPixmap &pix)
355{
356 d->pix->setPixmap( pix );
357}
358
359void FancyLabel::setColorFrom (const QColor &col)
360{
361 d->from = col;
362 d->frame->repaintBackground();
363}
364
365void FancyLabel::setColorTo (const QColor &col)
366{
367 d->to = col;
368 d->frame->repaintBackground();
369}
370
371void FancyLabel::setColorFont (const QColor &col)
372{
373 d->font = col;
374 d->frame->repaintBackground();
375}
376
377const QString &FancyLabel::text () const
378{
379 return d->textStr;
380}
381
382const QString &FancyLabel::help () const
383{
384 return d->helpStr;
385}
386
387const QPixmap *FancyLabel::pixmap () const
388{
389 return d->pix->pixmap();
390}
391
392const QColor &FancyLabel::colorFrom () const
393{
394 return d->from;
395}
396
397const QColor &FancyLabel::colorTo () const
398{
399 return d->to;
400}
401
402const QColor &FancyLabel::colorFont () const
403{
404 return d->font;
405}
406
407const Icon *FancyLabel::icon () const
408{
409 return d->pix->icon();
410}
411
412void FancyLabel::setIcon (const Icon *i)
413{
414 d->pix->setIcon (i);
415}
416
417void FancyLabel::setIcon (const QString &name)
418{
419 d->pix->setIcon(name);
420}
421
422void FancyLabel::setIcon(const QPixmap &p)
423{
424 QWidget::setIcon(p);
425}
426
427const QString &FancyLabel::iconName () const
428{
429 return d->pix->iconName();
430}
431
432FancyLabel::Shape FancyLabel::frameShape () const
433{
434 return (FancyLabel::Shape)(int)d->frame->frameShape();
435}
436
437void FancyLabel::setFrameShape (FancyLabel::Shape v)
438{
439 d->frame->setFrameShape( (QFrame::Shape)(int)v );
440 d->frame->repaintBackground();
441}
442
443FancyLabel::Shadow FancyLabel::frameShadow () const
444{
445 return (FancyLabel::Shadow)(int)d->frame->frameShadow();
446}
447
448void FancyLabel::setFrameShadow (FancyLabel::Shadow v)
449{
450 d->frame->setFrameShadow( (QFrame::Shadow)(int)v );
451 d->frame->repaintBackground();
452}
453
454int FancyLabel::lineWidth () const
455{
456 return d->frame->lineWidth();
457}
458
459void FancyLabel::setLineWidth (int v)
460{
461 d->frame->setLineWidth(v);
462 d->frame->repaintBackground();
463}
464
465int FancyLabel::midLineWidth () const
466{
467 return d->frame->midLineWidth();
468}
469
470void FancyLabel::setMidLineWidth (int v)
471{
472 d->frame->setMidLineWidth(v);
473 d->frame->repaintBackground();
474}
475
476void FancyLabel::setScaledContents(int width, int height)
477{
478 d->pix->setScaledContents(width, height);
479}
480
481void FancyLabel::setSmallFontSize(int s)
482{
483 Private::smallFontSize = s;
484}
485
486#include "fancylabel.moc"
Note: See TracBrowser for help on using the repository browser.