author | sandman <sandman> | 2002-09-24 23:41:04 (UTC) |
---|---|---|
committer | sandman <sandman> | 2002-09-24 23:41:04 (UTC) |
commit | 3e0fcd45ea0e177deccd5463b2a754d442c03061 (patch) (unidiff) | |
tree | 40a3e7050907ed082e71e6701849404f3c2ab6a6 /libopie/ocolorbutton.cpp | |
parent | 6398fb2352fd4b53a023ec2884b786b08af8bd9f (diff) | |
download | opie-3e0fcd45ea0e177deccd5463b2a754d442c03061.zip opie-3e0fcd45ea0e177deccd5463b2a754d442c03061.tar.gz opie-3e0fcd45ea0e177deccd5463b2a754d442c03061.tar.bz2 |
button with popup-menu for color selections -- this should be used instead
of the QToolButton + ColorPopupMenu construct
-rw-r--r-- | libopie/ocolorbutton.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/libopie/ocolorbutton.cpp b/libopie/ocolorbutton.cpp new file mode 100644 index 0000000..96e5612 --- a/dev/null +++ b/libopie/ocolorbutton.cpp | |||
@@ -0,0 +1,118 @@ | |||
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 | #include <qcolor.h> | ||
32 | #include <qpixmap.h> | ||
33 | #include <qimage.h> | ||
34 | |||
35 | #include <qpe/resource.h> | ||
36 | |||
37 | class OColorButtonPrivate { | ||
38 | public: | ||
39 | QPopupMenu *m_menu; | ||
40 | QColor m_color; | ||
41 | }; | ||
42 | |||
43 | OColorButton::OColorButton ( QWidget *parent, const char *name ) | ||
44 | : QPushButton ( parent, name ) | ||
45 | { | ||
46 | d = new OColorButtonPrivate; | ||
47 | |||
48 | d-> m_menu = new ColorPopupMenu ( black, 0, 0 ); | ||
49 | setPopup ( d-> m_menu ); | ||
50 | //setPopupDelay ( 0 ); | ||
51 | connect ( d-> m_menu, SIGNAL( colorSelected ( const QColor & )), this, SLOT( updateColor ( const QColor & ))); | ||
52 | |||
53 | updateColor ( black ); | ||
54 | setMinimumSize ( sizeHint ( ) + QSize ( 8, 0 )); | ||
55 | } | ||
56 | |||
57 | OColorButton::~OColorButton ( ) | ||
58 | { | ||
59 | delete d; | ||
60 | } | ||
61 | |||
62 | QColor OColorButton::color ( ) const | ||
63 | { | ||
64 | return d-> m_color; | ||
65 | } | ||
66 | |||
67 | void OColorButton::setColor ( const QColor &c ) | ||
68 | { | ||
69 | updateColor ( c ); | ||
70 | } | ||
71 | |||
72 | void OColorButton::updateColor ( const QColor &c ) | ||
73 | { | ||
74 | d-> m_color = c; | ||
75 | |||
76 | QImage img ( 16, 16, 32 ); | ||
77 | img. fill ( 0 ); | ||
78 | |||
79 | int r, g, b; | ||
80 | c. rgb ( &r, &g, &b ); | ||
81 | |||
82 | int w = img. width ( ); | ||
83 | int h = img. height ( ); | ||
84 | |||
85 | int dx = w * 20 / 100; // 15% | ||
86 | int dy = h * 20 / 100; | ||
87 | |||
88 | for ( int y = 0; y < h; y++ ) { | ||
89 | for ( int x = 0; x < w; x++ ) { | ||
90 | double alpha = 1.0; | ||
91 | |||
92 | if ( x < dx ) | ||
93 | alpha *= ( double ( x + 1 ) / dx ); | ||
94 | else if ( x >= w - dx ) | ||
95 | alpha *= ( double ( w - x ) / dx ); | ||
96 | if ( y < dy ) | ||
97 | alpha *= ( double ( y + 1 ) / dy ); | ||
98 | else if ( y >= h - dy ) | ||
99 | alpha *= ( double ( h - y ) / dy ); | ||
100 | |||
101 | int a = int ( alpha * 255.0 ); | ||
102 | if ( a < 0 ) | ||
103 | a = 0; | ||
104 | if ( a > 255 ) | ||
105 | a = 255; | ||
106 | |||
107 | img. setPixel ( x, y, qRgba ( r, g, b, a )); | ||
108 | } | ||
109 | } | ||
110 | img. setAlphaBuffer ( true ); | ||
111 | |||
112 | QPixmap pix; | ||
113 | pix. convertFromImage ( img ); | ||
114 | setPixmap ( pix ); | ||
115 | |||
116 | emit colorSelected ( c ); | ||
117 | } | ||
118 | |||