File: IconFactory.cpp

package info (click to toggle)
subcommander 2.0.0~b5p2-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,344 kB
  • sloc: cpp: 63,594; sh: 4,050; xml: 1,992; makefile: 1,134; ansic: 786; ruby: 251; lisp: 24
file content (153 lines) | stat: -rw-r--r-- 4,368 bytes parent folder | download | duplicates (4)
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
/* ====================================================================
 * Copyright (c) 2007-2008  Martin Hauner
 *                          http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */

// sc
#include "IconFactory.h"
#include "sublib/Utility.h"

// qt
#include <QtGui/QPixmap>
#include <QtGui/QImage>
#include <QtGui/QPainter>


/**
 * Create the "normal" @c QImage for the given @a iconImage. Just returns
 * the image as we get it from disk.
 */
static QPixmap loadPixmap( const QString& iconImage )
{
  return QPixmap( getIconDir() + iconImage );
}

/**
 * Create the "active" @c Pixmap for the given @a iconImage. The "active"
 * pixmap is created by making every pixel darker.
 */
static QPixmap createActive( const QPixmap& iconImage )
{
  QImage srcImage = iconImage.toImage();
  QSize  size     = srcImage.size();

  for( int y = 0; y < size.height(); y++ )
  {
    for( int x = 0; x < size.width(); x++ )
    {
      QRgb value = srcImage.pixel(x,y);
      int  alpha = qAlpha(value);

      if( !value && !alpha )
        continue;

      QColor c(value);
      c = c.dark(120);

      // darkening the color looses the alpha information so we have
      // to create the new rgb value by hand.
      QRgb newvalue = qRgba(c.red(),c.green(),c.blue(),alpha);
      srcImage.setPixel(x,y,newvalue);
    }
  }

  return QPixmap::fromImage(srcImage);
}

/**
 * Create the "disabled" @c Pixmap for the given @a iconImage. The "disabled"
 * pixmap is created by reducing the alpha value of each pixel to increase its
 * transparency.
 */
static QPixmap createDisabled( const QPixmap& iconImage )
{
  QImage srcImage   = iconImage.toImage();
  QImage srcImage32 = srcImage.convertToFormat(QImage::Format_ARGB32);
  QSize  size       = srcImage32.size();

  for( int y = 0; y < size.height(); y++ )
  {
    for( int x = 0; x < size.width(); x++ )
    {
      QRgb value = srcImage32.pixel(x,y);
      int  alpha = qAlpha(value);

      if( !value && !alpha )
        continue;

      QColor c(value);

      // => more or less greyscale
      int h, s, v;
      c.getHsv(&h,&s,&v);
      c.setHsv(h,15,v);

      // increase transparency
      double newalpha = alpha * 0.65;

      QRgb newvalue = qRgba(c.red(),c.green(),c.blue(),(int)newalpha);
      srcImage32.setPixel(x,y,newvalue);
    }
  }

  return QPixmap::fromImage(srcImage32);
}

/**
 * Create an @c Icon from the given @a iconImage. If an @a overlayImage is
 * given it is placed in the bottom right corner of the @a iconImage. 
 */
static QIcon createIcon( const QString& iconImage, const QString& overlayImage = QString() )
{
  QPixmap source = loadPixmap(iconImage);

  if( !overlayImage.isNull() )
  {
    QPixmap  overlay = loadPixmap(overlayImage);
    QPainter painter(&source);
    painter.drawPixmap( 16, 16, overlay );
  }

  QIcon icon;
  icon.addPixmap( source,                 QIcon::Normal );
  icon.addPixmap( createActive(source),   QIcon::Active );
  icon.addPixmap( createDisabled(source), QIcon::Disabled );
  return icon;
}

/*
 * Create an toggle @c Icon from the given @a iconImage. If an @a overlayImage
 * is given, it is placed in the bottom right corner of the @a iconImage as an
 * "On" marker. 
 */
static QIcon createToggleIcon( const QString& iconImage, const QString& overlayImage )
{
  QPixmap sourceOff = loadPixmap(iconImage);
  QPixmap sourceOn  = sourceOff.copy();

  QPixmap overlay = loadPixmap(overlayImage);
  QPainter painter(&sourceOn);
  painter.drawPixmap( 0, 0, overlay );

  QIcon icon;
  icon.addPixmap( sourceOff,                 QIcon::Normal, QIcon::Off );
  icon.addPixmap( sourceOn,                  QIcon::Normal, QIcon::On );
  icon.addPixmap( createActive(sourceOff),   QIcon::Active, QIcon::Off );
  icon.addPixmap( createActive(sourceOn),    QIcon::Active, QIcon::On );
  icon.addPixmap( createDisabled(sourceOff), QIcon::Disabled );
  return icon;
}

QIcon IconFactory::createIcon( const QString& iconImage, const QString& overlayImage )
{
  return ::createIcon( iconImage, overlayImage );
}

QIcon IconFactory::createToggleIcon( const QString& iconImage, const QString& overlayImage )
{
  return ::createToggleIcon( iconImage, overlayImage );
}