summaryrefslogtreecommitdiff
path: root/libopie2/opieui/oclickablelabel.cpp
Unidiff
Diffstat (limited to 'libopie2/opieui/oclickablelabel.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opieui/oclickablelabel.cpp173
1 files changed, 173 insertions, 0 deletions
diff --git a/libopie2/opieui/oclickablelabel.cpp b/libopie2/opieui/oclickablelabel.cpp
new file mode 100644
index 0000000..4c4e581
--- a/dev/null
+++ b/libopie2/opieui/oclickablelabel.cpp
@@ -0,0 +1,173 @@
1/*
2 This file is part of the Opie Project
3 Copyright (C) Maximillian Reiß <harlekin@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 <opie2/oclickablelabel.h>
31
32using namespace Opie;
33
34/**
35 * This constructs the clickable ButtonLabel
36 *
37 * @param parent The parent of this label
38 * @param name A name of this label @see QObject
39 * @param fl The windowing flags
40 */
41OClickableLabel::OClickableLabel(QWidget* parent, const char* name, WFlags fl)
42 :QLabel(parent,name,fl)
43{
44 textInverted=false;
45 isToggle=false;
46 isDown=false;
47 showState(false);
48 setFrameShadow(Sunken);
49}
50
51/**
52 * This method makes the label behave as a toggle button
53 *
54 * @param t Whether or not to behave like a toggle button
55 */
56void OClickableLabel::setToggleButton(bool t)
57{
58 isToggle=t;
59}
60
61/**
62 * @internal
63 */
64void OClickableLabel::mousePressEvent( QMouseEvent * /*e*/ )
65{
66 if (isToggle && isDown)
67 {
68 showState(false);
69 }
70 else
71 {
72 showState(true);
73 }
74}
75
76/**
77 * @internal
78 */
79void OClickableLabel::mouseReleaseEvent( QMouseEvent *e )
80{
81 if (rect().contains(e->pos()) && isToggle) isDown=!isDown;
82
83 if (isToggle && isDown)
84 {
85 showState(true);
86 }
87 else
88 {
89 showState(false);
90 }
91
92 if (rect().contains(e->pos()))
93 {
94 if (isToggle)
95 {
96 emit toggled(isDown);
97 }
98 emit clicked();
99 }
100}
101
102/**
103 * @internal
104 */
105void OClickableLabel::mouseMoveEvent( QMouseEvent *e )
106{
107 if (rect().contains(e->pos()))
108 {
109 if (isToggle && isDown)
110 {
111 showState(false);
112 }
113 else
114 {
115 showState(true);
116 }
117 }
118 else
119 {
120 if (isToggle && isDown)
121 {
122 showState(true);
123 }
124 else
125 {
126 showState(false);
127 }
128 }
129}
130
131/**
132 * this toggles the label and inverts the color of
133 * the label
134 * @param on
135 */
136void OClickableLabel::showState(bool on)
137{
138 if (on)
139 {
140 //setFrameShape(Panel);
141 setInverted(true);
142 setBackgroundMode(PaletteHighlight);
143 }
144 else
145 {
146 //setFrameShape(NoFrame);
147 setInverted(false);
148 setBackgroundMode(PaletteBackground);
149 }
150 repaint();
151}
152
153void OClickableLabel::setInverted(bool on)
154{
155 if ( (!textInverted && on) || (textInverted && !on) )
156 {
157 QPalette pal=palette();
158 QColor col=pal.color(QPalette::Normal, QColorGroup::Foreground);
159 col.setRgb(255-col.red(),255-col.green(),255-col.blue());
160 pal.setColor(QPalette::Normal, QColorGroup::Foreground, col);
161 setPalette(pal);
162 textInverted=!textInverted;
163 }
164}
165
166/**
167 * @param on if the Label is down or up
168 */
169void OClickableLabel::setOn(bool on)
170{
171 isDown=on;
172 showState(isDown);
173}