summaryrefslogtreecommitdiffstats
path: root/src/k3bflatbutton.cpp
blob: 92001143d489f96d3ed689350bd980c6d14d6ac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 *
 * $Id: k3bflatbutton.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2005 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bflatbutton.h"
#include "k3bthememanager.h"
#include "k3bapplication.h"

#include <kaction.h>
#include <kiconloader.h>
#include <kglobal.h>

#include <tqpainter.h>
#include <tqtooltip.h>
#include <tqfontmetrics.h>
#include <tqpixmap.h>


K3bFlatButton::K3bFlatButton( TQWidget *tqparent, const char *name )
  : TQFrame( tqparent, name/*, WNoAutoErase*/ ),
    m_pressed(false)
{
  init();
}


K3bFlatButton::K3bFlatButton( const TQString& text, TQWidget *tqparent, const char *name )
  : TQFrame( tqparent, name/*, WNoAutoErase*/ ),
    m_pressed(false)
{
  init();
  setText( text );
}


K3bFlatButton::K3bFlatButton( KAction* a, TQWidget *tqparent, const char *name )
  : TQFrame( tqparent, name/*, WNoAutoErase*/ ),
    m_pressed(false)
{
  init();

  setText( a->text() );
  TQToolTip::add( this, a->toolTip() );
  setPixmap( KGlobal::iconLoader()->loadIcon( a->icon(), KIcon::NoGroup, 32 ) );
  connect( this, TQT_SIGNAL(clicked()), a, TQT_SLOT(activate()) );
}


K3bFlatButton::~K3bFlatButton() {}


void K3bFlatButton::init()
{
  setHover(false);
  setMargin(5);
  setFrameStyle( TQFrame::Box|TQFrame::Plain );

  connect( k3bappcore->themeManager(), TQT_SIGNAL(themeChanged()), this, TQT_SLOT(slotThemeChanged()) );
  connect( kapp, TQT_SIGNAL(appearanceChanged()), this, TQT_SLOT(slotThemeChanged()) );
  slotThemeChanged();
}


void K3bFlatButton::setText( const TQString& s )
{
  m_text = s;
  m_text.remove( '&' );

  update();
}


void K3bFlatButton::setPixmap( const TQPixmap& p )
{
  m_pixmap = p;
  update();
}


void K3bFlatButton::enterEvent( TQEvent* )
{
  setHover(true);
}


void K3bFlatButton::leaveEvent( TQEvent* )
{
  setHover(false);
}


void K3bFlatButton::mousePressEvent( TQMouseEvent* e )
{
  if( e->button() == Qt::LeftButton ) {
    emit pressed();
    m_pressed = true;
  }
  else
    e->ignore();
}


void K3bFlatButton::mouseReleaseEvent( TQMouseEvent* e )
{
  if( e->button() == Qt::LeftButton ) {
    if( m_pressed  )
      emit clicked();
    m_pressed = false;
  }
  else
    e->ignore();
}


void K3bFlatButton::setHover( bool b )
{
  if( b ) {
    setPaletteBackgroundColor( m_foreColor );
    setPaletteForegroundColor( m_backColor );
  } else {
    setPaletteBackgroundColor( m_backColor );
    setPaletteForegroundColor( m_foreColor );
  }

  m_hover = b;

  update();
}


TQSize K3bFlatButton::tqsizeHint() const
{
  // height: pixmap + 5 spacing + font height + frame width
  // width: max( pixmap, text) + frame width
  return TQSize( TQMAX( m_pixmap.width(), fontMetrics().width( m_text ) ) + frameWidth()*2, 
		m_pixmap.height() + fontMetrics().height() + 5 + frameWidth()*2 );
}


void K3bFlatButton::drawContents( TQPainter* p )
{
  TQRect rect = contentsRect();

//   if( m_hover )
//     p->fillRect( rect, m_foreColor );
//   else if( tqparentWidget() ) {
//     TQRect r( mapToParent( TQPoint(lineWidth(), lineWidth()) ), 
// 	     mapToParent( TQPoint(width()-2*lineWidth(), height()-2*lineWidth() )) );
    
//     tqparentWidget()->tqrepaint( r );
//   }

  p->save();

  TQRect textRect = fontMetrics().boundingRect( m_text );
  int textX = TQMAX( 0, ( rect.width() - textRect.width() ) / 2 );
  int textY = textRect.height();

  if( !m_pixmap.isNull() ) {
    p->translate( rect.left(), rect.top() );
    textX = TQMAX( textX, (m_pixmap.width() - textRect.width()) / 2 );
    textY += 5 + m_pixmap.height();

    int pixX = TQMAX( TQMAX( 0, (textRect.width() - m_pixmap.width()) / 2 ), 
		     ( rect.width() - m_pixmap.width() ) / 2 );
    p->drawPixmap( pixX, 0, m_pixmap );
    p->drawText( textX, textY, m_text ); 
  }
  else
    p->drawText( rect, TQt::AlignCenter, m_text );

  p->restore();
}


void K3bFlatButton::setColors( const TQColor& fore, const TQColor& back )
{
  m_foreColor = fore;
  m_backColor = back;

  setHover( m_hover );
}


void K3bFlatButton::slotThemeChanged()
{
  if( K3bTheme* theme = k3bappcore->themeManager()->currentTheme() ) {
    setColors( theme->foregroundColor(), theme->backgroundColor() );
  }
}

#include "k3bflatbutton.moc"