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