2
* tool_button.cpp - implementation of iTALC-tool-button
4
* Copyright (c) 2006-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
6
* This file is part of iTALC - http://italc.sourceforge.net
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.
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.
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.
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>
37
#include "tool_button.h"
38
#include "fast_qimage.h"
42
const int MARGIN = 10;
43
const int ROUNDED = 2000;
45
bool toolButton::s_toolTipsDisabled = FALSE;
46
bool toolButton::s_iconOnlyMode = FALSE;
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 ),
56
m_img( fastQImage( _pixmap.toImage() ).scaled( 32, 32 ) ),
60
m_altLabel( _alt_label ),
64
setAttribute( Qt::WA_NoSystemBackground, true );
69
if( _receiver != NULL && _slot != NULL )
71
connect( this, SIGNAL( clicked() ), _receiver, _slot );
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 ) ),
89
m_altLabel( _alt_label ),
90
m_title( _a->text() ),
93
setAttribute( Qt::WA_NoSystemBackground, true );
98
if( _receiver != NULL && _slot != NULL )
100
connect( this, SIGNAL( clicked() ), _receiver, _slot );
101
connect( _a, SIGNAL( triggered( bool ) ), _receiver, _slot );
109
toolButton::~toolButton()
116
void toolButton::setIconOnlyMode( bool _enabled )
118
s_iconOnlyMode = _enabled;
119
QList<toolButton *> tbl = QApplication::activeWindow()->findChildren<toolButton *>();
120
foreach( toolButton * tb, tbl )
129
void toolButton::addTo( QToolBar * _tb )
131
QAction * a = _tb->addWidget( this );
132
a->setText( text() );
138
void toolButton::enterEvent( QEvent * _e )
141
if( m_colorizeLevel == 0 )
145
if( !s_toolTipsDisabled && !m_title.isEmpty() && !m_descr.isEmpty() )
147
QPoint p = mapToGlobal( QPoint( 0, 0 ) );
148
int scr = QApplication::desktop()->isVirtualDesktop() ?
149
QApplication::desktop()->screenNumber( p ) :
150
QApplication::desktop()->screenNumber( this );
153
QRect screen = QApplication::desktop()->availableGeometry(
156
QRect screen = QApplication::desktop()->screenGeometry( scr );
159
toolButtonTip * tbt = new toolButtonTip( m_pixmap, m_title,
161
QApplication::desktop()->screen( scr ), this );
162
connect( this, SIGNAL( mouseLeftButton() ),
163
tbt, SLOT( close() ) );
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 ) );
180
QToolButton::enterEvent( _e );
186
void toolButton::leaveEvent( QEvent * _e )
188
if( checkForLeaveEvent() )
190
QToolButton::leaveEvent( _e );
197
void toolButton::mousePressEvent( QMouseEvent * _me )
199
emit mouseLeftButton();
200
QToolButton::mousePressEvent( _me );
206
void toolButton::paintEvent( QPaintEvent * _pe )
208
const bool active = isDown() || isChecked();
209
const QColor ctbl[2][4] = {
211
QColor( 80, 160, 255 ),
212
QColor( 32, 64, 192 ),
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 )
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(),
236
if( m_colorizeLevel )
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(),
247
p.setBrush( QBrush() );
249
p.fillRect( rect(), QColor( 0, 0, 0, active ? 64 : 0 ) );
251
QPen pen( QColor( 255, 255, 255, 96 ) );
252
pen.setWidthF( 1.4f );
254
p.drawRoundRect( 0, 0, width()-1, height()-1, 1000 / width(),
257
pen.setColor( QColor( 0, 0, 0, 128 ) );
259
p.drawRoundRect( 1, 1, width()-3, height()-3, 1000 / width(),
262
p.drawRoundRect( 2, 2, width()-2, height()-2, 1000 / width(),
265
const int dd = isDown() ? 1 : 0;
266
QPoint pt = QPoint( ( width() - m_img.width() ) / 2 + dd, 3 + dd );
269
pt.setY( ( height() - m_img.height() ) / 2 - 1 + dd );
271
p.drawImage( pt, m_img );
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 );
289
bool toolButton::checkForLeaveEvent( void )
291
if( QRect( mapToGlobal( QPoint( 0, 0 ) ), size() ).
292
contains( QCursor::pos() ) )
294
QTimer::singleShot( 20, this, SLOT( checkForLeaveEvent() ) );
298
emit mouseLeftButton();
300
if( m_colorizeLevel == 255 )
312
void toolButton::updateColorLevel( void )
317
m_colorizeLevel = qMax( 0, m_colorizeLevel - 10 );
318
again = m_colorizeLevel > 0;
322
m_colorizeLevel = qMin( 255, m_colorizeLevel + 10 );
323
again = m_colorizeLevel < 255;
328
QTimer::singleShot( 10, this, SLOT( updateColorLevel() ) );
335
void toolButton::updateSize( void )
338
f.setPointSizeF( 7.5 );
343
setFixedSize( 52, 48 );
345
else if( m_label.size() > 14 || m_altLabel.size() > 14 )
347
setFixedSize( 96, 48 );
351
setFixedSize( 88, 48 );
359
p.setBrush( Qt::color1 );
360
p.setPen( Qt::color1 );
361
p.drawRoundRect( 0, 0, width() - 1, height() - 1,
362
1000 / width(), 1000 / height() );
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 ) ),
378
m_description( _description ),
379
m_dissolveSize( 24 ),
380
m_toolButton( _tool_btn )
382
setAttribute( Qt::WA_DeleteOnClose, TRUE );
383
setAttribute( Qt::WA_NoSystemBackground, TRUE );
385
resize( sizeHint() );
392
QSize toolButtonTip::sizeHint( void ) const
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 );
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() ) +
410
void toolButtonTip::paintEvent( QPaintEvent * _pe )
413
p.drawImage( 0, 0, m_bg );
419
void toolButtonTip::resizeEvent( QResizeEvent * _re )
421
const QColor color_frame = QColor( 48, 48, 48 );
422
m_bg = QImage( size(), QImage::Format_ARGB32 );
423
m_bg.fill( color_frame.rgba() );
425
p.setRenderHint( QPainter::Antialiasing );
426
QPen pen( color_frame );
427
pen.setWidthF( 1.5 );
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,
437
p.drawRoundRect( 0, 0, width() - 1, height() - 1,
438
ROUNDED / width(), ROUNDED / height() );
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 )
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 ) );
456
p.setPen( Qt::black );
458
p.drawImage( MARGIN, MARGIN, m_icon );
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 );
468
p.drawText( QRect( title_x, title_y + MARGIN,
469
width() - MARGIN - title_x,
470
height() - MARGIN - title_y ),
471
Qt::TextWordWrap, m_description );
474
QWidget::resizeEvent( _re );
480
void toolButtonTip::updateMask( void )
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
489
p.setBrush( Qt::color1 );
490
p.setPen( Qt::color1 );
491
p.drawRoundRect( 0, 0, width() - 1, height() - 1,
492
ROUNDED / width(), ROUNDED / height() );
496
QPoint pt = m_toolButton->mapToGlobal( QPoint( 0, 0 ) );
497
const int dx = pt.x()-x();
498
if( dx < 10 && dx >= 0 )
500
p.fillRect( dx, 0, 10, 10, Qt::color1 );
503
/* p.setBrush( Qt::color0 );
504
p.setPen( Qt::color0 );
506
if( m_dissolveSize > 0 )
509
for( Q_UINT16 y = 0; y < height() + size; y += size )
511
Q_INT16 s = m_dissolveSize * width() / 128;
512
for( Q_INT16 x = width(); x > -size; x -= size, s -= 2 )
518
p.drawEllipse( x - s / 2, y - s / 2, s, s );
525
/* if( --m_dissolveSize >= 0 )
527
QTimer::singleShot( 20, this, SLOT( updateMask() ) );
534
#include "tool_button.moc"