summaryrefslogtreecommitdiff
path: root/libqtaux/ocolorbutton.cpp
authormickeyl <mickeyl>2004-01-15 13:59:58 (UTC)
committer mickeyl <mickeyl>2004-01-15 13:59:58 (UTC)
commitdde789ef19fa3a3913805e452ac1e3400688e8a0 (patch) (unidiff)
treea33a0ce31750ad56cfd3362585d4e7b32ca0a456 /libqtaux/ocolorbutton.cpp
parent3c74c5343a8432c494ace71d94cca354c01ef1d3 (diff)
downloadopie-dde789ef19fa3a3913805e452ac1e3400688e8a0.zip
opie-dde789ef19fa3a3913805e452ac1e3400688e8a0.tar.gz
opie-dde789ef19fa3a3913805e452ac1e3400688e8a0.tar.bz2
more libopie1 --> libopie2 with namespace cleanups and code layout cleanups
Diffstat (limited to 'libqtaux/ocolorbutton.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libqtaux/ocolorbutton.cpp150
1 files changed, 150 insertions, 0 deletions
diff --git a/libqtaux/ocolorbutton.cpp b/libqtaux/ocolorbutton.cpp
new file mode 100644
index 0000000..d2ad873
--- a/dev/null
+++ b/libqtaux/ocolorbutton.cpp
@@ -0,0 +1,150 @@
1/*
2 This file is part of the Opie Project
3 Copyright (C) Robert Griebl <sandman@handhelds.org>
4 =. Copyright (C) The Opie Team <opie-devel@handhelds.org>
5 .=l.
6           .>+-=
7 _;:,     .>    :=|. This program is free software; you can
8.> <`_,   >  .   <= redistribute it and/or modify it under
9:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
10.="- .-=="i,     .._ License as published by the Free Software
11 - .   .-<_>     .<> Foundation; either version 2 of the License,
12     ._= =}       : or (at your option) any later version.
13    .%`+i>       _;_.
14    .i_,=:_.      -<s. This program is distributed in the hope that
15     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
16    : ..    .:,     . . . without even the implied warranty of
17    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
18  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
19..}^=.=       =       ; Library General Public License for more
20++=   -.     .`     .: details.
21 :     =  ...= . :.=-
22 -.   .:....=;==+<; You should have received a copy of the GNU
23  -_. . .   )=.  = Library General Public License along with
24    --        :-=` this library; see the file COPYING.LIB.
25 If not, write to the Free Software Foundation,
26 Inc., 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA.
28*/
29
30#include "ocolorpopupmenu.h"
31#include "ocolorbutton.h"
32
33/* OPIE */
34#include <qpe/resource.h>
35
36/* QT */
37#include <qcolor.h>
38#include <qpixmap.h>
39#include <qimage.h>
40
41using namespace Opie;
42
43struct OColorButtonPrivate
44{
45 QPopupMenu *m_menu;
46 QColor m_color;
47};
48
49/**
50 * This concstructs a Color Button with @param color as the start color
51 * It'll use a OColorPopupMenu internally
52 *
53 * @param parent The parent of the Color Button
54 * @param color The color from where to start on
55 * @param name @see QObject
56 */
57OColorButton::OColorButton ( QWidget *parent, const QColor &color, const char *name )
58 : QPushButton ( parent, name )
59{
60 d = new OColorButtonPrivate;
61
62 d-> m_menu = new OColorPopupMenu ( color, 0, 0 );
63 setPopup ( d-> m_menu );
64 //setPopupDelay ( 0 );
65 connect ( d-> m_menu, SIGNAL( colorSelected ( const QColor & )), this, SLOT( updateColor ( const QColor & )));
66
67 updateColor ( color );
68
69 QSize s = sizeHint ( ) + QSize ( 12, 0 );
70 setMinimumSize ( s );
71 setMaximumSize ( s. width ( ) * 2, s. height ( ));
72}
73
74/**
75 * This destructs the object
76 */
77OColorButton::~OColorButton ( )
78{
79 delete d;
80}
81
82/**
83 * @return Returns the current color of the button
84 */
85QColor OColorButton::color ( ) const
86{
87 return d-> m_color;
88}
89
90/**
91 * This method sets the color of the button
92 * @param c The color to be set.
93 */
94void OColorButton::setColor ( const QColor &c )
95{
96 updateColor ( c );
97}
98
99/**
100 * @internal
101 */
102void OColorButton::updateColor ( const QColor &c )
103{
104 d-> m_color = c;
105
106 QImage img ( 16, 16, 32 );
107 img. fill ( 0 );
108
109 int r, g, b;
110 c. rgb ( &r, &g, &b );
111
112 int w = img. width ( );
113 int h = img. height ( );
114
115 int dx = w * 20 / 100; // 15%
116 int dy = h * 20 / 100;
117
118 for ( int y = 0; y < h; y++ )
119 {
120 for ( int x = 0; x < w; x++ )
121 {
122 double alpha = 1.0;
123
124 if ( x < dx )
125 alpha *= ( double ( x + 1 ) / dx );
126 else if ( x >= w - dx )
127 alpha *= ( double ( w - x ) / dx );
128 if ( y < dy )
129 alpha *= ( double ( y + 1 ) / dy );
130 else if ( y >= h - dy )
131 alpha *= ( double ( h - y ) / dy );
132
133 int a = int ( alpha * 255.0 );
134 if ( a < 0 )
135 a = 0;
136 if ( a > 255 )
137 a = 255;
138
139 img. setPixel ( x, y, qRgba ( r, g, b, a ));
140 }
141 }
142 img. setAlphaBuffer ( true );
143
144 QPixmap pix;
145 pix. convertFromImage ( img );
146 setPixmap ( pix );
147
148 emit colorSelected ( c );
149}
150