summaryrefslogtreecommitdiffstats
path: root/src/tastybutton.cpp
blob: 9f89c05bb1956ae748e79585a4a286440fa80a9f (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
/***************************************************************************
 *   Copyright (C) 2006 by Marco Martin   *
 *   notmart@gmail.com   *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "tastybutton.h"

#include <qimage.h>
#include <qpainter.h>
#include <qpalette.h>
#include <qcolor.h>
#include <kimageeffect.h>

TastyButton::TastyButton(QWidget *parent)
 : QToolButton(parent)
{
  iconEffect = new KIconEffect();
  iconEffect->init();
}


TastyButton::~TastyButton()
{
}

void TastyButton::drawButton( QPainter * p )
{
  //background image or pseudo transparency
  if( parentWidget()->erasePixmap() )
  {
    const QPixmap erasePix(size());
    QPainter buffPainter(&erasePix);
    buffPainter.drawPixmap(QPoint(0, 0), *parentWidget()->erasePixmap(), geometry());
    buffPainter.end();

    QImage eraseImg = erasePix.convertToImage();
    if( isDown() )
    {
      KImageEffect::fade(eraseImg, 0.25, black);
      p->drawPixmap(rect(), eraseImg );
    }
    else if( uses3D() )
    {
      KImageEffect::fade(eraseImg, 0.4, white);
      p->drawPixmap(rect(), eraseImg );
    }
    else
      p->drawPixmap(rect(), erasePix );

    //lightScore = 0 grey, >0 lighter <0 darker
    int lightScore = 0;
    //chacks the brightness of 10 pixels of the diagonal
    //we won't be 100% sure if this reflects the global brightess of the image
    //but it seems to work well
    int x = 0;
    int y = 0;
    while( x < eraseImg.width() && y < eraseImg.height() && x < 10 )
    {
      x=++y;
      //deciding the text color to use
      int h,s,v;
      QColor(eraseImg.pixel(x,y)).getHsv( h, s, v );
      // is purely empirical :)
      if( v > 140) lightScore++;
      else lightScore--;
    }
    if( lightScore < 0 )
      setPaletteForegroundColor(white);
    else
      setPaletteForegroundColor(black);
  }
  //simple background color
  else
  {
    if( isDown() )
    {
      p->fillRect(rect(), colorGroup().background().dark(120));
    }
    else if( uses3D() )
    {
      p->fillRect(rect(), colorGroup().background().light(120));
    }
    else
    {
      p->fillRect(rect(), colorGroup().background());
    }
  }

  drawButtonLabel(p);
}

void TastyButton::setIconSet(  QIconSet is )
{
   is.setPixmap(iconEffect->apply(is.pixmap(),
                                  (int)KIcon::Panel,
                                  (int)KIcon::ActiveState),
                                  QIconSet::Automatic,
                                  QIconSet::Active);

   QToolButton::setIconSet( is );
}