Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kwin / clients / oxygen / oxygenbutton.cpp
blob9cf561d87c278599fb6bc4ecf6fe048eb04aba6b
1 //////////////////////////////////////////////////////////////////////////////
2 // oxygenbutton.cpp
3 // -------------------
4 // Oxygen window decoration for KDE. Buttons.
5 // -------------------
6 // Copyright (c) 2006, 2007 Riccardo Iaconelli <riccardo@kde.org>
7 // Copyright (c) 2006, 2007 Casper Boemann <cbr@boemann.dk>
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to
11 // deal in the Software without restriction, including without limitation the
12 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13 // sell copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 // IN THE SOFTWARE.
26 //////////////////////////////////////////////////////////////////////////////
27 #include <math.h>
28 #include <QPainterPath>
29 #include <QPainter>
30 #include <QPen>
31 #include <QBitmap>
33 #include <kdecoration.h>
34 #include <kglobal.h>
35 #include <KColorUtils>
36 #include <kdebug.h>
37 #include <KColorScheme>
39 #include "oxygenclient.h"
40 #include "oxygenbutton.h"
41 #include "oxygen.h"
43 namespace Oxygen
45 // class OxygenClient;
47 extern int BUTTONSIZE;
48 extern int DECOSIZE;*/
50 // static const int DECOSIZE = 8;
51 //////////////////////////////////////////////////////////////////////////////
52 // OxygenButton Class //
53 //////////////////////////////////////////////////////////////////////////////
55 //////////////////////////////////////////////////////////////////////////////
56 // OxygenButton()
57 // ---------------
58 // Constructor
60 OxygenButton::OxygenButton(OxygenClient &parent,
61 const QString& tip, ButtonType type)
62 : KCommonDecorationButton((::ButtonType)type, &parent)
63 , client_(parent)
64 , helper_(parent.helper_)
65 , type_(type)
66 , lastmouse_(0)
67 , colorCacheInvalid_(true)
69 setAutoFillBackground(false);
70 setAttribute(Qt::WA_OpaquePaintEvent, false);
71 setFixedSize(OXYGEN_BUTTONSIZE, OXYGEN_BUTTONSIZE);
72 setCursor(Qt::ArrowCursor);
73 setToolTip(tip);
76 OxygenButton::~OxygenButton()
80 //declare function from oxygenclient.cpp
81 QColor reduceContrast(const QColor &c0, const QColor &c1, double t);
84 QColor OxygenButton::buttonDetailColor(const QPalette &palette)
86 if (client_.isActive())
87 return palette.color(QPalette::Active, QPalette::ButtonText);
88 else {
89 if (colorCacheInvalid_) {
90 QColor ab = palette.color(QPalette::Active, QPalette::Button);
91 QColor af = palette.color(QPalette::Active, QPalette::ButtonText);
92 QColor nb = palette.color(QPalette::Inactive, QPalette::Button);
93 QColor nf = palette.color(QPalette::Inactive, QPalette::ButtonText);
95 colorCacheInvalid_ = false;
96 cachedButtonDetailColor_ = reduceContrast(nb, nf, qMax(qreal(2.5), KColorUtils::contrastRatio(ab, KColorUtils::mix(ab, af, 0.4))));
98 return cachedButtonDetailColor_;
102 //////////////////////////////////////////////////////////////////////////////
103 // sizeHint()
104 // ----------
105 // Return size hint
107 QSize OxygenButton::sizeHint() const
109 return QSize(OXYGEN_BUTTONSIZE, OXYGEN_BUTTONSIZE);
112 //////////////////////////////////////////////////////////////////////////////
113 // enterEvent()
114 // ------------
115 // Mouse has entered the button
117 void OxygenButton::enterEvent(QEvent *e)
119 KCommonDecorationButton::enterEvent(e);
120 if (status_ != Oxygen::Pressed) {
121 status_ = Oxygen::Hovered;
123 update();
126 //////////////////////////////////////////////////////////////////////////////
127 // leaveEvent()
128 // ------------
129 // Mouse has left the button
131 void OxygenButton::leaveEvent(QEvent *e)
133 KCommonDecorationButton::leaveEvent(e);
134 // if we wanted to do mouseovers, we would keep track of it here
135 status_ = Oxygen::Normal;
136 update();
139 //////////////////////////////////////////////////////////////////////////////
140 // pressSlot()
141 // ------------
142 // Mouse has pressed the button
144 void OxygenButton::pressSlot()
146 status_ = Oxygen::Pressed;
147 update();
149 //////////////////////////////////////////////////////////////////////////////
150 // drawButton()
151 // ------------
152 // Draw the button
154 void OxygenButton::paintEvent(QPaintEvent *)
156 QPainter painter(this);
157 QPalette pal = palette(); // de-const-ify
159 if (type_ == ButtonMenu) {
160 // we paint the mini icon (which is 16 pixels high)
161 int dx = (width() - 16) / 2;
162 int dy = (height() - 16) / 2;
163 painter.drawPixmap(dx, dy, client_.icon().pixmap(16));
164 return;
167 // Set palette to the right group.
168 // TODO - fix KWin to do this for us :-).
169 if (client_.isActive())
170 pal.setCurrentColorGroup(QPalette::Active);
171 else
172 pal.setCurrentColorGroup(QPalette::Inactive);
174 if(client_.maximizeMode() == OxygenClient::MaximizeRestore)
175 painter.translate(0,-1);
177 QColor bg = helper_.backgroundTopColor(pal.window());
179 QLinearGradient lg = helper_.decoGradient(QRect(4,4,13,13), buttonDetailColor(pal));
181 if(status_ == Oxygen::Hovered) {
182 if(type_ == ButtonClose) {
183 QColor color = KColorScheme(pal.currentColorGroup()).foreground(KColorScheme::NegativeText).color();
184 lg = helper_.decoGradient(QRect(4,4,13,13), color);
185 painter.drawPixmap(0, 0, helper_.windecoButtonFocused(pal.button(), color,7));
187 else{
188 QColor color = KColorScheme(pal.currentColorGroup()).decoration(KColorScheme::HoverColor).color();
189 painter.drawPixmap(0, 0, helper_.windecoButtonFocused(pal.button(), color, 7));
192 else
193 painter.drawPixmap(0, 0, helper_.windecoButton(pal.button()));
195 painter.setRenderHints(QPainter::Antialiasing);
196 painter.setBrush(Qt::NoBrush);
197 painter.setPen(QPen(lg, 2.2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
198 switch(type_)
200 case ButtonSticky:
201 painter.drawPoint(QPointF(10.5,10.5));
202 break;
203 case ButtonHelp:
204 painter.translate(1.5, 1.5);
205 painter.drawArc(7,5,4,4,135*16, -180*16);
206 painter.drawArc(9,8,4,4,135*16,45*16);
207 painter.drawPoint(9,12);
208 break;
209 case ButtonMin:
210 painter.drawLine(QPointF( 7.5, 9.5), QPointF(10.5,12.5));
211 painter.drawLine(QPointF(10.5,12.5), QPointF(13.5, 9.5));
212 break;
213 case ButtonMax:
214 switch(client_.maximizeMode())
216 case OxygenClient::MaximizeRestore:
217 case OxygenClient::MaximizeVertical:
218 case OxygenClient::MaximizeHorizontal:
219 painter.drawLine(QPointF( 7.5,11.5), QPointF(10.5, 8.5));
220 painter.drawLine(QPointF(10.5, 8.5), QPointF(13.5,11.5));
221 break;
222 case OxygenClient::MaximizeFull:
224 painter.translate(1.5, 1.5);
225 //painter.setBrush(lg);
226 QPoint points[4] = {QPoint(9, 6), QPoint(12, 9), QPoint(9, 12), QPoint(6, 9)};
227 //QPoint points[4] = {QPoint(9, 5), QPoint(13, 9), QPoint(9, 13), QPoint(5, 9)};
228 painter.drawPolygon(points, 4);
229 break;
232 break;
233 case ButtonClose:
234 painter.drawLine(QPointF( 7.5,7.5), QPointF(13.5,13.5));
235 painter.drawLine(QPointF(13.5,7.5), QPointF( 7.5,13.5));
236 break;
237 case ButtonAbove:
238 if(isChecked()) {
239 QPen newPen = painter.pen();
240 newPen.setColor(KColorScheme(pal.currentColorGroup()).decoration(KColorScheme::HoverColor).color());
241 painter.setPen(newPen);
244 painter.drawLine(QPointF( 7.5,14), QPointF(10.5,11));
245 painter.drawLine(QPointF(10.5,11), QPointF(13.5,14));
246 painter.drawLine(QPointF( 7.5,10), QPointF(10.5, 7));
247 painter.drawLine(QPointF(10.5, 7), QPointF(13.5,10));
248 break;
249 case ButtonBelow:
250 if(isChecked()) {
251 QPen newPen = painter.pen();
252 newPen.setColor(KColorScheme(pal.currentColorGroup()).decoration(KColorScheme::HoverColor).color());
253 painter.setPen(newPen);
256 painter.drawLine(QPointF( 7.5,11), QPointF(10.5,14));
257 painter.drawLine(QPointF(10.5,14), QPointF(13.5,11));
258 painter.drawLine(QPointF( 7.5, 7), QPointF(10.5,10));
259 painter.drawLine(QPointF(10.5,10), QPointF(13.5, 7));
260 break;
261 default:
262 break;
273 } //namespace Oxygen