~ubuntu-branches/debian/lenny/italc/lenny

« back to all changes in this revision

Viewing changes to ima/src/tool_button.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Winnertz
  • Date: 2008-06-17 13:46:54 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080617134654-cl0gi4u524cv1ici
Tags: 1:1.0.9~rc3-1
* Package new upstream version
  - upstream ported the code to qt4.4 (Closes: #481974)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * tool_button.cpp - implementation of iTALC-tool-button
 
3
 *
 
4
 * Copyright (c) 2006-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
 
5
 * 
 
6
 * This file is part of iTALC - http://italc.sourceforge.net
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public
 
10
 * License as published by the Free Software Foundation; either
 
11
 * version 2 of the License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public
 
19
 * License along with this program (see COPYING); if not, write to the
 
20
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
21
 * Boston, MA 02111-1307, USA.
 
22
 *
 
23
 */
 
24
 
 
25
 
 
26
#include <QtCore/QTimer>
 
27
#include <QtGui/QAction>
 
28
#include <QtGui/QApplication>
 
29
#include <QtGui/QBitmap>
 
30
#include <QtGui/QDesktopWidget>
 
31
#include <QtGui/QLabel>
 
32
#include <QtGui/QLayout>
 
33
#include <QtGui/QLinearGradient>
 
34
#include <QtGui/QPainter>
 
35
#include <QtGui/QToolBar>
 
36
 
 
37
#include "tool_button.h"
 
38
#include "fast_qimage.h"
 
39
 
 
40
 
 
41
 
 
42
const int MARGIN = 10;
 
43
const int ROUNDED = 2000;
 
44
 
 
45
bool toolButton::s_toolTipsDisabled = FALSE;
 
46
bool toolButton::s_iconOnlyMode = FALSE;
 
47
 
 
48
 
 
49
toolButton::toolButton( const QPixmap & _pixmap, const QString & _label,
 
50
                                const QString & _alt_label,
 
51
                                const QString & _title, 
 
52
                                const QString & _desc, QObject * _receiver, 
 
53
                                const char * _slot, QWidget * _parent ) :
 
54
        QToolButton( _parent ),
 
55
        m_pixmap( _pixmap ),
 
56
        m_img( fastQImage( _pixmap.toImage() ).scaled( 32, 32 ) ),
 
57
        m_colorizeLevel( 0 ),
 
58
        m_fadeBack( FALSE ),
 
59
        m_label( _label ),
 
60
        m_altLabel( _alt_label ),
 
61
        m_title( _title ),
 
62
        m_descr( _desc )
 
63
{
 
64
        setAttribute( Qt::WA_NoSystemBackground, true );
 
65
        setText( m_title );
 
66
 
 
67
        updateSize();
 
68
 
 
69
        if( _receiver != NULL && _slot != NULL )
 
70
        {
 
71
                connect( this, SIGNAL( clicked() ), _receiver, _slot );
 
72
        }
 
73
 
 
74
}
 
75
 
 
76
 
 
77
 
 
78
 
 
79
toolButton::toolButton( QAction * _a, const QString & _label,
 
80
                                const QString & _alt_label,
 
81
                                const QString & _desc, QObject * _receiver, 
 
82
                                const char * _slot, QWidget * _parent ) :
 
83
        QToolButton( _parent ),
 
84
        m_pixmap( _a->icon().pixmap( 128, 128 ) ),
 
85
        m_img( fastQImage( m_pixmap.toImage() ).scaled( 32, 32 ) ),
 
86
        m_colorizeLevel( 0 ),
 
87
        m_fadeBack( FALSE ),
 
88
        m_label( _label ),
 
89
        m_altLabel( _alt_label ),
 
90
        m_title( _a->text() ),
 
91
        m_descr( _desc )
 
92
{
 
93
        setAttribute( Qt::WA_NoSystemBackground, true );
 
94
        setText( m_title );
 
95
 
 
96
        updateSize();
 
97
 
 
98
        if( _receiver != NULL && _slot != NULL )
 
99
        {
 
100
                connect( this, SIGNAL( clicked() ), _receiver, _slot );
 
101
                connect( _a, SIGNAL( triggered( bool ) ), _receiver, _slot );
 
102
        }
 
103
 
 
104
}
 
105
 
 
106
 
 
107
 
 
108
 
 
109
toolButton::~toolButton()
 
110
{
 
111
}
 
112
 
 
113
 
 
114
 
 
115
 
 
116
void toolButton::setIconOnlyMode( bool _enabled )
 
117
{
 
118
        s_iconOnlyMode = _enabled;
 
119
        QList<toolButton *> tbl = QApplication::activeWindow()->findChildren<toolButton *>();
 
120
        foreach( toolButton * tb, tbl )
 
121
        {
 
122
                tb->updateSize();
 
123
        }
 
124
}
 
125
 
 
126
 
 
127
 
 
128
 
 
129
void toolButton::addTo( QToolBar * _tb )
 
130
{
 
131
        QAction * a = _tb->addWidget( this );
 
132
        a->setText( text() );
 
133
}
 
134
 
 
135
 
 
136
 
 
137
 
 
138
void toolButton::enterEvent( QEvent * _e )
 
139
{
 
140
        m_fadeBack = FALSE;
 
141
        if( m_colorizeLevel == 0 )
 
142
        {
 
143
                updateColorLevel();
 
144
        }
 
145
        if( !s_toolTipsDisabled && !m_title.isEmpty() && !m_descr.isEmpty() )
 
146
        {
 
147
                QPoint p = mapToGlobal( QPoint( 0, 0 ) );
 
148
                int scr = QApplication::desktop()->isVirtualDesktop() ?
 
149
                                QApplication::desktop()->screenNumber( p ) :
 
150
                                QApplication::desktop()->screenNumber( this );
 
151
 
 
152
#ifdef Q_WS_MAC
 
153
                QRect screen = QApplication::desktop()->availableGeometry(
 
154
                                                                        scr );
 
155
#else
 
156
                QRect screen = QApplication::desktop()->screenGeometry( scr );
 
157
#endif
 
158
 
 
159
                toolButtonTip * tbt = new toolButtonTip( m_pixmap, m_title,
 
160
                                                        m_descr,
 
161
                                QApplication::desktop()->screen( scr ), this );
 
162
                connect( this, SIGNAL( mouseLeftButton() ),
 
163
                                                        tbt, SLOT( close() ) );
 
164
 
 
165
                if( p.x() + tbt->width() > screen.x() + screen.width() )
 
166
                        p.rx() -= 4;// + tbt->width();
 
167
                if( p.y() + tbt->height() > screen.y() + screen.height() )
 
168
                        p.ry() -= 30 + tbt->height();
 
169
                if( p.y() < screen.y() )
 
170
                        p.setY( screen.y() );
 
171
                if( p.x() + tbt->width() > screen.x() + screen.width() )
 
172
                        p.setX( screen.x() + screen.width() - tbt->width() );
 
173
                if( p.x() < screen.x() )
 
174
                        p.setX( screen.x() );
 
175
                if( p.y() + tbt->height() > screen.y() + screen.height() )
 
176
                        p.setY( screen.y() + screen.height() - tbt->height() );
 
177
                tbt->move( p += QPoint( -4, 46 ) );
 
178
                tbt->show();
 
179
        }
 
180
        QToolButton::enterEvent( _e );
 
181
}
 
182
 
 
183
 
 
184
 
 
185
 
 
186
void toolButton::leaveEvent( QEvent * _e )
 
187
{
 
188
        if( checkForLeaveEvent() )
 
189
        {
 
190
                QToolButton::leaveEvent( _e );
 
191
        }
 
192
}
 
193
 
 
194
 
 
195
 
 
196
 
 
197
void toolButton::mousePressEvent( QMouseEvent * _me )
 
198
{
 
199
        emit mouseLeftButton();
 
200
        QToolButton::mousePressEvent( _me );
 
201
}
 
202
 
 
203
 
 
204
 
 
205
 
 
206
void toolButton::paintEvent( QPaintEvent * _pe )
 
207
{
 
208
        const bool active = isDown() || isChecked();
 
209
        const QColor ctbl[2][4] = {
 
210
                                {
 
211
                                        QColor( 80, 160, 255 ),
 
212
                                        QColor( 32, 64, 192 ),
 
213
                                        QColor( 8, 16, 96 ),
 
214
                                        QColor( 0, 64, 224 )
 
215
                                },
 
216
                                {
 
217
                                        QColor( 255, 255, 64, m_colorizeLevel ),
 
218
                                        QColor( 192, 160, 32, m_colorizeLevel ),
 
219
                                        QColor( 96, 48, 0, m_colorizeLevel ),
 
220
                                        QColor( 192, 160, 0, m_colorizeLevel )
 
221
                                }
 
222
                                } ;
 
223
 
 
224
        QPainter p( this );
 
225
        p.fillRect( rect(), Qt::black );
 
226
        p.setRenderHint( QPainter::Antialiasing );
 
227
        QLinearGradient lingrad( 0, 0, 0, height() );
 
228
        lingrad.setColorAt( 0, ctbl[0][0] );
 
229
        lingrad.setColorAt( 0.38, ctbl[0][1] );
 
230
        lingrad.setColorAt( 0.42, ctbl[0][2] );
 
231
        lingrad.setColorAt( 1, ctbl[0][3] );
 
232
        p.setBrush( lingrad );
 
233
        p.drawRoundRect( 1, 1, width()-2, height()-2, 1000 / width(),
 
234
                                                        1000 / height() );
 
235
 
 
236
        if( m_colorizeLevel )
 
237
        {
 
238
                lingrad = QLinearGradient( 0, 0, 0, height() );
 
239
                lingrad.setColorAt( 0, ctbl[1][0] );
 
240
                lingrad.setColorAt( 0.38, ctbl[1][1] );
 
241
                lingrad.setColorAt( 0.42, ctbl[1][2] );
 
242
                lingrad.setColorAt( 1, ctbl[1][3] );
 
243
                p.setBrush( lingrad );
 
244
                p.drawRoundRect( 1, 1, width()-2, height()-2, 1000 / width(),
 
245
                                                        1000 / height() );
 
246
        }
 
247
        p.setBrush( QBrush() );
 
248
 
 
249
        p.fillRect( rect(), QColor( 0, 0, 0, active ? 64 : 0 ) );
 
250
 
 
251
        QPen pen( QColor( 255, 255, 255, 96 ) );
 
252
        pen.setWidthF( 1.4f );
 
253
        p.setPen( pen );
 
254
        p.drawRoundRect( 0, 0, width()-1, height()-1, 1000 / width(),
 
255
                                                        1000 / height() );
 
256
        QPen pen2 = pen;
 
257
        pen.setColor( QColor( 0, 0, 0, 128 ) );
 
258
        p.setPen( pen );
 
259
        p.drawRoundRect( 1, 1, width()-3, height()-3, 1000 / width(),
 
260
                                                        1000 / height() );
 
261
        p.setPen( pen2 );
 
262
        p.drawRoundRect( 2, 2, width()-2, height()-2, 1000 / width(),
 
263
                                                        1000 / height() );
 
264
 
 
265
        const int dd = isDown() ? 1 : 0;
 
266
        QPoint pt = QPoint( ( width() - m_img.width() ) / 2 + dd, 3 + dd );
 
267
        if( s_iconOnlyMode )
 
268
        {
 
269
                pt.setY( ( height() - m_img.height() ) / 2 - 1 + dd );
 
270
        }
 
271
        p.drawImage( pt, m_img );
 
272
 
 
273
        if( s_iconOnlyMode )
 
274
        {
 
275
                return;
 
276
        }
 
277
        const QString l = ( active && m_altLabel.isEmpty() == FALSE ) ?
 
278
                                                        m_altLabel : m_label;
 
279
        const int w = p.fontMetrics().width( l );
 
280
        p.setPen( Qt::black );
 
281
        p.drawText( ( width() - w ) / 2 +1+dd, height() - 4+dd, l );
 
282
        p.setPen( Qt::white );
 
283
        p.drawText( ( width() - w ) / 2 +dd, height() - 5+dd, l );
 
284
}
 
285
 
 
286
 
 
287
 
 
288
 
 
289
bool toolButton::checkForLeaveEvent( void )
 
290
{
 
291
        if( QRect( mapToGlobal( QPoint( 0, 0 ) ), size() ).
 
292
                                        contains( QCursor::pos() ) )
 
293
        {
 
294
                QTimer::singleShot( 20, this, SLOT( checkForLeaveEvent() ) );
 
295
        }
 
296
        else
 
297
        {
 
298
                emit mouseLeftButton();
 
299
                m_fadeBack = TRUE;
 
300
                if( m_colorizeLevel == 255 )
 
301
                {
 
302
                        updateColorLevel();
 
303
                }
 
304
                return( TRUE );
 
305
        }
 
306
        return( FALSE );
 
307
}
 
308
 
 
309
 
 
310
 
 
311
 
 
312
void toolButton::updateColorLevel( void )
 
313
{
 
314
        bool again;
 
315
        if( m_fadeBack )
 
316
        {
 
317
                m_colorizeLevel = qMax( 0, m_colorizeLevel - 10 );
 
318
                again = m_colorizeLevel > 0;
 
319
        }
 
320
        else
 
321
        {
 
322
                m_colorizeLevel = qMin( 255, m_colorizeLevel + 10 );
 
323
                again = m_colorizeLevel < 255;
 
324
        }
 
325
        update();
 
326
        if( again )
 
327
        {
 
328
                QTimer::singleShot( 10, this, SLOT( updateColorLevel() ) );
 
329
        }
 
330
}
 
331
 
 
332
 
 
333
 
 
334
 
 
335
void toolButton::updateSize( void )
 
336
{
 
337
        QFont f = font();
 
338
        f.setPointSizeF( 7.5 );
 
339
        setFont( f );
 
340
 
 
341
        if( s_iconOnlyMode )
 
342
        {
 
343
                setFixedSize( 52, 48 );
 
344
        }
 
345
        else if( m_label.size() > 14 || m_altLabel.size() > 14 )
 
346
        {
 
347
                setFixedSize( 96, 48 );
 
348
        }
 
349
        else
 
350
        {
 
351
                setFixedSize( 88, 48 );
 
352
        }
 
353
 
 
354
        // set mask
 
355
        QBitmap b( size() );
 
356
        b.clear();
 
357
 
 
358
        QPainter p( &b );
 
359
        p.setBrush( Qt::color1 );
 
360
        p.setPen( Qt::color1 );
 
361
        p.drawRoundRect( 0, 0, width() - 1, height() - 1,
 
362
                                        1000 / width(), 1000 / height() );
 
363
        setMask( b );
 
364
}
 
365
 
 
366
 
 
367
 
 
368
 
 
369
 
 
370
 
 
371
 
 
372
toolButtonTip::toolButtonTip( const QPixmap & _pixmap, const QString & _title,
 
373
                                const QString & _description,
 
374
                                QWidget * _parent, QWidget * _tool_btn ) :
 
375
        QWidget( _parent, Qt::ToolTip ),
 
376
        m_icon( fastQImage( _pixmap ).scaled( 72, 72 ) ),
 
377
        m_title( _title ),
 
378
        m_description( _description ),
 
379
        m_dissolveSize( 24 ),
 
380
        m_toolButton( _tool_btn )
 
381
{
 
382
        setAttribute( Qt::WA_DeleteOnClose, TRUE );
 
383
        setAttribute( Qt::WA_NoSystemBackground, TRUE );
 
384
 
 
385
        resize( sizeHint() );
 
386
        updateMask();
 
387
}
 
388
 
 
389
 
 
390
 
 
391
 
 
392
QSize toolButtonTip::sizeHint( void ) const
 
393
{
 
394
        QFont f = font();
 
395
        f.setBold( TRUE );
 
396
        int title_w = QFontMetrics( f ).width( m_title );
 
397
        QRect desc_rect = fontMetrics().boundingRect( QRect( 0, 0, 250, 100 ),
 
398
                                        Qt::TextWordWrap, m_description );
 
399
 
 
400
        return QSize( MARGIN + m_icon.width() + MARGIN +
 
401
                                qMax( title_w, desc_rect.width() ) + MARGIN,
 
402
                        MARGIN + qMax( m_icon.height(), fontMetrics().height() +
 
403
                                                MARGIN + desc_rect.height() ) +
 
404
                                                                MARGIN );
 
405
}
 
406
 
 
407
 
 
408
 
 
409
 
 
410
void toolButtonTip::paintEvent( QPaintEvent * _pe )
 
411
{
 
412
        QPainter p( this );
 
413
        p.drawImage( 0, 0, m_bg );
 
414
}
 
415
 
 
416
 
 
417
 
 
418
 
 
419
void toolButtonTip::resizeEvent( QResizeEvent * _re )
 
420
{
 
421
        const QColor color_frame = QColor( 48, 48, 48 );
 
422
        m_bg = QImage( size(), QImage::Format_ARGB32 );
 
423
        m_bg.fill( color_frame.rgba() );
 
424
        QPainter p( &m_bg );
 
425
        p.setRenderHint( QPainter::Antialiasing );
 
426
        QPen pen( color_frame );
 
427
        pen.setWidthF( 1.5 );
 
428
        p.setPen( pen );
 
429
        QLinearGradient grad( 0, 0, 0, height() );
 
430
        const QColor color_top = palette().color( QPalette::Active,
 
431
                                                QPalette::Window ).light( 120 );
 
432
        grad.setColorAt( 0, color_top );
 
433
        grad.setColorAt( 1, palette().color( QPalette::Active,
 
434
                                                QPalette::Window ).
 
435
                                                        light( 80 ) );
 
436
        p.setBrush( grad );
 
437
        p.drawRoundRect( 0, 0, width() - 1, height() - 1,
 
438
                                        ROUNDED / width(), ROUNDED / height() );
 
439
        if( m_toolButton )
 
440
        {
 
441
                QPoint pt = m_toolButton->mapToGlobal( QPoint( 0, 0 ) );
 
442
                p.setPen( color_top );
 
443
                p.setBrush( color_top );
 
444
                p.setRenderHint( QPainter::Antialiasing, FALSE );
 
445
                p.drawLine( pt.x() - x(), 0,
 
446
                                pt.x() + m_toolButton->width() - x() - 2, 0 );
 
447
                const int dx = pt.x() - x();
 
448
                p.setRenderHint( QPainter::Antialiasing, TRUE );
 
449
                if( dx < 10 && dx >= 0 )
 
450
                {
 
451
                        p.setPen( pen );
 
452
                        p.drawImage( dx+1, 0, m_bg.copy( 20, 0, 10-dx, 10 ) );
 
453
                        p.drawImage( dx, 0, m_bg.copy( 0, 10, 1, 10-dx*2 ) );
 
454
                }
 
455
        }
 
456
        p.setPen( Qt::black );
 
457
 
 
458
        p.drawImage( MARGIN, MARGIN, m_icon );
 
459
        QFont f = p.font();
 
460
        f.setBold( TRUE );
 
461
        p.setFont( f );
 
462
        const int title_x = MARGIN + m_icon.width() + MARGIN;
 
463
        const int title_y = MARGIN + fontMetrics().height() - 2;
 
464
        p.drawText( title_x, title_y, m_title );
 
465
 
 
466
        f.setBold( FALSE );
 
467
        p.setFont( f );
 
468
        p.drawText( QRect( title_x, title_y + MARGIN,
 
469
                                        width() - MARGIN - title_x,
 
470
                                        height() - MARGIN - title_y ),
 
471
                                        Qt::TextWordWrap, m_description );
 
472
 
 
473
        updateMask();
 
474
        QWidget::resizeEvent( _re );
 
475
}
 
476
 
 
477
 
 
478
 
 
479
 
 
480
void toolButtonTip::updateMask( void )
 
481
{
 
482
        // as this widget has not a rectangular shape AND is a top
 
483
        // level widget (which doesn't allow painting only particular
 
484
        // regions), we have to set a mask for it
 
485
        QBitmap b( size() );
 
486
        b.clear();
 
487
 
 
488
        QPainter p( &b );
 
489
        p.setBrush( Qt::color1 );
 
490
        p.setPen( Qt::color1 );
 
491
        p.drawRoundRect( 0, 0, width() - 1, height() - 1,
 
492
                                        ROUNDED / width(), ROUNDED / height() );
 
493
 
 
494
        if( m_toolButton )
 
495
        {
 
496
                QPoint pt = m_toolButton->mapToGlobal( QPoint( 0, 0 ) );
 
497
                const int dx = pt.x()-x();
 
498
                if( dx < 10 && dx >= 0 )
 
499
                {
 
500
                        p.fillRect( dx, 0, 10, 10, Qt::color1 );
 
501
                }
 
502
        }
 
503
/*      p.setBrush( Qt::color0 );
 
504
        p.setPen( Qt::color0 );
 
505
 
 
506
        if( m_dissolveSize > 0 )
 
507
        {
 
508
                const int size = 16;
 
509
                for( Q_UINT16 y = 0; y < height() + size; y += size )
 
510
                {
 
511
                        Q_INT16 s = m_dissolveSize * width() / 128;
 
512
                        for( Q_INT16 x = width(); x > -size; x -= size, s -= 2 )
 
513
                        {
 
514
                                if( s < 0 )
 
515
                                {
 
516
                                        s = 0;
 
517
                                }
 
518
                                p.drawEllipse( x - s / 2, y - s / 2, s, s );
 
519
                        }
 
520
                }
 
521
        }*/
 
522
 
 
523
        setMask( b );
 
524
 
 
525
/*      if( --m_dissolveSize >= 0 )
 
526
        {
 
527
                QTimer::singleShot( 20, this, SLOT( updateMask() ) );
 
528
        }*/
 
529
}
 
530
 
 
531
 
 
532
 
 
533
 
 
534
#include "tool_button.moc"