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